/**
* Assign the view handler
*/

viewHandler = Home;

/**
* Creates a new object with methods used by the Home page
*
* @author				Matt Gifford
* @copyright			2008 Timeshifting Interactive Limited
*/
function Home()
	{
	// Step 1. Define Properties

	var _instance = this;

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


	// Step 2. Define Public Methods

	/**
	* Sets up the initial page state and event handlers
	*/
	this.init = function()
		{
		// Call generic page init method
		this.base.init.call(this);

		// Setup image fading
		var imgs = document.getElementById('homeFeaturedFullsize').getElementsByTagName('div');
		_TOTAL_IMAGES = imgs.length;
		this.previousImage = imgs.length;
		var thumbnails = document.getElementById('homeFeaturedThumbnails').getElementsByTagName('blockquote')
		thumbnails[0].className = 'active';

		// Add event handlers to thumbnails
		for (var x = 0; x < thumbnails.length; x++)
			{
			thumbnails[x].onmouseover = __eventHandlerJumpToProduct;
			var divs = thumbnails[x].getElementsByTagName('div');
			for (var y = 0; y < divs.length; y++)
				{
				divs[y].onclick = new Function("window.location='" + thumbnails[x].href + "';");
				}
			
			// Add event handlers for ie6
			if (this.isTrident6 == true)
				{
				thumbnails[x].onmouseout = function()
					{
					this.className = this.className.replace(/\s?hover/g, '');
					}
				}
			}

		// Start the content rotation
		xhtml.animationTimeout = setTimeout("xhtml.advance();", 3000);
		}


	/**
	* Changes the homepage product 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('homeFeaturedFullsize' + this.previousImage).style.zIndex = 110;
		document.getElementById('homeFeaturedFullsize' + this.currentImage).style.zIndex = 120;

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

		// Update the active thumbnail
		var anchors = document.getElementById('homeFeaturedThumbnails').getElementsByTagName('blockquote');
		for (var x = 0; x < anchors.length; x++)
			{
			anchors[x].className = '';
			}
		anchors[this.currentImage-1].className = 'active';

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


	/**
	* Fades in the current image
	*
	* @param		percentage		The current percentage level
	* @return		void
	*/
	this.fadeIn = function(percentage)
		{
		// Set new opacity
		var increament = 1;
		if (55 < percentage)
			{
			increament = 2;
			}
		if (85 < percentage)
			{
			increament = 2;
			}
		percentage += increament;
		if (100 < percentage)
			{
			percentage = 100;
			}

		this.setOpacity(document.getElementById('homeFeaturedFullsize' + this.currentImage), percentage);

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

			// Continue the content rotation
			xhtml.animationTimeout = setTimeout("xhtml.advance();", 3000);
			}
		}


	/**
	* 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
					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;
			}
		}


	// Step 3. Define Private Methods


	/**
	* Event Handler: Jumps to the product moused over and cancels the current animation
	*/
	function __eventHandlerJumpToProduct()
		{
		// Cancel the animation
		clearTimeout(xhtml.animationTimeout);

		// Update the active thumbnail
		var anchors = document.getElementById('homeFeaturedThumbnails').getElementsByTagName('blockquote');
		var currentIndex = 1;
		for (var x = 0; x < anchors.length; x++)
			{
			anchors[x].className = '';
			if (anchors[x] == this)
				{
				xhtml.currentImage = x + 1;
				}
			}
		this.className = 'active';

		// Add event handlers for ie6
		if (xhtml.isTrident6 == true)
			{
			this.className += ' hover';
			}

		// Update the fullsize images
		xhtml.previousImage = xhtml.currentImage - 1;
		if (xhtml.previousImage < 1)
			{
			xhtml.previousImage = _TOTAL_IMAGES;
			}

		for (var x = 1; x <= _TOTAL_IMAGES; x++)
			{
			document.getElementById('homeFeaturedFullsize' + x).style.zIndex = 110;
			xhtml.setOpacity(document.getElementById('homeFeaturedFullsize' + x), 100);
			document.getElementById('homeFeaturedFullsize' + x).style.visibility = 'hidden';
			}

		// Update the current fullsize image
		document.getElementById('homeFeaturedFullsize' + xhtml.currentImage).style.zIndex = 120;
		document.getElementById('homeFeaturedFullsize' + xhtml.currentImage).style.visibility = 'visible';

		// Restart the content rotation
		xhtml.animationTimeout = setTimeout("xhtml.advance();", 3000);
		}
	}
