$(document).ready(function(){
	
    //Date picker		
	if ($("[id$='_when']").length > 0)
        $("[id$='_when']").datepicker({ dateFormat: 'dd/mm/yy' });    

    var extLinks = function(){
        var e = $('a[rel="external"]');
        e.attr("title", e.attr("title") + " - Link opens in a new window");
        e.click(function(){
            window.open($(this).attr('href'));
            return false;
        });
    };
    extLinks();
    
    //how to use this material pop up thingy
    $(".how2").hide().addClass('bubble');
    $(".how2 li:last").after("<li><a href='#non'>Close</a></li>");
    
    $(".toggle").wrapInner("<a href='#non'></a>");
    
    $(".toggle a, .bubble a").click(function(){
        $(".how2").slideToggle();
        return false;
    });
    
    //download tools on video detail page
    $(".grey ul, .download-vid ul").hide();
    
    $(".grey h2, .download-vid h3").wrapInner("<a href='#non'></a>");
    
    $(".grey h2 a, .download-vid h3 a").click(function(){
        $(this).parent().next("ul").slideToggle();
        return false;
    });
    
    //download tools on media search page
    $("td.files ul").hide();
    
    $("td.files a.hide_link").click(function(){
        if ($(this).next("div.hide_wrap").contents("ul").hasClass("open")) {
            $(this).next("div.hide_wrap").contents("ul").removeClass("open").slideToggle();
        }
        else {
            $(this).parent(".files").children("div.hide_wrap").contents("ul").removeClass("open").hide();
            $(this).next("div.hide_wrap").contents("ul").addClass("open").slideToggle();
        }
        return false;
    });
    
    $("#print li.first").after("<li><a href='javascript:window.print();'><img src='/images/print.gif' alt='Print' /></a></li>");
    
    //Favourites folder show hide
    $("ul.folderHide").hide();
    
    $("ul.favFolders li span.expandFolder").wrapInner("<a href='#'></a>")
    
    $("ul.favFolders li span.expandFolder a").click(function(){
        $(this).parent().nextAll("ul.folderHide").slideToggle();
        $(this).toggleClass("collapse");
        return false;
    });
	
	//google analytics - track downloads through events
	 $('td.files .video a').each(function() {
	 	var downLoadtitle = $(this).parents('tr').children('td.title').text();
		
      $(this).bind('click', function() {
        if (pageTracker) {
          pageTracker._trackEvent('Videos', 'Downloaded', $(this).attr('class') + ' - ' + downLoadtitle); 
		}
      });
    });

	 $('td.files .audio a').each(function() {
	 	var downLoadtitle = $(this).parents('tr').children('td.title').text();
      $(this).bind('click', function() {
        if (pageTracker) {
          pageTracker._trackEvent('Audio', 'Downloaded', $(this).attr('class') + ' - ' + downLoadtitle);
        }
      });
    });
	
	 $('td.files .transcript a').each(function() {
	 	var downLoadtitle = $(this).parents('tr').children('td.title').text();
      $(this).bind('click', function() {
        if (pageTracker) {
          pageTracker._trackEvent('Audio', 'Transscipt', $(this).attr('class') + ' - ' + downLoadtitle);
        }
      });
    });

	$('td.files .transcript a').each(function() {
	 	var downLoadtitle = $(this).parents('tr').children('td.title').text();
      $(this).bind('click', function() {
        if (pageTracker) {
          pageTracker._trackEvent('CPD', 'Downloaded', $(this).attr('class') + ' - ' + downLoadtitle);
        }
      });
    });
	
	//Track all WAM links in Google Analytics (they always contain idoc.ashx in the URL)
	$('a[href*="idoc.ashx"]').each(function() {
		var dlPath = window.location.pathname;
		var dlText = $(this).text();
		var dlURL = $(this).attr('href');
		$(this).bind('click', function() {
			if (pageTracker) {
				pageTracker._trackEvent('Resources', dlPath, dlText);
			}
			window.open(dlURL);
			return false;
		});
	});
		
	
    
    //Info tip
    $("div.tip").hide();
    
    $("a.tipLink").click(function(){
        $(this).parent().next("div.tip").slideToggle()
        return false;
    });
	
/*
	$("[id$='_useDeliveryAddress']").toggle(function(){
		$(this).attr("checked", true);
	}, function(){
		$(this).attr("checked", false);
        return false;		
	});
	
*/

    $("[id$='_useDeliveryAddress']").click(function(){
		if($(this).is(":checked") ) {
        $("[id$='_billFirstName']").addClass("disabled").attr("readonly", true);
		$("[id$='_billLastName']").addClass("disabled").attr("readonly", true);
		$("[id$='_billEmail']").addClass("disabled").attr("readonly", true);
		$("[id$='_billAddress1']").addClass("disabled").attr("readonly", true);
		$("[id$='_billAddress2']").addClass("disabled").attr("readonly", true);
		$("[id$='_billTown']").addClass("disabled").attr("readonly", true);
		$("[id$='_billPostcode']").addClass("disabled").attr("readonly", true);
		$("[id$='_billCountry']").addClass("disabled").attr("disabled", true);			
		} else {
        $("[id$='_billFirstName']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billLastName']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billEmail']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billAddress1']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billAddress2']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billTown']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billPostcode']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billCountry']").removeClass("disabled").attr("disabled", "");			
		}
    });

	$("[id$='_useDelegateAddress']").click(function(){
		if($(this).is(":checked") ) {
        $("[id$='_billFirstName']").addClass("disabled").attr("readonly", true);
		$("[id$='_billLastName']").addClass("disabled").attr("readonly", true);
		$("[id$='_billEmail']").addClass("disabled").attr("readonly", true);
		$("[id$='_billAddress1']").addClass("disabled").attr("readonly", true);
		$("[id$='_billAddress2']").addClass("disabled").attr("readonly", true);
		$("[id$='_billTown']").addClass("disabled").attr("readonly", true);
		$("[id$='_billPostcode']").addClass("disabled").attr("readonly", true);
		$("[id$='_billCountry']").addClass("disabled").attr("disabled", true);			
		} else {
        $("[id$='_billFirstName']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billLastName']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billEmail']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billAddress1']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billAddress2']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billTown']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billPostcode']").removeClass("disabled").attr("readonly", "");
		$("[id$='_billCountry']").removeClass("disabled").attr("disabled", "");			
		}
    });

    if (!$(".addMyZone").is(":checked"))
	    $("div.alsoMyzone").hide();
	
	$(".addMyZone").click(function(){
		if ($(this).is(":checked")) {
			$("div.alsoMyzone").slideDown();
			$("[id$='_signUpEmail']").val($("[id$='_email']").val());			
		} else {
			$("div.alsoMyzone").slideUp();
		}
    });
    
});
