/*************************
 * Application constants *
 *************************/

// used to configure the left-hand navigation menus
//
var NAV_MENU = new Array (
  {label:"Home", buttonUrl:"welcome.html", submenu:null},
  {label:"News", buttonUrl:"news.html", submenu:null},
  {label:"About Glacier", buttonUrl:null, submenu:new Array (
    {label:"Contact Us", buttonUrl:"contact_us.html", submenu:null},
    {label:"Facility Information", buttonUrl:"facility_information.html", submenu:null},
    {label:"History", buttonUrl:"history.html", submenu:null},
    {label:"Photo Gallery", buttonUrl:"photo_gallery.html", submenu:null}
  )},
  {label:"Container Hauling", buttonUrl:"container_hauling.html", submenu:null},
  {label:"C & D Recycling", buttonUrl:null, submenu:new Array (
    {label:"Recycling Process", buttonUrl:"recycling_process.html", submenu:null},
    {label:"What We Recycle", buttonUrl:null, submenu:new Array (
      {label:"Wood", buttonUrl:"wood_recycling.html", submenu:null},
      {label:"Concrete", buttonUrl:"concrete.html", submenu:null},
      {label:"Stumps and Land Clear", buttonUrl:"stumps_and_land_clear.html", submenu:null},
      {label:"Asphalt Roofing", buttonUrl:"asphalt_roofing.html", submenu:null},
      {label:"Metals", buttonUrl:"metals.html", submenu:null},
      {label:"Plastics", buttonUrl:"plastics.html", submenu:null},
      {label:"Cardboard - OCC", buttonUrl:"cardboard_occ.html", submenu:null},
      {label:"Carpet Pad", buttonUrl:"carpet_pad.html", submenu:null},
      {label:"Comingled Debris", buttonUrl:"comingled_construction_debris.html", submenu:null}
    )},
    {label:"Green Building", buttonUrl:"green_building.html", submenu:null}
  )},
  {label:"Wood Recycling", buttonUrl:"wood_recycling.html", submenu:new Array (
    {label:"Pulp Furnish", buttonUrl:"pulp_furnish.html", submenu:null},
    {label:"Biomass Fuels", buttonUrl:"biomass_fuels.html", submenu:null},
    {label:"Mulches", buttonUrl:"mulches.html", submenu:null}
  )},
  {label:"Recycling Facts", buttonUrl:"recycling_facts.html", submenu:null},
  {label:"F. A. Q.", buttonUrl:"faq.html", submenu:new Array (
    {label:"Glossary of Terms", buttonUrl:"glossary.html", submenu:null}
  )},
  {label:"Links", buttonUrl:"links.html", submenu:null}
);

// used to configure the right-hand extras content
//
var EXTRAS_MENU = new Array (
  {label:"Glacier in the News", buttonUrl:"news_utc_in_abbeyance.html?pageNo=1", submenu:new Array (
    {label:null, buttonUrl:"news_utc_in_abbeyance.html?pageNo=1", imgPath:"images/mini_glacier_in_the_news.gif"}
  )},
  {label:"Facility Information", buttonUrl:"facility_information.html?pageNo=3", submenu:new Array (
    {label:null, buttonUrl:"facility_information.html?pageNo=3", imgPath:"images/mini_auburn_map.png"}
  )},
  {label:"Container Hauling", buttonUrl:"container_hauling.html?pageNo=6", submenu:new Array (
    {label:null, buttonUrl:"container_hauling.html?pageNo=6", imgPath:"images/mini_glacier_9_19_003.png"}
  )},
  {label:"Wood Recycling", buttonUrl:"wood_recycling.html?pageNo=10", submenu:new Array (
    {label:null, buttonUrl:"wood_recycling.html?pageNo=10", imgPath:"images/mini_glacier_9_19_008.png"}
  )},
  {label:"Glacier At Work", buttonUrl:"photo_gallery.html?pageNo=5", submenu:new Array (
    {label:null, buttonUrl:"photo_gallery.html?pageNo=5", imgPath:"images/mini_gaw.jpg"}
  )}
);

/*
 * -------- No user-serviceable parts below. DO NOT EDIT! --------
 */

var NAV_ARROW_RIGHT = 0;
var NAV_ARROW_DOWN = 1;

