/**************** Simple menu object ****************/
/*                                                  */
/* Description:  See below                          */
/*                                                  */

/* Simple menu constructor */
function SimpleMenu(sTarget) {
	// Collection
	this.items = null;
	// Properties
	this.source = getObjectByID(sTarget);
	this.indentWidth = 12;
	this.spacing = 3;
	// Methods
	this.show = m_ShowMenu;
	this.add = m_AddNode;
	// Error
	this.hasErrors = false;
	this.lastError = "";
}
/* Node object */
function m_MenuNode() {
	// Collection
	this.items = null;
	// Properties
	this.id = "";
	this.expanded = 0;
	this.visible = 0;
	this.text = "";
	this.cssclass = "";
	this.image = "";
	this.onclick = "";
	this.href = "";
	this.target = "";
	// Methods
	this.add = m_AddNode;
	this.render = m_RenderNode;
}
/* Add node to list */
function m_AddNode(oRef) {
	this.items[this.items.length] = oRef;
}
/* Set error */
function m_SetError(oRef, sError) {
	oRef.hasError = true;
	oRef.lastError = sError;
}

/****************************************************/
/* Fill functions                                   */

/* Show menu */
function m_ShowMenu() {
	if (this.source != null) {
		m_FillMenuObject(this, this.source.childNodes, true);
		m_ClearSourceContent(this.source, "menu");
		m_RenderMenu(this.source, this, this.indentWidth, this.spacing);
	} else m_SetError(this, "No valid document container specified.")
}
/* Fill object with nodes */
function m_FillMenuObject(oRef, oNList, bFirstLevel) {
	var oNode, oMenuItem;
	bFirstLevel = (bFirstLevel != null) ? bFirstLevel : false;
	if (oRef.items == null) oRef.items = new Array();
	for (var i = 0; i < oNList.length; i++) {
		oMenuItem = oNList.item(i);
		if (oMenuItem.tagName == "MENU") {
			oNode = new m_MenuNode();
			oNode.id = m_GenerateID();
			oNode.text = (oMenuItem.getAttribute("text") != null) ? oMenuItem.getAttribute("text") : "";
			oNode.cssclass = (oMenuItem.getAttribute("cssclass") != null) ? oMenuItem.getAttribute("cssclass") : "";
			oNode.image = (oMenuItem.getAttribute("image") != null) ? oMenuItem.getAttribute("image") : "";
			oNode.onclick = (oMenuItem.getAttribute("jsclick") != null) ? oMenuItem.getAttribute("jsclick") : "";
			oNode.href = (oMenuItem.getAttribute("href") != null) ? oMenuItem.getAttribute("href") : "";
			oNode.target = (oMenuItem.getAttribute("target") != null) ? oMenuItem.getAttribute("target") : "";
			oNode.visible = (oMenuItem.getAttribute("expanded") != null || bFirstLevel) ? "1" : "0";
			m_FillMenuObject(oNode, oMenuItem.childNodes)
			oRef.add(oNode);
		}
	}
}

/****************************************************/
/* Render functions                                 */

/* Render menu */
function m_RenderMenu(oTarget, oRef, lIndentWidth, lSpacing) {
	if (oRef.items != null) {
		var oCont;
		for (var i = 0; i < oRef.items.length; i++) {
			oCont = oRef.items[i].render(lIndentWidth, lSpacing);
			m_RenderMenu(oCont, oRef.items[i], lIndentWidth, lSpacing);
			oTarget.appendChild(oCont);
		}
	}
}
/* Render node */
function m_RenderNode(lIndentWidth, lSpacing) {
	var oContainer = document.createElement("DIV");
	oContainer.id = this.id;
	oContainer.setAttribute("node", "1");
	oContainer.setAttribute("expanded", this.expanded);
	oContainer.style.paddingTop = lSpacing + "px";
	oContainer.style.marginLeft = lIndentWidth + "px";
	oContainer.style.display = (this.visible != 0) ? "block" : "none";
	if (this.items != null) oContainer.setAttribute("hasnodes", "1");
	if (this.image != "") oContainer.appendChild(m_GetImage(this));
	
	oContainer.appendChild(m_GetTextContainer(this));
	return oContainer;
}
/* Get textcontainer for window */
function m_GetTextContainer(oRef) {
	var oText = document.createElement("SPAN");
	if (oRef.cssclass != "") oText.className = oRef.cssclass;
	oText.appendChild(m_GetTextLink(oRef));
	oText.onclick = new Function("m_ShowSubNodes(this.parentNode)");
	return oText;
}
/* Get text element with optional onclick */
function m_GetTextLink(oRef) {
	var oText = document.createElement("SPAN");
	if (oRef.href != "") {
		if (oRef.target != "") {
			oText.onclick = new Function("window.open('" + oRef.href + "', '" + oRef.target + "')");
		} else oText.onclick = new Function("window.location='" + oRef.href + "'");
	} else if (oRef.onclick != "") oText.onclick = new Function(oRef.onclick);
	oText.style.cursor = "hand";
	oText.appendChild(document.createTextNode(oRef.text));
	return oText;
}
/* Get image container */
function m_GetImage(oRef) {
	var oImage = document.createElement("IMG");
	oImage.setAttribute("src", oRef.image);
	oImage.setAttribute("border", "0");
	return oImage;
}

/****************************************************/
/* Misc functions                                   */

/* Clear contents of source */
function m_ClearSourceContent(oRef, sTag) {
	oRef.innerHTML = "";
}

/* Onclick, show sub nodes */
function m_ShowSubNodes(oRef) {
	for (var i = 0; i < oRef.childNodes.length; i++) {
		if (oRef.childNodes.item(i).tagName != null) {
			if (oRef.childNodes.item(i).getAttribute("node") != null) {
				oRef.childNodes.item(i).style.display = (oRef.getAttribute("expanded") != 0) ? "none" : "block";
			}
		}
	}
	oRef.setAttribute("expanded", (oRef.getAttribute("expanded") == 1) ? 0 : 1);
}

/* Get object by ID */
function getObjectByID(sRef) {
	return (document.getElementById) ? document.getElementById(sRef) : document.all(sRef);
}
/* Get random id on initilize */
function m_GenerateID() {
	var sChars = "abcdefghijklmnopqrstuwvxyzABCDEFGHIJKLMNOPQRSTUWVXYZ";
	var sOut = "";
	for (var i = 0; i < 10; i++) {
		sOut += sChars.charAt(Math.round((sChars.length - 1) * Math.random()));
	}
	return sOut;
}


/************************ Description ************************/
// 
// IMPLEMENTATION
// Add a reference to this js-file in your webpage. Build a
// menu-structure inside a simple DIV with a unique id, for
// an example of the menu-structure see the example below.
// Add your own javascript block and declare a global variable.
// Then make an onload function where you assign the global
// variable with new SimpleMenu([DivName]), where the [DivName]
// is replaced with the name of the DIV. Then call the
// function "show()" on your global variable. All done.
// 
// MENU STRUCTURE:
// <menu>
//    <menu></menu>
// </menu>
// <menu></menu>
// 
// Nested tags are treated as a sub-menu to its owner. There's
// no practical limit on how many levels that can be specified.
// 
// AVAILABLE ATTRIBUTES:
// text     =  The menu text
// cssclass =  Ref to a CSS-class
// image    =  The path to an image, shown left of the text
// jsclick  =  Onclick javascript-event
// href     =  Link to trigger onclick, overrides onlick
// target   =  If target of link should be other than current page
// 
// Only text attribute is required.
//
//
// Author:       Emil, Brun Jenkins
// Distrubution: Freely
/*************************************************************/

