// Written by Dean Edward, 2005
// With input from Tino Zijdel, Matthias Miller, Diego Perini
// Http://dean.edwards.name/weblog/2005/10/add-event/
Function addEvent (element, type, handler ){
If (element. addEventListener ){
Element. addEventListener (type, handler, false );
} Else {
// Assign each event handler a unique ID
If (! Handler. $ guid) handler. $ guid = addEvent. guid ++;
// Create a hash table of event types for the element
If (! Element. events) element. events = {};
// Create a hash table of event handlers for each element/event pair
Var handlers = element. events [type];
If (! Handlers ){
Handlers = element. events [type] = {};
// Store the existing event handler (if there is one)
If (element ["on" + type]) {
Handlers [0] = element ["on" + type];
}
}
// Store the event handler in the hash table
Handlers [handler. $ guid] = handler;
// Assign a global event handler to do all the work
Element ["on" + type] = handleEvent;
}
};
// A counter used to create unique IDs
AddEvent. guid = 1;
Function removeEvent (element, type, handler ){
If (element. removeEventListener ){
Element. removeEventListener (type, handler, false );
} Else {
// Delete the event handler from the hash table
If (element. events & element. events [type]) {
Delete element. events [type] [handler. $ guid];
}
}
};
Function handleEvent (event ){
Var returnValue = true;
// Grab the event object (IE uses a global event object)
Event = event | fixEvent (this. ownerDocument | this.doc ument | this). parentWindow | window). event );
// Get a reference to the hash table of event handlers
Var handlers = this. events [event. type];
// Execute each event handler
For (var I in handlers ){
This. $ handleEvent = handlers [I];
If (this. $ handleEvent (event) === false ){
ReturnValue = false;
}
}
Return returnValue;
};
Function fixEvent (event ){
// Add W3C standard event methods
Event. preventDefault = fixEvent. preventDefault;
Event. stopPropagation = fixEvent. stopPropagation;
Return event;
};
FixEvent. preventDefault = function (){
This. returnValue = false;
};
FixEvent. stopPropagation = function (){
This. cancelBubble = true;
};