/**
 * Delay execution for the given number of milliseconds
 *
 * param	int Number of milliseconds
 * return	void
 */
function sleep (milliseconds)
{
	var sleep	= new Date ();
	var wakeup	= (sleep.getTime () + milliseconds);
	
	while (sleep.getTime () < wakeup)
	{
		sleep = new Date ();
	}
}

// ------------------------------------------------------------------

/**
 * Object that handles rotating banners for all
 *	BannerRotators that have been added to the object.
 */
var BannerController =
{
	handlers:	[],
	rotators:	[],

	/**
	 * Adds a BannerRotator to the BannerController
	 *
	 * param	BannerRotator
	 * return	void
	 */
	addRotator: function (rotator)
	{
		this.rotators[this.rotators.length] = rotator;
	},

	/**
	 * Start rotating the banners for the BannerRotators 
	 *	that have been added to the BannerController
	 *
	 * return	void
	 */
	start: function ()
	{
		for (var i=0; i<this.rotators.length; i++)
		{
			this.rotate (i);
			this.handlers[i] = setInterval ('BannerController.rotate (' + i + ')', this.rotators[i].delay);
			
			// It is important that we pause briefly so that we aren't
			//	trying to rotate all the banners at the same time.
			//	Simultaneous rotations has proven to randomly skip 
			//	rotating any given banner from time to time. A four
			//	millisecond pause per banner seems to be the minimum
			//	time requirement to keep things running smoothly.
			sleep (this.rotators.length * 4);
		}
	},

	/**
	 * Stop rotating the banners for the BannerRotators 
	 *	that have been added to the BannerController
	 *
	 * return	void
	 */
	stop: function ()
	{
		for (var i=0; i<this.handlers.length; i++)
		{
			clearInterval (this.handlers[i]);
			this.handlers[i] = undefined;
		}
		this.handlers = [];
	},

	/**
	 * Rotate the BannerRotator at index (i)
	 *
	 * param	int Index of BannerRotator to be rotated
	 * return	void
	 */
	rotate: function (i)
	{
		// For IE apply a fade effect
		if (document.all && this.rotators[i].fade)
		{
			this.rotators[i].img.style['filter'] = 'blendTrans(duration=2)';
			this.rotators[i].img.filters.blendTrans.Apply();
		}
		
		// Rotate the banner
		if (document.images && ('object' == typeof this.rotators[i].banners[this.rotators[i].rotation].img))
		{
			this.rotators[i].img.src = this.rotators[i].banners[this.rotators[i].rotation].img.src;
		} else 
		{
			this.rotators[i].img.src = this.rotators[i].banners[this.rotators[i].rotation].img;
		}
		this.rotators[i].href.href = this.rotators[i].banners[this.rotators[i].rotation].uri;
		
		// Increment to the next banner in the queue
		this.rotators[i].rotation++;
		if (this.rotators[i].rotation >= this.rotators[i].banners.length)
		{
			this.rotators[i].rotation = 0;
		}

		// For IE play the fade effect
		if (document.all && this.rotators[i].fade)
		{
			this.rotators[i].img.filters.blendTrans.Play();
		}
	}
}

// ------------------------------------------------------------------

/**
 * Class for handling all the images and uris for a banner
 *
 * param	mixed Image element or id of image element
 * param	mixed Anchor element or id of anchor element
 * param	int Number of milliseconds to delay between rotating banners
 * param	boolean Whether or not to fade between banners in IE
 * return	void
 */
function BannerRotator (img, href, delay, fade)
{
	if ('string' == typeof img)
	{
		img = document.getElementById (img);
	}

	if ('string' == typeof href)
	{
		href = document.getElementById (href);
	}
	
	this.img		= img;
	this.href		= href;
	this.delay		= ('undefined' == typeof delay) ? 5000 : delay;
	this.fade		= ('undefined' == typeof fade)  ? true : fade;
	this.rotation	= 0;
	this.banners	= [];

	/**
	 * Add a banner to the BannerRotator
	 *
	 * param	string URI for the banner's anchor tag
	 * param	string Location of the banner's image source
	 * return	void
	 */
	this.addBanner = function (uri, img)
	{
		// Attempt to preload image
		if (document.images)
		{
			var src = img;
			var img = new Image();
			img.src = src;
		}

		this.banners[this.banners.length] = {uri:uri, img:img};
	}
}
