var oEtalage = 
{
	/**
	 * The maximum amout of items to show
	 * @var int
	 */
	LIMIT			: 5,	
		
	/**
	 *  This array holds info objects about each item
	 *  @var Array
	 */
	aItems			: new Array(),

	/**
	 * The time in ms before an item switches
	 * @var int
	 */
	iInterval		: 5000,
	
	/**
	 * The time in ms before playing starts after it was by a mouse over/out
	 * @var int
	 */
	iResetTimeout	: 2000,
	
	/**
	 * The ID of the interval
	 * @var int
	 */
	iIntervalID		: -1,
	
	/**
	 * The index of the currently active item
	 * @var int
	 */
	iCurrentItem	: -1,
	
	/**
	 * Whether the 'animation' is active
	 * @var bool
	 */
	bPlaying		: false,	

	/**
	 * Whether the 'animation' is paused
	 * @var bool
	 */
	bPaused			: false,	

	/**
	 * Add an item to the collection if the length is below limit
	 * @param	string sButtonID	The HTML ID of the button that selects this item
	 * @param	string sTitle		The title of the item
	 * @param	string Intro		The contents of HTML ID of the image container
	 * @param	string sLink		The link URI of the item
	 */
	addItem			: function(sButtonID, sImageID, sTitle, sIntro, sLink)
	{
		if(oEtalage.aItems.length >= oEtalage.LIMIT)
		{
			return;
		}
			
		oEtalage.aItems.push(
		{
			sButtonID: sButtonID,
			sImageID: sImageID,
			sTitle: sTitle,
			sIntro: sIntro,
			sLink: sLink
		});
		
		var iItemID = oEtalage.aItems.length - 1;
		$(sButtonID).observe
		(
			'mouseover',
			function()
			{
				oEtalage.showItem(iItemID);
				oEtalage.pause(); 
			}
		);
		
		$(sButtonID).observe
		(
			'mouseout',
			function()
			{
				setTimeout
				(
						
					function()
					{
						oEtalage.iIntervalID = -1;
						oEtalage.iCurrentItem = -1;						
						oEtalage.bPlaying = false;	
						oEtalage.bPaused = false;
					},
					oEtalage.iResetTimeout
				);
			}
		)
	},
	
	/**
	 * Stop an item by ID
	 * @param int	iID		ID in the item array
	 */
	showItem : function(iID)
	{
		if(oEtalage.iCurrentItem != iID)
		{			
			for(var i = 0; i < oEtalage.aItems.length; i++)
			{
				var oItem = oEtalage.aItems[i];
				if(iID == i)
				{					
					// Make the attribute setting IE compatible		
					$(oItem.sButtonID).setAttribute('class', 'active');					
					$(oItem.sButtonID).setAttribute('className', 'active');					
					$(oItem.sImageID).style.display = 'block';					
					$(oItem.sImageID).style.zIndex = 10;
					$('etalage_readmore_link').href = oItem.sLink;
					$('etalage_itembar_title').innerHTML = oItem.sTitle;
					$('etalage_content').innerHTML = oItem.sIntro;
					oEtalage.iCurrentItem = iID;
				}
				else
				{
					$(oItem.sButtonID).setAttribute('class', '');
					$(oItem.sButtonID).setAttribute('className', '');
					$(oItem.sImageID).style.display = 'none';					
					$(oItem.sImageID).style.zIndex = i;
				
				}
			}
		}
		
	},
	
	/**
	 * Pause playing (if playing)
	 */
	pause : function()
	{
		if(!oEtalage.bPaused)
		{
			if(oEtalage.iIntervalID > -1)
			{
				clearInterval(oEtalage.iInterval);
			}
			oEtalage.bPaused = true;
		}
	},
	
	/**
	 * Stop playing (if not playing or paused)
	 */
	play : function(iInterval)
	{
		if((!oEtalage.bPlaying || oEtalage.bPaused) && oEtalage.aItems.length)
		{
			if(!oEtalage.bPaused)
			{
				oEtalage.showItem(0);
			}
			oEtalage.iIntervalID = setInterval(oEtalage.showNext, oEtalage.iInterval);
			oEtalage.bPlaying = true;
			oEtalage.bPaused = false;
		}
	},
	
	/**
	 * Stop playing (if playing)
	 */
	stop : function()
	{
		if(oEtalage.bPlaying)
		{
			clearInterval(oEtalage.iIntervalID);
			oEtalage.bPlaying = false;
		}
		
	},
	
	/**
	 * Show next item
	 */
	showNext : function()
	{
		if(!oEtalage.bPaused)
		{
			var iNextID = oEtalage.iCurrentItem + 1;
			if(oEtalage.iCurrentItem >= oEtalage.aItems.length - 1)
			{
				iNextID = 0;
				
			}
			oEtalage.showItem(iNextID);
		}
	}
	
}

// Call the etalage to start after document is loaded:
document.observe('dom:loaded', oEtalage.play);
