////////////////////////
// SDMenu constructor //
////////////////////////

function SDMenu(id) {
	if (!document.getElementById || !document.getElementsByTagName) return false;
	
	// config settings
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("div");
	this.remember = true;
	this.speed = 7;
	this.markCurrent = true;
	this.oneSmOnly = true;
}

//////////////////
// SDMenu::init //
//////////////////

SDMenu.prototype.init = function() {
	var mainInstance = this;
	
	// loop through submenus (the empty div tags that surround the section span, subsection spans and article a tags)
	for (var i = 0; i < this.submenus.length; i++){
	
		// put an onclick event on the first span tag in the current submenu (the sections span tag)
		// this.parentNode is a clever way to pass the empty div around the section to the toggleMenu method
		this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
			mainInstance.toggleMenu(this.parentNode);
		};
	}
	
	// loop through all article a tags...
	// if you find one with the current pages url as its href, 
	// change its className to make it highlighted
	if (this.markCurrent) {
		var links = this.menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
			if (links[i].href == document.location.href) {
				links[i].className = "current";
				break;
			}
	}
	
	// if the remember config setting is on...
	if (this.remember) {
	
		// see if there is a cookie storing the current section for this menu
		var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
		var match = regex.exec(document.cookie);
		
		// if there is...
		if (match) {
			// split match[1] into individual characters
			var states = match[1].split("");
			// loop through states (one for each section)
			for (var i = 0; i < states.length; i++)
				// (the submenus are the empty div tags around the section/subsection/article tags)
				// if state is 0, set current submenus className to "collapsed"
				// if state is not 0, set current submenus className to nothing ("")
				this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
		}
	}
	
};

////////////////////////
// SDMenu::toggleMenu //
////////////////////////

// this method just calls the appropriate method to
// either animate a submenu open (if its shut) or
// animate it shut (if its open)

SDMenu.prototype.toggleMenu = function(submenu) {
	
	// if the targeted submenu is already collapsed...
	if (submenu.className == "collapsed")
		// trigger the expand menu animation on it
		this.expandMenu(submenu);
	else
		// otherwise trigger the collapse menu animation on it
		this.collapseMenu(submenu);
};

////////////////////////
// SDMenu::expandMenu //
////////////////////////

SDMenu.prototype.expandMenu = function(submenu) {

	// set fullHeight to the "offsetHeight" of the section span tag (the first span tag) in the targeted submenu
	var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	
	// loop through all the article tags (a) in this submenu and add their "offsetHeight" to fullHeight
	var links = submenu.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) fullHeight += links[i].offsetHeight;
	var moveBy = Math.round(this.speed * links.length);
	
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < fullHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
			mainInstance.memorize();
		}
	}, 30);
	this.collapseOthers(submenu);
};

//////////////////////////
// SDMenu::collapseMenu //
//////////////////////////

SDMenu.prototype.collapseMenu = function(submenu) {

	// set minHeight to the "offsetHeight" of the section span tag (the first span tag) in the targeted submenu
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = ""; //(minHeight-1) + "px";
			submenu.className = "collapsed";
			mainInstance.memorize();
		}
	}, 30);
};

////////////////////////////
// SDMenu::collapseOthers //
////////////////////////////

SDMenu.prototype.collapseOthers = function(submenu) {
	if (this.oneSmOnly) {
		for (var i = 0; i < this.submenus.length; i++)
			if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};

///////////////////////
// SDMenu::expandAll //
///////////////////////

SDMenu.prototype.expandAll = function() {
	var oldOneSmOnly = this.oneSmOnly;
	this.oneSmOnly = false;
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className == "collapsed")
			this.expandMenu(this.submenus[i]);
	this.oneSmOnly = oldOneSmOnly;
};

/////////////////////////
// SDMenu::collapseAll //
/////////////////////////

SDMenu.prototype.collapseAll = function() {
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className != "collapsed")
			this.collapseMenu(this.submenus[i]);
};

//////////////////////
// SDMenu::memorize //
//////////////////////

SDMenu.prototype.memorize = function() {
	if (this.remember) {
		var states = new Array();
		for (var i = 0; i < this.submenus.length; i++)
			states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
		var d = new Date();
		d.setTime(d.getTime() + (6 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
	}
};