/* mhaList version 1.0 Beta by Martin Hintzmann Andersen http://www.hintzmann.dk */
if (!dk) {
  var dk = {} ;
  dk.hintzmann = {} ;
  dk.hintzmann.mhaList = {};
} else if (!dk.hintzmann) {
  dk.hintzmann = {};
  dk.hintzmann.mhaList = {};
} else {
  dk.hintzmann.mhaList = {};
}
var ml = dk.hintzmann.mhaList;

ml.cfg = {
  folderIcon        : 'iconPlus.gif',
  openFolderIcon    : 'iconMinus.gif',
  itemIcon          : 'iconLeaf.gif',
  useIcons          : false
}
ml.isSupported = (document.createElement &&
                  document.getElementById && 
                  document.getElementsByTagName);

ml.buildList = function(sID) {
	if (!ml.isSupported) return;
	this.oList = document.getElementById(sID);
	if (!this.oList) return;
 
	this.id			 = sID;
	this.menu       = ml.hasClass(this.oList, "ListMenu")
//	this.horizontal = this.menu?ml.hasClass(this.oList, "horizontal"):false; 
//	this.vertical   = this.menu?ml.hasClass(this.oList, "vertical"):false;

	this.tree       = ml.hasClass(this.oList, "ListTree")
	this.noIcons    = this.tree?ml.hasClass(this.oList, "noIcons"):false;
	this.noCache    = this.tree?ml.hasClass(this.oList, "noCache"):false;
/*	this.oneSibling = this.tree?ml.hasClass(this.oList, "oneSibling"):false;*/ /* TODO */

	var aLI = this.oList.getElementsByTagName("li");
	for (var i=0;i<aLI.length;i++) {
		var oLI = aLI[i];
		oLI.isTop = oLI.parentNode==this.oList;

		var aUL = oLI.getElementsByTagName("ul");
		if (aUL.length>0) { // Item has children
			var oUL = aUL[0];
			oLI.sub=oUL;
			if (this.menu) { // Menu item with children
				this.CreateMenuFolder(oLI,oUL);	
			} else if (this.tree) { // Tree item with children
				oUL.setAttribute('id',sID+'-'+i);
				this.CreateTreeFolder(oLI,oUL);
			}
		} else { // Item has no children
			if (this.menu) { // Menu item without children
				ml.addClass(oLI, "noSubs")
			} else if (this.tree) { // Tree item without children
				this.CreateTreeItem(oLI);
			}

		}
	}
} 
ml.buildList.prototype.CreateTreeFolder = function( oLI, oUL) {
	if (!this.noCache) { oUL.open = ((ml.GetCookie(oUL.id)) == '1')?true:false; }
	oUL.open = (ml.hasClass(oUL, "open"))?true:oUL.open;
	oUL.open = (ml.hasClass(oUL, "closed"))?false:oUL.open;
	if (oLI.firstChild.tagName.toLowerCase()=="a") {
		if (this.noIcons) {
			oLI.firstChild.onclick = function() { ml.ToggleListNoIcons(oUL.id); return false; }
//			oLI.firstChild.onclick = function() { ml.ToggleList(oUL.id, this.noIcons); return false; }
			oUL.style.display = oUL.open?'block':'none'; 
		} else {
			var oIMG = document.createElement("img");
			oIMG.src = oUL.open ? ml.cfg.openFolderIcon : ml.cfg.folderIcon;
			var oA = document.createElement('a');
			oA.setAttribute('href','javascript:ml.ToggleList(\''+oUL.id+'\', '+this.noIcons+');');
			oA.appendChild(oIMG);
			oLI.insertBefore(oA,oLI.firstChild);
			oUL.style.display = oUL.open?'block':'none'; 
		}
	} else { 
		ml.CreateTreeItem(oLI)
	}
}
ml.buildList.prototype.CreateTreeItem = function(oLI) {
	if (!this.noIcons) {
		var oIMG = document.createElement("img");
		oIMG.src=ml.cfg.itemIcon;
		oLI.insertBefore(oIMG,oLI.firstChild);	
	}
}
ml.ToggleListNoIcons = function(sId) {
	ml.ToggleList(sId, true)
}
ml.ToggleListIcons = function(sId) {
	ml.ToggleList(sId, false)
}
ml.ToggleList = function (sId, noIcons) {
	var oUL = document.getElementById(sId); 
	if(oUL.style.display == "none") {
/*		if (false) {
			var tNode = oUL.parentNode.parentNode;
			var aLI = tNode.getElementsByTagName("ul")
			for (var i = 0; i < tNode.childNodes.length; i++) {
				node = tNode.childNodes[i];
				if (node.tagName == 'LI') { / * TODO * /
					alert(node.innerHTML)
				}
  //              if ((node != root) && (node.childNodes.length > 1) && isExpanded(node))
   //               toggleChildren(node);
              
            }
		}*/
		oUL.style.display = "block";  
		if (!noIcons) {oUL.parentNode.firstChild.firstChild.src = ml.cfg.openFolderIcon;}
		oUL.open = true;
		ml.SetCookie(sId, "1");
	}
	else if(oUL.style.display == "block") {
		oUL.style.display = "none";
		if (!noIcons) {oUL.parentNode.firstChild.firstChild.src = ml.cfg.folderIcon;}
		oUL.open = false;
		ml.SetCookie(sId, "0");
	}
}
		 
