var currentPanel = 1;
var numPanels = 0;
var timer;
var interval = 15000;

$(document).ready(function() {
	try {
		sIFR.replaceElement(".promotioncontent>h1", named({sFlashSrc: "/swf/stonesans_spotlight_bold.swf", sColor: "#FFFFFF", sFlashVars:"offsetTop=1", sWmode:"transparent"}));
	} catch (e) {}
	
	$('#panel2, #panel3, #panel4').removeClass('hidden');
	$('#panel2, #panel3, #panel4').hide();
	$('#paneltabs').show();
	
	$('#newsticker ul a').css('float', 'left');
	$("#newsticker ul").liScroll().css('visibility', 'visible');; 
	
	numPanels = $("div.spotlightpanel").size();
	
	var panelCookie = $.cookie('panel');
	
	if (panelCookie) {
		var panel = parseInt(panelCookie);
		var nextPanel = (panel == numPanels) ? 1 : panel + 1;
		
		changePanel(nextPanel);
		$.cookie('panel', nextPanel);
	} else {
		timer = setTimeout(rotatePanels, interval);
		$.cookie('panel', 1);
	}
});

function rotatePanels() {
	var nextPanel = (currentPanel == numPanels) ? 1 : currentPanel + 1;

	var currentPanelName = '#panel' + currentPanel;
	var nextPanelName = '#panel' + nextPanel;
	
	var currentPanelTabName = '#paneltab' + currentPanel;
	var nextPanelTabName = '#paneltab' + nextPanel;
	
	var currentPanelTab = '/img/homepage_spotlights/sl_tab_grey_' + currentPanel + '.gif'
	var nextPanelTab = '/img/homepage_spotlights/sl_tab_' + nextPanel + '.gif';
	
	$('.spotlightpanel:not(' + nextPanelName + ')').css('z-index', 1);
	
	$(nextPanelName).css('z-index', 2).css('opacity', 1).fadeIn(2000, function() {
		$('.spotlightpanel:not(' + nextPanelName + ')').hide();		
		timer = setTimeout(rotatePanels, interval);
	});
	
	$(currentPanelTabName).attr('src', currentPanelTab);
	$(nextPanelTabName).attr('src', nextPanelTab);
	currentPanel = nextPanel;
}

function changePanel(panel) {
	clearTimeout(timer);
	
	$('.spotlightpanel').stop();
	
	$('.spotlightpanel:not(#panel' + panel + ')').css('z-index', 1);
	$('#panel' + panel).css('z-index', 2).css('opacity', 1).show();
	$('#paneltab' + currentPanel).attr('src', '/img/homepage_spotlights/sl_tab_grey_' + currentPanel + '.gif');
	$('#paneltab' + panel).attr('src', '/img/homepage_spotlights/sl_tab_' + panel + '.gif');
	$('.spotlightpanel:not(#panel' + panel + ')').hide();
	
	currentPanel = panel;
	timer = setTimeout(rotatePanels, interval);
}