
/*
*	Børge Warvik - bowarv at gmail dot com
*	Created: 2007-09-29
*	Last edit: 2007-10-04
*
*	Animates the sponsors on the page. 
*/

if(typeof TUIL=="undefined"){var TUIL={};}

TUIL.sponsors = function()
{
	// conatiner = parent div, children = parent div children, el = active div
	var container = null, children = null, el = null;
	
	// How long to show ads, 1000 = 1 sec
	var showtime = 5000;
	
	// animation time, fade out = 1 sec, fade id = 1 sec
	var duration = 1;
	
	// current ad index 
	var currentIndex = 0;
	
	// Initialize the animation
	var start = function()
	{
		setTimeout(fadeOut, showtime);
	};
	
	// Fades out the current ad
	var fadeOut = function()
	{
		el = children[currentIndex];

		new Effect.Opacity( el, { duration: duration, from: 1, to: 0, afterFinish: hide } );
	};
	
	// Fades in the next ad
	var fadeIn = function()
	{
		el = children[currentIndex];
		new Effect.Opacity( el, { duration: duration, from: 0, to: 1, beforeStart: show } ) ;
	};
	
	// Hides the current element after the opacity is set to 0 - if not ads will not have same top position
	var hide = function()
	{
		el.addClassName("hidden");
		currentIndex = currentIndex + 1;
		currentIndex = currentIndex == children.length ? 0 : currentIndex;
		fadeIn();
	};
	
	// Set element to shows opacity to 0 and remove hidden value
	var show = function()
	{
		el.setStyle( { opacity: 0 } );
		el.removeClassName( "hidden" );
		setTimeout(fadeOut, showtime);
	};
	
	// Public functions
	return {
		init: function()
		{
			container = $("fpccond");
			children = container.getElementsByClassName("fpccon_outer");

			start();			
		}
	};
}();

// Run script when window has loaded
Event.observe( window, 'load', TUIL.sponsors.init );
