// JavaScript Document

//Document Ready
$(function(){

	/* Country Switcher */
	
	//Opens the country switcher.
	$('#CountrySwitch button').click(function(){
		$('#CountrySwitch').animate({
			height: '+=72'									  
		}, 100);
		setTimeout(function(){CloseSwitch();});		
	});
	
	//Retrieves the country the user selected.
	$('#CountrySwitch li.SwitchItem').click(function(){
		$('#CountrySwitch li.Current').text($(this).text());
		var SelectedCountry = $(this).attr('id');
		switch(SelectedCountry){
			case	'AUSwitch':
				window.location = '/au/';
				break;
			case 'NZSwitch':
					window.location = '/nz/';
				break;
			case 'USSwitch':
				window.location = '/us/';
				break;
		}
	});
	
	//Closes the country switcher.
	function CloseSwitch(){
		var CurrentStatus = 'open';
		//Run on any click.
		$('*').click(function(){
			if($(this).attr('id') != 'CountrySwitch' && $(this).attr('id') != 'CountrySwitchArrow' && $(this).hasClass('SwitchItem') == false && CurrentStatus == 'open'){ 								  
				$('#CountrySwitch').animate({
					height: '-=72px'									  
					}, 100);
				CurrentStatus = 'closed';
				return false;
			}
		});
	}
	
	
	/* Primary Navigation */
	//Fades the menu in and out as required.
	$('#PrimaryNavigation li').hover(function(){
		$(this)						 
			.find('ul')
			.stop('true', 'true')
			.fadeIn(300);

		//Hide deeper level lists.
		$(this)	
			.find('ul ul')
			.hide();
	}, function(){
		$(this)
			.find('ul')
			.stop('true', 'true')
			.fadeOut(300);
	});
	
	//Adds an arrow to the secondlevel primary navigation items with a nested menu.
	$('#PrimaryNavigation ul:first li li').each(function(){
		if ($(this).children('ul').length > 0){
			$(this).addClass('Arrow');
		}
	});
	
	if ($('#PrimaryNavigation ul:first  li li.selected').children('ul').length > 0){
		$(this).addClass('Arrow Arrow');
	}
	
	//Keeps the hover-state on the primary navigation item when hovering over a nested menu.
	$('#PrimaryNavigation ul:first li ul').hover(function(){
		$(this).parent('li').children('a').addClass('Hover');
	},
	function(){
		$(this).parent('li').children('a').removeClass('Hover');
	});
	
	//Stops the above function from underlining the secondary parent a.
	$('#PrimaryNavigation ul:first li ul li ul').hover(function(){
		$(this).parent('li').children('a').removeClass('Hover').parent('ul').parent('ul').parent('li').children('a').addClass('Hover');
	},
	function(){
		$(this).parent('li').children('a').removeClass('Hover');
	});

	$('#PrimaryNavigation ul:first li ul li.selected').mouseout(function(){
		if($(this).hasClass('Arrow') == false){
			if ($(this).children('ul').length > 0){
				$(this).addClass('Arrow');
			}
		}		
	});
	
	//Sets the "Current" state on the nav items that point to the current page.
	$('a[href="' + location.pathname + '"]').addClass('Current');
	

	/* Hide Subscription labels on click. */
	//Name
	$('#HomePage #SubscribeControls #CLFullName').click(function(){					 
		if($(this).attr('value') == "name"){
			$(this).attr('value', '');
		}
	});
	//Email
	$('#HomePage #SubscribeControls #CLEmailAddress').click(function(){																	 
		if($(this).attr('value') == "email"){
			$(this).attr('value', '');
		}
	});
	

	
});
