// return a reference to the supplied object, whether we refer to it by name or by reference
function getLazyReference(obj)
{
	if (obj.id)
	{
		return obj;
	}
	else
	{
		return document.getElementById(obj);
	}
}

// actually add an event listener, regardless of browser
function addListener(obj, eventType, listenerFunction)
{
	if (obj.addEventListener)
	{
//trace("adding " + eventType + ": " + listenerFunction);
		obj.addEventListener(eventType, listenerFunction, true);
		
	}
	else if (obj.attachEvent)
	{
		var success = obj.attachEvent("on" + eventType, listenerFunction);
	}
}

function cancelEvent(e)
{
	e.cancelBubble = true;
	
	if (e.preventDefault)
	{
		e.preventDefault()
	}
	
	if (e.stopPropagation)
	{
		e.stopPropagation()
	}
	
	return false;
}

// yank an element out of an array (by reference, so nothing is returned)
function arrayRemove(array, obj)
{
	for (var a=0; a<array.length; a++)
	{
		if (array[a] == obj)
		{
			array.splice(a, 1);
		}
	}
}

// recursively find the X position of the specified element.	
function getLeft(obj)
{
	if (obj.offsetParent)
	{
		return obj.offsetLeft + getLeft(obj.offsetParent);
	}
	else
	{
		return obj.offsetLeft;
	}
}

// recursively find the Y position of the specified element.	
function getTop(obj)
{
	if (obj.offsetParent)
	{
		var off = obj.offsetTop + getTop(obj.offsetParent);
		return off;
		//return obj.offsetTop + getTop(obj.offsetParent);
	}
	else
	{
		return obj.offsetTop;
	}
}

// recursively find the first descendant of this object with the specified className
function getDescendantByClassName(obj, className)
{
	if (obj.className == className)
	{
		return obj;
	}
	
	for (var a=0; a<obj.childNodes.length; a++)
	{
		/*
		if (obj.childNodes[a].className == className)
		{
			return obj.childNodes[a];
		}
		*/
		
		var descendant = getDescendantByClassName(obj.childNodes[a], className);
		if (descendant != null)
		{	
			return descendant;
		}
	}
	
	return null;
}

function trace(obj, goBig)
{
	var txt = document.getElementById('txt');
	if (txt == null)
	{
		return;
	}
	
	txt.value += obj + "\r\n";
	
	if (goBig)
	{
		for (key in obj)
		{
			txt.value += key + ":" + obj[key] + "\r\n";
			
		}
		txt.value += "\r\n\r\n";
	}

}

function PngSrc(image, src)
{
	// IE6 doesn't like PNGs, so we'll try GIFs instead
	if (document.all && navigator.appVersion.indexOf("MSIE 6") > -1)
	{
		src = src.replace(".png", ".gif");
	}
	
	image.src = src;
}

function moveElementTo(element, x, y)
{
	var obj = getLazyReference(element);

	obj.style.left = x + 'px';
	obj.style.top = y + 'px';
}

function slideElementTo(element, x, y, stepCount, hideOnArrival)
{
	var obj = getLazyReference(element);
	var currentX = parseInt(obj.style.left);
	var currentY = parseInt(obj.style.top);
	
	var newX = currentX + (x - currentX)/stepCount;
	var newY = currentY + (y - currentY)/stepCount;
	obj.style.left = newX + 'px';
	obj.style.top = newY + 'px';
	
	if (stepCount > 1)
	{
		hideOnArrival = (hideOnArrival) ? true : false; // deal with possible nulls
		var script = "slideElementTo('" + obj.id + "', " 
						+ x + ", " 
						+ y + ", " 
						+ (stepCount-1) + ", "
						+ hideOnArrival + ")";
		
		setTimeout(script, 30);  
	}
	else if (hideOnArrival)
	{
		obj.style.display = "none";
	}
	
}    

