//********************************************************
//HOME HEADER SLIDE CODE BEGIN
//********************************************************

var currentSlideItem = -1;
var totalSlideItems = $('#slideCntr .slideItem').length;
var slideAuto = true;

var cancelSlideAutoInterval = null;
var autoSlideInterval = null;

var slideInterval = 3000;
var stopDelay = 15000;
var transitionSpeed = 300;

var lostFocus = false;

$(window).focus(function()
{
	if(lostFocus)
	{
		//when widow receives focus, start this again
		startSlideAuto();
	}
});

$(window).blur(function()
{
	//stop the slide animation when window looses focus
	clearInterval(autoSlideInterval);
	autoSlideInterval = null;
	
	clearInterval(cancelSlideAutoInterval);
	cancelSlideAutoInterval = null;
	
	lostFocus = true;
});


for(var i = 0; i < totalSlideItems; i++)
{
	//.slideItemLink styled in CSS
	var html = 	'<a class="slideItemLink" href=""><span>Test</span></a>';
	$('#slideLinks').append(html);
}

//set the width of the bubble cntr
$('#slideLinks').css('width', totalSlideItems * 24);

//assign click event to each link
$('#slideLinks .slideItemLink').each(function(index)
{
	var pos = index;
	$(this).click(function()
	{
		slideItem(pos);
		slideAuto = false;
		
		//clear the autoSlideInterval and flag it
		clearInterval(autoSlideInterval);
		autoSlideInterval = null;
		
		clearInterval(cancelSlideAutoInterval);
		cancelSlideAutoInterval = setInterval(startSlideAuto, stopDelay);
		
		//remove css and add it to current item 
		$('#slideLinks .slideItemLink').each(function()
		{
			$(this).removeClass('selected');
		});
		
		$(this).addClass('selected');
		
		return false;
	});
});

function slideItem(position)
{
	if(position != undefined && position < totalSlideItems)
	{	
		var newXPos = -932 * position;
		currentSlideItem = position;
		
		//remove css and add it to current item 
		$('#slideLinks .slideItemLink').each(function(index)
		{
			$(this).removeClass('selected');
			
			if(position == index)
			{
				$(this).addClass('selected');
			}
			
		});
		
		$('#slideCntr').animate({left: newXPos}, transitionSpeed);
		
		return false;
	}
	
	return false;
}

function autoAnimate()
{
	if(!slideAuto)
		return false;
	
	if(currentSlideItem < totalSlideItems - 1)
	{
		currentSlideItem += 1;
	}
	else
	{
		currentSlideItem = 0;
	}
	
	//cacel the cancel auto slide interval
	if(cancelSlideAutoInterval != null)
	{
		//clear the cancelSlideAutoInterval and flag it
		clearInterval(cancelSlideAutoInterval);
		cancelSlideAutoInterval = null;
	}
	
	//call the autoAnimate function
	slideItem(currentSlideItem);
	
	//start the timer for auto slide
	if(autoSlideInterval == null)
	{
		if(autoSlideInterval != null)
			clearInterval(autoSlideInterval);
		
		autoSlideInterval = setInterval(autoAnimate, slideInterval);
	}
}

function startSlideAuto()
{
	slideAuto = true;
	autoAnimate();
}

startSlideAuto();

//********************************************************
//HOME HEADER SLIDE CODE END
//********************************************************
