// Main menu

var mnu_showSubTimeout = null;
var mnu_hideSubTimeout = null;
var mnu_showSubId = null;
var mnu_subDisplayedId = null;
var mnu_showSubTimeoutInterval = 200;
var mnu_hideSubTimeoutInterval = 500;
var mnu_canShowSub = false;

function mnu_activate_sub() {
  mnu_canShowSub = true;
}

function mnu_menu_mouseover(obj, subId) {
  if (mnu_hideSubTimeout != null) {
    clearTimeout(mnu_hideSubTimeout);
    mnu_hideSubTimeout = null;
  }
}

function mnu_main_mouseover(obj, subId) {
  if (mnu_hideSubTimeout != null) {
    clearTimeout(mnu_hideSubTimeout);
    mnu_hideSubTimeout = null;
  }

  if (!mnu_canShowSub)
    return;

  if (mnu_showSubTimeout == null && mnu_subDisplayedId == null) {
    mnu_showSubId = subId;
    mnu_showSubTimeout = setTimeout(function() {mnu_showSubMenu(subId);}, mnu_showSubTimeoutInterval);
    return;
  }

  if (mnu_showSubTimeout != null) {
    if (mnu_showSubId != subId) {
      clearTimeout(mnu_showSubTimeout);
      mnu_showSubId = subId;
      mnu_showSubTimeout = setTimeout(function() {mnu_showSubMenu(subId);}, mnu_showSubTimeoutInterval);
      return;
    }
  }
  else {
    mnu_showSubMenu(subId);
  }
}

function mnu_main_mouseout(obj, subId) {
  mnu_showSubId = null;
  if (mnu_hideSubTimeout != null) {
    clearTimeout(mnu_hideSubTimeout);
    mnu_hideSubTimeout = null;
  }

  if (mnu_showSubTimeout != null) {
    clearTimeout(mnu_showSubTimeout);
    mnu_showSubTimeout = null;
  }

  if (mnu_subDisplayedId != null)
    mnu_hideSubTimeout = setTimeout(function() {mnu_hideSubMenu();}, mnu_hideSubTimeoutInterval);
}

function mnu_sub_mouseover(obj, subId) {
  if (mnu_hideSubTimeout != null) {
    clearTimeout(mnu_hideSubTimeout);
    mnu_hideSubTimeout = null;
  }
}

function mnu_sub_mouseout(obj, subId) {
  mnu_showSubId = null;
  if (mnu_hideSubTimeout != null)
    clearTimeout(mnu_hideSubTimeout);
  mnu_hideSubTimeout = setTimeout(function() {mnu_hideSubMenu();}, mnu_hideSubTimeoutInterval);
}

function mnu_showSubMenu(subId) {
  mnu_showSubTimeout = null;
  if (mnu_subDisplayedId == subId)
    return;

  mnu_hideSubMenu();
  var el = document.getElementById(subId);
  if (el != null) {
    el.style.display="";
  }

  mnu_showSubId = null;
  mnu_subDisplayedId = subId;
}

function mnu_hideSubMenu() {
  if (mnu_subDisplayedId == null)
    return;

  var el = document.getElementById(mnu_subDisplayedId);
  if (el != null) {
    el.style.display="none";
  }
  mnu_subDisplayedId = null;
}

function hrefNewsId(href) {
  var newsId = null;
  var locate = "tx_ttnews[tt_news]=";
  var pos = href.indexOf(locate);
  if (pos > 0) {
    var epos = href.indexOf("&", pos + 1);
    if (epos < 0) epos = href.length;
    newsId = href.substr(pos + locate.length, epos - pos - locate.length);
  }
  return newsId;
}

function childWithTag(node, tag) {
  if (node == null || node.childNodes == null)
    return null;

  for (var i=0; i<node.childNodes.length; i++) {
    var childTag = node.childNodes[i].tagName;
    if (childTag != null && childTag.toUpperCase() == tag)
      return node.childNodes[i];
  }

  return null;
}

function setActiveNewsItem() {
  var table = document.getElementById("news-list-items");
  if (table == null || table.rows.length == 0)
    return;

  var pageNewsId = hrefNewsId(location.href);
  if (pageNewsId == null)
    return;

  for (var i=0; i<table.rows.length; i++) {
    var cells = table.rows[i].cells;
    var link = childWithTag(cells[1], "A");
    if (link != null) {
      var linkNewsId = hrefNewsId(link.href);
      if (linkNewsId == pageNewsId) { 
        table.rows[i].style.fontWeight = "bold";
        break;
      }
    }
  }
}

