/*****
    File Name: domFunctions.js
    Author: Ryan Kinal
	Date: 05/19/06
	Description:  General-use functions, mostly for event handling
	Updated By:
	Updated Date: 6/16/06
	Last Change: Added a find to the Array prototype
*****/


/*****
	Cross-browser event handling for IE5+, NS6+, and Mozilla/Gecko
		By Scott Andrews
*****/
function addEvent(p_elm, p_evType, p_fn, p_useCapture)
{
	if (p_elm.addEventListener)
	{
		p_elm.addEventListener(p_evType, p_fn, p_useCapture);
		return true;
	}
	else if (p_elm.attachEvent)
	{
		return p_elm.attachEvent("on" + p_evType, p_fn);
	}
	else
	{
		//TODO We may want to create a new function each time and keep adding fn to it. If we do though, we can't remove them easily. If we don't, every add removes the previous add.
		p_elm["on" + p_evType] = fn;
		return true;
	}
}

/***
	Cross-browser event removal
***/
function removeEvent(p_elm, p_evType, p_fn, p_useCapture)
{
	if (p_elm.removeEventListener)
	{
		p_elm.removeEventListener(p_evType, p_fn, p_useCapture);
		return true;
	}
	else if (p_elm.detachEvent)
	{
		return p_elm.detachEvent("on" + p_evType, p_fn);
	}
	else
	{
		//TODO If we do add multiple event handlers with DOM 1, how do we remove them?
		p_elm["on" + p_evType] = null;
		return true;
	}
}

/*****
	Checks for double click on element p_elem, and calls function p_function if a double click happens
*****/
function makeDoubleClick(p_elem, p_func, p_bubble, p_speed)
{
	var DOUBLE_CLICK_SPEED = (p_speed) ? p_speed : 300;

	var dbl = function(p_e)
	{
		addEvent(p_elem, "click", p_func, false);
		var timeOut = setTimeout(function() { removeEvent(p_elem, "click", p_func, false); }, DOUBLE_CLICK_SPEED);
	}
	
	addEvent(p_elem, "click", dbl, p_bubble);
}

/*****
	Checks for click and hold on element p_elem, and calls function p_function if a click and hold happens
*****/
function makeClickAndHold(p_elem, p_func, p_bubble, p_time)
{
	var CLICK_AND_HOLD_LENGTH = (p_time) ? p_time : 600;
	cnhEvent = new Object();
	
	
	var cnh = function(p_e)
	{
		var pos = mousePos(p_e);
		cnhEvent.x = pos.x;
		cnhEvent.y = pos.y;
		var fireEvent = setTimeout(function() {var ev = p_e; p_func(ev); }, CLICK_AND_HOLD_LENGTH);
		addEvent(window, "mouseup", function() { clearTimeout(fireEvent); }, p_bubble);
	}
	
	addEvent(p_elem, "mousedown", cnh, p_bubble);
}

/*****
	Returns mouse position from event p_e.  This should only be called
	from an event handler, as Internet Explorer will not have an event
	to pass to this function, and instead relies on window.event.  This
	will return the mouse position as an object.  obj.x is the mouse's
	x position, obj.y is the mouse's y position.

	Author:  Ryan Kinal
*****/
function mousePos(p_e)
{
	var posx = 0;
	var posy = 0;

	if (!p_e)
	{
		var e = window.event;
	}
	else
	{
		var e = p_e;
	}

	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}

	var returnObj = new Object();
	returnObj.x = posx;
	returnObj.y = posy;
	return returnObj;
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/***
	A very simple find, added to the array prototype.
	Simply call with myArray.find(element)

	Author: Ryan Kinal
***/
Array.prototype.find = function(p_elem)
{
	for (var i = 0; i < this.length; i++)
	{
		if (this[i] == p_elem)
		{
			return i;
		}
	}

	return false;
}

Array.prototype.remove = function(p_elem)
{
	var index = this.find(p_elem);
	var before = this.slice(0, index);
	var after = this.slice(index + 1);
	before = before.concat(after);
	return before;
}

function createXHR()
{
	var xhr;
	if (window.ActiveXObject)
	{
		try
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			alert(e.message);
			xhr = null;
		}
	}
	else
	{
		xhr = new XMLHttpRequest();
	}

	return xhr;
}


String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/, '');
}
