/**
* Approach Doctor Lovaas Page JavaScript Definitions
*
* @author				Matt Gifford
* @copyright			2006 Timeshifting Interactive Limited
* @version			1.0
*/


window.onload = new Function("xhtml.init(); casesMenu.init();");


/**
* Creates a new Cases Menu object that handles scrolling the case study links
*
* @author      		Matt Gifford
* @copyright			2006 Timeshifting Interactive Limited
* @version        	1.0
*
* @param			currentPage			Sets the current page displayed
*/
function CasesMenu(currentPage)
	{
	// Step 1. Define properties

	var initialized = false;
	var currentPage = (typeof(currentPage) != 'undefined' ? currentPage : 1);
	var totalPages = 1;
	var frameWidth = 624;
	var stepSize = 24;		// Step size must divide evenly into the frame width
	var scrollInProgress = false;


	// Step 2. Define public methods

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

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

		// Update buttons
		updateButtons();

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


	/**
	* 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) * frameWidth * -1;
			var newOffset = (currentPage - 1) * frameWidth * -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) * frameWidth * -1;
			var newOffset = (currentPage - 1) * frameWidth * -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 * stepSize;
		document.getElementById('casesMenuLinksContent').style.left = currentOffset + 'px';

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

	

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

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