videoGallery = new VideoGallery();


/**
* Creates a new Video Gallery object that handles scrolling the thumbnails and updating the full size image
*
* @author      		Matt Gifford
* @copyright			2006 Timeshifting Interactive Limited
* @version        	1.0
*/
function VideoGallery()
	{
	// Step 1. Define properties

	var initialized = false;
	var currentPage = 1;
	var totalPages = 2;
	var pageWidth = 504;
	var scrollInProgress = false;


	// Step 2. Define public methods

	/**
	* Changes the fullsize image to the one specified
	*
	* param		id			The video id to play
	*/
	this.view = function(id)
		{
		switch (id)
			{
			case 1:
				var filename = "videos/pilatesVideo.swf";
				break;

			case 2:
				var filename = "videos/jeffVideo.swf";
				break;
			case 3:
				var filename = "videos/slideshow.swf";
				break;
			
			default:
				return;	 // Invalid video id, so return without doing anything
			}
		
		// Update or replace the existing flash object
		var so = new SWFObject(filename, "pilates", "535", "335", "8", "#000000");
		so.write("flashContainer");
		}



	/**
	* Scrolls the thumbnail container left, to show the additional thumbail(s) that are on the right
	*/
	this.next = function()
		{
		// Check if the class has been initialized
		if (initialized == false)
			{
			init();
			}
		
		// Check if we can scroll
		if (currentPage < totalPages && scrollInProgress == false)
			{
			// Increase the current page
			currentPage++;

			// Update the container location
			var currentOffset = (currentPage - 2) * pageWidth * -1;
			var newOffset = (currentPage - 1) * pageWidth * -1;
			this.scroll(currentOffset, newOffset, -1);

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

	/**
	* Scrolls the thumbnail container right, to show the previous displayed (and now hidden) thumnail(s)
	*/
	this.prev = function()
		{
		// Check if the class has been initialized
		if (initialized == false)
			{
			init();
			}
		
		// Check if we can scroll
		if (1 < currentPage && scrollInProgress == false)
			{
			// Increase the current page
			currentPage--;

			// Update the container location
			var currentOffset = (currentPage) * pageWidth * -1;
			var newOffset = (currentPage - 1) * pageWidth * -1;
			this.scroll(currentOffset, newOffset, 1);

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


	/**
	* Scrolls the thumbnail container to the specified location
	*
	* @param		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(currentOffset, newOffset, direction)
		{
		scrollInProgress = true;

		// Update the container location
		currentOffset += direction * 21;
		document.getElementById('videoGalleryThumbnailsContent').style.left = currentOffset + 'px';

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


	// Step 3. Define private methods

	/**
	* Initializes the class's member data
	*/
	function init()
		{
		// Get the total number thumbnail anchors
		var anchors = document.getElementById('videoGalleryThumbnailsContent').getElementsByTagName('a');

		// If there's more than one screen full of thumbnails (4 items), calculate the number of pages
		if (4 < anchors.length)
			{
			totalPages = Math.ceil(anchors.length / 4);
			}
		
		// Set the initialized as done
		initialized = true;
		}
	

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

		// Update next button
		if (currentPage == totalPages)
			{
			document.getElementById('videoGalleryNext').className = 'disabled';
			}
		else
			{
			document.getElementById('videoGalleryNext').className = '';
			}
		}
	}