// Global variable - if the carousel is in the middle of an action, do not allow the start of another action
// avoids additional cloning of list elements

$inSession = false;


$( '#widgetProgramsLeft' ).click(moveWidgetProgramsLeft);
$( '#widgetProgramsRight' ).click(moveWidgetProgramsRight);

function moveWidgetProgramsLeft()
{
	if ($inSession == true)
	{
		return;
	}
	else
	{
		$inSession = true;
		
		//clone last element to the beginning
		$( '#carouselProductsList li:last' ).clone().prependTo( '#carouselProductsList' );
		//immediately move list to the left
		$( '#carouselProductsList' ).animate({"margin-left": "-=188px"}, 0);
		
		//movelist
		$( '#carouselProductsList' ).animate(
			{"margin-left": "+=188px"}, 
			800, 
			function()
			{ 
				//removelastelement 
				$( '#carouselProductsList li:last' ).remove();
				$inSession = false;
		
			}
		);
	}


}


function moveWidgetProgramsRight()
{

	if ($inSession == true)
	{
		return;
	}
	else
	{
		$inSession = true;
		//clone first element to the end
		$( '#carouselProductsList li:first' ).clone().appendTo( '#carouselProductsList' );
		
		//movelist
		$( '#carouselProductsList' ).animate(
			{"margin-left": "-=188px"}, 
			800, 
			function()
			{ 
				//movelistright immediately
				$( '#carouselProductsList' ).animate({"margin-left": "+=188px"}, 0);
				
				//removefirstelement 
				$( '#carouselProductsList li:first' ).remove();
		
				
				$inSession = false;
			}
		);
	}


}

$(document).ready()
{
	//start the animation after 4 seconds on page load
	//setTimeout(moveWidgetProgramsRight, 4000);
	
	//set the animation loop interval to 4 seconds
	var interval = setInterval(moveWidgetProgramsRight, 4000);
	
	//on hover, pause the animation loop
	$('#widgetPrograms').hover(function() {
        clearInterval(interval);
    }, function() {
        interval = setInterval(moveWidgetProgramsRight, 4000);
    });
    $('#widgetProgramsRight').hover(function() {
        clearInterval(interval);
    }, function() {
        interval = setInterval(moveWidgetProgramsRight, 4000);
    });
    $('#widgetProgramsLeft').hover(function() {
        clearInterval(interval);
    }, function() {
        interval = setInterval(moveWidgetProgramsRight, 4000);
    });



};
