/* Behaviour v 2.1 
 * (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)
    {


        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);
	
	var menuopen = $$('#navi > ul li');
	menuopen.each ( function (item) 
		{
			item.onmouseover = showMenu;
			item.onmouseout = hideMenu;
		}
	);
	
	if ($('subnav'))
		if ( $$('#subnav li').length == 0 ) $('subnav').hide();
	
	
});

function showMenu() {
	if (hider.tout) {
		clearTimeout(hider.tout);
	}
	
	if (!this.menu) {
		this.menu = $( this.getElementsByTagName('ul')[0] );
		if (!this.menu) return;
		document.body.appendChild(this.menu);
		this.menu.className = "menu";
		var offs = $(this).cumulativeOffset();
		this.menu.setStyle( 
			{
				position:"absolute",
				top:(offs[1]+20)+"px",
				left:offs[0]+"px"
			}
		);
		this.menu.onmouseout = hideMenu;
	}
	this.menu.show();
	if (hider.menu && hider.menu != this.menu)  {hider.menu.hide(); hider.menu = null };
	hider.menu = this.menu;
}

var hider = {}

function hideMenu() {;
	if (!hider.menu) return;
	if (hider.tout) clearTimeout(hider.tout);
	hider.tout = setTimeout(function() { hider.menu.hide(); hider.menu = null; }, 300);
}
 	 
