// Menu Bar Functions

initialized=0

menu_header_color_normal='#606060' // grey; should match that in CSS
menu_header_color_active='#000000' // black
menu_bgcolor_normal='#FFFFFF' // white
menu_bgcolor_active='#F7F04A' // yellow

// Note: colors set in menu CSS
// initial text color of menu header items
// text of submenu items
// background of menu item selected

// initializes variables and constants
function initialize_variables()
{
  // location of menu bar (from top left corner, in pixels)
  menu_top = 135
  menu_left = 9
  
  // border around menu bar in pixels (area for mouse to move off the menu and trigger hideMenu)
  top_border = 10
  bottom_border = 40
  left_border = 5
  right_border = 30
  bottom_border_fudge_factor = 20

  // initialize variables
  image_dir = "images/"
  base_urls = [""] // normal, protected, no_base

  // following sizes should match those used in the python script to generate the images
  menu_title_height = 22        // height of menu title in pixels
  menu_item_height = 15         // height of menu item in pixels
  column_widths = [90, 145, 107, 116, 111, 90, 90]  // width of each column
  max_menu_items = 3           // number of items in the longest sub-menu
  max_height = max_menu_items*menu_item_height + menu_title_height

  main_menus = ["About","Contest Overview","Contest Day","Training Day","Resources","Archives","Contact"]
  sub_menus = [
  // format: menu_file_name, url, index to base_urls (0-normal,1-protected,2-no base)

 
  ["Background","background.html",0,
   "Rules","rules.html",0,
  ],
  ["Schedule","schedule.html",0,
   "Kit","kit.html",0,
   "FAQ","faq.html",0,
  ],
  ["Logistics","contest_logistics.html",0,
   "Results","contest_results.html",0,
  ],
  ["Logistics","training_logistics.html",0,
   "Results","training_results.html",0,
  ],
  ["Training","resources_training.html",0,
   "Online","resources_online.html",0,
   "Mentors","resources_mentors.html",0,
  ],
  ["2005","../2005/",0,
   "2006","../2006/",0,
   "2007","../2007/",0,
  ],
  ["Information","contact.html",0,
  ],
  ]

  initialized=1
}

// writes HTML code for the menus and sub-menus
function write_menus(path_to_root) {
  if (initialized==0)
    initialize_variables(path_to_root)

  column_left = menu_left
  submenus_html = new Array(main_menus.length)

  // write menu headers and generate submenus html
  document.writeln()
  document.writeln('<div id="MenuHeaders">')
  for (index=0; index<main_menus.length; index++) {
    title = main_menus[index]
    width = column_widths[index]
    if (index>0)
      column_left += column_widths[index-1]
    menu_items = sub_menus[index]

    document.writeln('  <div id="Menu'+index+'" class="MenuHeader" style="position:absolute; width:'+width+'px; height:'+menu_title_height+'px; ' +
      'z-index:100; left:'+column_left+"px; top:"+menu_top+'px; color:'+menu_header_color_normal+'"')
    document.writeln('    onMouseOver="showSubMenu('+index+')">')
    document.writeln('    '+title)
    document.writeln('  </div>')

    // generate submenu html
    html = '    <div id="SubMenu'+index+'" style="position:absolute; width:'+width+'px; height:'+max_height+'px; z-index:100; ' +
      'left:'+column_left+"px; top:"+(menu_top+menu_title_height)+'px; visibility:hidden; background:'+menu_bgcolor_normal+'" \n'
    html += '      onMouseOver="showSubMenu('+index+')">'
    html += '      <ul class="SubMenuList">\n'
    for (j=0; j<menu_items.length-2; j=j+3) {
      menu_title = menu_items[j]
      menu_url = menu_items[j+1]
      url_base_index = menu_items[j+2]
      html += '        <li><a href="'+base_urls[url_base_index]+menu_url+'">'+menu_title+'</a></li>\n'
    }
    html += '      </ul>\n'
    html += '    </div>\n' // end of submenu
    submenus_html[index] = html
  }
  document.writeln('</div>') // end of MenuHeaders

  // write submenus
  for (index=0; index<main_menus.length; index++) {
    document.writeln()
    document.writeln(submenus_html[index])
  }
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

menus_showing = 0
last_menu_showing = -1
function showSubMenu(index) {
  if (menus_showing==0)
    showMenus()
  else if (last_menu_showing != index) {
    last_menu_header = MM_findObj("Menu"+last_menu_showing)
    if (last_menu_header!=null)
      last_menu_header.style.color=menu_header_color_normal
    last_submenu = MM_findObj("SubMenu"+last_menu_showing)
    if (last_submenu)
      last_submenu.style.background=menu_bgcolor_normal
  } else {
    return // same menu as before
  }

  new_menu_header = MM_findObj("Menu"+index)
  if (new_menu_header!=null)
    new_menu_header.style.color=menu_header_color_active
  new_submenu = MM_findObj("SubMenu"+index)
  if (new_submenu) {
    new_submenu.style.background=menu_bgcolor_active
  }
  last_menu_showing = index
}

function showMenus() {
  for (i=0; i<main_menus.length; i++) {
    menu_header = MM_findObj("Menu"+i)
    if (menu_header!=null)
      menu_header.style.background=menu_bgcolor_active
    submenu = MM_findObj("SubMenu"+i)
    if (submenu!=null)
      if (submenu.style)
        submenu.style.visibility='visible'  
  }
  show_borders()
  menus_showing = 1
}

function hideMenus() {
  for (i=0; i<main_menus.length; i++) {
    menu_header = MM_findObj("Menu"+i)
    if (menu_header!=null) {
      menu_header.style.background=menu_bgcolor_normal
      menu_header.style.color=menu_header_color_normal
    }
    submenu = MM_findObj("SubMenu"+i)
    if (submenu!=null)
      if (submenu.style) {
        submenu.style.visibility='hidden'  
        submenu.style.background=menu_bgcolor_normal
      }
  }
  hide_borders()
  menus_showing = 0
}

// write HTML code for the area around the menubar
// so that the menu is deactivated when the mouse enters this area
var borders=["TopBorder","BottomBorder","LeftBorder","RightBorder"]
var bordersShowing=0

function write_borders() {
  max_width = 0
  for (i=0; i<7; i++)
    max_width += column_widths[i]
  top_edge = menu_top - top_border
  left_edge = menu_left - left_border
  top_of_bottom_edge = menu_top + max_height
  left_of_right_edge = menu_left + max_width
  max_border_width = max_width+left_border+right_border
  max_border_height = max_height+top_border+bottom_border
  write_border("Top",max_border_width,top_border,left_edge,top_edge)
  write_border("Bottom",max_border_width,bottom_border,left_edge,top_of_bottom_edge+bottom_border_fudge_factor)
  write_border("Left",left_border,max_border_height,left_edge,top_edge)
  write_border("Right",right_border,max_border_height,left_of_right_edge,top_edge)
}

// write HTML code for a single border
function write_border(name,width,height,left,top) {
  document.writeln('<div id="'+name+'Border" style="position:absolute; width:'+width+'px; height:'+height+'px; ' +
    'z-index:100; left:'+left+'px; top:'+top+'px; visibility:hidden;"')
  document.writeln('onMouseOver="hideMenus()"></div>')
}

function hide_borders() {
  bordersShowing=0
  for (i=0; i<4; i++) {
    MM_findObj(borders[i]).style.visibility = 'hidden'
  }
}

function show_borders() {
  if (bordersShowing==1)
    return
  bordersShowing=1
  for (i=0; i<4; i++) {
    MM_findObj(borders[i]).style.visibility = 'visible'
  }
}
