
// --------------------------------------------------------------------------
// domAdditions.js
// DOM extensions + utility functions.
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// document.createElement()
// Adds attribute and child parameters.
// --------------------------------------------------------------------------

// check to prevent infinite loop is script is double loaded
if (typeof document._createElement == "undefined")
{
	document._createElement = document.createElement;
};

document.createElement = function (tag, attributes, children)
{
	var element = document._createElement(tag);

	for (var i in attributes)
	{
		if (i == "className" || i == "class")
		{
			element.className = attributes[i];
		}
		else
		{
			element.setAttribute(i, attributes[i]);
		};
	};

	if (arguments.length > 2 && children)
	{
		if (typeof children == "object" && children.constructor == Array)
		{
			for (var i = 0; i < children.length; i++)
			{
				_addChild(element, children[i]);
			};
		}
		else
		{
			_addChild(element, children);
		};
	};

	return element;
};

function _addChild (el, child)
{
	if (typeof child == "object")
	{
		el.appendChild(child);
	}
	else if (typeof child == "string" || typeof child == "number")
	{
		// element.appendChild(document.createTextNode(children));
		el.innerHTML += child;
	};
};

/*
// --------------------------------------------------------------------------
// document.clearChildren()
// Destroy child nodes. It would be nice if this method were prototyped
// onto all HTML elements [element.removeChildren()].
// --------------------------------------------------------------------------
document.clearChildren = function (el)
{
	while (el.childNodes.length)
	{
		el.removeChild(el.firstChild);
	};
};
*/

// --------------------------------------------------------------------------
// $()
// Shorthand.
// --------------------------------------------------------------------------
function $ (id)
{
	return document.getElementById ? document.getElementById(id) : document.all[id];
};

// --------------------------------------------------------------------------
// hitch()
// Associate an object + method, and force the right this context.
// Usage: button.onclick = hitch(Davey, Davey.init);
// --------------------------------------------------------------------------
function hitch (obj, meth)
{
	return function ()
	{
		return typeof meth == "function" ? meth.apply(obj, arguments) : obj[meth].apply(obj, arguments);
	};
};

// --------------------------------------------------------------------------
// getStyle()
// Cross-browser computed style attribute access. This probably wants to be
// attached to document or HTMLElement or something.
// --------------------------------------------------------------------------
function getStyle (el, styleProp)
{
	if (window.getComputedStyle)
	{
		return window.getComputedStyle(el, null).getPropertyValue(styleProp);
	}
	else if (document.defaultView && document.defaultView.getComputedStyle)
	{
		try
		{
			return document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
		}
		catch (e)
		{
			return "";
		};
	}
	else if (el.currentStyle)
	{
		styleProp = styleProp.replace(/\-(.)/g, function ()
		{
			return arguments[1].toUpperCase();
		});
		return el.currentStyle[styleProp];
	};

	return null;
};

// --------------------------------------------------------------------------
// insertAfter()
// --------------------------------------------------------------------------
function insertAfter (obj, sibling)
{
	if (!obj || !sibling || !sibling.parentNode)
	{
		return null;
	};

	return sibling.nextSibling ? sibling.parentNode.insertBefore(obj, sibling.nextSibling) : sibling.parentNode.appendChild(obj);
};
