// NAV ************************************************************************
// COMMUNITY OBJECT *************************************
var community	= {
    current	: null,
    timer	: {
        over    : null,
        reset    : null
    },
    init    : function () {
		var self    = this;

        $$('#community>li:not(.logout)').each(function(li, i) {
            li.observe('mouseover', function(event) {
                clearTimeout(self.timer.reset);
                community.over(i);
            }).observe('mouseout', function(){
                self.timer.reset = setTimeout('community.reset()', 500);
            });
        });

        $('community-sub').observe('mouseover', function() {
            clearTimeout(self.timer.reset);
        }).observe('mouseout', function() {
            self.timer.reset = setTimeout('community.reset()', 500);
        })
        $$('#community-sub ul').each(function(ul) {
            ul.observe('mouseover', function() {
                clearTimeout(self.timer.reset);
            })
        })
    },
    over    : function (i) {
	    if (i != this.current) {
	        $('community').down('li', this.current).down('a').removeClassName('hover');
	        $('community').down('li', i).down('a').addClassName('hover');

	        $('community-sub').down('table', this.current).hide();

	    	this.current    = i;
	    }
	    $('community-sub').down('table', i).show();
    },
    reset     : function () {
        if (this.current != this.level) {
			$('community').down('li', this.current).down('a').removeClassName('hover');
            if(this.level != null) {
                $('community').down('li', this.level).down('a').addClassName('hover');
            }
        }
        $('community-sub').down('table', this.current).hide();

        this.current    = this.level;
    }
}

// MENU OBJECT ********************************************
var menu    = {
    level	: null,
    current	: null,
    timer	: {
        over    : null,
        reset    : null
    },
    set		: function (level) {
		this.level		= level;
		this.current    = this.level;
    },
    init    : function () {
		var self    = this;

		if(this.level != null) {
		    $('menu')
		        .down('li', this.level)
		        .down('a').addClassName("hover");
		}

		$$('#menu>li:not(.end)').each(function(li, i) {
		    li.observe('mouseover', function(event) {
		        clearTimeout(self.timer.reset);
		        menu.over(i);
		    }).observe('mouseout', function(){
		        self.timer.reset = setTimeout('menu.reset()', 500);
		    });
		});

		$('menu-sub').observe('mouseover', function() {
		    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) {
	        $('menu').down('li', this.current).down('a').removeClassName('hover');
	        $('menu').down('li', i).down('a').addClassName('hover');

	        $('menu-sub').down('table', this.current).hide();

	    	this.current    = i;
	    }
	    $('menu-sub').down('table', i).show();

	    if($('horizontalAdvertising') != undefined) {
	    	$('horizontalAdvertising').down('div.hide-on-popup').setStyle({display: 'none'});
	    }
    },
    reset     : function () {
        if (this.current != this.level) {
			$('menu').down('li', this.current).down('a').removeClassName('hover');
            if(this.level != null) {
                $('menu').down('li', this.level).down('a').addClassName('hover');
            }
        }
        $('menu-sub').down('table', this.current).hide();

   	    if($('horizontalAdvertising') != undefined) {
        	$('horizontalAdvertising').down('div.hide-on-popup').setStyle({display: 'block'});
   	    }

        this.current    = this.level;
    },
    redirect    : function(url) {
		location    = url;
	}
}

// ONLOAD EVENT ***********************
Event.observe(window, 'load', function() {
    // Init the nav system
    community.init();
    menu.init();
});
