// Timer Configuration
var DELAY = 15000;

$(function()
{
	$("div#imagetickerhide").hide();
	
	var currentIndex;
	var ss = $('div#front-img-ticker');
	var elementContainer = ss.find('div.text-container div.text');
	var titleContainer = elementContainer.children('h3.title');
	var textContainer = elementContainer.children('p');
	var links = $("div.links ul li");
	var totalSlides = links.length;
	
	links.each(function()
	{
		var i = $(this).parent().children().index($(this));
		$(this).text(i+1);
		
		$(this).click(function()
		{
			showSlide(i+1);
		});
	});
	
	// Show the first slide by default
	$("div.links ul li:first").click();
	
	// Start the timer
	var timer;
	resetTimer();
	
	function resetTimer()
	{
		if(timer)
			clearTimeout(timer);
		timer = setTimeout(onTimerComplete, DELAY);
	};
	
	function showSlide(index)
	{
		if(index > totalSlides)
			index = 1;
		currentIndex = index;
		resetTimer();
		links.removeClass('selected');
		links.eq(index-1).addClass('selected');
		titleContainer.html(this['ss_title'+index]);
		textContainer.html(this['ss_text'+index]);
		ss.css('background-image', 'url('+this['ss_image'+index]+')');
	};
	
	function onTimerComplete()
	{
		showSlide(currentIndex+1);
	};
});
