var menu_sel; // currently selected menu


// ===  class MenuItem definition  ========================================

function MenuItem (name, href, tag) { // Defines object type Menu
  this.name = name;
  this.href = href;
  this.tag = tag;
  this.submenu = [];
}


MenuItem.prototype.addSubmenu = function (item) { // method to add a submenu to an MenuItem object
  if (item.constructor != MenuItem) { return; }
  this.submenu.push(item);
  return item;
};


MenuItem.prototype.create = function (level) { // method to display a MenuItem
  if (level === undefined) { level = 0; }
  if (level === 0) { document.write ("<LI CLASS=\"first\">"); } else { document.write("<LI CLASS=\"level\">"); }
//  if (this.submenu.length) { document.write("&nbsp;&nbsp;&nbsp;&nbsp;"); }
  if (this.href && (this.tag != menu_sel) ) { document.write("<A HREF=\""+this.href+"\">"); } else { document.write ("<I>"); }
  if (!level) { document.write("<BAR>"); }
  document.write (this.name);
  if (!level) { document.write("</BAR>"); }
  if (this.href && (this.tag != menu_sel)) { document.write("</A>"); } else { document.write ("</I>"); }
  if (this.submenu.length) {
    document.write("<UL>");
    for (var x in this.submenu) { if (this.submenu[x].constructor == MenuItem) { this.submenu[x].create(++level); } }
    document.write("</UL>");
  }
  document.write ("</LI>");
};


// ===  Define object MenuSettings  ====================================

function MenuSettings () { // Defines object MenuSettings
  this.CookieName = "MenuState";
  this.CookiePath = "";
  this.closed = "url(images/right.png)";
  this.open = "url(images/down.png)";
  this.link = "url()";
  this.containerul = "containerul";
  this.imagewidth = "12px";
  this.xshift = "11px";
  this.yshiftfirst = "8px";
  this.yshiftother = "3px";
}


MenuSettings.prototype.get = function (options) {
  if ((options.constructor == String) && (this.hasOwnProperty(options))) {
    switch (options) {
      case "CookieName":
      return this.CookieName;
      case "CookiePath":
      return this.CookiePath;
      case "closed":
      return this.closed.substring(4,this.closed.length-1);
      case "open":
      return this.open.substring(4,this.open.length-1);
      case "link":
      return this.link.substring(4,this.link.length-1);
      case "containerul":
      return this.containerul;
      case "imagewidth":
      return this.imagewidth;
      case "xshift":
      return this.xshift;
      case "yshiftfirst":
      return this.yshiftfirst;
      case "yshiftother":
      return this.yshiftother;
    }
  } else { return; }
};


MenuSettings.prototype.set = function (settings) {
  if (! settings.constructor == String) { return; }
  var settingsArray = settings.split(";");
  for(var j in settingsArray) {
    if (settingsArray[j].constructor == String) {
      var set = settingsArray[j].split("=")[0].replace(/ /g,"");
      if (! this.hasOwnProperty(set)) { continue; }
      var val = settingsArray[j].split("=")[1].replace(/ /g,"");
      switch (set) {
      case "CookieName":
	if (val === undefined) { val = this.get("CookieName"); } // name of cookie storing menu state
	this.CookieName = val;
	break;
      case "CookiePath":
	if (val === undefined) { val = this.get("CookiePath"); }		 // path for cookie
	this.CookiePath = val;
	break;
      case "closed":
	if (val === undefined) { val = this.get("closed"); } // image for closed submenu
	/*	  var tmp = this.closed;
	  val = tmp.substring(4,tmp.length-1);
	  document.write("closed: "+val+"<BR>");
	  }*/
	this.closed = "url("+val+")";
	break;
      case "open":
	if (val === undefined) { val = this.get("open"); }	 // image for open submenu
	/*	  var tmp = this.open;
	  val = tmp.substring(4,tmp.length-1);
	  document.write("open: "+val+"<BR>");
	  }*/
	this.open = "url("+val+")";
	break;
      case "link":
	if (val === undefined) { this.get("link"); }	 // image for closed submenu
	/*	  var tmp = this.link;
	  val = tmp.substring(4,tmp.length-1);
	  document.write("link: "+val+"<BR>");
	  }*/
	this.link = "url("+val+")";
	break;
      case "containerul":
	if (val === undefined) { val = this.get("containerul"); }	  // ID for menu container UL
	this.containerul = val;
	break;
      case "imagewidth":
	if (val === undefined) { val = this.get("imagewidth"); }	// width of menu images
	this.imagewidth = val;
	break;
      case "xshift":
	if (val === undefined) { val = this.get("xshift"); }	// horiz. shift for submenus
	this.xshift = val;
	break;
      case "yshiftfirst":
	if (val === undefined) { val = this.get("yshiftfirste"); }	// horiz. shift for submenus
	this.yshiftfirst = val;
	break;
      case "yshiftother":
	if (val === undefined) { val = this.get("yshiftother"); }	// horiz. shift for submenus
	this.yshiftother = val;
	break;
      }
    }
  }
};


