// NAV ************************************************************************
// Nav object
var menu	= {
    level		: [],
    current		: null,
    timer		: {
        over    : null,
        reset	: null
    },
    set			: function (level_0, level_1) {
                this.level        = [level_0, level_1];
                this.current    = this.level[0];
            },
    init		: function () {var self    = this;
                
                if(this.level[0] != null) {
                    $('menu')
                        .down('li', this.level[0])
                        .down('a').addClassName("selected");
                }
    
                if(this.level[1] != null) {
                    $('menu-sub')
                        .down('ul', this.level[0]).show()
                        .down('li', this.level[1])
                        .down('a').addClassName("selected");
                }
                
                $$('#menu>ul>li').each(function(li, i) {
                    li.observe('mouseover', function(event) {
                        clearTimeout(self.timer.over);
                        clearTimeout(self.timer.reset);
                        self.timer.over    = setTimeout('menu.over(' + i + ')', 200);
                    }).observe('mouseout', function(){
                        self.timer.reset = setTimeout('menu.reset()', 500);
                    });
                });

                $('menu-sub').observe('mouseover', function() {
                    clearTimeout(self.timer.over);
                    clearTimeout(self.timer.reset);
                }).observe('mouseout', function() {
                    self.timer.reset = setTimeout("menu.reset()", 500);
                })
                $$('#menu-sub ul').each(function(ul) {
                    ul.observe('mouseover', function() {
                        clearTimeout(self.timer.reset);
                    })
                })
                
            },
    over    : function (i) {
                if (i == this.current)    return;
                
                var self    = this;
                
                $('menu').down('li', this.current).down('a').removeClassName('selected');
                $('menu').down('li', i).down('a').addClassName('selected');
                
                $('menu-sub').down('ul', self.current).hide();
                $('menu-sub').down('ul', i).show();

                this.current    = i;
    },
    reset     : function () {
                if (this.current == this.level[0]) return;
                
                var self    = this;
                
                $('menu').down('li', this.current).down('a').removeClassName('selected');
                $('menu-sub').down('ul', self.current).hide();
                
                if(self.level[0] != null) {
                    $('menu').down('li', this.level[0]).down('a').addClassName('selected');
                }
                
                if(self.level[0] != null) {
                    $('menu-sub').down('ul', self.level[0]).show();
                }
                
                this.current    = this.level[0];
            },
    redirect    : function(url) {
                    location    = url;
                }
}

// ONLOAD EVENT ***********************
Event.observe(window, 'load', function() {
    // Init the menu system
    menu.init();
});
