/*
systym: the NIH-syndrome javascript library
copyright (c) 2009 Chris Rose-Mathew
*/

//===================================================
// class: Utils
//===================================================
// This module provides a set of utility functions
// that enable map clicking, dragging and screen position 
// queries.
// Thanks to Peter-Paul Koch at http://www.quirksmode.org
// for the basis of this code and his great tutorial site.
//===================================================
function Utils()
{
	//Properties
	this.credits = "Base on code by Peter-Paul Koch at http://www.quirksmode.org";
	this.curleft = 0;
	this.curtop = 0;
	this.docPosX = 0;
	this.docPosY = 0;
	
	// Object methods
	this.discoverProperties = discoverProperties;
	// Window methods
	this.getWindowWidth = getWindowWidth;
	this.getWindowHeight = getWindowHeight;
	this.getScrollX = getScrollX;
	this.getScrollY = getScrollY;
	// Element methods
	this.getElementPositionX = getElementPositionX;
	this.getElementPositionY = getElementPositionY;
	this.getElementPosition = getElementPosition;
	// Mouse methods
	this.getMousePositionClient = getMousePositionClient;
	this.getMousePositionElement = getMousePositionElement;
	this.getDocumentClickX = getDocumentClickX;
	this.getDocumentClickY = getDocumentClickY;

	//====================================================
	// discoverProperties()
	//====================================================
	// Inspect an object and pop up an alert displaying
	// the names and types of it's members.
	// It is possible to include inherited properties
	// (those from up the prototype chain)
	//====================================================
	function discoverProperties(obj, includeInherited)
	{
		var text = "Properties: \n";
		for (var prop in obj)
		{
			//alert("Property: " + prop);
			var p = obj[prop];
			if ((obj.hasOwnProperty && obj.hasOwnProperty(prop)) || includeInherited === true)
			{
				if (typeof(p) == "object")
				{
					//alert("An object");
					if (p === null)
						text += (prop + " : " + "Object (null)" + "\n");
					else if (p.constructor == (new Array).constructor)
						text += (prop + " : " + "Array" + "\n");
					else if (p.constructor == (new Date).constructor)
						text += (prop + " : " + "Date" + "\n");
					else if (p.constructor == (new RegExp).constructor)
						text += (prop + " : " + "RegExp" + "\n");
					else
						text += (prop + " : " + "Object\n");
				}
				else if (typeof(p) == "string")
				{
					//alert("A string");
					if (prop == "CLASS_NAME")
						text += (prop + " : " + "String (" + p + ")\n");
					else
						text += (prop + " : " + "String" + "\n");
				}
				else if (typeof(p) == "number")
					text += (prop + " : " + "Number" + "\n");
				else if (typeof(p) == "boolean")
					text += (prop + " : " + "Boolean" + "\n");
				else if (typeof(p) == "function")
					text += (prop + " : " + "Function" + "\n");
				else if (typeof(p) == "undefined")
					text += (prop + " : " + "Undefined" + "\n");
				else
					text += (prop + " : " + "Unknown" + "\n");
			}
		}
		alert(text);
	}

	//====================================================
	// getScrollX()
	//====================================================
	function getScrollX()
	{
		return window.pageXOffset ? window.pageXOffset : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
	}

	//====================================================
	// getScrollY()
	//====================================================
	function getScrollY()
	{
		return window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	}
	
	//====================================================
	// getWindowWidth()
	//====================================================
	function getWindowWidth()
	{
		if (self.innerWidth)
			return self.innerWidth;
		if (document.documentElement && document.documentElement.clientWidth)
			return document.documentElement.clientWidth;
		if (document.body)
			return document.body.clientWidth;
		return 0;
	}
	
	//====================================================
	// getWindowHeight()
	//====================================================
	function getWindowHeight()
	{
		if (self.innerHeight)
			return self.innerHeight;
		if (document.documentElement && document.documentElement.clientHeight)
			return document.documentElement.clientHeight;
		if (document.body)
			return document.body.clientHeight;
		return 0;
	}

	//=================================================
	// find the X position of an element relative to the
	// entire document.
	// Thanks to Peter-Paul Koch at http://www.quirksmode.org
	// for this code.
	//=================================================
	function getElementPositionX(element)
	{

		this.curleft = 0
		if (element.offsetParent)
		{
			while (element.offsetParent)
			{
				this.curleft += element.offsetLeft
				element = element.offsetParent
			}
		}
		else if (element.x)
			this.curleft += element.x
		return this.curleft
	}

	//=================================================
	// find the Y position of an element relative to the
	// entire document.
	// Thanks to Peter-Paul Koch at http://www.quirksmode.org
	// for this code.
	//=================================================
	function getElementPositionY(element)
	{
		this.curtop = 0
		if (element.offsetParent)
		{
			while (element.offsetParent)
			{
				this.curtop += element.offsetTop
				element =element.offsetParent
			}
		}
		else if (element.y)
			this.curtop += element.y
		return this.curtop
	}

	//=================================================
	// Find the coordinates of the top-left corner of
	// an HTML DOM element relative to the client-window
	// of the browser
	// An object with .x and .y members is returned
	//=================================================
	function getElementPosition(element)
	{
		if (!element)
			return null;
		
		var ret = new Object();
		ret.x = getElementPositionX(element);
		ret.y = getElementPositionY(element);
		return ret;
	}

	//=================================================
	// Find the position of the mouse cursor relative to
	// the client-window of the browser. The position is
	// returned as an object with .x and .y members.
	//=================================================
	function getMousePositionClient(event)
	{
		if (!event)
			return null;
		
		var ret = new Object();
		if(event.offsetX || event.offsetY)
		{
			// Internet Explorer
			ret.x = event.offsetX;
			ret.y = event.offsetY;
		}
		else
		{
			// FireFox
			ret.x = event.pageX;
			ret.y = event.pageY;
		}
		return ret;
	}

	//=================================================
	// Find the position of the mouse cursor relative to
	// the top-left corner of a given HTML DOM element.
	// The position is returned as an Object with both
	// .x and .y members.
	//=================================================
	function getMousePositionElement(event, element)
	{
		//alert(event + ", " + element)
		if (!event || !element)
			return null;
		
		// find out elements' top-left coords
		var elementPosition = util.getElementPosition(element);
		//alert("Element position: " + elementPosition.x + ", " + elementPosition.y);
		
		var ret = new Object();
		if(event.offsetX || event.offsetY)
		{ 	//For Internet Explorer
			ret.x = event.offsetX - elementPosition.x;
			ret.y = event.offsetY - elementPosition.y;
		}
		else
		{ 	//For FireFox
			ret.x = event.pageX - elementPosition.x;
			ret.y = event.pageY - elementPosition.y;
		}
		return ret;
	}

	//=================================================
	// find document click position.
	// Thanks to Peter-Paul Koch at http://www.quirksmode.org
	// for the basis of this code.
	//=================================================
	function getDocumentClickX(e)
	{

		this.docPosX = 0
		if (!e)
		{
			var e = window.event
		}
		if (e.pageX)
		{
			this.docPosX = e.pageX
		}
		else if (e.clientX)
		{
			this.docPosX = e.clientX + document.body.scrollLeft
		}
		return this.docPosX
	}

	//=================================================
	// find document click position.
	// Thanks to Peter-Paul Koch at http://www.quirksmode.org
	// for the basis of this code.
	//=================================================
	function getDocumentClickY(e)
	{

		this.docPosY = 0
		if (!e)
		{
			var e = window.event
		}
		if (e.pageY)
		{
			this.docPosY = e.pageY
		}
		else if (e.clientY)
		{
			this.docPosY = e.clientY + document.body.scrollLeft
		}
		return this.docPosY
	}
}

