// Simple Menu System (2 level)
// copyright Spleen Advertising GmbH <box@spleen.de>
// version 0.3

var debug = false;
var showSub = true; // if set to true sub menus are shown on mouse over

var mainImgPath = "images/menu/";
var subImgPath = "images/menu/";

var out = "i";
var over = "a";

function MenuBar() {
   // properties:
   this.menus = new Array();
   this.selectedMenu;
   this.selectedSubMenu;

   // methods:
   this.addMenu = MenuBar_addMenu;
   this.enterMenu = MenuBar_enterMenu;
   this.leaveMenu = MenuBar_leaveMenu;
   this.selectMenu = MenuBar_selectMenu;
	 this.setActiveItem = MenuBar_setActiveItem;
}

function MenuBar_setActiveItem(itemName ) {

  var aMenu = xbGetElement(itemName);
  if (aMenu) {
  	swapSubMenu(itemName.substring(5), over)
    aMenu.onclick = null;
    aMenu.onmouseover = null;
  	aMenu.onmouseout = function () { parent.leaveMenu(menuId, itemName.substring(5));};
  } else {
  	if (debug) alert("no menu <a> with id " + itemName);
	}
}

function MenuBar_addMenu(menuId, itemsArray) {
   this.menus[menuId] = new Menu(this, menuId, itemsArray);
}

// event handler for mouse over
function MenuBar_enterMenu(menuId, subId) {
  if (subId || showSub) {
      menu = this.menus[menuId];
      if (this.selectedMenu) this.selectedMenu.hide();
      menu.show(subId);
  } else {
    swapMenu(menuId, over);
  }
}

// event handler for mouse out
function MenuBar_leaveMenu(menuId, subId) {
  menu = this.menus[menuId];
  if (subId || showSub) {
      menu.hide(subId);
      if (this.selectedMenu) {
      	this.selectedMenu.show(this.selectedSubMenu);
      }
  } else if (this.selectedMenu != menu) {
      swapMenu(menuId, out);
  }
}

// event handler for click
function MenuBar_selectMenu(menuId, subId) {
  menu = this.menus[menuId];
  if (this.selectedMenu) this.selectedMenu.hide(this.selectedSubMenu);
  menu.show(subId);
  this.selectedMenu = menu;
  this.selectedSubMenu = subId;
}

function Menu(parent, menuId, itemsArray) {
   // properties:
   this.id = menuId;
   this.items = itemsArray;
   this.bar = parent;
   this.selected;

   // methods:
   this.show = Menu_show;
   this.hide = Menu_hide;

   this.hide();
   var aMenu = xbGetElement("menu-" + this.id);
   if (aMenu) {
       aMenu.onclick = function() { parent.selectMenu(menuId); };
       aMenu.onmouseover = function() { parent.enterMenu(menuId); };
       aMenu.onmouseout = function() { parent.leaveMenu(menuId); };
   } else {
       if (debug) alert("no menu <a> with id menu-" + this.id);
   }

   var aMenu = xbGetElement("sub-" + this.id);
   if (aMenu) {
       // aMenu.onclick = function () { parent.selectMenu(menuId); };
       aMenu.onmouseover = function () { parent.enterMenu(menuId); };
       aMenu.onmouseout = function () { parent.leaveMenu(menuId); };
   } else if (this.items) {
       if (debug) alert("no tag with id sub-" + this.id);
   }

   if (this.items) {
     for (var i=0; i < this.items.length; i++) {
       var menuName = "menu-" + this.items[i];
       var aMenu = xbGetElement(menuName);
       if (aMenu) {

           aMenu.onclick = function () { parent.selectMenu(menuId, this.name.substring(5)); };
           aMenu.onmouseover = function () { parent.enterMenu(menuId, this.name.substring(5));};
           aMenu.onmouseout = function () { parent.leaveMenu(menuId,  this.name.substring(5));};
       } else {
           if (debug) alert("no menu <a> with id " + menuName);
       }
     }
   }

   this.hide();
}

function Menu_show(subId) {
  setVisibility("sub-" + this.id, "visible");
  swapMenu(this.id, over);
  if (subId) {
      swapSubMenu(subId, over);
  }
}

function Menu_hide(subId) {
  setVisibility("sub-" + this.id, "hidden");
  swapMenu(this.id, out);
  if (subId) {
      swapSubMenu(subId, out);
  }
}


function xbGetElement(elId) {
var el;
if ( document.getElementById ) {
  el = document.getElementById(elId);
} else if ( document.all ) {
  el = document.all[elId];
}
if (!el) el = document.anchors[elId]; // anchors are identified by name not id
if (!el) el = document.images[elId]; // images are identified by name not id
return el;
}


// visibility: visible|hidden
function setVisibility(elId, visibility) {
  var el = xbGetElement(elId);
  if (el) {
      if ( document.layers ) { // NN4
        el.visibility = visibility;
      } else { // DOM
        el.style.visibility = visibility;
      }
  }
}

// type: out|over (is appended to image name)
function swapMenu(name, type) {
  var bild = xbGetElement("img-" + name);
  if (bild) {
     bild.src = mainImgPath + name + "_" + type + ".gif";
  }
}

// type: out|over (is appended to image name)
function swapSubMenu(name, type) {
  var bild = xbGetElement("img-" + name);
  if (bild) {
     bild.src = subImgPath + "um_" + type + ".gif";
  }
	var div = xbGetElement("div_txt-" + name);
	if (type == over) {
		div.style.color = "white";
	} else {
		div.style.color = "#575757";
	}
}
