Lib.Utils.NamespaceUtils.createIfNecessary('Lib.Utils');

/**
* created by: Nate Minshew
* date created: 09/06/2005
* description: This is a javascript class that handles modifying events on javascript objects.  It allows you to add
*   and remove multiple events like 'onclick' and 'onchange' to html elements.  This is provided in replace of changing
*   the function that the event is set to, for example:
*
*   window.onload = function () { ... };
*
*   This is bad because no other object can now set onload events without overridding the current event.  This class
*   solves that problem.
*/
Lib.Utils.EventUtils = {};

Lib.Utils.EventUtils.EVENTS = new Array();

/**
* created by: Nate Minshew
* date created: 09/06/2005
* access level: public
* description: Adds an event to the specified object.
*
* _obj - Object the event is to be applied to.
* _evType - Type of event to add, ei. 'click' for 'onclick'.
* _fn - Function to call for the event.
*/
Lib.Utils.EventUtils.addEvent = function(obj, type, fn, ctxObj) {
    if (Lib.Utils.ObjectUtils.isDefined(ctxObj)) {
        fn = Lib.Utils.ObjectUtils.weakBind(fn, ctxObj);
    }

    if (obj.addEventListener) {
        obj.addEventListener(type, fn, false);
        var id = Lib.Utils.EventUtils.EVENTS.length;
        Lib.Utils.EventUtils.EVENTS.push(fn);
        return id;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + type, fn);
        var id = Lib.Utils.EventUtils.EVENTS.length;
        Lib.Utils.EventUtils.EVENTS.push(fn);
        return id;
    } else {
        return -1;
    }
}

/**
* created by: Nate Minshew
* date created: 09/06/2005
* access level: public
* description: Removes an event from the specified object.
*
* _obj - Object the event is to be removed from.
* _evType - Type of event to remove, ei. 'click' for 'onclick'.
* _fn - Function the event was calling.
*/
Lib.Utils.EventUtils.removeEvent = function(obj, type, id) {
    var fn = Lib.Utils.EventUtils.EVENTS[id];
    if (obj.removeEventListener) {
        obj.removeEventListener(type, fn, false);
        return true;
    } else if (obj.detachEvent) {
        var r = obj.detachEvent("on" + type, fn);
        return r;
    } else {
        return false;
    }
}

/**
* created by: Nate Minshew
* date created: 03/30/2006
* access level: public
* description:
*   This method cancels the default action taken by the specified event.
*
* @param event - Event containing the action to cancel.
*/
Lib.Utils.EventUtils.cancelDefaultAction = function(event) {
    if (Lib.Utils.ObjectUtils.isDefined(event) && Lib.Utils.ObjectUtils.isDefined(event.preventDefault)) {
        event.preventDefault();
    } else if (Lib.Utils.ObjectUtils.isDefined(event)) {
        event.returnValue = false;
    }
}

/**
* created by: Nate Minshew
* date created: 03/30/2006
* access level: public
* description:
*   This method cancels event bubbling for the specified event.  This means that any other actions tied to the event at
*   a higher level will be canceled.
*
* @param event - Event object to cancel bubbling on.
*/
Lib.Utils.EventUtils.cancelEventBubbling = function(event) {
    if (Lib.Utils.ObjectUtils.isDefined(event) && Lib.Utils.ObjectUtils.isDefined(event.stopPropagation)) {
        event.stopPropagation();
    } else if (Lib.Utils.ObjectUtils.isDefined(event)) {
        event.cancelBubble = true;
    }
}