$(document).ready(function(){
		 
	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;
	});

	//print page tool	
	$("#print li").before("<li class='first'><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;
	});
	
	//Info tip
	$("div.tip").hide();
	
	$("a.tipLink").click(function () {
		$(this).parent().next("div.tip").slideToggle()
		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();
		}
    });
	
	//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;
		});
	});
	
});
