/**
* A virtual scrollbar handler class for the Homepage Latest News box
*
* @author      		Matt Gifford
* @copyright			2007 Timeshifting Interactive Limited
* @version        	1.0
*/


latestNews = new LatestNews();
latestNews.previousOnload = window.onload;
window.onload = new Function("latestNews.previousOnload(); latestNews.init();");


/**
* A virtual scrollbar handler class for the Homepage Latest News box
*
* @author      		Matt Gifford
* @copyright			2007 Timeshifting Interactive Limited
* @version        	1.0
*/
function LatestNews()
	{
	// Step 1. Define properties

	var initialized = false;
	var currentPage = 1;
	var totalPages = 1;
	var newsHeight = 600;
	var scrollInProgress = false;

	var ua = navigator.userAgent.toLowerCase();
	var macintosh = (ua.indexOf("macintosh") != -1 || ua.indexOf("powerpc") != -1) ? 1 : 0;
	var isPresto = ((ua.indexOf("opera") != -1) ? 1 : 0);
	var isKhtml = ((ua.indexOf('konqueror') != -1 || ua.indexOf('khtml') != -1 || (ua.indexOf('safari') != -1 && macintosh) && !this.isPresto ) ? 1 : 0);
	var isGecko = ((navigator.product && navigator.product.toLowerCase() == "gecko" && !this.isKhtml && !this.isPresto) ? 1 : 0);
	var isTasman = ((navigator.appName && navigator.appName == 'Microsoft Internet Explorer' && macintosh && ua.indexOf('msie 5') != -1 && !this.isPresto && !this.isKhtml && !this.isGecko) ? 1 : 0);
	var isTrident = ((navigator.appName && navigator.appName == 'Microsoft Internet Explorer' && ua.indexOf('msie') && !this.isPresto && !this.isKhtml && !this.isGecko && !this.isTasman) ? 1 : 0);


	// Step 2. Define public methods

	/**
	* Initializes the class's member data
	*/
	this.init = function()
		{
		var navigationInnerHeight = 95;
		this.grabberRange = 75;

		// Get the total number thumbnail anchors
		this.contentHeight = document.getElementById('homeActionNewsContentWrapper').offsetHeight;

		// Init grabber vars
		this.grabberY = 0;												// the location of the tile content div, relative to the images container
		this.previousMouseY = 0;									// the previous mouse location, used to calculate the offset to move the tiles div
		this.grabberBeingDragged = false;

		// Add event handlers to the grabber
		document.getElementById('grabber').onmousedown = function(event)
			{
			latestNews.dragItem(this.id, event);
			}
		document.onmousemove = function(event)
			{
			if (latestNews.grabberBeingDragged == true)
				{
				latestNews.moveItem(this.id, event);
				}
			}
		document.onmouseup = function(event)
			{
			if (latestNews.grabberBeingDragged == true)
				{
				latestNews.dropItem(this.id, event);
				}
			}
		
		// Set the initialized as done
		initialized = true;
		}


	// initialises the dragging of the grabber
	this.dragItem = function(id, event)
		{
		if(this.grabberBeingDragged == true) return;

		// store the initial location
		this.grabberBeingDragged = true;

		// get mouse location for internet explorer
		if(isTasman || isTrident)
			{
			this.previousMouseY = window.event.clientY;
			}
		// get mouse location for gecko & khtml
		else
			{
			this.previousMouseY = event.pageY;
			}

		// update the div position
		document.getElementById('grabber').style.top = parseInt(this.grabberY) + 'px';
		}


	// Move the grabber
	this.moveItem = function(id, event)
		{
		if(this.grabberBeingDragged == false) return;

		// get mouse location for internet explorer
		if(isTasman || isTrident)
			{
			var mouseY = window.event.clientY;
			}
		// get mouse location for gecko & khtml
		else
			{
			var mouseY = event.pageY;
			}

		// calculate offset and update div location vars
		this.grabberY = (((mouseY * 1) - (this.previousMouseY * 1)) * 1) + (this.grabberY * 1);

		// update the stored mouse location
		this.previousMouseY = mouseY;

		// move the grabber and the container
		this.scrollContainer();
		}



	// handles the grabber being dropped
	this.dropItem = function(id, event)
		{
		// reset the variables for the dragged attachment filename div
		this.grabberBeingDragged = false;
		}


	this.scrollContainer = function()
		{
		// bound bottom right corner
		if(this.grabberRange < this.grabberY)
			{
			this.grabberY = this.grabberRange;
			}

		// bound top left corner
		if(this.grabberY < 0)
			{
			this.grabberY = 0;
			}

		// move the grabber div
		document.getElementById('grabber').style.top = this.grabberY + 'px';

		// calculate new location for the container
		var newContainerY = parseInt((this.grabberY/75) * (this.contentHeight - 95));
		document.getElementById('homeActionNewsContentWrapper').style.top = '-' + newContainerY + 'px';
		}
	}
