/**
* Assign the view handler
*/

viewHandler = Federations;

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

	// Page specific config
	var _itemWidth = 300;
	var _itemsPerWindow = 3;
	var _objContentDiv = document.getElementById('federationsNewsContent');
	var _objPrevButton = document.getElementById('federationsNewsPrev');
	var _objNextButton = document.getElementById('federationsNewsNext');

	var _instance = this;
	var _currentPage = 1;
	var _totalPages = 1;
	var _scrollInProgress = false;
	var _initialized = false;
	var _pageWidth = _itemWidth * _itemsPerWindow;
	var _scrollStepSize = 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);

		// Get the total number thumbnail anchors
		var divs = _objContentDiv.getElementsByTagName('div');

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

		// Calculate the scroll step size
		_scrollStepSize = _itemWidth / 12;
		if (Math.floor(_scrollStepSize) != (_itemWidth / 12))
			{
			_scrollStepSize = _itemWidth / 10;
			if (Math.floor(_scrollStepSize) != (_itemWidth / 10))
				{
				throw("Assertion: Item width is not divisible by 10 or 12");
				}
			}

		// 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 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 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 * _scrollStepSize;
		_objContentDiv.style.left = currentOffset + 'px';

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

	// Step 3. Define Private Methods

	/**
	* Updates the previous and next scroll buttons, setting them as disabled where necessary
	*/
	function _updateButtons()
		{
		// Update previous button
		if (_currentPage == 1)
			{
			_objPrevButton.className = 'disabled';
			}
		else
			{
			_objPrevButton.className = '';
			}

		// Update next button
		if (_currentPage == _totalPages)
			{
			_objNextButton.className = 'disabled';
			}
		else
			{
			_objNextButton.className = '';
			}
		}
	}
