/************************************************************************

* Code to drive tree menu                                               *

*************************************************************************/



// Adapted from University of Delaware and Morten's JavaScript Tree Menu

//http://www.treemenu.com/



/************************************************************************

* Define the MenuItem object.                                           *

*************************************************************************/

// Constructor

function MenuItem(text, url, target, icon) {

  this.text = text;

  if (url)

    this.url = url;

  else

    this.url = "";

  if (target)

    this.target = target;

  else

    this.target = "";

  if (icon)

    this.icon      = icon;

  else

    this.icon      = "";



  this.submenu     = null;

  this.expanded    = false;

  this.makeSubmenu = makeSubmenu;

}



// Methods.

function makeSubmenu(menu) {

  this.submenu = menu;

}





/***********************************************************************

* Define the Menu object.                                              *

************************************************************************/

// Constructor.

function Menu() {

  this.items   = new Array();

  this.addItem = addItem;

}

// Methods.

function addItem(item) {

  this.items[this.items.length] = item;

}





/***********************************************************************

* Functions to draw the menu.                                          *

************************************************************************/

// Global variables used to draw the menu.

// Width of the menu frame in pixels.

var width = 400;



// Increase if any of the

// menu text wraps and the menu becomes broken.



// Declare Menu Frame Document

var doc = parent.frames["menu"].document;



var level;



// List for menu bar images.

var bar = new Array();



// Array for tracking which item we are on in each level.

var indices = new Array();



function displayMenu(menu) {

  level = 0;

  doc.open("text/html", "replace");

  doc.writeln('<link rel="stylesheet" type="text/css" href="tree.css">');

  doc.writeln('<body bgcolor="#ffffff" text="#000000" link="#0000ff" alink="#ff0000" vlink="#0000ff">');

  doc.writeln('<table width=550 cellpadding=0 cellspacing=0 border=0>');

  doc.writeln('<tr>');

  doc.writeln('<td align=right><img border="0" src="images/2udlogoa.gif" width="528" height="68"></td>');

  doc.writeln('</tr>');

  doc.writeln('<tr>');

  doc.writeln('<td align=right><img border="0" src="images/2udlogob.gif" width="143" height="19"><a href="http://www.continuingstudies.udel.edu/home/registration.shtml"><img border="0" src="images/2udlogoc.gif" width="74" height="19"></a><a href="http://www.continuingstudies.udel.edu/home/maps.shtml"><img border="0" src="images/2udlogod.gif" width="35" height="19"></a><a href="http://www.continuingstudies.udel.edu/home/webwho.shtml"><img border="0" src="images/2udlogoe.gif" width="60" height="19"></a><a href="http://www.continuingstudies.udel.edu/"><img border="0" src="images/2udlogof.gif" width="38" height="19"></a><a href="http://www.continuingstudies.udel.edu/home/search.shtml"><img border="0" src="images/2udlogog.gif" width="47" height="19"></a><a href="http://www.continuingstudies.udel.edu/home/sitemap.shtml"><img border="0" src="images/2udlogoh.gif" width="51" height="19"></a><a href="http://www.continuingstudies.udel.edu/home/contact.shtml"><img border="0" src="images/2udlogoi.gif" width="68" height="19"></a><img border="0" src="images/dot.gif" width="10" height="19"></td>');

  doc.writeln('</tr>');

  doc.writeln('<tr>');

  doc.writeln('</tr>');

  doc.writeln('<tr>');

  //doc.writeln('<td align=right><img src="images/moved.gif" width="300" height="50" border="0"></td>');

  doc.writeln('<td align=right><img src="images/moved.gif" width="300" height="50" border="0" usemap="#movedMap"><map name="movedMap"><area shape="rect" coords="1,20,298,47" href="http://www.continuingstudies.udel.edu" target="_blank"></map></td>');

  doc.writeln('</tr>');

  doc.writeln('<tr>');

  doc.writeln('<td align=right><br><br></td>');

  doc.writeln('</tr>');

  doc.writeln('<tr>');

  doc.writeln('<td valign=top align="left"><font class="black">Please use the site map below to navigate our new site.');

  doc.writeln('<br>');

  doc.writeln('If you need further assistance,');

  doc.writeln('contact <a href="mailto:ce-webmaster@udel.edu" class="big">&nbsp;ce-webmaster@udel.edu.</a></font></td>');

  doc.writeln('</tr>');

  doc.writeln('<tr>');

  doc.writeln('<td align=left><br><br></td>');

  doc.writeln('</tr>');

  doc.writeln('</table>');

  doc.writeln('<left>');

  doc.writeln('<P>');

  doc.writeln('<blockquote>');

  doc.writeln('<table border=0 cellpadding=0 cellspacing=0 width=' + width + '>');

  doc.writeln('<tr valign=top><td><img src="images/menu_root.gif" align=left border=0 vspace=0 hspace=0><font face="Arial" size=2>&nbsp;Professional and Continuing Studies</font></td></tr>');

  listItems(menu);

  doc.writeln('</table>');

  doc.writeln('</blockquote>');

  doc.close();

  return false;

}



