this.menus = new Array();

var currentMenu = null;
var hasClicked = false;

//---------------------------------------------------------------------------
function getElement(id) {
  if (ie4) return document.all[id];
  return document.getElementById(id);
}
//---------------------------------------------------------------------------
function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) curleft += obj.x;
	return curleft;
}

//---------------------------------------------------------------------------
function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) curtop += obj.y;
	return curtop;
}
//---------------------------------------------------------------------------
function getMenu(menuId) {
  for (var i = 0; i < menus.length; i++) {
    if (menus[i].id == menuId) {
      return menus[i];
    }
  }
  return null;
}
//---------------------------------------------------------------------------
function showMenu(menuId) {

  var menuObj = getMenu(menuId); 

  if (menuObj) {
    var relObj = getElement(menuObj.relObjId);

    if (relObj) {
      var x = findPosX(relObj);
      var y = findPosY(relObj);
      var w = relObj.offsetWidth;
      var h = relObj.offsetHeight;
       
      // Huvudmeny
      if (menuObj.parent == null) {
        if (currentMenu != null && currentMenu != menuObj) {
          currentMenu.hide();
          currentMenu = null;
        }

        if (menuObj.hideTimeoutId != 0) {
          clearTimeout(menuObj.hideTimeoutId);
          menuObj.hideTimeoutId = 0;
        }

        menuObj.setPosition(x, y + h /*- 1*/);
        menuObj.setWidth(w);
        menuObj.show();
        currentMenu = menuObj;
      }
      // Undermeny
      else {
        if (menuObj.parent.currentSubMenu != null && menuObj.parent.currentSubMenu != menuObj) {
          menuObj.parent.currentSubMenu.hide();
          menuObj.parent.currentSubMenu = null;
        }

        if (menuObj.hideTimeoutId != 0) {
          clearTimeout(menuObj.hideTimeoutId);
          menuObj.hideTimeoutId = 0;
        }

        menuObj.setPosition(x + w, y - 1);
        menuObj.show();
        menuObj.parent.currentSubMenu = menuObj;
      }
    }
  }
}
//---------------------------------------------------------------------------
function hideMenu(menuId) {
  var menuObj = getMenu(menuId); 
  if (menuObj) {
    if (menuObj.hideTimeoutId != 0) {
      clearTimeout(menuObj.hideTimeoutId);
      menuObj.hideTimeoutId = 0;
    }
    menuObj.hideTimeoutId = setTimeout("realHideMenu('" + menuId + "')", 650);

    if (menuObj.isSubMenu) {
      var parent = menuObj.parent;
      while (parent != null) {
        if (parent.hideTimeoutId != 0) {
          clearTimeout(parent.hideTimeoutId);
          parent.hideTimeoutId = 0;
        }
        parent.hideTimeoutId = setTimeout("realHideMenu('" + parent.id + "')", 650);
        parent = parent.parent;
      }
    }
  }
}
//---------------------------------------------------------------------------
function realHideMenu(menuId) {
  var menuObj = getMenu(menuId); 
  if (menuObj) {
    if (menuObj.currentSubMenu == null) {
      menuObj.hide();
      menuObj.hideTimeoutId = 0;
      if (menuObj.parent == null) {
        if (currentMenu != null && currentMenu == menuObj) {
          currentMenu = null;
        }
      }
      else {
        if (menuObj.parent.currentSubMenu != null && menuObj.parent.currentSubMenu == menuObj) {
          menuObj.parent.currentSubMenu = null;
        }
      }
    }
  }
}
//---------------------------------------------------------------------------
function resetHideOfMenu(menuId) {
  var menuObj = getMenu(menuId); 
  if (menuObj) {
    if (menuObj.hideTimeoutId != 0) {
      clearTimeout(menuObj.hideTimeoutId);
      menuObj.hideTimeoutId = 0;
    }

    if (menuObj.isSubMenu) {
      var parent = menuObj.parent;
      while (parent != null) {
        resetHideOfMenu(parent.id);
        parent = parent.parent;
      }
    }
  }
}
//---------------------------------------------------------------------------
function hideCurrentMenu() {
  if (currentMenu != null) {
    if (currentMenu.hideTimeoutId != 0) {
      clearTimeout(currentMenu.hideTimeoutId);
      currentMenu.hideTimeoutId = 0;
    }
    currentMenu.hide();
    currentMenu = null;
  }
}



