//variables used by functions

this.onload = init;

//if some browser does not know the Node object
if (!window.Node) {
	var Node = {            // If there is no Node object, define one
		ELEMENT_NODE: 1,    // with the following properties and values.
		ATTRIBUTE_NODE: 2,  // Note that these are HTML node types only.
		TEXT_NODE: 3,       // For XML-specific nodes, you need to add
		COMMENT_NODE: 8,    // other constants here.
		DOCUMENT_NODE: 9,
		DOCUMENT_FRAGMENT_NODE: 11
	}
}

//stores all possible events
var arrEvents = new Array('onclick', 'ondblclick', 'onmousedown', 'onmouseup', 'onmouseover', 'onmousemove', 'onmouseout', 'onkeypress', 'onkeydown', 'onkeyup', 'onchange', 'onfocus', 'onblur', 'onselect');

//array function to check for a certain value in an array. call: array.contains(value)
Array.prototype.contains = function(value) {	for (var i = 0; i < this.length; i++) {		if (this[i] == value) return true;	}	return false;}


//functions:
//start lib_:
function init(){
	traverseTags(document);
}




//checks if the id attribute is a event
function isEventTag(idAttribute){
	var spliter = idAttribute.split('_');//spliter is the array with all parameters form the id attribute: e.g. onclick_somefunction_sometag_param1_param2_param3
	if(arrEvents.contains(spliter[0])) return spliter;
	return false;
}

//recursively traverses all tags an checks id attributes for events
function traverseTags(n){
	var children = n.childNodes;
	if(n.nodeType == Node.ELEMENT_NODE  && n.attributes.length > 0){//only if the node as any attributes
		for(var i = 0; i < n.attributes.length; i++){

			if(n.attributes[i].nodeName == 'id' && isEventTag(n.attributes[i].nodeValue)){//only if it is an event id-attribute
				var params = isEventTag(n.attributes[i].nodeValue);
				
				var eve = getEventOfString(params.shift());//in the first place of the array we have the name of the event

				var funct = window[params.shift()];//at the 2nd place of the array we have the function name, window['functionname'] points to the function

				addEvent(n, eve, funct, false);//{function: setFunction}
				n.params = params;//adding the params to the element, so that the funct can access it later
				break;
			}
		}
	}
	
	for(var i=0; i < children.length; i++) {    // Loop through the children     
		 traverseTags(children[i]);
	}			
}

//prevents a certain event from calling its default browser event
function prevent(e){
	if(typeof e.preventDefault != 'undefined'){
		e.preventDefault();
	}
	else{//iex
		event.returnValue = false;
	}
}

//e.g. cuts the 'on' out of onclick, used to set the event.
function getEventOfString(eventstr){
	return eventstr.slice(2, eventstr.length);					
}
				
				
				
				
// the following is written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// thank you guys!!!
// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};				