ml.buildList.prototype.CreateMenuFolder = function(oLI,oUL) {
	oLI.onmouseover = ml.showMenu;
	oLI.onmouseout = ml.hideMenu;
	ml.addClass(oLI, "hasSubs")
}
ml.showMenu = function () {
	var oLI=this;
	oLI.className += " active";
	var iOffsetWidth = oLI.offsetWidth;
	var iOffsetHeight = oLI.offsetHeight;
	if (oLI.isTop){
		var iOffsetLeft = getOffsetLeft(oLI,true);
		var iOffsetTop = getOffsetTop(oLI,true);
		if (ml.hasClass(oLI.parentNode,'horizontal')) {
			oLI.sub.style.left = iOffsetLeft + "px";
			oLI.sub.style.top  = iOffsetTop + iOffsetHeight + "px";
		} else {
			oLI.sub.style.left = iOffsetLeft + iOffsetWidth + "px";
			oLI.sub.style.top  = iOffsetTop + "px";
		}
	} else {
		oLI.sub.style.left=oLI.offsetWidth + "px";
		oLI.sub.style.top=oLI.offsetTop + "px";
	}
	oLI.sub.style.visibility = "visible";
}
ml.hideMenu = function () {
	var oLI=this;
	oLI.sub.style.visibility = "hidden";
	ml.removeClass(oLI, "active");
}




// Helper functions
ml.GetCookie = function(name) { 
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) {
			var endstr = document.cookie.indexOf (";", j);
			if (endstr == -1) endstr = document.cookie.length;
			return unescape(document.cookie.substring(j, endstr));
 		}
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}
ml.SetCookie = function(key, value) {
	document.cookie = key + "=" + escape(value);
}

ml.hasClass = function (el, className) {
	var cs, j
	cs = el.className.split(" ");
	for (j = 0; j < cs.length; j++) {
		if (cs[j] == className) {
			return true;
		}
	}
	return false;
}
ml.removeClass = function (el, className) {
  var cs, j, remainClass
  remainClass = new Array();
  cs = el.className.split(" ");
  for (j = 0; j < cs.length; j++) {
    if (cs[j] != className) {
      remainClass.push(cs[j]);
	 }
  }
  el.className = remainClass.join(" ");
}
ml.addClass = function (el, className) {
	if (el.className==null) 
		el.className='';
	el.className+=(el.className.length>0?' ':'')+className;
}
ml.addEvent = function (oEl, sEvent, sFunction, useCapture) {	
	if (oEl) {
		if (oEl.addEventListener) { // MOZ
			oEl.addEventListener(sEvent,sFunction,useCapture);
			return true;
		} else if (oEl.attachEvent) { // IE5
			var r = oEl.attachEvent("on"+sEvent,sFunction);
			return r;
		} else if (document.all) { // IE4
			eval('oEl.on'+sEvent+'='+sFunction+';');
		} else {
		  alert("Handler could not be attached");
		}
	}
}


// DOM extentions  v1.0
// http://www.dithered.com/javascript/dom_extensions/index.html
// code by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)


// offsetTop and offsetLeft corrections
// note: IE5 Mac will not include page margins in calculations.
function getOffsetTop(element, deep) {
	return getOffsetProperty(element, 'Top', deep);
}

function getOffsetLeft(element, deep) {
	return getOffsetProperty(element, 'Left', deep);
}
function getOffsetProperty(element, property, deep) {
   var offsetValue = 0;
   offsetProperty = 'offset' + property;
   
   do {
      offsetValue += element[offsetProperty];
      element = element.offsetParent;
   } while (deep == true && element != document.body && element != null);
   return offsetValue;
}

function isUndefined(property) {
  return (typeof property == 'undefined');
}
// Array.push() - Add an element to the end of an array
if (isUndefined(Array.prototype.push) == true) {
  Array.prototype.push = function() {
  	var currentLength = this.length;
  	for (var i = 0; i < arguments.length; i++) {
  		this[currentLength + i] = arguments[i];
  	}
  	return this.length;
  };
}
