//////////////////////////////////////////////////////////////////////////////
//	Event
//
//	Description:
//		Event wrapper.
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
//	Jax.Event
//////////////////////////////////////////////////////////////////////////////
Jax.Event = function(evnt) {
    
    var m_event;
    
    // Declare class and initialize instance.
    Jax.oop.declare(Jax.Event);
    this.setInstance();
    
    //////////////////////////////////////////////////////////////////////////
    //	initialize
    //////////////////////////////////////////////////////////////////////////
    this.initialize = function(evnt) {
        this.setEvent(evnt);
    }
    
    //////////////////////////////////////////////////////////////////////////
    //  checkType
    //////////////////////////////////////////////////////////////////////////
    Jax.Event.checkType = function(eventType) {
        var retval = false;
        Jax.EventTypes.forEach(function(item) {
            if (item[0] == eventType) {
                retval = true;
            }
            return retval;
        });
        return retval;
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	getElement
    //////////////////////////////////////////////////////////////////////////
    this.getElement = function() {
        return (m_event.currentTarget) ? 
            m_event.currentTarget : m_event.srcElement;
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	getEvent
    //////////////////////////////////////////////////////////////////////////
    this.getEvent = function() { return m_event; }
        
    //////////////////////////////////////////////////////////////////////////
    //	getEventType
    //////////////////////////////////////////////////////////////////////////
    this.getEventType = function() { return m_event.type; }
        
    //////////////////////////////////////////////////////////////////////////
    //	setEvent
    //////////////////////////////////////////////////////////////////////////
    this.setEvent = function(evnt) { 
        m_event = window.event || evnt;
        if (m_event != undefined && m_event.getEvent != undefined) {
            m_event = m_event.getEvent();
        }
    }
    
    //////////////////////////////////////////////////////////////////////////
    //      stopPropagation
    //////////////////////////////////////////////////////////////////////////
    this.stopPropagation = function() {
        if (m_event.stopPropagation) {
            m_event.stopPropagation();
        }
        else {
            m_event.cancelBubble=true
        }
    }    
    
    //////////////////////////////////////////////////////////////////////////
    // initialize object.
    //////////////////////////////////////////////////////////////////////////
    this.initialize(evnt);        
}

//////////////////////////////////////////////////////////////////////////
// Jax.EventTypes
//////////////////////////////////////////////////////////////////////////
Jax.EventTypes = new Hashtable (
    ["abort", "abort"], 
    ["blur", "onblur"],
    ["change", "change"],
    ["click", "onclick"],
    ["dblclick", "dblclick"],
    ["dragdrop", "dragdrop"],
    ["error", "error"],
    ["focus", "onfocus"],
    ["keydown", "onkeydown"],
    ["keypress", "onkeypress"],
    ["keyup", "onkeyup"],
    ["load", "load"],
    ["mousedown", "onmousedown"],
    ["mouseout", "onmouseout"],
    ["mouseover", "onmouseover"],
    ["reset", "reset"],
    ["resize", "resize"],
    ["scroll", "scroll"],
    ["select", "select"],
    ["submit", "submit"],
    ["unload", "unload"]
);

//////////////////////////////////////////////////////////////////////////
// Jax.Event.create
//////////////////////////////////////////////////////////////////////////
Jax.Event.create = function(evnt) {
    var theEvent = window.event || evnt;
    switch (theEvent.type) {
        case "keydown":
        case "keypress":
        case "keyup":
            return new Jax.KeyEvent(evnt);
            
        case "mousedown":
        case "mouseout":
        case "mouseover":
            return new Jax.MouseEvent(evnt);
        
        default:
            return new Jax.Event(evnt);
    }
}

//////////////////////////////////////////////////////////////////////////////
//	Jax.KeyEvent
//////////////////////////////////////////////////////////////////////////////
Jax.KeyEvent = function(evnt) {
    var m_keyCode = null;

    // Declare class and initialize instance.
    Jax.oop.declare(Jax.KeyEvent, Jax.Event);
    this.setInstance();
    
    // Set event.
    this.setEvent(evnt);
    
    //////////////////////////////////////////////////////////////////////////
    //	isAlphaKey
    //////////////////////////////////////////////////////////////////////////
    this.isAlphaKey = function() {
        var theChar = String.fromCharCode(this.getKey());
        var reChar = new RegExp(/[a-zA-Z]/);
        return reChar.test(theChar);
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	isBackspaceKey
    //////////////////////////////////////////////////////////////////////////
    this.isBackspaceKey = function() {
        return this.getKey() == 8;
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	isEnterKey
    //////////////////////////////////////////////////////////////////////////
    this.isEnterKey = function() {
        return this.getKey() == 13;
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	isTabKey
    //////////////////////////////////////////////////////////////////////////
    this.isTabKey = function() {
        return this.getKey() == 9;
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	isNumberKey
    //////////////////////////////////////////////////////////////////////////
    this.isNumberKey = function() {
        var theChar = String.fromCharCode(this.getKey());
        var reChar = new RegExp(/[0-9]/);
        return reChar.test(theChar);
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	isWhitespaceKey
    //////////////////////////////////////////////////////////////////////////
    this.isWhitespaceKey = function() {
        var theChar = String.fromCharCode(this.getKey());
        var reChar = new RegExp(/[\s]/);
        return reChar.test(theChar) || this.isBackspaceKey();
    }
    
    //////////////////////////////////////////////////////////////////////////
    //	getKey
    //////////////////////////////////////////////////////////////////////////
    this.getKey = function() {
        var evnt = this.getEvent();
        if (m_keyCode == null) {
            if (window.event) {
                m_keyCode = evnt.keyCode;
            }
            else {
                m_keyCode = (evnt.keyCode == 0) ? evnt.charCode : evnt.keyCode;
            }
        }
        
        return m_keyCode;
    }
}

//////////////////////////////////////////////////////////////////////////////
//	Jax.MouseEvent
//////////////////////////////////////////////////////////////////////////////
Jax.MouseEvent = function(evnt) {

    // Declare class and set instance.
    Jax.oop.declare(Jax.MouseEvent, Jax.Event);
    this.setInstance();
    
    // Set event.
    this.setEvent(evnt);
    
    //////////////////////////////////////////////////////////////////////////
    //	getKey
    //////////////////////////////////////////////////////////////////////////
    this.getButton = function() {
        var button;
        if (window.event) {
            button = this.getEvent().button;
        }
        else {
            button = this.getEvent().button << 1;
            if (button == 0) button |= 1;
        }
        return button;
    }
}

Jax.MouseEvent.LBUTTON = 1;
Jax.MouseEvent.RBUTTON = 2;
Jax.MouseEvent.MBUTTON = 4;