function Menu(dom)
{
	this.dom = dom;
	this.init();
}

Menu.prototype = {
	init : function() {
		var that = this;
		this.dom.find('a').click(function() {
			Cookies.add('active_menu', this.id.replace('menu_', ''));
			that.activateParents(jq(this));
		});
		this.dom.find('a + ul').prev().click(function(e) {
			// Prevent default so menu can fade in without going to a link actually
			e.preventDefault();
			
			var id = this.id.replace('menu_', '');
			if(that.toggle(jq(this)) && window.covers && covers[id]) {
				jq('#content')
					.html('')
					.css('background-image', 'url(' + baseUrl + covers[id] + ')')
					.css('background-repeat', 'no-repeat')
					.css('background-position', '30px 10px');
			}
		});
		this.dom.find('a.active').each(function() {
			that.activateParents(jq(this));
		});
	},

	toggle : function(anchor) {
		var status = null;
		if(anchor.next().css('display') == 'none') {
			this.deactivateAll();
			this.activate(anchor);
			anchor.next().slideDown();	
			Cookies.add(anchor.attr('id'), 1);
			status = true;
		} else {
			this.deactivateAll();
			anchor.next().slideUp();
			Cookies.remove(anchor.attr('id'), 1);
			status = false;
		}
		this.activateParents(anchor);
		return status;
	},
	
	activate : function(anchor) {
		anchor.addClass('active');
	},
	
	deactivate : function(anchor) {
		anchor.removeClass('active');
	},
	
	activateParents : function(child) {
		var that = this;
		child.parents('ul').prev('a').each(function() {
			that.activate(jq(this));
		});
	},
	
	deactivateAll : function() {
		this._forEach('deactivate');
	},
	
	activateAll : function() {
		this._forEach('activate');
	},
	
	_forEach : function(callback) {
		var that = this;
		this.dom.find('a').each(function() {
			that[callback](jq(this));
		});
	}
}

