/* mhaList version 1.2 by Martin Hintzmann Andersen http://www.hintzmann.dk */

var ua = window.navigator.userAgent
var BrowserType = 'SAFARI';

if (ua.indexOf('MSIE') > 0 )      
         BrowserType = 'MSIE';
else if (ua.indexOf('Safari') > 0 )                  
         BrowserType = 'SAFARI';
else if (ua.indexOf('Firefox') > 0 )
         BrowserType = 'FIREFOX';
else
         BrowserType = 'SAFARI';


function getInlineValue(BT)
{
 	switch (BT)
  {
	  case 'MSIE':
		  return 'inline';
			break;
  	case 'SAFARI':
			return 'inline-block';
		case 'FIREFOX':
			return '-moz-inline-box';
		default:
		  return 'inline-block';		 
 	}
}
			 
function stopEventBubble(evt)
{
  //event.cancelBubble = true;
	
  if (BrowserType != 'FIREFOX')
  {
   window.event.cancelBubble = true;
  }
  else
  {
   evt.stopPropagation();
  }
}

var ml = {};

ml.cfg = {
  folderIcon        : 'img/iconPlus.gif',
  openFolderIcon    : 'img/iconMinus.gif',
  itemIcon          : 'img/iconLeaf.gif',
  useIcons          : false,
  cookieDomainName  : '/'
}
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
			this.CreateMenuItem(oLI);
			if (this.menu) { // Menu item without children

			} 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;
			if (oLI.firstChild.href == "") {
				oLI.firstChild.setAttribute('href','javascript:ml.ToggleList(\''+oUL.id+'\', '+this.noIcons+');');
			}
			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;
	if ((oLI.className == 'Top') && (BrowserType != 'MSIE'))
	{
	 	oLI.style.display = getInlineValue(BrowserType);
		 oLI.style.width = oLI.offsetWidth - 2 + 'px';
//		 alert(oLI.width);
	}

	//ml.addClass(oLI, "hasSubs")
	//ml.addClass(oLI, "Top")
//ml.addClass(oLI, "Dropdown")
	//oLI.className += "Dropdown";
	//alert(oLI.id + ' - ' + oLI.className);
}

ml.buildList.prototype.CreateMenuItem = function(oLI) {
	oLI.onmouseover = ml.showItem;
	oLI.onmouseout = ml.hideItem;
	
	if ((oLI.className == 'Top') && (BrowserType != 'MSIE'))
	{
	 	 oLI.style.display = getInlineValue(BrowserType);
		 oLI.style.width = oLI.offsetWidth - 2 + 'px';
	}

	//oLI.onclick = oLI.onclick;//ml.clickItem;
	
	//ml.addClass(oLI, "noSubs")
	//ml.addClass(oLI, "Top")
	//oLI.className += " Top";
	//alert(oLI.className);
    //ml.addClass(oLI, "Dropdown")
	//oLI.className += "Dropdown";
	//alert('Item ' + oLI.id + ' - ' + oLI.className);
}

ml.showItem = function () {
    var oLI=this;
    oLI.className += "_active";
	}

ml.hideItem = function () {
    var oLI=this;
	//oLI.sub.style.visibility = "hidden";
	//ml.removeClass(oLI, "_active");
	tmpClassName = oLI.className;
	tmpClassName = tmpClassName.replace("_active","");
	oLI.className = tmpClassName;
	
}

function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
	        curleft += obj.offsetLeft
	        curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

ml.showMenu = function () {
	//hl_over(this);
	var oLI=this;
	//alert('Item ' + oLI.id + ' - ' + oLI.className);
	//ml.addClass(oLI, "_active")
	oLI.className += "_active";
	
	//oLI.title = oLI.onclick;
	xyPos = findPos(oLI);
	
	//var iOffsetWidth = xyPos[0];
	var iOffsetHeight = xyPos[1];
	var iOffsetLeft = xyPos[0];
	var iOffsetWidth = oLI.offsetWidth;
	//var iOffsetHeight = oLI.offsetHeight;
	
	if (BrowserType != 'FIREFOX')
	{
	 		iOffsetLeft = 0;
	   iOffsetHeight = 0;
	}
	else
	{
	 iOffsetLeft += 1;
	}
	
//	if (1==1){
		//var iOffsetLeft = getOffsetLeft(oLI,true);
		if (ml.hasClass(oLI.parentNode,'horisontal')) {
			oLI.sub.style.left = iOffsetLeft -1 + "px";
			oLI.sub.style.top  = iOffsetHeight + 24 + "px";
		} 
		else 
		{
		 	//oLI.sub.style.left= iOffsetLeft + oLI.offsetWidth + "px";
			oLI.sub.style.left= oLI.offsetWidth - 2 + "px";
			//oLI.sub.style.top  = iOffsetHeight -2 + "px";
			oLI.sub.style.top  =  -1 + "px";
		}
		
//	} else {
//		oLI.sub.style.left=0 + "px";
//	}
	oLI.sub.style.visibility = "visible";
}
ml.hideMenu = function () {
	var oLI=this;
	oLI.sub.style.visibility = "hidden";
	tmpClassName = oLI.className;
	tmpClassName = tmpClassName.replace("_active","");
	oLI.className = tmpClassName;
}

// 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) + "; path="+ml.cfg.cookieDomainName;
}
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)


// offsetMiddle and offsetLeft corrections
// note: IE5 Mac will not include page margins in calculations.
   // function getOffsetMiddle(element, deep) {
	// return getOffsetProperty(element, 'Middle', 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;
  };
}

