var Voetbalnieuws = Voetbalnieuws || {};

Voetbalnieuws.NavigationBar = new Class({
	Implements: [Options],
	options: {
		current_class: 'current_page_item'
	},
	initialize: function(bar, options) {
		this.setOptions(options);
		this.bar = $(bar);
		this.bar.getChildren('li').each(function(li) {
			this.add(li);
		}, this);

		if(this.hovered) this.hovered.setStyle('display', 'block');

	},
	add: function(li)
	{
		var ul = li.getElement('ul');
		var self = this;

		if(ul)
		{
			li.store('navigation_bar:ul', ul);
			if(li.hasClass(this.options.current_class))
			{
				this.hovered = ul;
			}
		}

		li.addEvents({
			'mouseenter': function() {
				var ul = this.retrieve('navigation_bar:ul');
				
				if(self.hovered)
				{
					self.bar.getElement('a.selected').removeClass('selected');
				}
				
				this.getElement('a').addClass('selected');

				if(!ul || (self.hovered && ul == self.hovered))
				{
					if(!ul) self.hovered.setStyle('display', 'none');
					return;
				}

				if(self.hovered)
				{
					self.hovered.setStyle('display', 'none');
				}
				
				self.hovered = ul;
				ul.setStyle('display', 'block');
				ul.fade('hide');
				ul.fade('in');
			}
		});
	}
});