/* Behaviour v 2.1 
 *
 * @author Jussi Löf
 * (c) 2007 Ambientia Ltd.
 *
 * usage HTML:
 *
 * <p func="myFunctionName" anyAttribute="optional,attributes,can,be,given"> Foo & Bar </p>
 *
 * usage javascript:
 *
 * Behaviour.list.mousedown_myFunctionName = function (node) {
 *    // the first param is the event source
 *    var clickedNode = node;
 *    //get your attributes this way
 *    var myAttributes = node.getAttribute('myAttributes');
 *    // do something...
 * }
 *
 */


var Behaviour = {
    list : {},

    handle : function (event)
    {
        //IE compability
        if (!event) event = window.event;

        try { 
              //cancel event if there is behaviour
              if (Behaviour.trigger(event.srcElement || event.target, event.type)) {
                  Event.stop(event);
              }
         
         } catch (err) {}
    },

    trigger : function (node, eventType)
    {
        do { 
                try {
                    if (node.getAttribute('func')) {
                        var hashName = eventType + "_" + node.getAttribute('func');
                        if (this[hashName]) this[hashName](node);
                        else this.list[hashName](node);
                        return true;
                        break;
                    } else {
                        var hashName = eventType + "_" + (node.id ? node.id : '') + "_" + (node.className ? node.className : '');
                        this.list[hashName](node);
                        return node.breakChain;
                        break;
                    }
                } catch (err) {} 
        } while ( (node = node.parentNode) )
        return false;
    }
}

Event.observe(window, "load", function () {
    Event.observe(document.body, "click", Behaviour.handle);
    Event.observe(document.body, "mousedown", Behaviour.handle);
    Event.observe(document.body, "mouseover", Behaviour.handle); 
    Event.observe(document.body, "mouseout", Behaviour.handle); 
});