function listItems(menu) {

  var i;

  for (i = 0; i < menu.items.length; i++) {

    indices[level] = i;

	displayItem(menu.items[i], (i == menu.items.length - 1));

  }

}





function displayItem(item, last) {

  var frm = "parent.frames['code']";

  var cmd = frm + ".displayMenu(" + frm + ".menu)";

  var ref = frm + ".menu.items[" + indices[0] + "]";

  var i, img, more;



  // Start the table row.

  doc.write('<tr valign=top><td>');



  // Draw descending bars from upper levels.

  for (i = 0; i < level; i++)

    if (bar[i])

      doc.write('<img src="images/menu_bar.gif" align=left border=0 vspace=0 hspace=0>');

    else

      doc.write('<img src="images/menu_pixel.gif" align=left border=0 vspace=0 hspace=0 width=18 height=18>');



  // Determine this is a submenu item that contains other submenus.

  more = false;

  if (item.submenu)

    for (i = 0; i < item.submenu.items.length; i++)

      if (item.submenu.items[i].submenu != null)

        more = true;



  // Draw tee bar or corner if this item is not a submenu or if it is a

  // submenu but doesn't contain other submenus.

  if (!more) {

    if (last)

      img = "images/menu_corner.gif";

    else

      img = "images/menu_tee.gif";

    doc.write('<img src="' + img + '" align=left border=0 vspace=0 hspace=0>');

  }



  // Set up a reference to this item for the onCLick event.

  for (i = 1; i <= level; i++)

    ref += ".submenu.items[" + indices[i] + "]";



  // Write the start of the link tag so all of the following images and text

  // will be clickable.

  if (item.url != "")

    doc.write('<a href="' + item.url + '" target="' + item.target + '" >');

  else {

    if (item.expanded)

      doc.write('<a class="black" href="#" onClick="' + ref + '.expanded = false; return ' + cmd + ';">');

    else

      doc.write('<a class="black" href="#" onClick="' + ref + '.expanded = true; return ' + cmd + ';">');

  }



  // For a submenu item that contains other submenus, draw a tee bar or corner

  // with a plus or minus sign.

  if (more) {

    if (item.expanded) {

      if (last)

        img = "images/menu_corner.gif";

      else

        img = "images/menu_tee.gif";

    }

    else {

      if (last)

        img = "images/menu_corner.gif";

      else

        img = "images/menu_tee.gif";

    }

    doc.write('<img src="' + img + '" align=left border=0 vspace=0 hspace=0>');

  }



  // If the item is a submenu, draw an open or closed folder icon. Otherwise

  // draw a link icon.

  if (item.submenu) {



    if (item.expanded)

      img = "images/menu_folder_open.gif";

    else

      img = "images/menu_folder_closed.gif";

  }

  else {

    if (item.icon != "")

      img = item.icon;

    else if (item.url.indexOf("http://") == 0)

      img = "images/menu_link_local.gif";

    else

      img = "images/menu_link_local.gif";

  }

  doc.write('<img src="' + img + '" align=left border=0 vspace=0 hspace=0>');

  // Write the link text and finish the link and table row.

  doc.write('<font face="Arial" size=1>&nbsp;' + item.text + '</font>');

  doc.write('</a>');

  doc.writeln('</td></tr>');



  // If the item is a submenu and it is expanded, make a recursive call to

  // draw its item list.

  if (item.expanded) {

    if (!last)

      bar[level] = true;

    else

      bar[level] = false;

    level++;

    listItems(item.submenu);

    level--;

  }

  else

    bar[i] = false;

}
