/* menu.js */

var usemenus;
var active = false;
var active_item = false;
var initialized = false;
var maybe_popdown_flag;
var ignore_out = false;

var last_item_over = 0;

var NS4 = (document.layers) ? true : false;
var IE4 = (document.all) ? true : false;
      
if (NS4 || IE4) {
  usemenus = true;
} else {
  usemenus = false;
}
      
function show_menu(menu, show)
{
  if (IE4) {
      menu.style.visibility = (show) ? "visible" : "hidden";
  } else {
      menu.visibility = (show) ? "show" : "hide";
  }
}

function maybe_popdown()
{
  if (ignore_out) {
    ignore_out = false;
    maybe_popdown_flag = false;
    return;
  }

  if (maybe_popdown_flag)
    popdown();
  maybe_popdown_flag = false;
}

var timer_id;

function set_timer()
{
  if (timer_id)
    clearTimeout(timer_id);

  maybe_popdown_flag = true;
  timer_id = setTimeout("maybe_popdown()", 250);
}

// event handlers
function menu_item_out()
{
  if (last_item_over+200 > (new Date).getTime())
    return;

  if (!usemenus)
     return;

  set_timer();
}

// event handlers
function menu_out()
{
  if (!usemenus)
     return;

  set_timer();
}

function item_over(l)
{
  last_item_over = (new Date).getTime();

  if (IE4) {
    maybe_popdown_flag = false;
    if (active_item)
      active_item.style.background = menucolor;
    l.style.background = highlightcolor;
    active_item = l;
  } else {
    maybe_popdown_flag = false;

    if (!l)
      return false;

    if (active_item)
      active_item.document.bgColor = menucolor;
    l.document.bgColor = highlightcolor;
    active_item = l;
    
    return false;
  }
}

function popdown()
{
  if (IE4) {
    if (active_item)
      active_item.style.background = menucolor;
  } else {
    if (active_item)
      active_item.document.bgColor = menucolor;
  }
  active_item = false;
  if (active)
    show_menu(active, false);
  active = false;
}
    
function popup(menuid)
{
  var menu;
      
  if (!usemenus)
    return true;

  init_once();
      
  if (active) {
    ignore_out = true;
    popdown();
  }
      
  active = menu = eval("window."+menuid);

  if (NS4) {
    menu.top = top.frames[1].window.pageYOffset;
    menu.left = menu.offsety+top.frames[1].window.pageXOffset;
  } else {
    menu.style.pixelTop = top.frames[1].window.document.body.scrollTop;
    menu.style.pixelLeft = menu.offsety+top.frames[1].window.document.body.scrollLeft;
  }
  maybe_popdown_flag = false;

  show_menu(menu, true);
  
  return false;
}

function jump()
{
  top.frames[1].location.href = this.href;
  return true;
}

function create_menu_IE4(def, name)
{
  var  text;
  var  i, n;

  var container = parent.frames[1].document;

  text = "";

  text += "<DIV id='"+name+"'";
  text += "style='";
  text += "position:absolute; visibility: hidden; background: "+menucolor+";";
  text += " border: solid black 1px;"
  text += " top:0; left:"+def[0]+"; width: 10px;";
  text += "'>";
      
  n = def.length;
  for (i=2; i<n; i+=2) {
    text += "<DIV id='xyz'"; //+i+"'";
    text += " onMouseOver='top.frames[0].item_over(this)'";
//    text += " onClick='alert(this)'";
    text += " style='";

//  text += "position:absolute;";

    if (i != 2)
      text += "border-top: solid black 1px; ";

    text += " padding: 3px;"
    
    text += "'>";
    text += "<A style='";
    text += "color: "+menutext+"; font-family: Verdana, Arial, Helvetica; font-size: 10px; font-weight: 700;";
    text += "text-decoration: none;";
    text += "' ";
//    text += "href='http://www2.melody.se"+def[i+1]+"'>"+def[i]+"</A>";
    text += "href='"+def[i+1]+"'>"+def[i]+"</A>";
    text += "</DIV>";
  }
  
  text += "</DIV>";

  container.body.insertAdjacentHTML("BeforeEnd", text);

  menu = eval("parent.frames[1]."+name);
  
  var c;
  for (i=2; i<n; i+=2) {
    c = eval("parent.frames[1].xyz;"); //+i+";");
    c.onclick=jump;
    c.href= "http://www.flygbussarna.com"+def[i+1];
//	c.style.cursor = "hand";
  }

  return menu;
}
      
var menuheight = 18;

function create_menu_NS4(def)
{
  var  i, n;

  menu = new Layer(100, parent.frames[1]);

  var  doc = menu.document;

  doc.write("<BODY><DIV>");

  n = def.length;
  for (i=2; i<n; i+=2) {
    doc.write("<LAYER name='item"+i+"'");
    doc.write(" left=1 height="+(menuheight-1)+" top='"+(menuheight*(i-2)/2+1));
    doc.write(" bgcolor='"+menucolor+"'");
    doc.write(" onMouseOver='parent.frames[0].item_over(this)'>");

    doc.write("<LAYER top=2 left=4 name='x'>");
    doc.write("<A style='");
    doc.write("color: "+menutext+"; font-family: Verdana, Arial, Helvetica; font-size: 11px; font-weight: 700;");
    doc.write("text-decoration: none;'");
    doc.write(" onMouseOver='parent.frames[0].item_over(null)'");
//    doc.write(" href='http://www2.melody.se"+def[i+1]+"'");
    doc.write(" href='"+def[i+1]+"'");
    doc.write(">"+def[i]+"</A>");
    doc.write("</LAYER>");
    doc.write("</LAYER>");
  }

  doc.write("</DIV></BODY>");
  
  doc.close();

  var c, maxwidth = 0;
  for (i=2; i<n; i+=2) {
    c = eval("menu.document.layers.item"+i);
    c.captureEvents(Event.MOUSEUP);
    c.onmouseup=jump;
    c.href="http://www.flygbussarna.com"+def[i+1];
    maxwidth = Math.max(maxwidth, c.document.layers.x.clip.width);
  }
  
  for (i=2; i<n; i+=2) {
    c = eval("menu.document.layers.item"+i);
    c.clip.right = maxwidth+8;
    c.document.bgColor = menucolor;
  }

  menu.top = 0;
  menu.left = def[0];
  menu.clip.right = maxwidth+10;
  menu.clip.height = menuheight*((def.length-2)/2)+1

  menu.bgColor = "black";

  return menu;
}

function unload()
{
  initialized = false;
}      

function create_menu(def, name)
{
  var  menu;

  if (IE4) {
    parent.frames[1].document.body.onunload = unload;

    menu = create_menu_IE4(def, name);
    
  } else {
    parent.frames[1].onunload = unload;

    menu = create_menu_NS4(def);
  }

  menu.onmouseout = menu_out;
  menu.offsety = def[0];

  return menu;
}
  
function init_once()
{
  if (initialized)
      return;

  initialized = true;    
  create_menus();
}
