/**
* Assign the view handler
*/

viewHandler = History;

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

	// Thumbnail scrolling vars
	var _thumbnailsContentWidth = 0;
	var _thumbnailsContentRange = 0;
	var _thumbnailsScrollbarGrabberSize = 0;
	var _thumbnailsScrollbarGrabberRange = 0;
	_grabberBeingDragged = false;		// global




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

		// Initialize history scrolling
		this.initScrolling();
		}


	/**
	* Shows the specified history item
	*
	* @param			id			The item to display
	*/
	this.showItem = function(id)
		{
		// Hide all items
		var items = document.getElementById('items').getElementsByTagName('div');
		for (var x = 0; x < items.length; x++)
			{
			items[x].className = 'historyItem hidden';
			}

		// Display the selected item
		document.getElementById('historyItems' + id).className = 'historyItem';
		}


	/**
	* Initializes the history scrolling
	*/
	this.initScrolling = function()
		{
		// Calculate content size and range
		_thumbnailsContentWidth = parseInt(document.getElementById('historyThumbnailsContainer').getElementsByTagName('a').length * 129);
		_thumbnailsContentRange = (_thumbnailsContentWidth - 903) < 0 ? 0 : (_thumbnailsContentWidth - 903);

		// Calculate the grabber size and range
		var pages = _thumbnailsContentWidth / 903;
		pages = (pages < 1 ? 1 : pages);
		_thumbnailsScrollbarGrabberSize = parseInt(859 / pages);
		_thumbnailsScrollbarGrabberRange = 879 - _thumbnailsScrollbarGrabberSize;

		// Add event handlers
		document.onmousemove = __eventHandlerScrollBarGrabberMove;
		document.onmouseup = __eventHandlerScrollBarGrabberUp;
		document.getElementById('historyThumbnailsScrollbarGrabber').onmousedown = __eventHandlerScrollBarGrabberDown;

		// Set the grabber size and position
		document.getElementById('historyThumbnailsScrollbarGrabber').style.left = '0px';
		xhtml.grabberX = 0;
		xhtml.previousMouseX = 0;

		// Set the current location
		document.getElementById('historyThumbnailsContainer').style.left = '0px';
		}


	/**
	* Scrolls the blog entry up
	*/
	this.thumbnailsScrollLeft = function()
		{
		xhtml.grabberX -= 30;
		this.thumbnailsScrollMove();
		}


	/**
	* Scrolls the blog entry down
	*/
	this.thumbnailsScrollRight = function()
		{
		xhtml.grabberX += 30;
		this.thumbnailsScrollMove();
		}


	/**
	* Updates the location of the scrollbar grabber and blog content
	*/
	this.thumbnailsScrollMove = function()
		{
		if (_thumbnailsScrollbarGrabberRange == 0)
			{
			return;
			}

		// Range check grabber
		if (xhtml.grabberX < 0)
			{
			xhtml.grabberX = 0;
			}
		if (_thumbnailsScrollbarGrabberRange < xhtml.grabberX)
			{
			xhtml.grabberX = _thumbnailsScrollbarGrabberRange;
			}

		// Move the grabber
		document.getElementById('historyThumbnailsScrollbarGrabber').style.left = xhtml.grabberX + 'px';

		// Move the content
		document.getElementById('historyThumbnailsContainer').style.left = (parseInt((xhtml.grabberX / _thumbnailsScrollbarGrabberRange) * _thumbnailsContentRange) * -1) + 'px';
		}



	// Step 3. Define Private Methods

	/**
	* Starts the drag of the grabber
	*
	* @param		event		The browser event object
	*/
	function __eventHandlerScrollBarGrabberDown(event)
		{
		if (_grabberBeingDragged == true)
			{
			return;
			}
		else
			{
			_grabberBeingDragged = true;
			}

		// Get the event object if necessary
		event = !!event ? event : window.event;

		// Get the mouse location
		if(!!(window.attachEvent && !window.opera) == true)
			{
			xhtml.previousMouseX = window.event.clientX;
			}
		else
			{
			xhtml.previousMouseX = event.pageX;
			}

		// update the div position
		document.getElementById('historyThumbnailsScrollbarGrabber').style.left = parseInt(xhtml.grabberX) + 'px';
		}



	/**
	* Starts the drag of the grabber
	*
	* @param		event		The browser event object
	*/
	function __eventHandlerScrollBarGrabberMove(event)
		{
		if (_grabberBeingDragged == false)
			{
			return;
			}

		// Get the event object if necessary
		event = !!event ? event : window.event;

		// Get the mouse location
		if(!!(window.attachEvent && !window.opera) == true)
			{
			var mouseX = window.event.clientX;
			}
		else
			{
			var mouseX = event.pageX;
			}

		// calculate offset and update div location vars
		xhtml.grabberX = (((mouseX * 1) - (xhtml.previousMouseX * 1)) * 1) + (xhtml.grabberX * 1);

		// update the stored mouse location
		xhtml.previousMouseX = mouseX;

		// move the grabber and the container
		xhtml.thumbnailsScrollMove();
		}


	/**
	* End the drag of the grabber
	*/
	function __eventHandlerScrollBarGrabberUp()
		{
		_grabberBeingDragged = false;
		}
	}
