var timerId;

var main_bg_color = "#535353";
var black_color = "black";

var BASE = location.protocol+"//"+location.hostname+"/";
// alert(BASE);

function init(){
  buildMenuBar();
}
function buildMenuBar(){
  var line = '<table><tr>';
  for(var index = 0; index < menu.length; index++){
    var matches = menu[index].split("|");

    if (matches[1].indexOf('http://www') == -1)
    {
    line += '<td id="menu_option_' + index + '"><a href="' + BASE + matches[1] + '" onMouseOver="javascript:openMenu(' + index + ');" onMouseOut="javascript:closeMenu();" target=_' + matches[2] + ' >' + matches[0] + '</a></td>';
     }
    else
    {
    line += '<td id="menu_option_' + index + '"><a href="' + matches[1] + '" onMouseOver="javascript:openMenu(' + index + ');" onMouseOut="javascript:closeMenu();" target=_' + matches[2] + ' >' + matches[0] + '</a></td>';
    }
  }
  line += '</tr></table>';
  var menuObject = document.getElementById("menu");
  menuObject.innerHTML = line;
}
function openMenu(menuIndex){
  clearTimeout(timerId);
  hideMenu();
  showMenu(menuIndex);
}
function closeMenu(){
  timerId = setTimeout("hideMenu()", 1000);
}
function showMenu(menuOption){
  var link = "menu_option_" + menuOption;
  var menuOptionObject = document.getElementById(link);
  menuOptionObject.style.background = main_bg_color;
  if (sub[menuOption]!=null)
  {
  if(sub[menuOption].length > 0){
    var line = '<table>';
    for(var index = 0; index < sub[menuOption].length; index++)
    {

     var matches = sub[menuOption][index][0].split("|");
     if(matches[1]=='')

     {
     line += '<tr><td id="sub_option_' + index + '"><span';
      if(sub[menuOption][index][1].length > 0)
       {
        if (menuOption < menu.length/2)
         {
           if (matches[1].indexOf('http://www') == -1)
           {
            line += ' href="' + BASE + matches[1] + '" style="background:url(img/next.gif) no-repeat right;"';
            }
           else
           {
            line += ' href="' + matches[1] + '" style="background:url(img/next.gif) no-repeat right;"';
            }
         }
         else
         {
           if (matches[1].indexOf('http://www') == -1)
           {
           line += ' href="' + BASE + matches[1] + '" style="background:url(img/prev.gif) no-repeat left; "';
           }
           else
           {           line += ' href="' +  matches[1] + '" style="background:url(img/prev.gif) no-repeat left; "';
           }
         }

       }
       else
       {          if (matches[1].indexOf('http://www') == -1)
          {
           line += ' href="' + BASE + matches[1] + '"';
           }
          else
          {
           line += ' href="' + matches[1] + '"';
           }
       }

      line += ' onMouseOver="javascript:openSub(' + menuOption + ', ' + index + ');" onMouseOut="javascript:closeMenu();"';
      line += 'target=_' + matches[2] + ' >' + matches[0] + '</span></td></tr>';
     }
     else
     {
      line += '<tr><td id="sub_option_' + index + '"><a';
      if(sub[menuOption][index][1].length > 0)
      {
         if (menuOption < menu.length/2)
         {
          if (matches[1].indexOf('http://www') == -1)
          {
          line += ' href="' + BASE + matches[1] + '" style="background:url(img/next.gif) no-repeat right;"';
          }
          else
          {
          line += ' href="' + matches[1] + '" style="background:url(img/next.gif) no-repeat right;"';
          }
         }
         else
         {
          if (matches[1].indexOf('http://www') == -1)
          {
          line += ' href="' + BASE + matches[1] + '" style="background:url(img/prev.gif) no-repeat left; "';
          }
          else
          {          line += ' href="' + matches[1] + '" style="background:url(img/prev.gif) no-repeat left; "';
          }
         }
      }
      else
      {
//      alert ('2'+matches[1]);      	 if (matches[1].indexOf('http://www') == -1)
      	 {          line += ' href="' + BASE + matches[1] + '"';
      	 }         else
      	 {
          line += ' href="' + matches[1] + '"';
      	 }
      }


      line += ' onMouseOver="javascript:openSub(' + menuOption + ', ' + index + ');" onMouseOut="javascript:closeMenu();"';
      line += 'target=_' + matches[2] + ' >' + matches[0] + '</a></td></tr>';
     }

    }
    line += '</table>';

    var subObject = document.getElementById("sub");
    subObject.innerHTML = line;
    subObject.style.visibility = "visible";

    var position = getBound(menuOptionObject);
    var position2 = getBound(subObject);

    if (position[0] + position2[2] <= 1024)
    {
    var position = getBound(menuOptionObject);
    subObject.style.left = position[0] + "px";
    subObject.style.top = (position[3] + position[1]) + "px";
    }
    else
    {    subObject.style.left = position [0] + position[2] - position2[2] + "px";
    subObject.style.top = (position[3] + position[1]) + "px";
    }

   }

  }
}
function hideMenu(){
  for(var index = 0; index < menu.length; index++){
    var link = "menu_option_" + index;
    var menuOptionObject = document.getElementById(link);
    menuOptionObject.style.background = "";
  }
  var subObject = document.getElementById("sub");
  subObject.innerHTML = "";
  subObject.style.visibility = "hidden";
  var suberObject = document.getElementById("suber");
  suberObject.innerHTML = "";
  suberObject.style.visibility = "hidden";
}
function openSub(menuIndex, subIndex){
  clearTimeout(timerId);
  hideSub(menuIndex);
  showSub(menuIndex, subIndex);
}
function showSub(menuOption, subOption){
  var link = "sub_option_" + subOption;
  var subOptionObject = document.getElementById(link);
  subOptionObject.style.background = "#FE8800";
  if(sub[menuOption][subOption][1].length > 0){
    var line = '<table>';
    for(var index = 0; index < sub[menuOption][subOption][1].length; index++){
      var matches = sub[menuOption][subOption][1][index].split("|");

      if (matches[1].indexOf('http://www') == -1)
      {
      line += '<tr><td id="suber_option_' + index + '"><a href="' + BASE + matches[1] + '" onMouseOver="javascript:showSuber(' + menuOption + ', ' + subOption + ', ' + index + ');" onMouseOut="javascript:closeMenu();" target=_' + matches[2] + ' >' + matches[0] + '</a></td></tr>';
      }
      else
      {      line += '<tr><td id="suber_option_' + index + '"><a href="' +  matches[1] + '" onMouseOver="javascript:showSuber(' + menuOption + ', ' + subOption + ', ' + index + ');" onMouseOut="javascript:closeMenu();" target=_' + matches[2] + ' >' + matches[0] + '</a></td></tr>';
      }

    }
    line += '</table>';
    var suberObject = document.getElementById("suber");
    suberObject.innerHTML = line;
    suberObject.style.visibility = "visible";


    var position = getBound(subOptionObject);
    suberObject.style.left = (position[2] + position[0]) + "px";
    suberObject.style.top = position[1] + "px";

    if (menuOption < menu.length/2)
    {
    var position = getBound(subOptionObject);
    suberObject.style.left = (position[2] + position[0]) + "px";
    suberObject.style.top = (position[1]) + "px";
    }
    else
    {
    var position = getBound(subOptionObject);

    var position2 = getBound(suberObject);

    suberObject.style.left = position [0] - position2[2] + "px";
    suberObject.style.top = (position[1]) + "px";
    }

  }
}
function hideSub(menuIndex){
  for(var index = 0; index < sub[menuIndex].length; index++){
    var link = "sub_option_" + index;
    var subOptionObject = document.getElementById(link);
    subOptionObject.style.background = main_bg_color;
  }
  var suberObject = document.getElementById("suber");
  suberObject.innerHTML = "";
  suberObject.style.visibility = "hidden";
}
function showSuber(menuOption, subOption, suberOption){
  clearTimeout(timerId);
  hideSuber(menuOption, subOption);
  var link = "suber_option_" + suberOption;
  var suberOptionObject = document.getElementById(link);
  suberOptionObject.style.background = "#FE8800";
}
function hideSuber(menuOption, subOption){
  for(var index = 0; index < sub[menuOption][subOption][1].length; index++){
    var link = "suber_option_" + index;
    var suberOptionObject = document.getElementById(link);
    suberOptionObject.style.background = main_bg_color;
  }
}
function getBound(object){
  var current = object;
  var x = 0, y = 0;
  while(current){
     x += current.offsetLeft;
     y += current.offsetTop;
     current = current.offsetParent;
  }
  var width = object.offsetWidth;
  var height = object.offsetHeight;
//  alert ('x=' + x + 'y=' + y + 'w=' + width + 'h=' + height);
  return [x, y, width, height];

}
