﻿// Product details realted toggle
function productRelatedToggle()
{
    $('#related-toggle > li > a').click(function(e)
    {
        e.preventDefault();
        
        var showDiv = $(this).parent('li').attr('class');
        
        $('#related-toggle > li > a').each(function(i)
        {
            $(this).removeClass('active');
        })
        
        $(this).addClass('active');
        
        $('#featured-products > div').each(function(i)
        {
            if($(this).hasClass(showDiv))
            {
                $(this).show();
            }
            else
            {
                $(this).hide();
            }
        })
    });
}
// End

//Product details scroll bar (Tool tips, and Hover classes)
function setWidth(){
	//Set width for each individual block
	$('#product-nav > div.block').each(function(i){
		var tipList = $(this).children('ul');		
		$(tipList).children('li.tooltip:last').addClass("last");
		
		var widthBlock = $(this).width();
		$(this).css("width", widthBlock);		
	});
	
	$('div.sub-block').each(function(i){
		var tipList = $(this).children('ul');		
		$(tipList).children('li.tooltip:last').addClass("last");	
		
		var widthSubBlock = $(this).width();
		$(this).css("width", widthSubBlock);	
	});
	
	//Calculate width of seperators for centering sub-catagory
	$('div.seperator').each(function(i){
		//Sets width of wrapper
		var widthFull = $(this).width();
		$(this).css("width", widthFull);
		
		var seperatorChild = $(this).children("p")
		var widthShort = seperatorChild.width();
		var widthShortSet = widthShort + 8;
		var widthSHalf = widthShort / 2;
		
		seperatorChild.css({
			"margin-left" : -widthSHalf,
			"width" : widthShortSet	
		});
	});	

	//Set total width for full bar
	var productWidthTotal = 0;
	
	$('#product-nav > .block').each(function(i){
		productWidthTotal += 6 + $(this).width();	
	});
	
	$('#product-nav').css("width", productWidthTotal);
	
	
	$('#draggable-view > .block').each(function(i){
		var calWidthTotal = 0;
		
		$(this).children('.sub-block').each(function(i){
			calWidthTotal += 6 + $(this).width();	
		})
		
		$(this).css("width", calWidthTotal);	
	});
}

// Setting up the product details slider
function setSlider(){
	var productWidth = 0;
    var scrollSpace = 0;
    
    // Calculating products width
    $('#products > .category').each(function(i)
    {
        productWidth += $(this).outerWidth(true);
    });
    
    $('#products').width(productWidth);
    
    // Calculating allowed scroll width
    scrollSpace = productWidth - 840;
	
	var scrollPosition = $('.selected').position().left;
	var scrollViewable = scrollSpace + 60;
	var scrollFollow = 0;
	
	var scrollbar = $('#product-scrollbar');		
	var slider = $(scrollbar).slider({
		value: scrollPosition,
		min: 0,
		max: scrollSpace,
		slide: function (ev, ui) {
			if(ui.value <= scrollSpace){$('#products').css('left', '-' + ui.value + 'px');}
		}
	});
	
	if(scrollPosition < scrollViewable)
	{
		//console.log('scrollPosition < scrollViewable');		
		scrollFollow = scrollPosition - 40;		
		//console.log(scrollFollow);
		
		if(scrollFollow < 20)
		{
			//console.log('scrollFollow < 20');
			scrollFollow = 0;
			$('#products').css('left', '0');
			//console.log(scrollFollow);
		}
		else
		{
			$('#products').css('left', '-' + scrollFollow + 'px');
		}
	}
	else
	{
		$('#products').css('left', '-' + scrollSpace + 'px');
	}
}

// Enabling custom tooltips on items within the
// product details scrolbar
function enableToolTips()
{	
	$('.tooltip').hover(
		function()
		{
			if($(this).hasClass('selected') == false)
			{
				// Set content first to get proper width
				var tooltipText = $(this).find('.tooltip-text').html();
				
		        $('#tooltip > .content').html(tooltipText);
		        
		        // Do the rest
		        var elemPos = $(this).offset();
		        var tooltipWidth = $('#tooltip').width();
		        var elemWidth = $(this).width();
		        var movePos = {};
		        
		        movePos.left = elemPos.left - (tooltipWidth / 2) + (elemWidth / 2);
		        movePos.top = elemPos.top - 40;
		        
		        $('#tooltip > .content').html(tooltipText);
		        $('#tooltip').css({ left: movePos.left, top: movePos.top, visibility: 'visible' });	
			}
	    },
	    function()
	    {
	    	if($(this).hasClass('selected') == false)
			{
				$('#tooltip').css({ visibility: 'hidden' });
			}
	    }
    );
    
    var pTipContent = $('#protein-disclosure').html();
    
    $('span.p-disclosure > a').simpletip({
    	content: pTipContent, 
    	position: [48, 20]
    });
    
}

function prepCategories()
{
    $('.category > ul > li:last-child').addClass('last');
}

$(document).ready(function()
{
    prepCategories();
    productRelatedToggle();
	setSlider();
	enableToolTips();
});