/**
 * Copyright Kronos, Inc. All rights reserved. Do not use in any way.
 * Author: dross@kronos.com
 */

function getEventTargetAndFireScriptInScope(e, code){
	fireScriptInScope(document['com.kronos.event'].wrapInspection(e).getTarget(), code)
}
function fireScriptInScope(t, applyFunction){
	if(typeof(applyFunction)=='function'){
		applyFunction.apply(t)
	}else{
		(function(){eval(applyFunction)}).apply(t)
	}
}

document['com.kronos.event'] = {

	/**
	 *  Add a w3c dom event listener to a dom element.
	 */
	addEventListener: function(element, name, handler){
		info('addEventListener: adding event "'+name+'" to '+identifyObj(element))
		if(typeof(handler)=='function'){
			handler=new Function('event', 'getEventTargetAndFireScriptInScope(event,'+handler+')')
		}else{
			handler=new Function('event', 'getEventTargetAndFireScriptInScope(event,"'+handler+'")')
		}
		if(element.addEventListener){
			element.addEventListener(name, handler, true)
		}else if(element.attachEvent){
			element.attachEvent('on'+name, handler)
		}else{
			element['on'+name]=handler
		}
	},

	/**
	 * Remove a w3c dom event listener from a dom element.
	 */
	removeEventListener: function(element, name, handler){
		if(element.removeEventListener){
			element.removeEventListener(name, handler)
		}else{
			element.detachEvent(name, handler)
		}
	},

	/**
	 * Invoke the object specified by element[eventName].
	 * If object is a function, include arguments in the
	 * call otherwise evel object.
	 */
	invokeEvent: function(element, eventName, arguments){
		if(typeof(element[eventName])=='function'){
			return element[eventName](arguments)
		}
		var val=eval(element[eventName])
		if(typeof(val)=='function'){
			// invoke within the element's scope
			var key='com.kronos.event-utils.invokeEvent.'+eventName
			element[key]=val
			val=element[key](arguments)
			delete element[key]
		}
		return val
	},
	
	/** prototype methods for cross browser event inspection */
	wrapInspection:function(e){

		/** make sure we have an event object */
		e = (window.event)? window.event: (e)? e: new Object()
		
		/** returns the name of the event being fired, for example, click, blur or focus */
		e.getType = function(){
			return this.type
		}
		
		/** find the target object for this event */
		e.getTarget = function(){
			var target = null
			if(this.target){
				// w3c
				target = this.target
			}else if(this.srcElement){
				// ie
				target = this.srcElement
			}
			if(target){
				// if the event is managed by a delegate
				// then return the delegate object
				if(target.delegate){
					this.stopPropagation()
					target=target.delegate
				}
			}
			return target
		}

		e.getRelatedTarget = function(){
			return this.relatedTarget || this.toElement
		}
		
		/** determine if the shift key was pressed during the event */
		e.isShiftKey = function(){
			if(this.modifiers){
				return this.modifiers & Event.SHIFT_MASK
			}
			return this.shiftLeft || this.shiftKey
		}
		
		/** determine if the control key was preddes during the event */
		e.isControlKey = function(){
			if(this.modifiers){
				return this.modifiers & Event.CONTROL_MASK
			}
			return this.ctrlLeft || this.ctrlKey
		}
		
		/** stop the propagation of the event bubbling */
		if(!e.stopPropagation){
			e.stopPropagation = function(){
				this.cancelBubble = true
			}
		}
		
		/** return event with the inspection methods */
		return e
		
	}

}

