var Nav = Class.create();
Nav.prototype = {
	initialized : false,
	init_selected : null,
	top : null,
	top_items : null,
	hit : null,
	selected : null,
	over : null,
	revert : false,
	sd : null,

	_mouseover : null,
	_mouseout : null,

	initialize : function(top, hit, selected_definition, revert) {
        if (!this.initalized) {
            this._mouseover = this.mouseover.bindAsEventListener(this);
            this._mouseout = this.mouseout.bindAsEventListener(this);
            this.top = $(top);
			this.top_items = this.top.down().childElements();
            this.hit = $(hit);
			this.sd = selected_definition;
            this.create_observers();
			this.revert = revert || this.revert;
            this.initialized = true;
        }
	},

	create_observers : function() {
		for (var i=0, c=this.top_items.length; i<c; i++) {
			var li = this.top_items[i];
			if (li) {
    		    li.subnav = this.get_sub(li);
    		    if (li.subnav) {
        			li.subnav.hide();
        			var li_attr = li.readAttribute(this.sd.attribute);
        			if (!this.selected && li_attr && li_attr.indexOf(this.sd.string) != -1) {
        				this.selected = $(li);
        				this.mouseover(li);
        			}
        			li.observe('mouseover', this._mouseover);
                }
			}
		}
	},
	
	get_sub : function(elem) {
		var clsArray = elem.className.split(' ');
		var cls = clsArray[clsArray.length-1].replace('cms_', '');
		cls = cls.replace('_selected', '') + '_div';
		return $('navigation_' + cls); 
	},
	
	deselect : function(elem) {
		elem.observe('mouseover', this._mouseover);
		elem.removeClassName('hover');
		var a = elem.down('a');
		if (a) {
			a.removeClassName('hover');
		}		
		if (elem.subnav) {
			elem.subnav.hide();
		}
	},

    mouseover : function(event) {
		var elem = $(event.findElement ? event.findElement('li') : event);
		var a = elem.down('a');
		if (this.over) {
			this.deselect(this.over);
		}
		if (elem != this.selected) {
			elem.stopObserving('mouseover', this._mouseover);
			Event.observe(document, 'mousemove', this._mouseout);
		}
		elem.addClassName('hover');
		if (a) {
			a.addClassName('hover');
		}
		if (elem.subnav) {
			elem.subnav.show();
		}
		this.over = elem;
    },

    mouseout : function(event) {
		if (!event.element().descendantOf(this.hit) && event.element() != this.hit) {
			if (this.over) {
				this.deselect(this.over);
			}
			if (this.selected && this.revert) {
				this.mouseover(this.selected);
			}
		}
    }
}
Event.observe(window, 'load', function() {
	var li = $$('#navigation_main li.cms_theguild');
    if (li[0].hasClassName('selected') || window.location.pathname == '/') {
        li[0].addClassName('selected cms_theguild_selected');
		$('navigation_theguild_div').show();
    }
	var li = $$('#navigation_main li.cms_join');
	if (li[0].hasClassName('selected')) {
		$('navigation_join_div').show();
	}
	var li = $$('#navigation_main li.cms_news');
	if (li[0].hasClassName('selected')) {
		$('navigation_news_div').show();
	}
	var li = $$('#navigation_main li.cms_handbook');
	if (li[0].hasClassName('selected')) {
		$('navigation_handbook_div').show();
	}
	var li = $$('#navigation_main li.cms_resources');
	if (li[0].hasClassName('selected')) {
		$('navigation_resources_div').show();
	}
    var nav = new Nav('navigation_main', 'navigation_container', {attribute: 'class', string: 'selected'}, true);
});