$(document).ready(function() { 
	$("#selector").click(function(){
		
			$("#selector ul").slideToggle().addClass("open");
		
		return false;
	});
	$("body").click(function(){
		$("#selector ul").slideUp().removeClass();
	});
	
	$("#selector ul a").click(function(){
		
			$("#selector p").text($(this).text());
	});
	$("#email-address").focus(function(){
		$("#extra-fields, #subscribe-form p.required, #subscribe-form .close").fadeIn();
	});
	$(".close").click(function(){
		$("#extra-fields, #subscribe-form p.required, #subscribe-form .close").fadeOut();
		return false;
	});
	
$("#main-menu li").hover(function() { //Hover over event on list item	

	
	$(this).find("span").css({ 'display' : 'block' }); //Show the subnav

	var offs = $(this).position();
	var offsetLeft = offs.left;
	var liWidth = $(this).width();
	if ( location.hostname == 'www.whitesalt.com.au' )
	{
		var marginRight = 655 - offsetLeft - liWidth;
	}
	else {
		var marginRight = 734 - offsetLeft - liWidth;
	}

	// align menu right
	if (offsetLeft > 300) {
		$(this).find("span a").css('float' , 'right');

		var classTitle = $(this).find('a').attr("class");

		if ( classTitle != 'menu-link-active' )
		{
			$(this).find("span a:first").css('margin-right', marginRight+'px');
		}
	}

	// align menu left
	else {
		$(this).find("span a").css('float' , 'left');

		var classTitle = $(this).find('a').attr("class");

		if ( classTitle != 'menu-link-active' )
		{
			$(this).find("span a:first").css('margin-left', offsetLeft+'px');
		}		
	}

	$(this).css({ 'background' : 'url(art/navbg.png) center -48px no-repeat;'}); //Add background color + image on hovered list item
} , function() { //on hover out...
	$(this).find("span").hide(); //Hide the subnav
	$(this).css({ 'background' : 'none !important'}); //Ditch the background
	
});

	try
	{
		// show submenu for active item
		$(".menu-link-active").next('span').css("display", "block");

		var aoffs = $(".menu-link-active").parent().position();
		var aoffsetLeft = aoffs.left;	
		var liWidth =  $(".menu-link-active").parent().width();
		var amarginRight = 734 - aoffsetLeft - liWidth;

		// align menu right
		if (aoffsetLeft > 300) {
			var nextSpan = $(".menu-link-active").next("span");
			nextSpan.find('a:first').css('margin-right', amarginRight);
			nextSpan.find('a').css('float', 'right');
		}
		// left
		else {
			var nextSpan = $(".menu-link-active").next("span");
			nextSpan.css('margin-left', aoffsetLeft+'px');
			nextSpan.find('a').css('float', 'left');
		}
	}
	catch (e)
	{
	}

});


