//
// Based on the code written by Miha Hribar (http://hribar.info) with
// minor modifications by Jernej Kos.
//

/**
 * Initializes the menu elements.
 */

var barsActivated = new Object();
var barEnableTimers = new Object();

function initializeBar(id)
{
  if (!document.getElementsByTagName)
    return false;

  if (!document.getElementById)
    return false;

  var menu = document.getElementById(id);
  if (!menu)
    return false;

  menu.onclick = function() {
    barsActivated[id] = true;
    menu.onmouseover();
  };
  
  barEnableTimers[id] = false;

    // Method for menu display
  menu.onmouseover = function () {
    if (barsActivated[id] != true) {
      if (!barEnableTimers[id]) {
        barEnableTimers[id] = setTimeout(function() {
          barsActivated[id] = true;
          menu.onmouseover();
          barEnableTimers[id] = false;
        }, 100);
      }
      return;
    }

    if (this.timer != undefined) {
      clearTimeout(this.timer);
      this.timer = undefined;
    }

    var ul = this.getElementsByTagName("ul");
    if (!ul)
      return false;

    var li = this.getElementsByTagName("li");

    ul[1].style.display = "block";
    li[0].className = id + 'Active';

    return true;
  }

  // Method for menu hiding
  menu.onmouseout = function () {
    var ul = this.getElementsByTagName("ul");
    if (!ul)
      return false;

    var li = this.getElementsByTagName("li");

    if (barEnableTimers[id]) {
      clearTimeout(barEnableTimers[id]);
      barEnableTimers[id] = false;
    }

    var thisvar = this;
    if (this.timer == undefined) {
      this.timer = setTimeout(function () {
        barsActivated[id] = false;
        ul[1].style.display = "none";
        li[0].className = id;
      }, 200);
    }

    return true;
  }

  return true;
}