var NAV_ARROWS = new Array ("images/nav_arrow_right.png", "images/nav_arrow_down.png");

var formData = form_decode();
var currPageBuildNo = 0;
var pageNo = (formData.pageNo != null) ? formData.pageNo : 0;
var currCollMenuNo = 0;
var isOpen = (formData.isOpen != null) ? formData.isOpen : -1;

/***************************
 * HTML creation functions *
 ***************************/

// Create all navigation buttons. use this in all web pages to create the left hand navigation buttons.
//
// parentObj - the <div> element that will contain the navigation menu
//
function createAllNav (parentObj) {
  var parentObj = getElementById (parentObj);
  
  var nc = document.createElement ("div");
  nc.className = "nav_container";
  parentObj.appendChild (nc);
  
    var nbc = document.createElement ("div");
    nbc.className = "nav_buttons_container";
    nc.appendChild (nbc);
    
      buildNavHeaderTier (nbc);
}

// Function to build the top level navigation headers for the navigation menu. recursively calls a function to 
// create the underlying sections.
//
// parentObj - the <div> element that will contain the navigation headers
//
function buildNavHeaderTier (parentObj) {
  var parentObj = getElementById (parentObj);

  for (var i = 0; i < NAV_MENU.length; i++) {
    var mItem = NAV_MENU[i];
    
    if (pageNo == currPageBuildNo && mItem.buttonUrl != null) {
      createNavSectionHeaderHighlight (parentObj, mItem.label);
      currPageBuildNo++;
    } else {
      createNavSectionHeader (parentObj, mItem.label, mItem.buttonUrl);
    }

    createNavSeparator (parentObj);

    if (mItem.submenu != null && mItem.submenu.length > 0)
      buildNavSectionTier (parentObj, mItem.submenu, isOpen);
  }
}

// Function to build the sections for the navigation menu. recursively calls a function to create the subsections.
//
// parentObj - the <div> element that will contain the navigation sections
//
function buildNavSectionTier (parentObj, submenu, isOpen) {
  var parentObj = getElementById (parentObj);
  
  for (var i = 0; i < submenu.length; i++) {
    var mItem = submenu[i];
    
    var arrow = (mItem.submenu != null && mItem.submenu.length > 0)
      ? (currCollMenuNo == isOpen) ? NAV_ARROW_DOWN : NAV_ARROW_RIGHT
      : null;

    if (pageNo == currPageBuildNo && mItem.buttonUrl != null) {
      createNavSectionHighlight (parentObj, mItem.label);
      currPageBuildNo++;
    } else {
      createNavSection (parentObj, mItem.label, arrow, mItem.buttonUrl);
    }

    createNavSeparator (parentObj);
    
    if (mItem.submenu != null && mItem.submenu.length > 0 && arrow == NAV_ARROW_DOWN) {
      buildNavSubsectionTier (parentObj, mItem.submenu);
      createNavSeparator (parentObj);
    }
  }
}

// Function to build the subsections for the navigation menu.
//
// parentObj - the <div> element that will contain the navigation subsections
//
function buildNavSubsectionTier (parentObj, submenu) {
  var parentObj = getElementById (parentObj);
  
  for (var i = 0; i < submenu.length; i++) {
    var mItem = submenu[i];
    
    if (pageNo == currPageBuildNo && mItem.buttonUrl != null) {
      createNavSubsectionHighlight (parentObj, mItem.label);
      currPageBuildNo++;
    } else {
      createNavSubsection (parentObj, mItem.label, mItem.buttonUrl);
    }
  }
}

// Create navigation section header
//
// parentObj - object reference to or string label of containing element
// sLabel    - String containing label for the button
// sUrl      - String containing URL to new location when button is clicked
//
function createNavSectionHeader (parentObj, sLabel, sUrl) {
  var parentObj = getElementById (parentObj);
  
  var headDiv = document.createElement ("div");
  headDiv.className = "nav_button_section_header";
  if (sUrl != null && sUrl.length != 0) {
    headDiv.style.cursor = "pointer";
    headDiv.onmouseover = buttonMouseOverHandler;
    headDiv.onmouseover_color = "white";
    headDiv.onmouseout = buttonMouseOutHandler;
    headDiv.onmouseout_color = "#CCCCFF";
    headDiv.onclick = buttonClickHandler;
    headDiv.onclick_url = sUrl + "?pageNo=" + currPageBuildNo + "&isOpen=" + isOpen;
    currPageBuildNo++;
  }
  parentObj.appendChild (headDiv);
  
    var textSpan = document.createElement ("span");
    textSpan.className = "nav_button_section_header_text";
    headDiv.appendChild (textSpan);
    
      var textNode = document.createTextNode (sLabel);
      textSpan.appendChild (textNode);
}

