///////////////////////////////////////////////////////////////////////////
//University of Illinois Extension                                       //
//Office of Web Development                                              //
//Alessandro Bellina - bellina@uiuc.edu                                  //
//Copyright 2007 - Board of Trustees                                     //
//                                                                       //
//This code is not open source, and is protected by copyright law.       //
//Any use other than in U of I Extension websites is strictly prohibited.//
///////////////////////////////////////////////////////////////////////////

////////////////EVENTS////////////////////////////////
var Event = new EventBinding();
function EventBinding (){
	this.customEvents = null;
}
EventBinding.prototype.bind = function (target, eventName, src, fn){
	switch (eventName){
		case "click":
			target.onclick = function () {fn.apply (src);}
			break;
			
		case "mouseover":
			target.onmouseover = function () {fn.apply (src);}
			break;
			
		case "mouseout":
			target.onmouseout = function () {fn.apply (src);}
			break;
			
		case "mousedown":
			target.onmousedown = function () {fn.apply (src);}
			break;
			
		case "mouseup":
			target.onmouseup = function () {fn.apply (src);}
			break;
			
		case "mousemove":
			var temp = target.onmousemove;
			target.onmousemove = function (evt) {
				if (typeof (temp) == "function")
					temp(evt);
				fn.apply (src);
			}
			break;
			
		case "load":
			target.onload = function () {
				fn.apply(src);
			}	
			target.onreadystatechange = function () {fn.apply(src);}		
			break;
			
		case "unload":
			target.onunload = function () {fn.apply(src);}		
			break;
			
		case "focus":
			target.onfocus = function () {fn.apply(src);}
			break;
			
		case "blur":
			target.onblur = function () {fn.apply(src);}
			break;
			
		case "statechange":
			target.onreadystatechange  = function () {fn.apply (src);}
			break;
			
		case "resize":
			target.onresize  = function () {fn.apply (src);}
			break;
			
		case "scroll":
			target.onscroll = function (){fn.apply (src);}
			break;
			
		case "change":
			target.onchange = function (){fn.apply (src);}
			break;
			
		case "keypress":
			target.onkeypress = function (e){fn.apply (src,new Array(e));}
			break;
			
		case "keydown":
			target.onkeydown = function (e){fn.apply (src,new Array(e));}
			break;
			
		case "keyup":
			target.onkeyup = function (e){fn.apply (src,new Array(e));}
			break;
			
		default: //execute registered events if any
			if (this.customEvents.length == 0)
				break;
			for (var i=0; i<this.customEvents.length; i++){
				var thisEvent = this.customEvents[i];
				if (thisEvent.eventName == eventName && thisEvent.target == target){
					thisEvent.callBackFn = fn;
					thisEvent.callBackSrc = src;
					break;
				}
			}
			break;
	}
}

EventBinding.prototype.register = function (target, eventName){
	if (this.customEvents == null){
		this.customEvents = new Array();
	}
	var thisEvent = new _EventElement(target, eventName);	
	this.customEvents.push (thisEvent);	
}

EventBinding.prototype.trigger = function (target, eventName, args){
	for (var i=0; i<this.customEvents.length; i++){
		var thisEvent = this.customEvents[i];
		if (thisEvent.eventName == eventName && thisEvent.target == target){
			thisEvent.callBackArgs = new Array (args);
			thisEvent._catch();
		}
	}
}

function _EventElement (target, eventName){
	this.target = target;
	this.eventName = eventName;
	this.theTrigger = false;
	this.callBackFn = null;
	this.callBackSrc = null;
	this.callBackArgs =  null;
}
_EventElement.prototype._catch = function (){
	if (typeof (this.callBackFn) == "function"){
		this.callBackFn.apply (this.callBackSrc, this.callBackArgs);
	}
}