/*
	This lib contains a list of functions handels events. Main object is : EventUtil 
	
	general syntax:	 words inside [] means that they is part of several examples: ex: [body.scroll]Left, Top = body.scrollLeft && body.scrollTop
				eventType == type of event ex: onmouseover
				handler == a handler object
				bubbling == bool, if bubbling mode is on or off
				object == any object fited for the assigment
	
	common methods:
		create:	add an event:			object.addEventListener( eventType, handler, bubbling ); (DOM)
								object.attachEvent( "on"+eventType, handler ); (IE)
		add:		
		get:		get the type of event: 	var sType = oEvent.type;
				get ascii key code: 		IE: var iKeyCode = oEvent.keyCode;
								DOM: var iKeyCode = oEvent.charCode;
				get other special keys:	var bShift = oEvent.shiftKey, altKey, ctrlKey;
				get mouse cords:		var iClientX = oEvent.clientX; (same for Y then X = Y)
				get screen cords:		var iScreenX = oEvent.screenX; (same for Y then X = Y)
				get what sent the event:	IE: var oTarget = oEvent.srcElement;
								DOM: var oTarget = oEvent.target;
		actions:	provent default behavior:	IE: oEvent.returnValue = false;
								DOM: oEvent.preventDefault();
				cancel bubbling:		IE: oEvent.cancelBubble = true;
								DOM: oEvent.stopPropagation ();
		properties:	check if keyKode is Event.isChar
				document.[body.scroll]Left, Top
		Event types:	mouse:				click, dblclick, mousedown, mouseout, mouseover, mouseup, mousemove
								clientX, clientY, type, target (DOM), srcElement (IE), [shift], [ctr]l, [alt,] [meta]Key (DOM), button
				keyboard:			keydown, keypress, keyup
								keyCode, charCode (DOM), target (DOM), srcElement (IE), , [shift], [ctr]l, [alt,] [meta]Key (DOM)
				html:				load, unload, abort, error, select, change, submit, reset, resize, scroll, focus, blur

*/
//alert( "eventHandler.js init:ed" );

//THE Object:
var EventUtil = new Object();


//adds events | Usage: EventUtil.addEventHandler(oDiv, "mouseover", handleEvent);
EventUtil.addEventHandler = function (oTarget, sEventType, fnHandler)
{
	if(oTarget.addEventListener) oTarget.addEventListener(sEventType, fnHandler, false); //for DOM-compliant browsers
	else if(oTarget.attachEvent) oTarget.attachEvent("on" + sEventType, fnHandler);  //for IE
	else oTarget["on" + sEventType] = fnHandler; //for all others
};

//removes events
EventUtil.removeEventHandler = function (oTarget, sEventType, fnHandler)
{
	if(oTarget.removeEventListener) oTarget.removeEventListener(sEventType, fnHandler, false); //for DOM-compliant browsers
	else if(oTarget.detachEvent) oTarget.detachEvent("on" + sEventType, fnHandler); //for IE
	else oTarget["on" + sEventType] = null; //for all others
};

//convert IE none-DOM-compatible-code to DOM compatible code
EventUtil.formatEvent = function (oEvent, bDebug )
{
	if (isIE && isWin)
	{
		//show the OS and browser in a alert message
		if( bDebug ) alert( SysInf.write() );
		
		//convert keyKode to charCode, if there's been a keypressed otherwise charCode = 0;
		oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keyCode : 0;
		
		//sees so that bubbling is activated (IE only supports that)
		oEvent.eventPhase = 2;
		
		//if isChar contains a char (is not 0)
		oEvent.isChar = (oEvent.charCode > 0);
		
		//calculates the clients browser winows space from clientX and scrollLeft (same with Y)
		oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
		oEvent.pageY = oEvent.clientY + document.body.scrollTop;
		
		//translate preventDefault (IE uses returnValue = false;
		oEvent.preventDefault = function () { this.returnValue = false;	};
		
		//get's the rigth relatedTarget from IE's to- and fromElement.
		if (oEvent.type == "mouseout") oEvent.relatedTarget = oEvent.toElement;
		else if (oEvent.type == "mouseover") oEvent.relatedTarget = oEvent.fromElement;
		
		//stops bubbling (aka cancelBubble = true in IE)
		oEvent.stopPropagation = function () { this.cancelBubble = true; };
		
		//target is called srcElement in IE, fix this
		oEvent.target = oEvent.srcElement;
		
		//fixes the time propertie
		oEvent.time = (new Date).getTime();
	}
	
	//if not IE (hopfully DOM-compatible browser) just return oEvent as it whare.
	return oEvent;
};

//get the event object. | Usage: var oEvent = EventUtil.getEvent();
EventUtil.getEvent = function()
{
	//for IE (that has the window.event preperty)
	if (window.event) return this.formatEvent(window.event);
	
	//caller is a pointer to the calling function, in this case the eventHandler. the first argument in eventHanler is the event!
	else return EventUtil.getEvent.caller.arguments[0];
};

/*EXAMPLE USING THE CODE ABOVE:
<html>
	<head>
		<title>Mouse Events Example</title>
		<script type="text/javascript" src="systemDetect.js"></script>
		<script type="text/javascript" src="eventHandler.js"></script>
		<script type="text/javascript">
			EventUtil.addEventHandler(window, "load", function (){
				var oDiv = document.getElementById("div1");
				EventUtil.addEventHandler(oDiv, "mouseover", handleEvent);
				EventUtil.addEventHandler(oDiv, "mouseout", handleEvent);
				EventUtil.addEventHandler(oDiv, "mousedown", handleEvent);
				EventUtil.addEventHandler(oDiv, "mouseup", handleEvent);
				EventUtil.addEventHandler(oDiv, "click", handleEvent);
				EventUtil.addEventHandler(oDiv, "dblclick", handleEvent);
				}
			);
			function handleEvent()
			{
				var oEvent = EventUtil.getEvent();
				var oTextbox = document.getElementById("txt1");
				oTextbox.value += "\n>" + oEvent.type;
				oTextbox.value += "\n target is " + oEvent.target.tagName;
				if (oEvent.relatedTarget)
				{
					oTextbox.value += "\n relatedTarget is "
					+ oEvent.relatedTarget.tagName;
				}
			}
		</script>
	</head>
	<body>
		<p>Use your mouse to click and double click the red square.</p>
		<div style="width: 100px; height: 100px; background-color: red" id="div1"></div>
		<p><textarea id="txt1" rows="15" cols="50"></textarea></p>
	</body>
</html>
*/
//end of comment


