/***********************************************
* Switch Menu script- by Martial B of http://getElementById.com/
* Modified by Dynamic Drive for format & NS4/IE4 compatibility
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function Menu()
{
	this.addItem    = addItem;
	this.addSubItem = addSubItem;
	this.showMenu   = showMenu;

	HTMLstr = "";
	HTMLstr += "<!-- Keep all menus within masterdiv-->\n";
	HTMLstr += "<div id='masterdiv'>\n";

	HTMLstr += "  <!-- MAIN MENU STARTS -->\n";
	HTMLstr += "    <!-- MAIN_MENU_ITEM -->\n";
	HTMLstr += "  <!-- MAIN MENU ENDS -->\n";
  /*
	HTMLstr += "<!-- SUB MENU STARTS -->\n";
	HTMLstr += "<!-- SUB_MENU -->\n";
	HTMLstr += "<!-- SUB MENU ENDS -->\n";
  */
	HTMLstr += "</div>\n";
}


function addItem(idItem, text, hint, location, altLocation)
{
	var Lookup = "<!-- ITEM "+idItem+" -->";
	if (HTMLstr.indexOf(Lookup) != -1) // falls Zeichenkette schon im String steht
	{
		alert(idItem + " already exist");
		return;
	}
	var MENUitem = "";
	MENUitem += "\n<!-- ITEM "+idItem+" -->\n"; // baut Menuitem auf
  MENUitem += "\n";
  MENUitem += "<div class=\"menutitle\" onclick=\"SwitchMenu('"+idItem+"')\">"+text+"</div>\n";
  /* //*1
  MENUitem += "  <span class='submenu' id="+idItem+">\n";
  MENUitem += "  <ul>\n";
  MENUitem += "    <!-- NEXT ITEM OF SUB MENU "+idItem+" -->\n";
  MENUitem += "  </ul>\n";
  MENUitem += "  </span>\n";
  */
  //alternative Schreibweise
  MENUitem += "  <ul class='submenu' id="+idItem+">\n";
  MENUitem += "    <!-- NEXT ITEM OF SUB MENU "+idItem+" -->\n";
  MENUitem += "  </ul>\n";


/*
		if (hint != null)
			MENUitem += "title='"+hint+"' ";
*/
		if (location != null)
		{
      var Button = "<a class=\"menulink\" href='"+location+"'><div class=\"menutitle\" onclick=\"SwitchMenu('"+idItem+"')\">"+text+"</div></a>\n"
			MENUitem = MENUitem.replace("<div class=\"menutitle\" onclick=\"SwitchMenu('"+idItem+"')\">"+text+"</div>\n", Button);
      //MENUitem += "href='"+location+"' ";
			//MENUitem += "onmouseover=\"hideAll()\" ";
		}
/*
		else
		{
			if (altLocation != null)
				MENUitem += "href='"+altLocation+"' ";
			else
				MENUitem += "href='.' ";
			MENUitem += "onmouseover=\"displaySubMenu('"+idItem+"')\" ";
			MENUitem += "onclick=\"return false;\" "
		}
	}
*/
	MENUitem += "<!-- END OF ITEM "+idItem+" -->\n\n";
	MENUitem += "<!-- MAIN_MENU_ITEM -->\n";
	HTMLstr = HTMLstr.replace("<!-- MAIN_MENU_ITEM -->\n", MENUitem);
}


function addSubItem(idParent, text, hint, location, linktarget)
{
	var MENUitem = "";
	Lookup = "<!-- ITEM "+idParent+" -->";
	if (HTMLstr.indexOf(Lookup) == -1)	//prüft, ob Obermenü vorhanden
	{
		alert(idParent + " not found");
		return;
	}
	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->";
  if (HTMLstr.indexOf(Lookup) != -1)
	{
    MENUitem += "\n";
    if (hint==null) (hint="");
		var target=null;
    if (linktarget=null){
    	target="";
    }
		else if(linktarget=""){
			target=" target=_self";
    }
    else{
			target=" target="+linktarget;
    }
    //MENUitem += "- <a class='menulink' href="+location+" alt='"+hint+"'"+target+">"+text+"</a><br>\n";  //*1
    MENUitem += "<li><a class='menulink' href="+location+" alt='"+hint+"'"+target+">"+text+"</a></li>\n";
    MENUitem += "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";

	  //HTMLstr = HTMLstr.replace("<!-- SUB_MENU -->\n", MENUitem);
	}

	Lookup = "<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n";
	//MENUitem = "<a title='"+hint+"' href='"+location+"' target='"+linktarget+"'>"+text+"</a><br></td></tr>\n";
	//MENUitem += Lookup;
	HTMLstr = HTMLstr.replace("<!-- NEXT ITEM OF SUB MENU "+ idParent +" -->\n", MENUitem);

}


if (document.getElementById){
document.write('<style type="text/css">\n')
document.write('.submenu{display: none;}\n')
document.write('</style>\n')
}

function SwitchMenu(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	//var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //*1
  var ar = document.getElementById("masterdiv").getElementsByTagName("ul");
		if(el.style.display != "block"){
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="submenu")
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}

function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
	  if (offset != -1) {
	    offset += search.length
	    end = document.cookie.indexOf(";", offset);
	    if (end == -1){
	      end = document.cookie.length;
	    }
	    returnvalue=unescape(document.cookie.substring(offset, end))
	  }
	}
	return returnvalue;
}

function onloadfunction(){
	if (persistmenu=="yes"){
	  var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
	  var cookievalue=get_cookie(cookiename)
	  if (cookievalue!=""){
		  document.getElementById(cookievalue).style.display="block"
    }
	}
}

function savemenustate(){
	var inc=1, blockid=""
	while (document.getElementById("sub"+inc)){
	  if (document.getElementById("sub"+inc).style.display=="block"){
		  blockid="sub"+inc
		  break
		}
	inc++
	}
	var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname
	var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid
	document.cookie=cookiename+"="+cookievalue
}

if (window.addEventListener){
	window.addEventListener("load", onloadfunction, false)
}
else if (window.attachEvent){
	window.attachEvent("onload", onloadfunction)
}
else if (document.getElementById){
	window.onload=onloadfunction
}

if (persistmenu=="yes" && document.getElementById){
	window.onunload=savemenustate
}

function showMenu(){
	document.writeln(HTMLstr);
}