// ===  Define object Menu  ============================================

function Menu () { // Defines object Menu
  this.menuitems = [];		// contains sub menu (of type MenuItem)
  this.settings = new MenuSettings();
}

Menu.prototype.configure = function (string) {
  if (string.constructor == String) { this.settings.set(string); }
};

Menu.prototype.writeconfig = function () {
  var prop = ["CookieName", "CookiePath",
	      "closed", "open", "link",
	      "containerul", "imagewidth","xshift", "yshiftfirst",  "yshiftother"];
  for (var x in prop) {
    document.write (prop[x]+": "+this.settings.get(prop[x])+"<BR>");
  }
};


Menu.prototype.addMenu = function (item) { // method to add an item to a Menu
  if (item.constructor == MenuItem) { this.menuitems.push(item); }
};


Menu.prototype.writeCookie = function (li_elements) { // write cookie storing state of submenus
  // Runs through the menu and puts the "states" of each nested list into an array,
  // the array is then joined together and assigned to a cookie.
  var cookieArray = [];
  for(var q=0; q<li_elements.length; q++) {
    if (li_elements[q].childNodes.length > 0) {
      if (li_elements[q].childNodes[0].nodeName == "SPAN" &&
	  li_elements[q].getElementsByTagName("ul").length>0) {
        cookieArray[cookieArray.length] =
	  (li_elements[q].getElementsByTagName("ul")[0].style.display=="block");
      }
    }
  }
  document.cookie = this.settings.get("CookieName") + "=" + cookieArray.join(",") +
    ";expires=" + new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString() +
  (this.settings.get("CookiePath") ? ";path="+this.settings.get("CookiePath") : "");
};


Menu.prototype.showhide = function (el) { // function that switches a submenu
  var ul_el_style = el.getElementsByTagName("ul")[0].style;
  ul_el_style.display =
    (ul_el_style.display=="block") ? "none" : "block";
  el.getElementsByTagName("span")[0].style.backgroundImage =
  (ul_el_style.display=="block") ? this.settings.open : this.settings.closed;
};


