// CONSTANTS
var DEFAULT_ITEM_DURATION = 4; // 1 sec

// VARIABLES
var currentPageId = 0;
var totalPages = 0;
var container;
var loopTimeout;
var currentContentPage;
var previousItem;
var durationOfCurrentItem;


function initSlideshow(){
	container = $('div.MainBanner');
	totalPages = $('a', container).length;	
	tabsContainer = $('div.buttons');
	
	// init pause handlers
	$('a, div.buttons', container).mouseover(function(){
		pauseSlideshow();
	});
	$('a, div.buttons', container).mouseout(function(){
		continueSlideshow();
	});	
	
	// first item
	showNextItem();	
}


function showNextItem(id){
	// show specific page
	if(id >= 0){
		currentPageId = id;
	}
	
	previousItem = currentContentPage;
	currentContentPage = $('a.mainbanner'+currentPageId, container);
	
	// read how long should be shown this item
	durationOfCurrentItem = Number(currentContentPage.attr('rel'));
	if(isNaN(durationOfCurrentItem) || durationOfCurrentItem == 0){
		durationOfCurrentItem = DEFAULT_ITEM_DURATION;
	}

	if(previousItem){
		previousItem.addClass('hidden');
		$('td', tabsContainer).removeClass("active01");
		
	}
	if(currentContentPage){
		currentContentPage.removeClass('hidden');
		$('td.mainbanner'+currentPageId, tabsContainer).addClass("active01");
	}
	
	// timer for next item
	if(loopTimeout){
		clearTimeout(loopTimeout);
	}
	loopTimeout = setTimeout("showNextItem()", durationOfCurrentItem * 1000);
	
	if(currentPageId < totalPages-1){
		currentPageId++;
	}else{
		currentPageId = 0;
	}
}

function pauseSlideshow(){
	if(loopTimeout){
		clearTimeout(loopTimeout);
	}
}

function continueSlideshow(){
	loopTimeout = setTimeout("showNextItem()", durationOfCurrentItem * 1000);
}

