// This function creates a menu where each item is a list item in an unordered list
function ulmenu(menuItems) {
  document.write("<ul>\n");
  for (n = 0; n < menuItems.length; n++) {
    document.write('<li><a href="' + menuItems[n][1] + '">' + menuItems[n][0] + '</a></li>\n');
  }
  document.write("</ul>\n");
}

// This function creates a menu where each item is in its own paragraph, such as the announcements
function pmenu(menuItems) {
  document.write("<p>\n");
  for (n = 0; n < menuItems.length; n++) {
    document.write(menuItems[n][0] + ' <a href="' + menuItems[n][1] + '">more...</a>\n');
  }
  document.write("</p>\n");
}

// This function creates a horizonal menu such as the quick links menu
function horzmenu(menuItems) {
  for (n = 0; n < menuItems.length; n++) {
    document.write('<a href="' + menuItems[n][1] + '">' + menuItems[n][0] + '</a>');
    if (n < menuItems.length - 1)
      document.write(' | \n');
  }
  document.write("\n");
}

// This function creates the common navigation elements for each page
function commonnav(leftNavMenu,announceMenu,quickLinksMenu) {

  if (leftNavMenu != null) {
    document.write('<div id="leftnav">\n');
    document.write('  <div id="sectionLinks">\n');
    ulmenu(leftNavMenu);
    document.write('</div>\n');
  }
  
  if (announceMenu == null) {
    document.write('  <div id="headlines">&nbsp;</div></div>\n');
  } else {
    document.write('  <div id="headlines"><h3>News</h3> \n');
    pmenu(announceMenu);
    document.write('</div>\n');
    document.write('</div>\n');
  }

  if (quickLinksMenu != null) {
    document.write('<div id="globalNav"><strong>Quick links:</strong> ');
    horzmenu(quickLinksMenu);  
    document.write('</div>\n');
  }

  document.write('<div id="footer">\n');
  document.write('<table><tr>\n');
  document.write('<td><a href="http://web.mit.edu/"><img src="http://web.mit.edu/westgate/images/mit-blackred-footer3.gif"></a></td>\n');
  document.write('<td><a href="http://web.mit.edu/">Massachusetts Institute of Technology</a> | \n');
  document.write('<a href="http://web.mit.edu/westgate/">Westgate Family Housing Community</a><br>\n');
  document.write('<a href="http://whereis.mit.edu/map-jpg?mapterms=w85">540 Memorial Drive Cambridge MA 02139</a><br>\n');
  document.write('<a href="http://web.mit.edu/westgate/staffgov.html">Contact staff</a></td>\n');
  document.write('</tr></table>\n');
  document.write('</div>\n');
}

function leftonlynav(leftNavMenu) {
  commonnav(leftNavMenu,null,null);
}

// This function creates a gallery of thumbnail images
var galleryarray;
function drawgallery() {
  for (n = 0; n < galleryarray.length; n++) {
    if (galleryarray[n].length > 0) {
      document.write('<div class="galimg"><table><tr><td><a href="large.html?');
      document.write(galleryarray[n][2]);
      document.write('">\n');
      document.write('<img src="');
      document.write(galleryarray[n][3]);
      document.write('"></a></td></tr></table>\n</div>\n');
    }
  }
}

// This function is called in the header of the file which displays a large image
var byline;
function gallargehead() {
  byline = '';
  // Interpret image name
  if (self.location.search)
    imgname = self.location.search.substring(1);
  // Set image as background
  document.write('<style>\n');
  document.write('body { background-image: url(' + imgname + '); }\n');
  document.write('</style>');
  // Preload image
  imgpreload = new Image();
  imgpreload.src = imgname;
  // Fix spaces in image name to find in list
  posn = imgname.indexOf("%20");
  while (posn > -1) {
    imgname = imgname.substring(0,posn) + " " + imgname.substring(posn+3); 
    posn = imgname.indexOf("%20");
  }
  // Find image in gallery list
  for (n = 0; n < galleryarray.length; n++)
    if (galleryarray[n][2] == imgname) break;
  // If image was found
  if (galleryarray[n][2] == imgname) {
    if (galleryarray[n].length > 4)
      byline = galleryarray[n][4];
    // Set link to previous image
    if (n > 0 && galleryarray[n-1].length > 0) {
      previmg = galleryarray[n-1][2];
      prevpreload = new Image();
      prevpreload.src = previmg;
      previmg = "large.html?" + previmg;
    }
    // Set link to previous image
    if (n+1 < galleryarray.length && galleryarray[n+1].length > 0) {
      nextimg = galleryarray[n+1][2];
      nextpreload = new Image();
      nextpreload.src = nextimg;
      nextimg = "large.html?" + nextimg;
    }
  }
}

// This function creates the controls for the large image display
function gallargecontrols() {
  // Link to previous image
  if (previmg.length > 0) document.write('<a href="javascript:location.replace(previmg)">');
  document.write('&lt;&lt; previous image');
  if (previmg.length > 0) document.write('</a>');
  // Link to save current image
  document.write(' | <a href="' + imgname + '"> save/print image </a> | ');
  // Link to next image
  if (nextimg.length > 0) document.write('<a href="javascript:location.replace(nextimg)">');
  document.write('next image &gt;&gt;</a>');
  if (nextimg.length > 0) document.write('</a>');
}

// This function creates the "by-line" for the photo
function galbyline() {
  document.write(byline);
}