/*
 * Codestars spotlights
 * @author Floris Weijenburg (http://www.florisweijenburg.nl)
 * @copyright (c) 2008 Floris Weijenburg - All programming rights reserved.
 * @version 31-05-09
*/

CS.kernel.spotlights = function() 
{
	this.Init();
}

CS.kernel.spotlights.prototype.Init = function()
{
	CS.kernel.spotlights.activeSpotlightId = null;
		
	var nav = document.getElementById('spotlightNaviation');	
	var items = nav.getElementsByTagName('li');
	var i;
		 
	for(i=0; i < items.length; i++)
	{
		YAHOO.util.Event.addListener(items[i], "click", this.ShowSpotlightListener, this); 		
	}
	
	this.ShowSpotlight(0, true);
}

CS.kernel.spotlights.prototype.ShowSpotlightListener = function(e, obj) 
{
	var index = parseFloat(this.id);
	obj.ShowSpotlight(index, false);	
}

CS.kernel.spotlights.prototype.ShowSpotlight = function(index, addTimer) 
{
	var nav = document.getElementById('spotlightNaviation');	
	var items = nav.getElementsByTagName('li');
	var id = items[index].lang;
	var loopSec = 10;
	
	var spotlight = document.getElementById('spotlightTab_' + id);	
	
	// Hide old spotlight
	if(this.activeSpotlightId >= 0)
	{
		var oldItem = items[this.activeSpotlightId];
		this.HideSpotlight(oldItem);
	}
	
	if(spotlight)
	{
		spotlight.style.display = 'block';
		
		// Hi-lite the current list item
		items[index].className = 'spotlightHover';
	}
	
	// Set as current active spotlight id
	this.activeSpotlightId = index;
	
	if (addTimer == true) 
	{
		// Lauch next spotlight loop
		setTimeout(function(){ this.ShowNextSpotlight(items); }.bind(this), loopSec * 1000);
	}
}

CS.kernel.spotlights.prototype.HideSpotlight = function(item)
{
	var spotlight = document.getElementById('spotlightTab_' + item.lang);	
	
	if (spotlight) 
	{
		spotlight.style.display = 'none';
		item.className = '';
	}
}

CS.kernel.spotlights.prototype.ShowNextSpotlight = function(items)
{
	if(this.activeSpotlightId != 'undefined') 
	{
		// Show the next spotlight in the spotlights array	
		var nextIndex = (this.activeSpotlightId) + 1;
		
		if(nextIndex > items.length -1)
		{
			// Start at the first index again
			nextIndex = 0;
		}

		this.ShowSpotlight(nextIndex, true);
	}
	else 
	{
		// Show first spotlight
		this.ShowSpotlight(0, true);
	}
}
