/*
PrototypeEventTrigger.js - an extension of prototype.js
version 0.01.02 (2006-11-01)
(c) 2006 WATANABE Hiroaki <hwat at mac dot com>

See the LICENSE in this distribution for full license.

It is an extention of prototype.js (prototype.js 1.5.0_rc1)
requires prototype.js

references cited:
    http://www.howtocreate.co.uk/tutorials/javascript/domevents

Namespace: Event.Trigger
*/

Event.Trigger = {};
Event.Trigger.typeMapping = {
    // HTMLEvents.
    "abort":"fireHTMLEvents",
    "blur":"fireHTMLEvents",
    "change":"fireHTMLEvents",
    "error":"fireHTMLEvents",
    "focus":"fireHTMLEvents",
    "load":"fireHTMLEvents",
    "reset":"fireHTMLEvents",
    "resize":"fireHTMLEvents",
    "scroll":"fireHTMLEvents",
    "select":"fireHTMLEvents",
    "submit":"fireHTMLEvents",
    "unload":"fireHTMLEvents",
    // UIEevents. since they do not have their own key events module in DOM 2
    "DOMActivate":"fireUIEevents",
    "DOMFocusIn":"fireUIEevents",
    "DOMFocusOut":"fireUIEevents",
    // UIEevents. Also indirectly covers MouseEvents.
    "keydown":"fireUIEevents",
    "keypress":"fireUIEevents",
    "keyup":"fireUIEevents",
    // MouseEvents.
    "click":"fireMouseEvents",
    "mousedown":"fireMouseEvents",
    "mousemove":"fireMouseEvents",
    "mouseout":"fireMouseEvents",
    "mouseover":"fireMouseEvents",
    "mouseup":"fireMouseEvents",
    // MutationEvents.
    "DOMAttrModified":"fireMutationEvents",
    "DOMNodeInserted":"fireMutationEvents",
    "DOMNodeRemoved":"fireMutationEvents",
    "DOMCharacterDataModified":"fireMutationEvents",
    "DOMNodeInsertedIntoDocument":"fireMutationEvents",
    "DOMNodeRemovedFromDocument":"fireMutationEvents",
    "DOMSubtreeModified":"fireMutationEvents"
};
Event.Trigger.getAction = function (type){
    return Event.Trigger[Event.Trigger.typeMapping[type]] || null;
}

/*--------------------------------------
Event.Trigger.fireHTMLEvents
*/
Event.Trigger.fireHTMLEvents = function (id, type){
    var target = $(id);
    if( document.createEvent ){
        var evt = document.createEvent('HTMLEvents');
        evt.initEvent( type, true, true );
        evt.element = function (){ return evt.target; }
        target.dispatchEvent(evt);
    }else if( document.createEventObject ){
        var evt = document.createEventObject();
        evt.element = function (){ return evt.srcElement; }
        target.fireEvent('on'+type, evt);
    }
}
/*--------------------------------------
Event.Trigger.fireUIEevents
*/
Event.Trigger.fireUIEevents = function (id, type){
    var target = $(id);
    // in prototype.js, the observer changes type 'keypress' to 'keydown'
    // if UA is Safari.
    if( type == 'keypress'
        && ( navigator.appVersion.match(/Konqueror|Safari|KHTML/) || target.attachEvent)
    ) type = 'keydown';

    if( document.createEvent ){
        if( window.KeyEvent ) {
            var evt = document.createEvent('KeyEvents');
            evt.initKeyEvent( type, true, true, window, false, false, false, false, 0, 0 );
        }else{
            var evt = document.createEvent('KeyboardEvents');
            evt.initEvent( type, true, true );
        }
        target.dispatchEvent(evt);
    }else if( document.createEventObject ){
        var evt = document.createEventObject();
        evt.element = function (){ return evt.srcElement; }
        target.fireEvent('on'+type, evt);
    }
}
/*--------------------------------------
Event.Trigger.fireMouseEvents
*/
Event.Trigger.fireMouseEvents = function (id, type){
    var target = $(id);
    if( document.createEvent ){
        var evt = document.createEvent('MouseEvents');
        evt.initEvent( type, true, true );
        evt.element = function (){ return evt.target; }
        target.dispatchEvent(evt);
    }else if( document.createEventObject ){
        var evt = document.createEventObject();
        evt.element = function (){ return evt.srcElement; }
        target.fireEvent('on'+type, evt);
    }
}
/*--------------------------------------
Event.Trigger.fireMutationEvents
*/
Event.Trigger.fireMutationEvents = function (id, type){
    var target = $(id);
    if( document.createEvent ){
        var evt = document.createEvent('MutationEvents');
        evt.initEvent( type, true, true );
        // evt.initMutationEvent( type, true, true, null, null, null, 2 );
        evt.element = function (){ return evt.target; }
        target.dispatchEvent(evt);
    }else if( document.createEventObject ){
        var evt = document.createEventObject();
        evt.element = function (){ return evt.srcElement; }
        target.fireEvent('on'+type, evt);
    }
}
/*--------------------------------------
Event.Trigger.fire
*/
Event.Trigger.fire = function (id, type){
    var target = $(id);
    var action = Event.Trigger.getAction(type);
    return (action)(id,type);
}

