/**
* Global JavaScript Definitions
*
* @author      		Matt Gifford
* @copyright			2005 Timeshifting Interactive Limited
* @version        	1.0
*/


// Define constants
TRUE = true;
FALSE = false;
NULL = null;


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();
		menu = new dropdownMenu();
		this.initialized = 1;

		// Add Contact page email address
		if (document.getElementById('contactAddress'))
			{
			var anchor = document.createElement('a');
			anchor.href = 'mailto\u003Ainfo\u0040enzedparamedical\u002Eco\u002Enz';
			anchor.appendChild( document.createTextNode('info\u0040enzedparamedical\u002Eco\u002Enz') );
			document.getElementById('contactAddress').appendChild( anchor );
			}
		}


	/**
	* Adds new window handler to "offsite" links, and hides selection marquee around active links
	*/
	this.processLinks = function()
		{
		var links = document.getElementsByTagName('a');
		for(x=0; x<links.length; x++)
			{
			// Remove dotted border on links targeting the same page
			links[x].onfocus = function()
				{
				this.blur();
				}
			
			// Make offsite links open in new tab/window
			if(/\boffsite\b/.exec(links[x].className))
				{
				links[x].onclick = function()
					{
					window.open(this.href,'_blank');
					return false;
					}
				}
			
			// Make back to top links smooth scroll
			if(links[x].href.indexOf('#page') != -1)
				{
				links[x].href = 'javascript:xhtml.scrollToTop();';
				}
			}
		}

	
	/**
	* Displays of the specified page element(s), by setting the display propriety to block
	*
	* @param		elements			Either single element id, or an array of id's
	*/ 
	this.show = function(elements)
		{
		switch (typeof(elements))
			{
			case "string":
				if (document.getElementById(elements))
					{
					document.getElementById(elements).style.display = 'block';
					}
				break;

			case "array":
				for (var x = 0; x < elements.length; x++)
					{
					if (document.getElementById(elements[x]))
						{
						document.getElementById(elements[x]).style.display = 'block';
						}
					}
				break;
			}
		}


	/**
	* Hides of the specified page element(s), by setting the display propriety to none
	*
	* @param		elements			Either single element id, or an array of id's
	*/ 
	this.hide = function(elements)
		{
		switch (typeof(elements))
			{
			case "string":
				if (document.getElementById(elements))
					{
					document.getElementById(elements).style.display = 'none';
					}
				break;

			case "array":
				for (var x = 0; x < elements.length; x++)
					{
					if (document.getElementById(elements[x]))
						{
						document.getElementById(elements[x]).style.display = 'none';
						}
					}
				break;
			}
		}


	/**
	* Shows & Hides of the specified page element(s)
	*
	* @param		showElements			Either single element id, or an array of id's to show
	* @param		hideElements			Either single element id, or an array of id's to hide
	*/ 
	this.showHide = function(showElements, hideElements)
		{
		this.show(showElements);
		this.hide(hideElements);
		}


	/**
	* Scrolls the page back to the top
	*/
	this.scrollToTop = function()
		{
		var yPos = window.scrollY ? window.scrollY : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

		// Offset scroll
		var offset = 175;
		if (yPos < 400) offset = 65;
		if (yPos < 150) offset = 30;
		if (yPos < 50) offset = 15;
		yPos -= offset;

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

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

		// Recurse
		if (yPos != 0)
			{
			setTimeout("xhtml.scrollToTop();", 10);
			}
		}
	}




function dropdownMenu() {		// CLASS
	this.menuTimeout = null;
	this.headings = new Array('globalHeaderNavigationLiving','globalHeaderNavigationWhy');
	this.menus = new Array('globalHeaderNavigationLivingMenu','globalHeaderNavigationWhyMenu');

	// Add event handlers to headings
	for (var x=0; x<this.headings.length; x++)
		{
		document.getElementById(this.headings[x]).onmouseover = function()
			{
			if (typeof(menu) != 'undefined')
				{
				clearTimeout(menu.menuTimeout);
				menu.show(this.id + 'Menu');
				}
			}
		document.getElementById(this.headings[x]).onmouseout = function()
			{
			if (typeof(menu) != 'undefined')
				{
				clearTimeout(menu.menuTimeout);
				menu.show(this.id + 'Menu');
				menu.menuTimeout = setTimeout("menu.hide();", 800); 
				}
			}
		}
	
	// Add show/hide event handlers to menus
	for (var x = 0; x < this.menus.length; x++)
		{
		var anchors = document.getElementById(this.menus[x]).getElementsByTagName('a');
		for (var y=0; y<anchors.length; y++)
			{
			anchors[y].onmouseover = function()
				{
				if (typeof(menu) != 'undefined')
					{
					clearTimeout(menu.menuTimeout);
					}
				}
			anchors[y].onmouseout = function()
				{
				if (typeof(menu) != 'undefined')
					{
					clearTimeout(menu.menuTimeout);
					menu.menuTimeout = setTimeout("menu.hide();", 800); 
					}
				}
			}
		}


	// Class member functions
	this.hide = function()
		{
		clearTimeout(menu.menuTimeout);
		for (var x = 0; x < this.menus.length; x++)
			{
			document.getElementById(this.menus[x]).style.visibility = 'hidden';
			}
		}

	this.show = function(id)
		{
		menu.hide();
		document.getElementById(id).style.visibility = 'visible';
		}
	}



/**
* Adds an event to a DOM object
*
* @param		obj					The object to attach the event handler to
* @param		eventType		The type of event to attach to, without the "on" prefix
* @param		func					A reference to the function to attach to the event
*
* @return		Boolean, whether for event handler was successfully attached
*/
function addEventHandler(obj, eventType, func)
	{
	if (obj.addEventListener)
		{
		obj.addEventListener(eventType, func, true);
		return true;
		}
	else if (obj.attachEvent)
		{
		return obj.attachEvent('on'+eventType, func);
		}
	else
		{
		return false;
		}
	}
