
/*
 * Syntax sugar
 */

function $(id)
{
	return document.getElementById(id);
}


/*
 * String extensions
 */

String.prototype.format = function()
{
	var str = this;

	for (var i = 0; i < arguments.length; i++)
	{
		str = str.replace('{' + (i) + '}', arguments[i]);
	}

	return str;
}


/*
 * General html utilities
 */

HtmlUtils = function() {}

HtmlUtils.show = function(elem)
{
	if (elem)
	{
		elem.style.visibility = '';
		elem.style.display = '';
	}
}

HtmlUtils.hide = function(elem)
{
	if (elem)
	{
		elem.style.visibility = 'hidden';
		elem.style.display = 'none';
	}
}

HtmlUtils.getChildById = function(domTree, id)
{
	if (!domTree || !domTree.childNodes) return null;
	if (domTree.id == id) return domTree;
	
	for (var i = 0; i < domTree.childNodes.length; i++)
	{
		var curChild = domTree.childNodes[i];

		if (curChild.id == id)
		{
			return curChild;
		}
		
		var childResult = HtmlUtils.getChildById(curChild, id);
		if (childResult)
		{
			return childResult;
		}
	}
	
	return null;
}


/*
 * Event extensions
 */

function EventLib(obj)
{
	if (obj.attachEvent && obj.detachEvent)
	{
		this.addEvent = function(obj, type, fn)
		{
			obj['e' + type + fn] = fn;
			obj[type + fn] = function()
			{
				obj['e' + type + fn](window.event);
			}
			obj.attachEvent('on' + type, obj[type + fn]);
		}

		this.removeEvent = function(obj, type, fn)
		{
			obj.detachEvent('on' + type, obj[type + fn]);
			obj[type + fn] = null;
		}
	}
	else if (obj.addEventListener && obj.removeEventListener)
	{
		this.addEvent = function(obj, type, fn)
		{
			obj.addEventListener(type, fn, false);
		}

		this.removeEvent = function(obj, type, fn)
		{
			obj.removeEventListener(type, fn, false);
		}
	}
}

Events = new EventLib(document);


