addEvent(window,"load",setupButtons);


function setupButtons() {
	var	els  //Array of page elements
	els = document.getElementsByTagName("button"); 		  
	for (var thisel = 0; thisel < els.length;  thisel++)	{
			addClass("button",els[thisel]);
			addEvent(els[thisel],"mouseover", _buttonover);
			addEvent(els[thisel],"mouseout", _buttonout);
			addEvent(els[thisel],"focus", _buttonover);
			addEvent(els[thisel],"blur", _buttonout);
			eval("try{addEvent(els[thisel],\"click\","+ els[thisel].id +"_click);}catch(e){}");
			eval("try{addEvent(els[thisel],\"dblclick\","+ els[thisel].id +"_click);}catch(e){}");
	}
}

function _keypress() {
	if(event.keyCode == 13) {
		this.click();	
	}
}

function _buttonover() {
	addClass("buttonOver",this);
}

function _buttonout() {
	removeClass("buttonOver",this);
}

function addClass(thisclassname,obj) {
	if (!hasClass(thisclassname,obj.className)) {
		obj.className +=" " + thisclassname;		
	}
}

function removeClass(thisclassname,obj) {
	var oldclass = obj.className;
	var newclass = "";
	var check ="";
	oldclass = oldclass.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
	cns = oldclass.split(" ");
	for (var thisclass = 0; thisclass < cns.length; thisclass++) {
		check = cns[thisclass];
		check = check.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
		if(check.toUpperCase() != thisclassname.toUpperCase()) {
			newclass += " " + cns[thisclass];		
		}
	}
	while(newclass.indexOf("  ") >-1) {
		newclass = newclass.replace("  "," ");
	}
	obj.className = newclass;
}

function hasClass(findclass,classnames) {
	var result = false;
	findclass=findclass.toUpperCase();
	classnames = classnames.toUpperCase();
	var pattern = new RegExp("(^|\\s)" + findclass +"(\\s|e)");
	if ( pattern.test(classnames) ) {
		result = true;
	}
return result;
}



/***************************/

// Javascript Event functions from 
// 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;
		}
	}
}
/***************************/



