﻿var rotating = false;
var containerSize = 670;
var startPos = 0;
var itemWidth = 165;
var itemCount = 0;
var padding = 0;
var currentSection = 'Infant';//Default start section
$(document).ready(
    
	function(){
	itemCount = $('div#Product_Wheel ul li').length;
		$('div#Right_Main_Scroll').mouseover(
		    function(){
		        
		        rotating = true;
		        animateInterval = setInterval("scrollRight()", 210);		 	               		        
		    }
		);			
		$('div#Right_Main_Scroll').mouseout(
		    function(){
		        
		        rotating = true;
		        clearInterval(animateInterval);		 	               		        
		    }
		);	
		$('div#Left_Main_Scroll').mouseover(
		    function(){
		        
		        rotating = true;
		        animateInterval = setInterval("scrollLeft()", 210);		 	               		        
		    }
		);			
		$('div#Left_Main_Scroll').mouseout(
		    function(){
		        
		        rotating = true;
		        clearInterval(animateInterval);		 	               		        
		    }
		);	
		$('a.product_image_link img').mouseover(
		    function(){		        
		        $(this).attr('src', $(this).attr('src').replace(/_sm\.jpg$/,'\_over.jpg'));
		    }
		 );
		 $('a.product_image_link img').mouseout(
		    function(){		        
		        $(this).attr('src', $(this).attr('src').replace(/_over\.jpg$/,'\_sm.jpg'));
		    }
		 );
		 $('a.product_image_link img').click(
		    function(){		
		        $('a.product_image_link img').each(
		            function(){		                
		                $(this).attr('src', $(this).attr('src').replace(/_click\.jpg$/,'\_sm.jpg'));
		            }
		        );        
		        		         
		        $(this).attr('src', $(this).attr('src').replace(/_over\.jpg$/,'\_click.jpg'));
		    }
		 );
		
	}	
);

function scrollRight(){
if(continueScrollingRight()){
    $('div#Left_Main_Scroll').removeClass('disabled');
    var startPosition = $('div#Product_Wheel').css('left').replace('px', '');
    var startNum = parseInt(startPosition);
    var newPos = startNum - itemWidth;  
    var wheelLength = itemCount * (itemWidth + padding);
    wheelLength = wheelLength - containerSize;      
    if(newPos < -wheelLength){
        newPos = -wheelLength;
    } 
    setSection(newPos);
    $('div#Product_Wheel').animate({left: newPos+'px'}, 200, 'linear');
    }
 else{
    $('div#Right_Main_Scroll').addClass('disabled');
    }
}

function scrollLeft(){
if(continueScrollingLeft()){
    $('div#Right_Main_Scroll').removeClass('disabled');
    var startPosition = $('div#Product_Wheel').css('left').replace('px', '');
    var startNum = parseInt(startPosition);
    var newPos = startNum + (itemWidth + padding);
    if(newPos > startPos){
        newPos = startPos;
    }        
    setSection(newPos);
    $('div#Product_Wheel').animate({left: newPos+'px'}, 200, 'linear');
    }
else{
    $('div#Left_Main_Scroll').addClass('disabled');
    }
}

//Determines whether or not to disable right scroll
function continueScrollingRight(){
    var startPosition = $('div#Product_Wheel').css('left').replace('px', '');
    var startNum = parseInt(startPosition);
    var wheelLength = $('div#Product_Wheel').css('width').replace('px', '');
    var wheelLength = itemCount * (itemWidth + padding);
    wheelLength = wheelLength - containerSize;      
    if(startPosition <= -wheelLength){    
        return false;
    }else{
        return true;
    }
}

//Determines whether or not to disable left scroll
function continueScrollingLeft(){
    var startPosition = $('div#Product_Wheel').css('left').replace('px', '');
    var startNum = parseInt(startPosition);    
    if(startNum >= startPos){
        return false;
    }else{
        return true;
    }
}

function nextSection(){
    switch(currentSection){
        case 'Infant':
            gotoSection('Child');            
            break;
        case 'Child':
            gotoSection('Adult');            
            break;
        case 'Adult':
            gotoSection('Sports');            
            break;
        case 'Sports':
            gotoSection('Infant');           
            break;
    }
}
function prevSection(){
    switch(currentSection){
        case 'Infant':
            gotoSection('Sports');            
            break;
        case 'Child':
            gotoSection('Infant');            
            break;
        case 'Adult':
            gotoSection('Child');            
            break;
        case 'Sports':
            gotoSection('Adult');            
            break;
    }
}
//Moves the carousel to selected section and updates the status bar at the bottom of the carousel.
function gotoSection(sectionName){
            var currCount = 0;
            $('li.product_list').each(
                function(){
                    if($(this).hasClass(sectionName)){
                        var newPos = startPos - (currCount * itemWidth);
                        $('div#Product_Wheel').animate({left: newPos+'px'}, 200, 'linear');
                        if(newPos >= startPos){
                           $('div#Left_Main_Scroll').addClass('disabled');
                        }else{
                            $('div#Left_Main_Scroll').removeClass('disabled');
                        }
                        var wheelLength = itemCount * (itemWidth + padding);
                        wheelLength = wheelLength - containerSize; 
                        if(newPos <= -wheelLength){
                            $('div#Right_Main_Scroll').addClass('disabled');
                        }else{
                            $('div#Right_Main_Scroll').removeClass('disabled');
                        }
                    }
                    else
                    {
                        currCount++;                        
                    }
                }
            );
            currentSection = sectionName;
            var imgName = '/images/Product_Carousel/Slider_Bottom_'+sectionName+'.gif';            
            $('img#section_bottom_img').attr('src', imgName);
            

}
//Used to update status bar at the bottom of the carousel.  Possible values are Infant, Child, Adult, or Sports
function setSection(currLocation){
       var currCount = 0;
       var section = 'Infant';
       var infantMarker = 0;
       var childMarker = 0;
       var AdultMarker = 0;
       var SportsMarker = 0;
       $('li.product_list').each(       
            function(){                
                
                if($(this).hasClass('Infant')){
                    infantMarker = currCount*(itemWidth + padding);
                }
                if($(this).hasClass('Child'))
                {
                    childMarker = currCount*(itemWidth + padding);
                }
                if($(this).hasClass('Adult'))
                {
                    AdultMarker = currCount*(itemWidth + padding);
                }
                if($(this).hasClass('Sports'))
                {
                    SportsMarker = currCount*(itemWidth + padding);
                }
                currCount++;                
            }
        );
        /*The 20px buffer is added so that the section will change before the section is hit. That way
          if the user stops in between items the marker will display the next section like it had scrolled all the way.
          This is simply a display detail.
        */
        if(currLocation <= (20 - infantMarker))
        {
                section = 'Infant';
        }
        if(currLocation <= (20 - childMarker))
        {
                section = 'Child';
        }
        if(currLocation <= (20 - AdultMarker))
        {
                section = 'Adult';
        }
        if(currLocation <= (20 - SportsMarker))
        {
                section = 'Sports';
        }
        currentSection = section;
        var imgName = '/images/Product_Carousel/Slider_Bottom_'+section+'.gif';            
        $('img#section_bottom_img').attr('src', imgName);
}

function setItem(id, imgName){
        var imgPath = 'url(/images/Product_Carousel/Product_Images/' + imgName+')';
        $('div#ctl00_ContentPlaceHolder_product_details').css('background-image', imgPath);
        var descSelector = 'div#'+id;
        $('div.product_desc').css('display', 'none');
        $(descSelector).css('display', 'block');
        
}