// Create navigation section header with hightlight (no hyperlink)
//
// parentObj - object reference to or string label of containing element
// sLabel    - String containing label for the button
//
function createNavSectionHeaderHighlight (parentObj, sLabel) {
  var parentObj = getElementById (parentObj);
  
  var headDiv = document.createElement ("div");
  headDiv.className = "nav_button_section_header";
  parentObj.appendChild (headDiv);
  
    var textSpan = document.createElement ("span");
    textSpan.className = "nav_button_section_header_text_hl";
    headDiv.appendChild (textSpan);
    
      var textNode = document.createTextNode (sLabel);
      textSpan.appendChild (textNode);
}

// Create navigation section
//
// parentObj - object reference to or string label of containing element
// sLabel    - String containing label for the button
// iArrowDir - Integer indicating the direction the arrow should point (See arrow directions constants above.) null means no arrow.
// sUrl      - String containing URL to new location when button is clicked
//
function createNavSection (parentObj, sLabel, iArrowDir, sUrl) {
  var parentObj = getElementById (parentObj);
  
  sUrl = (sUrl == null) ? getCurrentPath() : sUrl;
  
  var ion = (iArrowDir == NAV_ARROW_RIGHT) 
              ? currCollMenuNo 
              : (isOpen == currCollMenuNo) ? -1 : isOpen;

  var cpbn = (iArrowDir != null)
              ? pageNo
              : currPageBuildNo;
  
  var sectionDiv = document.createElement ("div");
  sectionDiv.className = "nav_button_section";
  sectionDiv.style.cursor = "pointer";
  sectionDiv.onmouseover = buttonMouseOverHandler;
  sectionDiv.onmouseover_color = "white";
  sectionDiv.onmouseout = buttonMouseOutHandler;
  sectionDiv.onmouseout_color = "#CCCCFF";
  sectionDiv.onclick = buttonClickHandler;
  sectionDiv.onclick_url = sUrl + "?pageNo=" + cpbn + "&isOpen=" + ion;
  parentObj.appendChild (sectionDiv);
  currPageBuildNo++;

  if (iArrowDir != null) {
    var imgSpan = document.createElement ("span");
    imgSpan.className = "nav_button_arrow";
    sectionDiv.appendChild (imgSpan);
    
      var img = document.createElement ("img");
      img.src = NAV_ARROWS[iArrowDir];
      img.border = "0";
      imgSpan.appendChild (img);
      currCollMenuNo++;
  }

    var textSpan = document.createElement ("span");
    textSpan.className = "nav_button_section_text";
    sectionDiv.appendChild (textSpan);
    
      var textNode = document.createTextNode (sLabel);
      textSpan.appendChild (textNode);
}

// Create navigation section with highlight (no hyperlink)
//
// parentObj - object reference to or string label of containing element
// sLabel    - String containing label for the button
// iArrowDir - Integer indicating the direction the arrow should point (See arrow directions constants above.) null means no arrow.
//
function createNavSectionHighlight (parentObj, sLabel) {
  var parentObj = getElementById (parentObj);
  
  var sectionDiv = document.createElement ("div");
  sectionDiv.className = "nav_button_section";
  parentObj.appendChild (sectionDiv);
  
    var textSpan = document.createElement ("span");
    textSpan.className = "nav_button_section_text_hl";
    sectionDiv.appendChild (textSpan);
    
      var textNode = document.createTextNode (sLabel);
      textSpan.appendChild (textNode);
}

