
//wrapper function for DOM method getElementById
function $(id)
{
	return document.getElementById(id);
}



//wrapper function for DOM method getElementsByTagName
function $$(parent,tag)
{
	p = $(parent) || document;
	return p.getElementsByTagName(tag);
}



function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}



function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}



//http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/
//cross browser addEventListener
function addEventLite( obj, type, fn ) 
{
	if (obj.addEventListener)
	{
		obj.addEventListener(type, fn, false);
	}
	else if (obj.attachEvent) 
	{
		obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
	}
}



//http://www.javascriptkit.com/jsref/event.shtml
//cross browser preventDefault
function stopDefault(e)
{
	//alert('default action has been cancelled');
	var evt = window.event || e;
 	if (evt.preventDefault)  //W3C
	{
  		evt.preventDefault();
	}	
 	else //IE browser
	{ 
  		return false;
	}
}

//Peter Paul Koch - www.quirksmode.org
//dynamically getting style of element without need for inline style.
function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
	{
		var y = x.currentStyle[styleProp];
	}
	else if (window.getComputedStyle)
	{
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	}
	
	return y;
}




/**
* This is used to change the className property of a given element.
* http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
* NOTE: function name changed to be more descriptive.
* @param a:String
* defines the action you want the function to perform. See below &
* @param el:element reference
* the element in question.
* @param c1:String
* the name of the first class
* @param c2:String
* the name of the second class
* 
* & Possible actions are:
* 
* swap
* replaces class c1 with class c2 in element el. c2 is optional if not swapping. 
(i.e. changeClassName('add',tohide,'hidden'))
* add
* adds class c1 to the element el.
* remove
* removes class c1 from the element el.
* check
* test if class c1 is already applied to element el and returns true or false.
*/
function changeClassName(a,el,c1,c2)
{
	switch (a)
	{
		case 'swap':
			el.className=!changeClassName('check',el,c1)?el.className.replace(c2,c1):el.className.replace(c1,c2);
		break;
		case 'add':
			if(!changeClassName('check',el,c1)){el.className+=el.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=el.className.match(' '+c1)?' '+c1:c1;
			el.className=el.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(el.className)
		break;
	}
}



// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler)
{
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else
	{
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers)
		{
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
	
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler)
{
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}

function handleEvent(event)
{
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers)
	{
		if (!Object.prototype[i])
		{
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}

	if (this.$$handler) this.$$handler = null;

	return returnValue;
}

function fixEvent(event)
{
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function()
{
	this.returnValue = false;
}
fixEvent.stopPropagation = function()
{
	this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener)
{
	document.onreadystatechange = function()
	{
		if (window.onload && window.onload != handleEvent)
		{
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}
