var SlidingMenu = function(id) {
	this.init(id);
};

SlidingMenu.prototype =  {
	_isActive: function () {
		return this.element.parent().find("li").hasClass(".active");
	},
	
	_isOpen: function () {
		return this.element.hasClass("open");
	},
	
	_close: function () {
		this.items.slideUp("fast");
		this.archives.slideUp("fast");
		this.element.parent().find("li.open").removeClass("open");
		this.element.parent().removeClass("expanded");
	},
	
	_open: function () {
		this._closeOthers();
		this.items.slideDown("fast");
		this.element.addClass("open");
		this.element.parent().addClass("expanded");
	},
	
	_show: function () {
		this.items.css("display","block");
		this.element.addClass("open");
		this.element.parent().addClass("expanded");
	},
	
	_setEvents: function () {
		var scope = this;
		$(scope.element).click(function () {
			this.blur();
			(scope._isOpen()) ? scope._close() : scope._open();
		})
	},
	
	_closeOthers: function () {
		$("#menu li:not(.section-title)").slideUp("fast");
		$("#menu li.open").removeClass("open");
		$("#menu ul.expanded").removeClass("expanded");
	},
	
	init: function (element) {
		this.element = $(element);
		this.items = this.element.nextAll("li").not(".archive");
		this.archives = this.element.parent().find("li.archive");
		this._setEvents();
		
		// If the section contains the 'active' menu item, show it.
		if(this._isActive()) { this._show(); } else { this._close(); }
	}
}

var SlidingArchive = function(id) {
	this.init(id);
};

SlidingArchive.prototype =  {
	_isActive: function () {
		return this.element.parent().find("li.archive").hasClass(".active");
	},
	
	_isOpen: function () {
		return this.element.hasClass("open");
	},
	
	_close: function () {
		this.items.slideUp("fast");
		this.element.removeClass("open");
		this.current.slideDown("fast");
	},
	
	_open: function () {
		this.items.slideDown("fast");
		this.element.addClass("open");
		this.current.slideUp("fast");
	},
	
	_show: function () {
		this.items.css("display","block");
		this.element.addClass("open");
		this.current.css("display","none");
	},
	
	_setEvents: function () {
		var scope = this;
		$(scope.element).click(function () {
			this.blur();
			(scope._isOpen()) ? scope._close() : scope._open();
		})
	},
	
	init: function (element) {
		this.element = $(element);
		this.items = this.element.parent().find("li.archive");
		this.current = this.element.parent().find("li").not(".archive").not(".archive-title").not(".archive-separator").not(".section-title");
		this._setEvents();
		
		// If the section contains the 'active' menu item, show it.
		if(this._isActive()) { this._show(); } else {  }
	}
}

// Ensure that the DOM is ready
$(document).ready(function () {
	var menu_items = $("li.section-title");
	for(var i = 0; i < menu_items.length; i++) {
		new SlidingMenu($(menu_items[i]));
	}
	var archive_items = $("li.archive-title");
	for(var i = 0; i < archive_items.length; i++) {
		new SlidingArchive($(archive_items[i]));
	}
	if($.browser.msie) {
		$("#content").css("paddingTop","59px");
		$("div.first").css("marginTop","0px");
	}
});

// close all
// open the parent that contains a li with 'active' class
// toggle open / close event on each
