/**
* 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 totalPages = 1;
	var pageWidth = 935;
	var thumbnailWidth = 187;
	var scrollRange = 0;

	this.flashPlaying = false;
	this.currentOffset = 0;
	this.scrollInProgress = false;

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



	// 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);

		// Load Default Media
		this.playVideo('intro');
		}


	/**
	* Scrolls the thumbnail container left, to show the additional thumbail(s) that are on the right
	*/
	this.filmsNext = function()
		{
		// Check if we can scroll
		if (scrollRange != this.currentOffset && this.scrollInProgress == false)
			{
			// Update the container location
			var newOffset = this.currentOffset - thumbnailWidth;
			this.scroll(newOffset, -1);

			// Update the scroll buttons
			updateButtons();
			}
		}


	/**
	* Scrolls the thumbnail container right, to show the previous displayed (and now hidden) thumnail(s)
	*/
	this.filmsPrev = function()
		{
		// Check if we can scroll
		if (this.currentOffset < 0 && this.scrollInProgress == false)
			{
			// Update the container location
			var newOffset = this.currentOffset + thumbnailWidth;
			this.scroll(newOffset, 1);

			// Update the scroll buttons
			updateButtons();
			}
		}


	/**
	* Scrolls the thumbnail container to the specified location
	*
	* @param		this.currentOffset		The current "left" location relative to the container div in pixels
	* @param		newOffset	 			The target "left" location to move to
	* @param		direction				Either -1 or 1 indicating whether the scroll is left or right
	*/
	this.scroll = function(newOffset, direction)
		{
		this.scrollInProgress = true;

		// Update the container location
		xhtml.currentOffset += direction * 10;

		// Bound the range
		if (direction < 0)
			{
			if (xhtml.currentOffset < newOffset)
				{
				xhtml.currentOffset = newOffset;
				}
			}
		else
			{
			if (0 < xhtml.currentOffset )
				{
				xhtml.currentOffset = 0;
				}
			if (xhtml.currentOffset > newOffset)
				{
				xhtml.currentOffset = newOffset;
				}
			}

		// Scroll the container
		document.getElementById('homeFilmsContent').style.left = this.currentOffset + 'px';

		// Check if we need to recurse
		if (this.currentOffset  != newOffset)
			{
			setTimeout('xhtml.scroll(' + newOffset + ', ' + direction + ');', 50);
			}
		else
			{
			this.scrollInProgress = false;
			}
		}


	/**
	* Plays the specified video
	*
	* @param		param		The video id to play
	*/
	this.playVideo = function(param)
		{
		// Retrieve the id
		if (typeof(param) == 'number')
			{
			var id = param;
			}
		else
			{
			var id = param;
			}

		// Map id to filename TODO
		var filename = 'videos/' + id + '.flv';
		//var filename = 'videos/make-it-happen-trailer-1.flv';

		// Delete any existing flash objects
		this.closeVideo();
		document.getElementById('flashContainer').style.zIndex = 20;

		// Hide the image
		document.getElementById('homeFeaturedImage').className = 'transparent';

		// Insert the movie into the container
		var so = new SWFObject("flash/mayhemplayer.swf", "mediaVideo", "960", "431", "8", "#000000");
		so.addParam("wmode", "opaque");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("allowFullScreen", "true");
		so.addVariable("videoPaths",filename);
		so.addVariable("videoCount", "1");
		so.addVariable("startIndex", "0");

		// Determine if we should be hiding the player controls
		if (id == 'intro')
			{
			so.addVariable("noSkin", "true");
			so.addVariable("closeOnFinish", "true");
			}
		else
			{
			so.addVariable("closeOnFinish", "true");
			}
		so.write('flashContainer');

		// Set flash as playing
		this.flashPlaying = true;
		}


	/**
	 * Closes the Video Player
	 */
	this.closeVideo = function()
		{
		if (this.isFirefox1 == true || this.isFirefox2 == true || this.isSafari == true)
			{
			// Hide the existing flash object
			var containerOld = document.getElementById('flashContainer');
			containerOld.style.visibility = 'hidden';
			containerOld.style.zIndex = 1;
			containerOld.style.top = '500px';
			containerOld.id = 'flashContainerOld' + (new Date()).getTime();

			// Make new container
			var containerNew = document.createElement('div');
			containerNew.id = 'flashContainer';
			document.getElementById('homeFeatured').appendChild( containerNew );

			// Queue old container for deletion
			setTimeout("document.getElementById('" + containerOld.id + "').parentNode.removeChild(document.getElementById('" + containerOld.id + "'));", 250);
			}
		else
			{
			// Delete the existing flash object (Internet Explorer)
			var objects = document.getElementById('flashContainer').getElementsByTagName('object');
			if (0 < objects.length)
				{
				document.getElementById('flashContainer').removeChild( objects[0] );
				}

			// Delete the existing flash object (Firefox & Safari)
			var embeds = document.getElementById('flashContainer').getElementsByTagName('embed');
			if (0 < embeds.length)
				{
				document.getElementById('flashContainer').removeChild( embeds[0] );
				}
			}

		// Show the image
		document.getElementById('flashContainer').style.zIndex = 10;
		document.getElementById('homeFeaturedImage').className = '';

		// Set flash as not playing
		document.getElementById('homeFeaturedImage').style.visibility = 'visible';
		this.flashPlaying = false;
		}



	/**
	* Advances the homepage branding image
	*/
	this.advanceImage = function()
		{
		if (xhtml.flashPlaying == true)
			{
			setTimeout("xhtml.advanceImage();", 10000);
			return;
			}

		// Advance image numbers
		this.previousImage++;
		this.currentImage++;
		if (this.totalImages < this.previousImage)
			{
			this.previousImage = 1;
			}
		if (this.totalImages < this.currentImage)
			{
			this.currentImage = 1;
			}

		// Update positions
		document.getElementById('homeFeaturedImage' + this.previousImage).style.zIndex = 15;
		document.getElementById('homeFeaturedImage' + this.currentImage).style.zIndex = 16;

		// Set new image visible but with an opacity of 0%
		this.setOpacity(document.getElementById('homeFeaturedImage' + this.currentImage), 0);
		document.getElementById('homeFeaturedImage' + this.currentImage).style.visibility = (xhtml.flashPlaying ? 'hidden' : 'visible');

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

		// Continue the content rotation
		setTimeout("xhtml.advanceImage();", 10000);
		}


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

		// Check if we are at 100%
		if (percentage < 100)
			{
			// If not, continue the fade in
			setTimeout("xhtml.fadeIn(" + percentage + ");", 10);
			}
		else
			{
			// Otherwise hide the old image
			document.getElementById('homeFeaturedImage' + 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;
			}
		}





	// Step 3. Define Private Methods

	/**
	* Updates the previous and next scroll buttons, setting them as disabled where necessary
	*/
	function updateButtons()
		{
		// Update previous button
		if (this.currentOffset == 0)
			{
			document.getElementById('homeFilmsPrev').className = 'disabled';
			}
		else
			{
			document.getElementById('homeFilmsPrev').className = '';
			}

		// Update next button
		if (this.currentOffset == scrollRange)
			{
			document.getElementById('homeFilmsNext').className = 'disabled';
			}
		else
			{
			document.getElementById('homeFilmsNext').className = '';
			}
		}
	}





	/**
	* Flash callback function
	*
	* @param		str		Content from the flash applet
	*/
	function getTextFromFlash(str)
		{
		if(str == "complete")
			{
			xhtml.closeVideo();
//			// Delete the existing flash container
//			var parent = document.getElementById('flashContainer').parentNode
//			//var child = document.getElementById('flashContainer')
//			parent.removeChild( document.getElementById('flashContainer') );
//
//			// Add the new container
//			var div = document.createElement('div');
//			div.id = 'flashContainer';
//			//parent.replaceChild(div, child);
//			parent.appendChild(div);
//			div.style.zIndex = "-1";
			}
		}

