/**
 * @projectDescription
 * Cross-browser wrapper for the W3C DOM Events model. YUI has the same type of thing in their Event
 * utility, but this encapsulates only DOM Event handling and is thus much smaller.
 *
 * @author adam.mcintyre@molecular.com
 * @namespace MOLECULAR.util.CBEvent
 * @version 1.0
 */

if(typeof MOLECULAR == 'undefined') {
    MOLECULAR = {};
}
if(typeof MOLECULAR.util == 'undefined') {
    MOLECULAR.util = {};
}
MOLECULAR.util.CBEvent = function() {
    return{getTarget:function(e) {
        var evt = this.normalizeEvent(e);
        var target;
        if(evt.target) {
            target = evt.target;
        }
        else if(evt.srcElement) {
            target = evt.srcElement;
        }
        return this.correctTextNode(target);
    },getEventSource:function(e) {
        var evt = this.normalizeEvent(e);
        if(evt.type == 'mouseover') {
            var target = evt.relatedTarget || evt.fromElement;
            return this.correctTextNode(target);
        }
        else {
            var target = evt.relatedTarget || evt.toElement;
            return this.correctTextNode(target);
        }
    },isInTarget:function(e, el) {
        var evt = this.normalizeEvent(e);
        var tgt = this.getTarget(evt);
        if(tgt.nodeName.toLowerCase() != el.nodeName.toLowerCase()) {
            return true;
        }
        var evtSource = this.getEventSource(evt);
        if(evtSource == el) {
            return true;
        }
        while(evtSource != tgt && evtSource.tagName != 'BODY') {
            evtSource = evtSource.parentNode;
        }
        if(evtSource == tgt) {
            return true;
        }
        else {
            return false;
        }
    },normalizeEvent:function(e) {
        if(!e) {
            return window.event;
        }
        return e;
    },correctTextNode:function(nd) {
        if(nd.nodeType == 3) {
            return nd.parentNode;
        }
        return nd;
    }}
}();

