jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function() {
$.preloadImages("/images/Products_Creams_1_Index.jpg", "/images/Products_Cultured_1_Index.jpg", "/images/Products_FlavMilk_1_Index.jpg", "/images/Products_Juices_1_Index.jpg", "/images/Products_Milk_1_Index.jpg", "/images/Products_PlusMilk_1_Index.jpg" , "/images/Products_Seasonal_1_Index.jpg");

	//hide subnavs
	$('.product_nav').hide();	   
	
	//serve the correct page
	var hash = window.location.hash.substr(1);
	var href = $('#products_menu dt a').each(function(){
		var href = $(this).attr('href');
		if(hash==href.substr(0,href.length-4)){
			var toLoad = hash+'.php #product_content';
			
			$('#product_content_wrapper').load(toLoad)
			$(this).parent().next().show();
		}											
	});
	
	//main nav link click function
	$('#products_menu dt a').click(function(){
		//hide any open subnavs
		$('.product_nav').slideUp();	 
		$('.product_nav li').removeClass("current");
		
		//show subnav
  		$(this).parent().next().slideDown();
  		
  		//pull only product_content div
		var toLoad = $(this).attr('href')+' #product_content';
		
		$('#product_content_wrapper').fadeOut('fast',loadContent);
		$('#load').remove();
		$('#product_content_outer_wrapper').append('<div id="load">LOADING...</div>');
		$('#load').fadeIn('normal');
		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
		
		function loadContent() {		
			
			$('#product_content_wrapper').load(toLoad,'',showNewContent());
			$('#product_content').css('display', 'none');
			$('.product_content_main').css('display', 'none');
			
		}
		function showNewContent() {

			$('#product_content_wrapper').fadeIn('slow',hideLoader());	
			
		}
		
		function hideLoader() {		
			
			$('#load').fadeOut('normal');
		}
		
	
		
		return false;
		
	});
	
	$('#products_menu ul li a').click(function(){
		$('.product_nav li').removeClass("current");
		$('.product_info_main').hide();
		$('.product_info').hide();	 		
		$(this).parent().addClass("current");
		var current_product=$(this).attr('href');
		$(current_product).fadeIn('slow');
		return false;
	});
	
	$('#plus_show_link').click(function(){
		$('.product_nav').slideUp();	 
		$('.product_nav li').removeClass("current");
			$("#plus_show_link_activate").next().slideDown();
			});
			
	
	
	//internal link click function
	$('a.product_link').livequery('click',function(){
		//$('#products_menu dt a').removeClass("bottom");
		//hide any open subnavs
		
		//show subnav
  		//$(this).parent().next().slideDown();
  		//$(this).addClass('bottom');
  		
  		//pull only product_content div
		var toLoad = $(this).attr('href')+' #product_content';
		$('#product_content_wrapper').fadeOut('fast',loadContent);
		$('#load').remove();
		$('#main').append('<div id="load">LOADING...</div>');
		$('#load').fadeIn('normal');
		window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-4);
		function loadContent() {		
			//$('.product_info_main img').css('display', 'none');
			
			$('#product_content_wrapper').load(toLoad,'',showNewContent());
			$('#product_content').css('display', 'none');
			$('.product_content_main').css('display', 'none');
			
		}
		function showNewContent() {
			//$('.product_info_main img').fadeIn('slow');
			$('#product_content_wrapper').fadeIn('slow',hideLoader());	
			
		}
		
		function hideLoader() {		
			
			$('#load').fadeOut('normal');
		}
		return false;
		
	});
	$('#juice_show_link').livequery('click',function(){
		$('.product_nav').slideUp();	 
		$('.product_nav li').removeClass("current");
			$("#juice_show_link_activate").next().slideDown();
			});
	
});
