var gHoverTimer;
var gCurrentHoverElement;

function OnHoverIn(lCurrentHoverElement)
{
	if (( gCurrentHoverElement != 0 ) && (lCurrentHoverElement.hasClass("HoverTarget")))
	{
		gCurrentHoverElement.removeClass("HoverTarget");
		gCurrentHoverElement = 0;
		clearTimeout(gHoverTimer);
	}
	

	if ( lCurrentHoverElement.hasClass("slidedown") == false )
	{
		lCurrentHoverElement.addClass("slidedown");
		lCurrentHoverElement.slideDown("fast", function(){ $(this).removeClass("slidedown")} );
	}
}

function HoverOut()
{
	gCurrentHoverElement.removeClass("HoverTarget");
	gCurrentHoverElement.slideUp("fast");
	gCurrentHoverElement = 0;
}

function OnHoverOut(lCurrentHoverElement)
{
	if (( gCurrentHoverElement != 0 ) && (!lCurrentHoverElement.hasClass("HoverTarget")))
		HoverOut();
	
	// reset the timer
	clearTimeout(gHoverTimer);
	
	gCurrentHoverElement = lCurrentHoverElement;
	gCurrentHoverElement.addClass("HoverTarget");
	gHoverTimer = setTimeout(HoverOut, 100);	
}

