var Menu = Class.create({
    initialize: function( menuObj ) {
        this.menuObj = menuObj;
        this.subSubMenuTriggers = this.menuObj.getElementsBySelector('li ul li span');
        this.subSubMenuTriggers.each(function(s) {
            new SubSubMenu(s, s.next(0));
        });
    }
});

var SubSubMenu = Class.create({
    initialize: function( triggerObj, subSubMenuObj ) {
        this.enabled = false;
        this.subSubMenuObj = subSubMenuObj;
        this.triggerObj = triggerObj;
        
        if (typeof(this.subSubMenuObj) == 'undefined') {
            return;
        }
        this.subSubMenuObj.hide();
        this.triggerObj.observe('mouseover', this.hover.bind(this));
        if (Prototype.Browser.IE) {
            Event.observe(this.triggerObj.parentNode.parentNode.parentNode.firstChild, 'mouseover', this.hoverOut.bind(this));
        } else {
            this.triggerObj.parentNode.parentNode.parentNode.firstDescendant().observe('mouseover', this.hoverOut.bind(this));
        }
    },

    hover: function() {
        if (!this.enabled) {
            Effect.BlindDown(this.subSubMenuObj, { duration: 0.5 });
            this.enabled = true;
        }
    },

    hoverOut: function() {
        this.subSubMenuObj.hide();
        this.enabled = false;
    }

});

function init_menu() {
    new Menu($$('#navigation ul')[0]);    
}

document.observe("dom:loaded", init_menu);