// Create navigation subsection
//
// parentObj - object reference to or string label of containing element
// sLabel    - String containing label for the button
// sUrl      - String containing URL to new location when button is clicked
//
function createNavSubsection (parentObj, sLabel, sUrl) {
  var parentObj = getElementById (parentObj);
  
  var sectionDiv = document.createElement ("div");
  sectionDiv.className = "nav_button_subsection";
  if (sUrl != null && sUrl.length != 0) {
    sectionDiv.style.cursor = "pointer";
    sectionDiv.onmouseover = buttonMouseOverHandler;
    sectionDiv.onmouseover_color = "white";
    sectionDiv.onmouseout = buttonMouseOutHandler;
    sectionDiv.onmouseout_color = "#CCCCFF";
    sectionDiv.onclick = buttonClickHandler;
    sectionDiv.onclick_url = sUrl + "?pageNo=" + currPageBuildNo + "&isOpen=" + isOpen;
    currPageBuildNo++;
  }
  parentObj.appendChild (sectionDiv);
  
    var textSpan = document.createElement ("span");
    textSpan.className = "nav_button_subsection_text";
    sectionDiv.appendChild (textSpan);
    
      var textNode = document.createTextNode (sLabel);
      textSpan.appendChild (textNode);
}

// Create navigation subsection with hightlight (no hyperlink)
//
// parentObj - object reference to or string label of containing element
// sLabel    - String containing label for the button
//
function createNavSubsectionHighlight (parentObj, sLabel) {
  var parentObj = getElementById (parentObj);
  
  var subsectionDiv = document.createElement ("div");
  subsectionDiv.className = "nav_button_subsection";
  parentObj.appendChild (subsectionDiv);
  
    var textSpan = document.createElement ("span");
    textSpan.className = "nav_button_subsection_text_hl";
    subsectionDiv.appendChild (textSpan);
    
      var textNode = document.createTextNode (sLabel);
      textSpan.appendChild (textNode);
}

// Create navigation button separator
//
// parentObj - object reference to or string label of containing element
//
function createNavSeparator (parentObj) {
  var parentObj = getElementById (parentObj);

  var separatorDiv = document.createElement ("div");
  separatorDiv.className = "nav_button_separator";
  parentObj.appendChild (separatorDiv);
}

// Function to create the right hand extras buttons.
//
// parentObj - the <div> element that will contain the extras
//
function createAllExtras (parentObj) {
  var parentObj = getElementById (parentObj);
  
  var ec = document.createElement ("div");
  ec.className = "extras_container";
  parentObj.appendChild (ec);
  
    var ecc = document.createElement ("div");
    ecc.className = "extras_content_container";
    ec.appendChild (ecc);
    
    buildExtrasSectionHeaders (ecc);
}

// Function to build the top level labels for the extras. recursively calls a function to create the contents.
//
// parentObj - the <div> element that will contain the extras content
//
function buildExtrasSectionHeaders (parentObj) {
  var parentObj = getElementById (parentObj);
  
  for (var i = 0; i < EXTRAS_MENU.length; i++) {
    var mItem = EXTRAS_MENU[i];
    
    createExtrasSectionHeader (parentObj, mItem.label, mItem.buttonUrl);
    createExtrasSeparator (parentObj);
    
    if (mItem.submenu != null && mItem.submenu.length > 0)
      buildExtrasSections (parentObj, mItem.submenu);
  }
}

// Function to build the extras content.
//
// parentObj - the <div> element that will contain the extras content
//
function buildExtrasSections (parentObj, submenu) {
  var parentObj = getElementById (parentObj);
  
  for (var i = 0; i < submenu.length; i++) {
    var mItem = submenu[i];
    
		if (mItem.imgPath != null && mItem.imgPath != "") {
    	createExtrasImgSection (parentObj, mItem.imgPath, mItem.buttonUrl);
		} else if (mItem.flashPath != null && mItem.flashPath != "") {
    	createExtrasFlashSection (parentObj, mItem.flashPath, mItem.buttonUrl);
		} else {
			// unknown extras type
		}
    createExtrasSeparator (parentObj);
  }
}