$(function(){
	// png fix
	$(document).pngFix();
	
	//Main Nav Drop Downs
	gCurrentHoverElement  = 0;
	
	$('#mainNav').children().hover(
      function () 
      {
      	OnHoverIn($(this).find('div:first'));     
      }, 
      function () 
      {
       	OnHoverOut($(this).find('div:first'));
      }
    );
	
	//Show/Hide Active Sub Navigation Sub Menus
    $('ul#subNav li div.subMenu').hide();
    $('ul#subNav li a.active').next().show();
        
    //All Products Filter Tabs

	// Hide the saying
	$(".productListings p.empty").css('display', 'none');



  	$('a.filterTab').click(function(){
        $('a.filterTab.active').removeClass("active");
        $(this).addClass("active");
              
        var filterVal = $(this).text().toLowerCase().replace(' ','-');

   		if (filterVal == 'all') {
			$('div.productListings ul li.hidden').fadeIn('slow').removeClass('hidden');
			$("p.empty").css('display', 'none');
   		} else {
   			$('div.productListings ul li').each(function() {
   				if(!$(this).hasClass(filterVal)) {
   					$(this).fadeOut('normal').addClass('hidden').removeClass('flag');
   				} else {
   					$(this).fadeIn('slow').removeClass('hidden').addClass('flag');
   				}
   			});
   		}

		

		// Go through each
		$('div.productListings').each(function() {
			
			// No visible
			var anyVisible = false;
			
			// Each <li> in this <div>
			$(this).find('li').each(function() {
				
				// Check for hidden
				if ($(this).hasClass("flag")) {
					anyVisible = true;
					
					console.log(anyVisible, this);
				}
				console.log(anyVisible, this);
			});
			
			if (anyVisible == true) {
				// Hide message
				$(this).find('.inner p.empty').css('display', 'none');
				console.log('hide',this);
			} else {
				// Show message
				$(this).find('.inner p.empty').css('display', 'block');
				console.log('show',this);
			}

		});
		
		if (filterVal == 'all') {
			$("p.empty").css('display', 'none');
		}

        return false
    });
    
	// Preload Dropdown Background Image
	$('<img />')
    	.attr('src', '/assets/img/bg_subNav_bright.png')
    	$(this).css("display","none");
	
	//Product Detail Tabs
  	$('a.infoTab').click(function(){
        $('a.infoTab.active').removeClass("active");
        $(this).addClass("active");
        $('.tabContent').hide();
        var showContent = $(this).attr("title");
        $("#"+showContent).show();
        return false        
    });
    
    //Product FAQ Tabs
  	$('a.faqTab').click(function(){
        $('a.faqTab.active').removeClass("active");
        $(this).addClass("active");

        $('div.tabContent').hide();
        $('h2').css("text-indent","-9999px");
        var showContent = $(this).attr("title");
        $("."+showContent).show().css("text-indent","0");

		// Set the active state in the subnav
		$(".faqLink").removeClass('active');
		$("#" + showContent + "Nav").addClass('active');

        return false        
    });
    
    //Product Carousel   
    $("#carousel").jcarousel({
      easing: 'easeOutCubic',
      animation: 500,
      scroll: 2
    });
    
    //Tool Tips    
    $('.tooltipSection a').tooltip({
    	track: true,
    	delay: 0,
    	showURL: false,
    	showTitle: " -",
    	showBody: "define:",
    	extraClass: "glossary",
    	fixPNG: true,
    	left: 0,
    	top: 3
    });
    $('.tooltipSection a').click(function(){
    	return false
    });
    
    //Accordions: Product Detail
    $('#ingredients dd').hide();
    $('#nutritionalInfo dd').hide();
    $('#ingredients dt, #nutritionalInfo dt').click(function() {
  	  $(this).toggleClass('active').next().slideToggle('normal');
    });
    
    //Accordions: Product FAQ
    $('#faqContainer dd').hide();
    $('#faqContainer dt').click(function() {
  	  $(this).toggleClass('active').next().slideToggle('normal');
    });
    
    //Accordions: Our Ingredients Glossary
    $('#glossaryContainer dd').hide();
    $('#glossaryContainer dt').click(function() {
  	  $(this).toggleClass('active').next().slideToggle('normal');
    });
    
    //Articles: Related Links
    $("div.relatedLinks li:even").css("clear", "both");
    
    //Shop: Store Listings
    $("div#storeListings dl:nth-child(3n+1)").css("clear", "right");

    //Shop: Shop Online Accordions
    $('#productAccordions dd').hide();
    $('#productAccordions dt').click(function() {
  	  $(this).toggleClass('active').next().slideToggle('normal');
    });
    
    
    //Show/Hide Comp  
  	$('#showComp').click(function(){
        $('#pageComp').show()
        $('#showComp').hide()
        $('#hideComp').show()
        return false
    });
    $('#hideComp').click(function(){
        $('#pageComp').hide()
        $('#hideComp').hide()
        $('#showComp').show()
        return false
    });



	// INSPIRATION

	// Inspiration Dedications Vote Ajax Call
	$(".inspiredMe").live("click", function() {

		// capture the url to load from the href 
	    var entry_id = $(this).attr("rel");
		
		//var vote = $(this).attr("vote");
		
		var thisElem = $(this);
		
		if ($(this).hasClass("active"))
		{
			var vote = "-";
			$(this).removeClass("active");
		} else {
			var vote = "+";
			$(this).addClass("active");
		}

		// load the second template with AJAX 
		$("#count-" + entry_id).load('/life/ajax_vote/' + entry_id + '/' + vote);

	    // just make sure the page doesn’t reload
	    return false;

	});
	
	
	// STORE LOCATOR
	
	// Submit zip code search as a clean url (/shop/store/48104/)
	$("form#zipSearch").submit(function() {
		var action = $("form#zipSearch").attr("action");
		var zip = $("form#zipSearch input#zipCode").val();
		window.location = action + zip;
		return false;
	});


	// Character countdown, uses limit_100_ class
	$('textarea.countdown').keyup(function(){ 
	      if(this.value.length >= 400) { 
	           //handle the over the limit part here 
	           $(this).addClass('overlimit'); 
	           this.value = this.value.substring(0, 400); 
	      } else { 
	          $(this).removeClass('overlimit'); 
	      } 
	     $('#remaining').text(400-this.value.length + " "); 
	});
	
	
	
});

window.onload = function() {
	var hero_height = ($("#productDetail .productDetailHero").height() - 20) + 'px';
	$("#productDetail #inner").css('padding-top', hero_height);
};









