/* Encapsulation */ (function() {



// --------
// PopupNav
// --------

function PopupNav(tree) {
  this.tree = tree;
  this.lookup = [];
  this.parent = null;
  this.index = 0;
  this.updateHandle = 0;
}



// process
//

PopupNav.prototype.process = function(context, instructions) {
  if (!context) context = document;
  if (!instructions) instructions = this.tree;

  var data, i=0;
  while (data = instructions[i++]) {
    var c=0;
    var matches = this.matchContext(context, data);
    while (context = matches[c++]) {
      this.processInstruction(context, data);
    }
  }
}



// matchContext
//

PopupNav.prototype.matchContext = function(context, data) {
  if (data.id) {
    var match = document.getElementById(data.id);
    return match ? [match] : [];
  }
  else if (data.elem && data.className) {
    var matches = [];
    var elemList = context.getElementsByTagName(data.elem);

    var elem, z=0;
    while (elem = elemList[z++]) {
      if (elem.className.indexOf(data.className) != -1) matches.push(elem);
    }

    return matches;
  }

  return [];
}



// processInstruction
//

PopupNav.prototype.processInstruction = function(context, data) {
  if (data.popup) {
    if (typeof(data.popup.index) == "undefined") {
      data.popup.index = this.lookup.length;
      this.lookup.push(data.popup);
    }

    // Get the anchors in the menu container
    var anchors = context.getElementsByTagName("a");

    // Assign the mouseovers and mouseouts
    var anchor, i=0;
    while (anchor = anchors[i++]) {
      anchor.popupNav = this;
      anchor.popupNavIndex = data.popup.index;
      anchor.onmouseover = this.onLinkMouseover;
      anchor.onmouseout = this.onLinkMouseout;
    }
  }

  if (data.nav) this.process(context, data.nav);
}



// update
//

PopupNav.prototype.update = function(parentMenuId, childMenuId) {
  // Reset the timeout
  this.updateHandle = 0;

  // Look for an existing popup nav
  var popup = document.getElementById("popupNav");

  // Exit if the appropriate popup nav is already visible
  if (popup && this.visible && (popup.popupNavIndex == this.index)) return;

  // Destroy the old popup nav is visible
  if (popup) popup.parentNode.removeChild(popup);

  // Exit if the popup nav is not supposed to be visible
  if (!this.visible) return;

  // Create the new popup nav
  popup = this.createPopup();

  // Attach the new popup nav
  this.parent.appendChild(popup);
}



// createPopup
//

PopupNav.prototype.createPopup = function() {
  var data = this.lookup[this.index];

  var popup = document.createElement("ul");
	popup.id = "popupNav";
	popup.popupNav = this;
	popup.popupNavIndex = this.index;
	popup.onmouseover = this.onPopupMouseover;
  popup.onmouseout = this.onPopupMouseout;

  // Create each link
  var item, i=0;
  while (item = data[i++]) {
    var li = document.createElement("li");
    popup.appendChild(li);

    var a = document.createElement("a");
    a.href = this.prepareLink ? this.prepareLink(item.href) : item.href;
    li.appendChild(a);

    var text = document.createTextNode(item.text);
    a.appendChild(text);
  }

  return popup;
}



// spawnUpdate
//

PopupNav.prototype.spawnUpdate = function() {
  var me = this;
  if (!this.updateHandle) this.updateHandle = setTimeout(function() {me.update()}, 0);
}



// onPopupMouseover
//

PopupNav.prototype.onPopupMouseover = function(e) {
  this.popupNav.visible = true;
  this.popupNav.spawnUpdate();
}



// onPopupMouseout
//

PopupNav.prototype.onPopupMouseout = function(e) {
  this.popupNav.visible = false;
  this.popupNav.spawnUpdate();
}



// onLinkMouseover
//

PopupNav.prototype.onLinkMouseover = function(e) {
  this.popupNav.visible = true;
  this.popupNav.parent = this.parentNode;
  this.popupNav.index = this.popupNavIndex;
  this.popupNav.spawnUpdate();
}



// onLinkMouseout
//

PopupNav.prototype.onLinkMouseout = function(e) {
  this.popupNav.visible = false;
  this.popupNav.spawnUpdate();
}





// --------
// Setup
// --------

function setupPopupNav() {
  var popupNav = new PopupNav(
    [
      {
        "id": "primaryNav",
        "nav":
          [
            {
              "elem": "li",
              "className": "resources",
              "popup":
                [
                  /*{"text": "Forestry Facts",    "href": "/resources/forestryFacts/"},*/
                  {"text": "Issue Papers",      "href": "/resources/issuePapers/"},
                  {"text": "Wildlife Research", "href": "/resources/wildlifeResearch/"},
                  {"text": "External Links",    "href": "/resources/externalLinks/"}
                ]
            }
          ]
      }
    ]
  );

  popupNav.prepareLink = function(href) {
    var currentDomain = document.domain;
    var domains = ["www.amforest.org", "preview.amforest.org", "www.beta.amforest.org", "preview.beta.amforest.org"];

    var comp, i=0;
    while (comp = domains[i++]) {
      if (currentDomain.indexOf(comp) != -1) return href;
    }

    return "http://" + domains[0] + href;
  }

  popupNav.process();
};



// onload assignment

if (window.onload) {
  var oldInit = window.onload;
  window.onload = function() {
    oldInit();
    setupPopupNav();
  }
}
else {
  window.onload = setupPopupNav;
}





})(); /* Encapsulation */