// Create extras section header
//
// parentObj - object reference to or string label of containing element
// sLabel    - String containing label for the button
// sUrl      - String containing URL to new location when button is clicked
//
function createExtrasSectionHeader (parentObj, sLabel, sUrl) {
  var parentObj = getElementById (parentObj);
  
  var headDiv = document.createElement ("div");
  headDiv.className = "extras_section_header";
  headDiv.onmouseover = buttonMouseOverHandler;
  headDiv.onmouseover_color = "white";
  headDiv.onmouseout = buttonMouseOutHandler;
  headDiv.onmouseout_color = "#CCCCFF";
  if (sUrl != null && sUrl.length != 0) {
    headDiv.onclick = buttonClickHandler;
    headDiv.onclick_url = sUrl;
  }
  parentObj.appendChild (headDiv);
  
    var textSpan = document.createElement ("span");
    textSpan.className = "extras_section_header_text";
    headDiv.appendChild (textSpan);
    
      var textNode = document.createTextNode (sLabel);
      textSpan.appendChild (textNode);
}

// Create extras image section
//
// parentObj 			- object reference to or string label of containing element
// sImageLocation - String containing label for the button
// sUrl      			- String containing URL to new location when button is clicked
//
function createExtrasImgSection (parentObj, sImageLocation, sUrl) {
  var parentObj = getElementById (parentObj);
  
  var sectionDiv = document.createElement ("div");
  sectionDiv.className = "extras_section";
  sectionDiv.onmouseover = buttonMouseOverHandler;
  sectionDiv.onmouseover_color = "white";
  sectionDiv.onmouseout = buttonMouseOutHandler;
  sectionDiv.onmouseout_color = "#CCCCFF";
  if (sUrl != null && sUrl.length != 0) {
    sectionDiv.onclick = buttonClickHandler;
    sectionDiv.onclick_url = sUrl;
  }
  parentObj.appendChild (sectionDiv);
  
  if (sImageLocation != null) {
    var img = document.createElement ("img");
    img.src = sImageLocation;
    img.border = "0";
    img.className = "extras_img";
    sectionDiv.appendChild (img);
  }
}

// Create extras flash section
//
// parentObj 			- object reference to or string label of containing element
// sImageLocation - String containing label for the button
// sUrl      			- String containing URL to new location when button is clicked
//
function createExtrasFlashSection (parentObj, sSwfLocation, sUrl) {
  var parentObj = getElementById (parentObj);
  
  var sectionDiv = document.createElement ("div");
  sectionDiv.className = "extras_section";
  sectionDiv.onmouseover = buttonMouseOverHandler;
  sectionDiv.onmouseover_color = "white";
  sectionDiv.onmouseout = buttonMouseOutHandler;
  sectionDiv.onmouseout_color = "#CCCCFF";
  if (sUrl != null && sUrl.length != 0) {
    sectionDiv.onclick = buttonClickHandler;
    sectionDiv.onclick_url = sUrl;
  }
  parentObj.appendChild (sectionDiv);
  
  if (sSwfLocation != null) {
    //var img = document.createElement ("img");
    //img.src = sImageLocation;
    //img.border = "0";
    //img.className = "extras_img";
    //sectionDiv.appendChild (img);
		
		var htmlObject = document.createElement ("object");
  	htmlObject.classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
    htmlObject.codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0";
    htmlObject.width="173";
    htmlObject.height="98";
    htmlObject.id="extras_flash"; // this is supposed to be the flash file name without the .swf extension but seems to work fine without it.
    htmlObject.align="middle";
		
/*
		// need to figure out why neither of these two solutions works. Look into DW and FL scripted version
		//
		htmlObject.innerHtml = 
		
      '<param name="allowScriptAccess" value="sameDomain" />' +
      '<param name="allowFullScreen" value="false" />' +
      '<param name="movie" value="mini_glacier_in_the_news.swf" />' +
      '<param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />' +
      '<embed ' +
      '  src="mini_glacier_in_the_news.swf" ' +
      '  quality="high" ' +
      '  bgcolor="#ffffff" ' +
      '  width="173" ' +
      '  height="98" ' +
      '  name="blah" ' +
      '  align="middle" ' +
      '  allowScriptAccess="sameDomain" ' +
      '  allowFullScreen="false" ' +
      '  type="application/x-shockwave-flash" ' +
      '  pluginspage="http://www.macromedia.com/go/getflashplayer"' +
      '/>';

			var p

			p = document.createElement ("param");
			p.name = "allowScriptAccess";
			p.value = "sameDomain";
			htmlObject.appendChild (p);

			p = document.createElement ("param");
			p.name = "allowFullScreen";
			p.value = "false";
			htmlObject.appendChild (p);

			p = document.createElement ("param");
			p.name = "movie";
			p.value = sSwfLocation;
			htmlObject.appendChild (p);

			p = document.createElement ("param");
			p.name = "quality";
			p.value = "high";
			htmlObject.appendChild (p);

			p = document.createElement ("param");
			p.name = "bgcolor";
			p.value = "#ffffff";
			htmlObject.appendChild (p);
			
			var htmlEmbed = document.createElement ("embed");
      htmlEmbed.src=sSwfLocation;
      htmlEmbed.quality="high";
      htmlEmbed.bgcolor="#ffffff";
      htmlEmbed.width="173";
      htmlEmbed.height="98";
      htmlEmbed.name="mini_glacier_in_the_news";
      htmlEmbed.align="middle";
      htmlEmbed.allowScriptAccess="sameDomain";
      htmlEmbed.allowFullScreen="false";
      htmlEmbed.type="application/x-shockwave-flash";
      htmlEmbed.pluginspage="http://www.macromedia.com/go/getflashplayer";
			htmlObject.appendChild (htmlEmbed);
*/
    sectionDiv.appendChild (htmlObject);
  }
}