// Menu class
//---------------------------------------------------------------------------
function Menu(id, relObjId) {
  this.id = id;
  this.htmlId = "menu_" + id;
  this.relObjId = relObjId;
  this.parent = null;
  this.created = false;
  this.isSubMenu = false;
  this.isVisible = false;

  this.items = new Array();
  this.itemCount = 0;

  this.hideTimeoutId = 0;
  this.currentSubMenu = null;

  this.setParent          = Menu_setParent;
  this.addItem            = Menu_addItem;
  this.setPosition        = Menu_setPosition;
  this.setWidth           = Menu_setWidth;
  this.show               = Menu_show;
  this.hide               = Menu_hide;
  this.hideCurrentSubmenu = Menu_hideCurrentSubmenu;
  this.create             = Menu_create;

  menus[menus.length] = this;
}
//---------------------------------------------------------------------------
function Menu_setParent(parent) {
  this.parent = parent;
}
//---------------------------------------------------------------------------
function Menu_addItem(id, text, href, title, selected) {
  var item = new MenuItem(this, id, text, href, title, selected);
  this.items[this.itemCount++] = item;
  return item;
}
//---------------------------------------------------------------------------
function Menu_setPosition(left, top) {
  if (!this.created) return;
  var layer = getElement(this.htmlId);
	
	var style = null;
	if (ie4) style = layer.style;
  if (w3c) style = layer.style;

	if (style == null) return;

  style.left = left;
  style.top  = top;
}
//---------------------------------------------------------------------------
function Menu_setWidth(width) {
  if (!this.created) return;
  var layer = getElement(this.htmlId);
	
	var style = null;
	if (ie4) style = layer.style;
  if (w3c) style = layer.style;

	if (style == null) return;

  style.width = width;
}
//---------------------------------------------------------------------------
function Menu_show() {
  if (!this.created) return;
  var layer = getElement(this.htmlId);
	
	var style = null;
	if (ie4) style = layer.style;
  if (w3c) style = layer.style;

	if (style == null) return;

  style.visibility  = "visible";
  this.isVisible = true;

  var relObj = getElement(this.relObjId);
  if (relObj) {
    if (relObj.oldClassName == null) {
      relObj.oldClassName = relObj.className;
      relObj.className = 'menuLinkBgOver';
    }
  }
}
//---------------------------------------------------------------------------
function Menu_hide() {
  if (!this.created) return;
  if (this.isMouseOver) return false;
  var layer = getElement(this.htmlId);
	
	var style = null;
	if (ie4) style = layer.style;
  if (w3c) style = layer.style;

	if (style == null) return;

  for (var i = 0; i < this.itemCount; i++) {
    if (this.items[i].subMenu != null) {
      if (this.items[i].subMenu.isVisible) {
        if (!this.items[i].subMenu.hide()) {
          return false;
        }
      }
    }
  }

  style.visibility  = "hidden";
  this.isVisible = false;

  var relObj = getElement(this.relObjId);
  if (relObj) {
    if (relObj.oldClassName != null) {
      relObj.className = relObj.oldClassName;
      relObj.oldClassName = null;
    }
  }
  return true;
}
//---------------------------------------------------------------------------
function Menu_hideCurrentSubmenu() {
  if (this.currentSubMenu != null) {
    this.currentSubMenu.hide();
    this.currentSubMenu = null;
  }
}
//---------------------------------------------------------------------------
function Menu_create() {
  document.writeln('<table border="0" cellspacing="1" cellpadding="0" id="' + this.htmlId + '" class="menuBg" style="position:absolute; visibility:hidden; left: 0px; top: 0px; background-color: #E0E0E0; zIndex: 5; background-image:none; width: 10px;" onMouseOver="resetHideOfMenu(\'' + this.id + '\');" onMouseOut="hideMenu(\'' + this.id + '\');">');

  for (var i = 0; i < this.itemCount; i++) {
    this.items[i].create();
  }

  document.writeln('</table>');

  this.created = true;
	
}


// MenuItem class
//---------------------------------------------------------------------------
function MenuItem(menu, id, text, href, title, selected) {
  this.menu = menu;
  this.id = id;
  this.htmlId = "item_" + id;
  this.text = text;
  this.href = href;
  this.title = title;
  this.selected = selected;
  this.subMenu = null;
  this.created = false;

  this.setSubMenu   = MenuItem_setSubMenu;
  this.create       = MenuItem_create;
}
//---------------------------------------------------------------------------
function MenuItem_setSubMenu(subMenu) {
  this.subMenu = subMenu;
  subMenu.isSubMenu = true;
  subMenu.parent = this.menu;
  subMenu.relObjId = this.menu.htmlId + "_" + this.htmlId;
}
//---------------------------------------------------------------------------
function MenuItem_create() {
  var omov = "this.className=\'menuLinkBgOver\';getMenu(\'" + this.menu.id + "\').hideCurrentSubmenu();";
  var omou = "this.className=\'\';";
  if (this.subMenu) {
    omov = 'showMenu(\'' + this.subMenu.id + '\')";';
    omou = 'hideMenu(\'' + this.subMenu.id + '\')";';
  }

  var childSelected = false;
  if (this.selected && this.subMenu != null) {
    for (var i = 0; i < this.subMenu.itemCount; i++) {
      if (this.subMenu.items[i].selected) {
        childSelected = true;
        break;
      }
    }
  }

  var hasHref = this.href != null && this.href.length > 0;

  document.writeln('<tr><td id="' + this.menu.htmlId + "_" + this.htmlId +
                   '" title="' + this.title + '" onMouseOver="resetHideOfMenu(\'' + this.menu.id + '\');' + omov +
                   '" onMouseOut="' + omou + '"' + (hasHref ? (' onClick="if (!hasClicked) document.location=\'' + this.href + '\';"') : '' ) + ' style="cursor: ' + (!hasHref ? 'default' : (ie4 ? 'hand' : 'pointer')) + ';" nowrap>' +
                   '<table border="0" cellspacing="4" cellpadding="0" width="100%"><tr><td nowrap>' + (hasHref ? ('<a href="' + this.href +
                   '" title="' + this.title + '" class="menuULink"' + (this.selected ? ' id="selectedU"' : '') + ' onClick="hasClicked=true;">&nbsp;' + this.text + '&nbsp;</a>') : '<span title="' + this.title + '" class="menuULink"' + (this.selected ? ' id="selectedU"' : '') + '>&nbsp;' + this.text + '&nbsp;</span>') +'</td>' + (this.subMenu != null ? ('<td align="right"><img src="/images/transpix.gif" width="4" height="7"><img src="/images/undermenypil' + (childSelected ? '_sel' : '') + '.gif" width="4" height="7"></td>') : '') + '</tr></table></td></tr>');
  this.created = true;	
}
