var Navbar = Class.create();
Navbar.prototype = {
	navItems: [],
	initialize: function(eles) {
		eles.each(function(id) {
			this.navItems.push(new NavItem(id));
		}.bind(this));
	}
}

var NavItem = Class.create();
NavItem.prototype = {
	initialize: function(id) {
		this.id = id;
		this.overItem = false;
		this.overDropDown = false;
		this.over = false;
		$('main').insert('<div id="n_' + this.id + '_secondary" class="secondaryNav"></div>');
		$('n_' + this.id + '_secondary').appendChild($('n_' + this.id + '_item').down('ul').remove());
		this.item = $('n_' + this.id + '_item')
		this.dropdown = $('n_' + this.id + '_secondary');
		Event.observe(this.item, 'mouseover', this.showItem.bindAsEventListener(this));
	},
	showItem: function() {
		if (!this.overItem) {
			this.overItem = true;
			this.showHide();
		}
	},
	showDropDown: function() {
		if (!this.overDropDown) {
			this.overDropDown = true;
			this.showHide();
		}
	},
	hideItem: function() {
		if (this.overItem) {
			this.overItem = false;
			this.showHide();
		}
	},
	hideDropDown: function() {
		if (this.overDropDown) {
			this.overDropDown = false;
			this.showHide();
		}
	},
	showHide: function() {
		if ((this.overItem || this.overDropDown) && !this.over) {
			this.over = true;
			this.dropdown.style.display='block';
			this.item.down('a').addClassName('over');
			Event.observe(this.item, 'mouseout', this.hideItem.bindAsEventListener(this));
			Event.observe(this.dropdown, 'mouseover', this.showDropDown.bindAsEventListener(this));
			Event.observe(this.dropdown, 'mouseout', this.hideDropDown.bindAsEventListener(this));
		} else if (!this.overItem && !this.overDropDown && this.over) {
			this.over = false;
			this.dropdown.style.display='none';
			this.item.down('a').removeClassName('over');
			Event.stopObserving(this.item, 'mouseout', this.hideItem.bindAsEventListener(this));
			Event.stopObserving(this.dropdown, 'mouseover', this.showDropDown.bindAsEventListener(this));
			Event.stopObserving(this.dropdown, 'mouseout', this.hideDropDown.bindAsEventListener(this));
		}
	}
}