var HorizontalMenu = new Class({
    menus: [],
    submenus: [],
    initialize: function(element, submenu_prefix) {
		this.element = element;
        this.submenu_prefix = submenu_prefix;
        this.menus = this.element.getElementsByTagName("li");
        for (i=0;i<this.menus.length;i++) {
            if (this.menus[i]) {
                this.submenus[i] = this.getSubmenu(this.menus[i]);
            }
        }
        this.url = window.location.toString().split(/[/]+/);
        this.addMenuMouseEvents();
        this.activemenu = this.getActiveMenu();
        this.showActiveMenu();
        this.showActiveSubmenu();
    },
    addMenuMouseEvents: function() {
        for (i=0;i<this.menus.length;i++) {
            if (this.menus[i]) {
                this.menus[i].onmouseover = function(id) {
                    this.showSubmenu(this.menus[id]);
                }.bind(this, i);
            }
        }
    },
    hideAllSubmenus: function() {
        for (i=0;i<this.submenus.length;i++) {
            if (this.submenus[i]) this.submenus[i].setStyle('display', 'none')
        }
        for (i=0;i<this.menus.length;i++) {
            this.menus[i].className = '';
        }
    },
    hideSubmenu: function(mainmenu) {
        var elm = this.getSubmenu(mainmenu);
        if (elm) elm.setStyle('display', 'none');
    },
    showSubmenu: function(mainmenu) {
        this.hideAllSubmenus();
        if (mainmenu) {
            mainmenu.className = 'active';
            var elm = this.getSubmenu(mainmenu);
            if (elm) elm.setStyle('display', 'block');
        }
    },
    showActiveMenu: function() {
        if (this.getActiveMenu()) this.showSubmenu($(this.getActiveMenu()));
    },
    showActiveSubmenu: function() {
        var active_subitem = this.getActiveSubmenu();
        if (active_subitem && $(this.getActiveMenu())) {
            var submenu = this.getSubmenu($(this.getActiveMenu()));
            if (submenu) {
                var anchors = submenu.getElementsByTagName("a");
                for (var i=0;i<anchors.length;i++) {
                    if (anchors[i].getAttribute("href").match("/"+active_subitem)) {
                        anchors[i].className = 'active';
                    }
                }
            }
        }
    },
    getActiveMenu: function() {
        return this.url[2];
    },
    getActiveSubmenu: function() {
        return this.url[3];
    },
    getSubmenu: function(mainmenu) {
        var name = mainmenu.getAttribute("id");
        var append = (name) ? name.toLowerCase() : "";
        return $(this.submenu_prefix+append);
    }
});
