/**
* Controls the rotation of the homepage branding graphics
*
* @author      		Matt Gifford
* @copyright			2007 Timeshifting Interactive Limited
* @version        	1.0
*/


homeBranding = new HomeBranding();
homeBranding.previousOnload = window.onload;
window.onload = new Function("homeBranding.previousOnload(); homeBranding.init();");


/**
* Controls the rotation of the homepage branding graphics
*
* @author      		Matt Gifford
* @copyright			2007 Timeshifting Interactive Limited
* @version        	1.0
*/
function HomeBranding()
	{
	// Step 1. Define properties

	var _TOTAL_IMAGES = 3;
	var initialized = false;

	this.opacityMode = ((navigator.appName && navigator.appName == 'Microsoft Internet Explorer') ? 'DX_IMAGE_TRANSFORM' : 'W3C');
	this.previousImage = _TOTAL_IMAGES;
	this.currentImage = 1;


	// Step 2. Define public methods

	/**
	* Initializes the class's member data
	*/
	this.init = function()
		{
		// Start the content rotation
		setTimeout("homeBranding.advance();", 5000);

		// Set the initialized as done
		initialized = true;
		}

	
	/**
	* Advances the homepage branding image
	*/
	this.advance = function()
		{
		// Advance image numbers
		this.previousImage++;
		this.currentImage++;
		if (_TOTAL_IMAGES < this.previousImage)
			{
			this.previousImage = 1;
			}
		if (_TOTAL_IMAGES < this.currentImage)
			{
			this.currentImage = 1;
			}

		// Update positions
		document.getElementById('homeBrandingImage' + this.previousImage).style.zIndex = 100;
		document.getElementById('homeBrandingImage' + this.currentImage).style.zIndex = 200;

		// Set new image visible but with an opacity of 0%
		this.setOpacity(document.getElementById('homeBrandingImage' + this.currentImage), 0);
		document.getElementById('homeBrandingImage' + this.currentImage).style.visibility = 'visible';

		// Start fade in for new image
		this.fadeIn(0);

		// Continue the content rotation
		setTimeout("homeBranding.advance();", 6000);
		}


	/**
	* Fades in the current image
	*
	* @param		percentage		The current percentage level
	* @return		void
	*/
	this.fadeIn = function(percentage)
		{
		// Set new opacity
		percentage += 10;
		this.setOpacity(document.getElementById('homeBrandingImage' + this.currentImage), percentage);

		// Check if we are at 100%
		if (percentage < 100)
			{
			// If not, continue the fade in
			setTimeout("homeBranding.fadeIn(" + percentage + ");", 10);
			}
		else
			{
			// Otherwise hide the old image
			document.getElementById('homeBrandingImage' + this.previousImage).style.visibility = 'hidden';
			}
		}


	/**
	* Sets the opacity of an object
	*
	* @param		obj							The object to set the opacity of
	* @param		opacityPercent			The opacity for the object as an integer from: 0 to 100
	*/
	this.setOpacity = function(obj, opacityPercent)
		{
		switch ( this.opacityMode )
			{
			case 'W3C':
				var opacityNumeric = opacityPercent/100;
				obj.style.MozOpacity = opacityNumeric;
				obj.style.KhtmlOpacity = opacityNumeric;
				obj.style.opacity = opacityNumeric;
				break;

			case 'DX_IMAGE_TRANSFORM':
				try
					{
					// Add filter if it hasn't been initialized
					if (obj.getAttribute('alphaEnabled') != 1)
						{
						obj.style.filter = 'alpha(opacity=' + opacityPercent + ')';
						obj.setAttribute('alphaEnabled', 1);
						}
					obj.filters.alpha.opacity = opacityPercent;
					}
				catch (err)
					{
					// DirectX filters are unavailable, and since they're only way of doing opacity in Internet Explorer sliently ignore and return
					return;
					}
				break;
			}
		}
	}
