var ua = {};
with (ua) {
  ua.test = function(s) {
    return navigator.userAgent.toLowerCase().indexOf(s.toLowerCase()) > -1;
  };
  ua.gecko = test("gecko") && !test("khtml");
  ua.khtml = test("khtml");
  ua.opera = test("opera");
  ua.ie = test("msie");
  ua.ie5 = test("msie 5");
  ua.ie6 = test("msie 6");
  ua.mac = test("mac");
  ua.win = test("windows");
}

function addCssRule(selector, properties, media) {
  var nl = document.getElementsByTagName("style");
  var n, a;
  if (nl.length > 0) a = nl[nl.length - 1].getAttribute("media");
  if (nl.length == 0 || (nl.length > 0 && a != null && a != "" && a != "all")) {
    n = document.createElement("style");
    n.setAttribute("type", "text/css");
    n.setAttribute("media", "all");
    document.getElementsByTagName("head")[0].appendChild(n);
  }
  else n = nl[nl.length - 1];
  var cssRule = selector + " { " + properties + " }";
  if (media) cssRule = "@media " + media + " { " + cssRule + " }";
  if (n.styleSheet) n.styleSheet.cssText += cssRule; // IE
  else n.appendChild(document.createTextNode(cssRule));
};

function Navigation() {
  var that = this;
  this.hovering = false;
  this.timerId = null;
  this.main = document.getElementById("navigation-list");
  this.lists = this.main.getElementsByTagName("ul");
  var n = document.getElementById("navigation");
  
  this.step = function() {
    var done = true;
    for (var i = 0, l = that.lists.length; i < l; i++) {
      var list = that.lists[i];
      list.marginTop = Math.min(0, list.marginTop + Math.max(1, -list.marginTop / 1.8));
      list.style.marginTop = list.marginTop + "px";
      if (list.marginTop < 0) done = false;
    }
    if (!done) setTimeout(that.step, 40);
  };
  this.show = function() {
    if (that.hovering) return;
    that.hovering = true;
    for (var i = 0, l = that.lists.length; i < l; i++) {
      var list = that.lists[i];
      list.marginTop = -list.offsetHeight;
      list.style.marginTop = list.marginTop + "px";
    }
    n.className = "hover";
    if (ua.ie5 || ua.ie6) hideSelectBoxes();
    that.step();
  };
  this.hide = function() {
    n.className = "";
    that.hovering = false;
    if (ua.ie5 || ua.ie6) showSelectBoxes();
  };
  
  n.onmouseout = function() {
    that.timerId = setTimeout(that.hide, 200);
  };
  this.main.onmouseover = function() {
    if (that.timerId != null) {
      clearTimeout(that.timerId);
      that.timerId = null;
    }
    that.show();
  };

  this.hide();
}

function hideSelectBoxes() {
  var nl = document.getElementsByTagName("select");
  for (var i = 0, l = nl.length; i < l; i++) {
    nl[i].style.visibility = "hidden";
  }
}
function showSelectBoxes() {
  var nl = document.getElementsByTagName("select");
  for (var i = 0, l = nl.length; i < l; i++) {
    nl[i].style.visibility = "visible";
  }
}



// execute on DOM load or window load
window.onload = function() {
  if (arguments.callee.done) return;
  else arguments.callee.done = true;
  // execute following
  var nav = new Navigation();

}



