//=======================================================================================================
//-------------------------------------------------------------------------------------------------------
//	Custom Javascript functions
//-------------------------------------------------------------------------------------------------------
//=======================================================================================================
//---------------------------------------------------------------------------------------------------------
//	JQuery  - putting all our jquery here so we can load it as a callback function on ajax loads.
//---------------------------------------------------------------------------------------------------------
function loadfunc()
{
	//	Microsoft sucks
	$(document).pngFix(); 

	$('a[href*="/results.php"]').click(function (event) {
		event.preventDefault();
		url = $(this).attr("href");
		$("div#search-results").load(url,'',function(){
		   loadfunc();
		 });
	});
   
	$('a.hovershow').click(function(event){
		event.preventDefault(); // Don't actually follow the link
	});

	//	Use a link with the class "toggler" to toggle any DOM element 
	//	by the ID selector found in the link's "target" attribute.
	$('a.toggler').click(function (event) {
		event.preventDefault(); // Don't actually follow the link

		$('.toggled').css('display','none');
		
		target = $(this).attr("target");
		$('#'+target).toggle();
	});

	//	Sign-In Toggle
	$('a#signin').click(function (event) {
		event.preventDefault(); // Don't actually follow the link
		
		$('#loginform').toggle();
		$('#signin').toggle();
	});
	
	//	Submit a form when a select is changed.
	$('select.change-submit').change( function () {
		$(this).parent().submit();
	});
	
	//	Product Images
	$('#product-images-nav a').click(function (event) {
		event.preventDefault(); // Don't actually follow the link
		
		$('#product-images img').css('display','none');
		$('#product-images-nav a').css('color','#A55B08');
		
		$(this).css('color','#E1CEB1');
		
		target = $(this).attr("target");
		$('#'+target).toggle();
	});

	//	Clear the background image on focu
	$("input#pw,input#uid").focus(function(){
	  $(this).css('background-image','none');
	});
	
	//	Clear PW and UID background if there is a value
	uidvalue = $('input#uid').attr('value');
	if (uidvalue) {
		$('input#uid').css('background-image','none');
	}
	
	pwvalue = $('input#pw').attr('value');
	if (pwvalue) {
		$('input#pw').css('background-image','none');
	}

	
	//	Clear input on focus
	$("input.select-clear").focus(function(){
	  $(this).attr('value','');
	});
	
	//-------------------------------------------------------------------
	//	Custom Menu DHTML                            
	//-------------------------------------------------------------------
	$('#nav-main a').bind("mouseenter",function(){
		// Is it the default? if so do nothing.
		var hoverisactive	= $(this).hasClass('active');
		var hoverid      	= $(this).attr('id').substr(2);
		var hoverimage   	= '#img-' + hoverid;
		var activeid     	= $('#nav-main .active').attr('id');
		
		if (activeid) {
			activeid        	= activeid.substr(2);
			var activeimage 	= '#img-' + activeid;
		};

		$(hoverimage).attr('src',menuimages_hover[hoverid]);

		//	Don't bother with the hover-active stuff if it's 
		//	already the default active menu.
		if(hoverisactive == false) 
		{	
			$(activeimage).attr('src',menuimages_default[activeid]);

			//	Make the default menu display disappear
			$('ul.active').css('display','none');

			//	Set the menu to active
			hovermenu = '#m-' + hoverid;
			$(hovermenu).addClass('hover-active');

			//	Sub Menu mouse OVER
			$('ul.hover-active').bind("mouseenter",function(){
				$(this).addClass('hover-active');
				$('ul.active').css('display','none');

				$(hoverimage).attr('src',menuimages_hover[hoverid]);
				$(activeimage).attr('src',menuimages_default[activeid]);
			});

			//	Sub Menu mouse out
			$('ul.hover-active').bind("mouseleave",function(){
				$(this).removeClass('hover-active');
				$('ul.active').css('display','block');
				$(hoverimage).attr('src',menuimages_default[hoverid]);
				$(activeimage).attr('src',menuimages_active[activeid]);
			});
		}

		//	Main Menu mouse out
		$(this).bind("mouseleave",function(){
			$('ul.hover-active').removeClass('hover-active');
			$('ul.active').css('display','block');
			$(hoverimage).attr('src',menuimages_default[hoverid]);
			$(activeimage).attr('src',menuimages_active[activeid]);
		});
	});

	//---------------------------------------------------------------------------------------------------------
	//	Custom sidebar for newsletter signup
	//---------------------------------------------------------------------------------------------------------
	// disabling autocomplete coz FF is messing up
	$('div#content-2 input').each(function(){
		$(this).attr('autocomplete','off');
	});
	
	// dev
		$('a#sidelink-1636').hover(function (event) {
			target = $(this).attr("target");
			$('#'+target).show();
		},function (event){
			target = $(this).attr("target");
			$('#'+target).hide();
		});

		$('div#side-1636').hover(function (event) {
			$(this).show();
		},function (event){
			$(this).hide()
		});
	// live
		$('a#sidelink-9845').hover(function (event) {
			target = $(this).attr("target");
			$('#'+target).show();
		},function (event){
			target = $(this).attr("target");
			$('#'+target).hide();
		});

		$('div#side-9845').hover(function (event) {
			$(this).show();
		},function (event){
			$(this).hide()
		});
		
	//---------------------------------------------------------------------------------------------------------
	//	Custom sidebar for newsletter signup
	//---------------------------------------------------------------------------------------------------------
		$('a#content-2-toggler').hover(function (event) {
			target = $(this).attr("target");
			$('#'+target).show();
		},function (event){
			target = $(this).attr("target");
			$('#'+target).hide();
		});

		$('div#content-2').hover(function (event) {
			$(this).show();
		},function (event){
			$(this).hide()
		});
}