// Create extras separator
//
// parentObj - object reference to or string label of containing element
//
function createExtrasSeparator (parentObj) {
  var parentObj = getElementById (parentObj);

  var separatorDiv = document.createElement ("div");
  separatorDiv.className = "extras_separator";
  parentObj.appendChild (separatorDiv);
}

/******************
 * Event Handlers *
 ******************/

// Button mouseover event handler
//
function buttonMouseOverHandler (evt) {
  if (! evt)
    var evt = window.event;
  
  this.style.color = this.onmouseover_color;
}

// Button mouseout event handler
//
function buttonMouseOutHandler (evt) {
  if (! evt)
    var evt = window.event;
  
  this.style.color = this.onmouseout_color;
}

// Button click event handler
//
function buttonClickHandler (evt) {
  location = this.onclick_url;
}

/*********************
 * Support functions *
 *********************/

// Browser independent way of finding specific elements by their ID tags.
//
function getElementById (e) {
  if (typeof (e) == 'string') {
    if (document.getElementById) 
      e = document.getElementById (e);
    else 
      if (document.all) 
        e = document.all[e];
      else 
        e = null;
  }
  return e;
}

function getCurrentPath() {
  var fullPath = document.location.href;
  
  return (fullPath.indexOf ("?") > 0) ? fullPath.substr (0, fullPath.indexOf ("?")) : fullPath;
}

// the javascript escape() function converts spaces to '+'s for some reason.
// this can be a pain because the unescape() function does not convert '+'s
// back to spaces. this function is used to convert '+'s to '%20's (the
// REAL code for an escaped space character.)
//
function unplus(str) {
  var newstr = '';

  for (var l = 0; l < str.length; l++) {
    var c = str.charAt(l);
    if (c == '+') {
      newstr += '%20';
    } else {
      newstr += c;
    }
  }
  return newstr;
}

// Decodes form data (typically used on document.location.search)
//
// Returns an associative array containing form key/value pairs.
//
function form_decode() {
  var str = document.location.search;
  if (str.charAt(0) == '?')
    str = str.substr(1);
  var a = str.split('&');
  var myform = new Object();
  for (var l = 0; l < a.length; l++) {
    var apos = a[l].indexOf('=');
    var n = unescape(unplus(a[l].substring(0,apos)));
    var v = unescape(unplus(a[l].substring(apos + 1)));
    myform[n] = v;
  }
  return myform;
}

// AJAX component. May not be used.
//
function getXmlHttpRequest() {
	var objXMLHttp = null;

	try {
		objXMLHttp = new XMLHttpRequest();
	} catch (tryMS) {
		try {
			objXMLHttp = new ActiveXObject ("Msxml2.XMLHTTP");
		} catch (tryOtherMS) {
			try {
				objXMLHttp = new ActiveXObject ("Microsoft.XMLHTTP");
			} catch (failed) {
				objXMLHttp = null;
			}
		}
	}

	return objXMLHttp;
}
