/**
* Global JavaScript Definitions
*
* @author      		Matt Gifford
* @copyright			2006 Timeshifting Interactive Limited
* @version        	1.1
*/


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


/**
* Creates a new WebPage object with methods used by all pages, can be extended to add page specific methods.
*
* @author      		Matt Gifford
* @copyright			2005 Timeshifting Interactive Limited
* @version        	1.0
*/
function WebPage()
	{
	// Step 1. Define Properties

	this.initialized = 0;

	// Step 2. Define Methods

	/**
	* Sets up the initial page state and event handlers
	*/
	this.init = function()
		{
		this.processLinks();
		this.initialized = 1;
		}


	/**
	* Adds new window handler to "offsite" links, and hides selection marquee around active links
	*/
	this.processLinks = function()
		{
		// Find the link page links prefix
		var prefix = window.location.toString();
		if (prefix.indexOf('#') != -1)
			{
			prefix = prefix.substring(0, prefix.indexOf('#'));
			}
		prefix += '#';

		// Process the links
		var links = document.getElementsByTagName('a');
		for(x=0; x<links.length; x++)
			{
			links[x].onfocus = function()
				{
				this.blur();
				}
			if(/\boffsite\b/.exec(links[x].className))
				{
				links[x].onclick = function()
					{
					window.open(this.href,'_blank');
					return false;
					}
				}
			
			// Make in page links smooth scroll, check if the href is a in page target
			if(links[x].href.substring(0, prefix.length) == prefix)
				{
				var funcCode = 'xhtml.smoothScroll("' + links[x].href.substring(prefix.length, links[x].href.length) + '");';
				links[x].href = '#';
				links[x].onclick = new Function(funcCode);
				}
			}
		}


	/**
	* Scrolls the page to the specified element
	*
	* @param			elementId			The ID of the element to scroll to
	* @param			elementY			The Y position of element (optional, will be calculated if not specified)
	*/
	this.smoothScroll = function(elementId, elementY)
		{
		// If the elements vertical location hasn't been specificed, calculate it
		if (arguments.length != 2)
			{
			// Get it's offset
			obj = document.getElementById(elementId);
			elementY = obj.offsetTop;

			// If its parent is relative or absolutely positioned, find it's offset and add it to the total
			while (obj.offsetParent)
				{
				obj = obj.offsetParent;
				elementY += obj.offsetTop;
				}
			
			// Make the scroll stop just above the target (looks nicer)
			elementY -= 20;
			if (elementY < 0)
				{
				elementY = 0;
				}

			// Check to see we're not trying to scroll off the end of the page
			var contentHeight = document.getElementById('page').offsetHeight;
			var windowHeight = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight));
			if ( (contentHeight - windowHeight) < elementY)
				{
				elementY = (contentHeight - windowHeight);
				}
			}

		// Get the current window scroll position
		var yPos = window.scrollY ? window.scrollY : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

		// Calculate the pixels remaining to scroll and the scroll step size
		var distanceLeft = Math.abs(yPos - elementY);
		var stepSize = 100;
		if (distanceLeft < 400) stepSize = 60;
		if (distanceLeft < 200) stepSize = 20;
		if (distanceLeft < 50) stepSize = 10;

		// Calculate the scroll
		if (yPos < elementY)
			{
			// Scroll down
			yPos += stepSize;

			// Check if we're scrolled past the target
			if (elementY < yPos)
				{
				yPos = elementY;
				}
			}
		else if (elementY < yPos)
			{
			// Scroll Up
			yPos -= stepSize;

			// Check if we're scrolled past the target
			if (yPos < elementY)
				{
				yPos = elementY;
				}
			}

		// Check for less than zero
		if (yPos < 0)
			{
			yPos = 0;
			}

		// Scroll window
		window.scrollTo(0, yPos);

		// If we haven't reached the target, run the another scroll step
		if (yPos != elementY)
			{
			setTimeout("xhtml.smoothScroll('" + elementId + "', " + elementY + ");", 10);
			}
		}
	}