Menu.prototype.create = function () { // method to create, display, and activate a menu for an "Menu" object
  var MenuObj = this;
  var settings = MenuObj.settings;
  var containerul = settings.get("containerul");
  document.write ("<STYLE>");
  document.write ("#"+containerul+", #"+containerul+" * ul {\n" +
		  //		  "  text-align:right;\n" +
		  "  margin:0px "+settings.get("xshift")+" 4px 0px;\n" +
		  //		  "  margin:8px 14px 4px 0px;\n" +
		  // (top right bottom left) Removes browser default margins applied to the lists.
		  "  padding:0px 0 0px 0;\n" + // Removes browser default padding applied to the lists.
		  "}");
  document.write ("#"+containerul+" li.first {\n" +
		  "  text-align:right;\n" +
		  "  margin: "+settings.get("yshiftfirst")+" -"+settings.get("xshift")+" 0px 0px;\n" +
		  //		  "  margin:0px 0px 8px 0px;\n" +
		  // A left margin to indent the list items and give the menu a sense of structure.
		  "  padding:0px 0 0px 0;\n" + // Removes browser default padding applied to the list items.
		  "  list-style-type:none;\n" +
		  // Removes the bullet point that usually goes next to each item in a list.
		  "}");
  document.write ("#"+containerul+" li.level {\n" +
		  "  text-align:right;\n" +
		  "  margin: "+settings.get("yshiftother")+" 0px 0px 0px;\n" +
		  //		  "  margin:0px 0px 8px 0px;\n" +
		  // A left margin to indent the list items and give the menu a sense of structure.
		  "  padding:0px 0 0px 0;\n" + // Removes browser default padding applied to the list items.
		  "  list-style-type:none;\n" +
		  // Removes the bullet point that usually goes next to each item in a list.
		  "}");
  document.write ("#"+containerul+" span.first {\n" +
		  /* Various styles to position the symbols next to the items in the menu. */
		  "  text-align:right;\n" +
		  "  margin-right: 0px !important;\n" +
		  "  position: relative;\n" +
		  "  float: right;\n" +
		  "  width: "+MenuObj.settings.get("imagewidth")+";\n" +
		  "  height:1.5em;\n" +
		  "  background-position: 100% 50%;\n" +
		  "  background-repeat:no-repeat;\n" +
		  "}");
  document.write ("#"+containerul+" span.level {\n" +
		  "  text-align:right;\n" +
		  // Various styles to position the symbols next to the items in the menu.
		  "  position: relative;\n" +
		  "  float: right;\n" +
		  "  width: "+MenuObj.settings.get("imagewidth")+";\n" +
		  "  height:1em;\n" +
		  "  background-position: 100% 50%;\n" +
		  "  background-repeat:no-repeat;\n" +
		  "}");
  document.write ("</STYLE>");
  document.write ("<UL id=\""+containerul+"\">");
  for (var x in MenuObj.menuitems) {
    if (MenuObj.menuitems[x].constructor == MenuItem) { MenuObj.menuitems[x].create(); }
  }
  document.write ("</UL>");

  // initiates menus ...
  var cookieCount = 0;
  if(document.cookie) { // reads existing cookie giving state of submenus ...
    var cookieArray=document.cookie.split(";");
    var cookieArray2 = [];
    for(var j in cookieArray){
      if (cookieArray[j].constructor == String) {
	cookieArray2[cookieArray[j].split("=")[0].replace(/ /g,"")] =
	  cookieArray[j].split("=")[1].replace(/ /g,"");
      }
    }
  }
  cookieArray = (document.cookie.indexOf(MenuObj.settings.get("CookieName")+"=")>=0) ?
                   cookieArray2[MenuObj.settings.get("CookieName")].split(",") : [];
  var container_UL = document.getElementById(containerul);
  // get menu container ul with ID containerul
  var li_elements = container_UL.getElementsByTagName("li");	// get list of LI elements
  var symbols, span_el;

  for(var o=0; o<li_elements.length; o++) { // loops through all <li>
    var li_element = li_elements[o];
    //    if (li_element.className == "first")
    //      { symbols = "Fsymbols"; } else { symbols = "symbols"; }
    symbols = li_element.className;
    if (li_element.getElementsByTagName("ul").length > 0) {
      // if <li> element contains a submenu (<ul> ...
      span_el = document.createElement("span");
      span_el.className = symbols;
      span_el.style.backgroundImage = (cookieArray.length>0) ?
	( (cookieArray[cookieCount]=="true") ? MenuObj.settings.open : MenuObj.settings.closed) : MenuObj.settings.closed;
      span_el.onclick = function () { MenuObj.showhide(this.parentNode); MenuObj.writeCookie(li_elements); };
      li_element.insertBefore(span_el,li_element.firstChild);
      li_element.getElementsByTagName("ul")[0].style.display = "none";
      if(cookieArray[cookieCount]=="true") {
        MenuObj.showhide(li_element);
      }
      cookieCount++;
    } else {
      span_el = document.createElement("span");
      span_el.className = symbols;
      span_el.style.backgroundImage = MenuObj.settings.link;
      li_element.insertBefore(span_el,li_element.firstChild);
    }
  }
};


