
$(window).load(function() {
		
	
	// product nav		
	if($('.pronav').length > 0) {
		
		//add 'all' link with product count and show all products
		var itemNumber = $('.pronav li ul li').length;
		$('.pronav > li.last a').append(' (' + itemNumber + ')');
		$('.pronav > li.last').addClass('selected');
		
		//move all pronav sub li's to imagenav
		var imageNavList = $('.pronav li ul li');
		$('.pronav li ul').remove();
		$('.imagenav').append(imageNavList);
		
		//show/hide items based on category
		$('.pronav a').click(function() {
			$('.imagenav li').fadeOut('fast');
			$(this).parent().siblings('li').removeClass('selected');
			$(this).parent().addClass('selected');
			var category = $(this).attr('href');
			var categoryItems = $('.imagenav li a[href*="'+ category +'"]').parent('li');
			$("#scrollbox ul").css('left','0px');
			$(categoryItems).fadeIn('slow');
			$('.left a').hide();
			$('.right a').hide();
			return false;
		});
		
		////add click function specifically for 'all'///
		$('.pronav li.last a').click(function() {
			$('.imagenav li').fadeOut('fast');
			$(this).parent().siblings('li').removeClass('selected');
			$(this).parent().addClass('selected');
			$('.imagenav li').fadeIn('slow');	
			$('.right a').show();
			return false;
		});
		
		//add address rel
		$('.imagenav li a').each(function(){
			var relhref = $(this).attr('href');
			$(this).attr('rel', function(i, val) {
				return 'address:/' + relhref;	
			});
		});
	
		//pull in content from subpages
		$('.imagenav li a').click(function () {
			$(this).address();
			$.address.value($(this).attr('href'));
			$('.behind').load(this.href+" #pullsource > *",null,function () {
				$('.behind .productinfo').append('<div class="retail"><p><span class="tip purplebutton" rel="#shop">Find a Retailer</span> <a class="pinkbutton" target="_blank" href="http://www.drugstore.com/">Buy Now!</a></p><div class="facebooklike"></div><div class="modal" id="shop"><h6>Retailers</h6><span class="close">x</span><p><a href="http://www.walmart.com/" target="_blank">WALMART</a><a href="http://www.familydollar.com/pages/home.aspx" target="_blank">FAMILY DOLLAR</a> <a href="http://www.kroger.com/Pages/default.aspx" target="_blank">KROGER</a> <a href="http://www.walgreens.com/" target="_blank">WALGREENS</a><a href="http://www.dollargeneral.com/Pages/index.aspx" target="_blank">DOLLAR GENERAL</a> <a href="http://www.target.com/" target="_blank">TARGET</a><a href="http://www.foodlion.com/" target="_blank">FOOD LION </a> <a href="http://www.kmart.com/" target="_blank">KMART</a><a href="http://www.cvs.com" target="_blank">CVS </a></p></div></div>');
				$('.infront').fadeOut('normal');
				$('.behind').fadeIn('normal', function() { 
					$('.infront').addClass('behind');
					$('.infront').removeClass('infront'); 
					$(this).addClass('infront'); 
					$(this).removeClass('behind');
				});
			});
			
			$('.tip').live('click', function() {
				$('.modal').show();
			});
			
			$('.close').live('click', function() {
				$('.modal').hide();
			});
			
			$('.close').click(function() {
				$('.modal').hide();
			});
		
			
			return false;
			
		});
				
		
		$('.imagenav li a img').hover(function(){
				$(this).animate({width:'91px',height:'166',top:'5px',left:'0px'},{queue:false,duration:50});
			}, function(){
				$(this).animate({width:'81px',height:'156',top:'10px',left:'5px'},{queue:false,duration:50});
		});
		
		
		var redirect = $.address.value();
		if(redirect == '/') 		
				$($('.imagenav li a')[0]).click();						
		else
				$('.imagenav li a').each(function() {
					if(('/'+$(this).attr('href')) == redirect) {
						$(this).click();
					}
				});
			
		

		
	};
	
	
	// ingredient nav		
	if($('.ingnav').length > 0) {
		
		//add address rel
		$('.ingnav li a').each(function(){
			var relhref = $(this).attr('href');
			$(this).attr('rel', function(i, val) {
				return 'address:/' + relhref;	
			});
		});
	
	
		//pull in content from subpages
		$('.ingnav li a').click(function () {
			$(this).address();
			$.address.value($(this).attr('href'));
			$('.behind').load(this.href+" #pullsource > *",null,function () {
				$('.infront').fadeOut('normal');
				$('.behind').fadeIn('normal', function() { 
					$('.infront').addClass('behind');
					$('.infront').removeClass('infront'); 
					$(this).addClass('infront'); 
					$(this).removeClass('behind');
				});
			});
			
			return false;
			
		});
				
		
		$('.ingnav li a img').hover(function(){
				$(this).animate({width:'160px',height:'140',top:'0px',left:'0px'},{queue:false,duration:50});
			}, function(){
				$(this).animate({width:'150px',height:'130',top:'5px',left:'5px'},{queue:false,duration:50});
		});
		
		
		var redirect = $.address.value();
		if(redirect == '/') 		
				$($('.ingnav li a')[0]).click();						
		else
				$('.ingnav li a').each(function() {
					if(('/'+$(this).attr('href')) == redirect) {
						$(this).click();
					}
				});
		
		
		
	};
	
	
	// scroll nav		
	var itemWidth = $('#scrollbox ul li.last').width();
	var itemCount = $('#scrollbox ul li').length;
	var viewWidth = $('#scrollbox').width();
	var maxNext = viewWidth-(itemWidth*itemCount)+(itemWidth*2.5);
	var maxPrev = -161;
	
	//hide prev
	$('.prev').hide();
	
	//next click	
	$('.next').click(function(){
		//if scrollbox is too far left, slide and then hide next button
		if($('#scrollbox ul').position().left < maxNext){			
			$("#scrollbox ul").animate({'left': '-='+itemWidth}, 'slow');
			$('.next').hide();
		}
		//otherwise just scroll and show both buttons
		else {
			$("#scrollbox ul").animate({'left': '-='+itemWidth}, 'slow');
			$('.next').show();
			$('.prev').show();	
		}
	});
	
	
	//prev click
	$('.prev').click(function(){
		//if scrollbox is too far left, slide and then hide next button
		if($('#scrollbox ul').position().left > maxPrev){
			$("#scrollbox ul").animate({'left': '+='+itemWidth}, 'slow');			
			$('.prev').hide();
		}
		else {
			$("#scrollbox ul").animate({'left': '+='+itemWidth}, 'slow');
			$('.prev').show();
			$('.next').show();
		};		
	});
	
	
	
});
