﻿$(document).ready(
	function(){
		/* Side Bar */
		/* Set / Remove expandable class */
		$('#SideBarMenu ul.topCategory > li').each(
			function(){
				if($(this).has('ul.subCategory')){
					$('ul.topCategory > li').has('ul.subCategory').children('a').addClass('expandable');				
				}
			}
		);	

		$('#SideBarMenu ul.subCategory > li').each(
			function(){
				if($(this).has('ul.subSubCategory')){
					$('ul.subCategory > li').has('ul.subSubCategory').children('a').addClass('expandable');
				}
			}
		);
		
		$('#SideBarMenu ul.topCategory > li').hover(
			function(){
				var leftOffSet = $(this).width();
				$('ul.subCategory', this).stop(true, true).css('left', leftOffSet).show(400);				
			}
			,
			function(){
				$('ul.subCategory', this).stop(true, true).delay(100).hide(400);
			}
		);

		$('#SideBarMenu ul.subCategory > li').hover(
			function(){			
				var leftOffSet = $(this).width();			
				$('ul.subSubCategory', this).stop(true, true).css({left:leftOffSet}).show(400);
			}
			,
			function(){
				$('ul.subSubCategory', this).stop(true, true).delay(100).hide(400);
			}
		);
		/* END Side Bar */	

		/* product page tab display */	
		$('a#ProductDescription').hover(
			function(){			
				$(this).css("cursor", "pointer");
			}
			
		);
		
		$('a#ProductIngredients').hover(
			function(){			
				$(this).css("cursor", "pointer");
			}
			
		);
		
		$('a#HowTo').hover(
			function(){			
				$(this).css("cursor", "pointer");
			}
			
		);		
		
		$('a#ProductDescription').click(
			function(){	
				$('div.DLproductDescription').show();
				$('div.DLproductIngredients').hide();
				$('div.DLproductHowTo').hide();
			}
		);
		
		$('a#ProductIngredients').click(
			function(){	
				$('div.DLproductDescription').hide();
				$('div.DLproductIngredients').show();
				$('div.DLproductHowTo').hide();
			}
		);
		
		$('a#HowTo').click(
			function(){	
				$('div.DLproductDescription').hide();
				$('div.DLproductIngredients').hide();
				$('div.DLproductHowTo').show();

			}
		);
		
		/* Product Fly Out */
	    $('span.productFlyOutIcon').mouseover(
	        function() {
	        	$('div.productFlyOut').hide(500);	        	
	        	$(this).parent().parent().find('div.productFlyOut').stop(true, true).show(500);
	        }
	    );
	    $('div.productFlyOut').mouseleave(
	        function() {
	            $('div.productFlyOut').stop(true, true).hide(500);
	        }
	    );

		/* Sub Cat Fly Out */
	    $('ul.grid span.subCatFlyOutIcon').mouseover(
	        function() {
	        	$('div.subCatFlyOut').hide(500);
	            $(this).parent().parent().find('div.subCatFlyOut').stop(true, true).show(500);
	        }
	    );
	    $('div.subCatFlyOut').mouseleave(
	        function() {
	            $('div.subCatFlyOut').stop(true, true).hide(500);
	        }
	    );
	    
		$('span.closeButton').click(
			function(){
				$('div.productFlyOut').stop(true, true).hide(500);
				$('div.subCatFlyOut').stop(true, true).hide(500);
			}
		);

		/* Footer Index */	
		$('span.footerIndexExpand').click(
			function(){
				if($('div#FooterIndexList').is(':visible')){
					$('div#FooterIndexList').slideUp(500);
					$(this).html('+');
				} else{					
					$('div#FooterIndexList').slideDown(500);
					var targetOffset = $('div#FooterIndexList').offset().top;
					$("html,body").animate({scrollTop: targetOffset-50}, 1000); 
					$(this).html('-');
				}
			}
		);
		
		$('h2#VisiblePageHeadline').showHideContent();
	}	
);

jQuery.fn.showHideContent = function() {
	
	var elementHtml = $(this).html();
	
	$(this).click(
		function(){
			if($('span.more').is(':visible')){			
				
				$('span.more').stop(true, true).delay(100).fadeOut(400);	
								
				if($('p#ShortPageContent').length){
					$('p#ShortPageContent').slideDown(600);
				}
				
				if($('h3#SubHeadline').length){
					$('h3#SubHeadline').slideDown(600);
				}
				
				return false;
			
			} else {
								
				$('span.more').stop(true, true).delay(300).fadeIn(400);				
				
				if($('p#ShortPageContent').length){
					$('p#ShortPageContent').slideUp(200);
				}
				
				if($('h3#SubHeadline').length){	
					$('h3#SubHeadline').slideUp(200);			
				}
				
				return false;
			}			
		}
	);
	
	if(($('p#ShortPageContent').length || $('h3#SubHeadline').length) && elementHtml.length){			
		$(this).append("<span class='more'>&nbsp;...more</span>");
	}
}

/* Client inline js code */

function setWatermark(text, event) {
        var defaultText = " Keyword, SKU #";
        if (event.type == "blur" && (text.value.length == 0 || text.value == " ") ) 
        {
            text.style.color = "gray";
            text.value = defaultText;
            return;
        }
        if ( event.type == "focus" && text.value == defaultText) {
           
            text.style.color = "gray";
            text.value = " ";
            setCaretPosition(text, 1);
            return;
        }
    }
  function setCaretPosition(elem, caretPos) {
   
    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }
}
