// Menu Bar Functions for Fall 2008 2.009 website

// 07 Sep 08: fix for Firefox 3 adding extra pixel to height of menu items

initialized=0
needsFirefox3Fix=0 // set to 1 if menu items need to be shortened in height by 1 px

menu_header_color_normal='#808080' // grey; should match that in CSS
menu_header_color_active='#000000' // black
menu_bgcolor_normal='#FFFFFF' // white
menu_bgcolor_active='#FEFE9C' // 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(path_to_root)
{
  // check to see if menu items need to be shortened in height by 1 px
  needsFirefox3Fix = isFirefox3();

  // location of menu bar (from top left corner, in pixels)
  menu_top = 8
  menu_left = 159
  
  // 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 = 30
  right_border = 30
  bottom_border_fudge_factor = 20

  // initialize variables
  image_dir = path_to_root+"imagesMasthead/"
  base_urls = [path_to_root, "https://web.mit.edu/2.009/www/",""] // normal, protected, no_base (outside 2.009 web_root)


  menu_title_height = 22        // height of menu title in pixels
  menu_item_height = 15         // height of menu item in pixels
  column_widths = [84, 95, 60, 93, 134, 130, 57]  // width of each column
  max_menu_items = 11           // number of items in the longest sub-menu
  max_height = max_menu_items*menu_item_height + menu_title_height

  main_menus = ["Information","Schedule","Teams","Team manual","Project","Resources","Gallery"]
  sub_menus = [
  // format: menu_file_name, url, index to base_urls (0-normal,1-protected,2-no base)
  ["Description","courseinfo/CourseDescription.html",0,
   "Organization","courseinfo/instructionComponents.html",0,
   "Topics","courseinfo/courseTopics.html",0,
   "Grading","grading/Grading.html",0,
   "Staff","courseinfo/People.html",0,
   "Mentors","courseinfo/teamMentors.html",0,
   "Sponsors","courseinfo/courseSponsors.html",0,
   "Mailing lists","courseinfo/MailingLists.html",0,
   "Site map","siteMap.html",0,
  ],
  ["Key dates","schedule/keyDates.html",0,
   "Syllabus","schedule/ScheduleChart.html#current",0,
   "Deliverables","assignments/Assignments.html",0,
   "Lecture notes","schedule/LectureList.html",0,
   "Lab notes","schedule/LabList.html",0,
   "Timesheet","assignments/DesignTimeSheet.html",0,
  ],
  ["All","teams/indexTeams.html",1,
   "Red","teams/teamPages/RedTeam.html",1,
   "Green","teams/teamPages/GreenTeam.html",1,
   "Blue","teams/teamPages/BlueTeam.html",1,
   "Yellow","teams/teamPages/YellowTeam.html",1,
   "Pink","teams/teamPages/PinkTeam.html",1,
   "Orange","teams/teamPages/OrangeTeam.html",1,
   "Purple","teams/teamPages/PurpleTeam.html",1,
   "Silver","teams/teamPages/SilverTeam.html",1,
   "labCAMs","teams/PappalardoCam.htm",1,
   "Wikis","wiki/index.html",0,
  ],
  ["Team roles","teammanual/TeamRoles.html",0,
   "Task forces","teammanual/definingTaskForces.html",0,
   "Ethics codes","teammanual/ethics.html",0,
   "Safety","teammanual/safety.html",0,
   "Workspace","teammanual/equipmentWorkspace.html",0,
   "Materials","teammanual/projectMaterials.html",0,
   "Purchases","teammanual/purchases.html",0,
   "Shipping","teammanual/shippingAddress.html",0,
   "Tax forms","teammanual/taxForms.html",0,
  ],
  ["Theme","project/projectTheme.html",0,
   "Workflow","project/projectWorkflow.html",0,
   "Budget","project/projectBudget.html",0,
   "3-ideas presentation","assignments/IdeasPresentation.html",0,
   "Sketch model review","assignments/SketchModelReview.html",0,
   "Mockup review","assignments/MockUpReview.html",0,
   "Assembly review","assignments/AssemblyProductContract.html",0,
   "Technical review","assignments/TechnicalReview.html",0,
   "Final presentation","assignments/FinalPresentation.html",0,
  ],
  [ "2.009 library page","http://libraries.mit.edu/2.009",2, 
   "References","resources/references.html",0,
   "Vendors & resources","resources/vendors.html",0,
   "Running meetings","resources/mediaAndArticles/3_MeetingPrimer.pdf",0,
   "Brainstorming","lectures/2_brainstormTutorial.pdf",0,
   "Sketching tutorials","resources/sketchingTutorials.html",0,
   "Sketch models","resources/sketchModelTutorials.html",0,
   "Provisional patents","resources/mediaAndArticles/provisionalPatents.html",0,
   "FTP to Athena","resources/mediaAndArticles/connectingToAthena.html",0,
   "Plotters & laser","resources/plotterLaserInstructions.html",0,
   "Phone–fax—scanning","resources/phoneFaxScanner.html",0,
  ],
  ["Fun!","experiments/experimentsIndex.html",0,
   "2005","http://web.mit.edu/2.009_gallery/www/2005/",2,
  ]
  ]

  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
    firefox3Fix = ""
    if (needsFirefox3Fix == 1) {
      firefox3Fix = ' style="margin-bottom:1px;"'
    }
    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'+firefox3Fix+'><a href="'+base_urls[url_base_index]+menu_url+'" style="width:'+width+'px">'+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])
  }
}

// checks if browser is Firefox 3
function isFirefox3() {
    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
	var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	if (ffversion>=3) { // do fix
	   return 1; 
	}
    }
    return 0;
}

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<column_widths.length; 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'
  }
}
