/************************************************/
/* Quicklinks Script                            */
/* Script to simulate the behavior and          */
/* appearance of a standard select form element */
/************************************************/

var quicklinks = {}; // Instantiate quicklinks namespace;

addClass = function (element, name) {
  /* 
  This function adds classes to any given element;

  Found in: quickLink.js;
  Hook in HTML: none;
  Variables required:
    element: DOM element to add the class to;
    name: name of class to be added;
  Referenced in: quickLink.js;
  Activated when: quicklinks.setQuickLinks is activated;
  */
  if (typeof element.className != "undefined") { // If there is no existing classNames;
    element.className = name; // Have className equal new class;
  } else {
    element.className = element.className + " " + name; // Else add class after existing classes;
  }
};

quicklinks.setQuickLinks = function () {
  /* 
  This function configures the quickLink display box;

  Found in: quickLink.js;
  Hook in HTML: 
    Div element with an ID of "quickLink";
    A element (ID of "selectButton") containing an IMG element which simulates the appearnce of a closed select element;
    UL element containing list of links;
  Variables required: none;
  Referenced in: quickLink.js;
  Activated when: quickLink.js is loaded into a page;
  */
  
  var showList = function () { // Function to show list of links;
    if (detailWindowStatus === null) { // If list is opening from a closed state...;
      if (document.addEventListener) { // Attach listener to hide list on mouseup;
        document.addEventListener("mouseup", hideList, true);
      } else {
        document.attachEvent("onmouseup", hideList);
      }
    }
    detailWindowStatus = quickLinkList; // Set status variable to list element;
    quickLinkList.style.display = "block"; // Display list element;
  };

  var hideList = function () {
    quickLinkList.style.display = "none"; // Hide list element;
    if (document.removeEventListener) { // Remove listener on mouseup event;
      document.removeEventListener("mouseup", hideList, true);
    } else {
      document.detachEvent("onmouseup", hideList);
    }
    detailWindowStatus = null;
  };
  
  var quickLinkDiv = document.getElementById("quickLink"); // Get div element containing list of links;
  var detailWindowStatus = null; // Instantiate variable to track status of list display;
  addClass(quickLinkDiv, "jsOn"); // Add class to div to transform appearance of list for JavaScript users;
  var quickLinkList = quickLinkDiv.getElementsByTagName("ul")[0]; // Get list of links;
  var quickLinkListArray = quickLinkDiv.getElementsByTagName("a"); // Get array of links in div;
  for (var i = 0; i < quickLinkListArray.length; i++) { // Iterate through links array;
    if (quickLinkListArray[i].id === "selectButton") { // If link is button to show list...;
      quickLinkListArray[i].onclick = function () {
        showList(); // Attach showList() method;
        return false; // Deactivate href of link;
      };
    } else { // Else if link is one of the list of links...;
      quickLinkListArray[i].onmouseover = function () {
        // Add class to parent LI element on mouseover;
        this.parentNode.className = " hoverSelect"; 
      };
      quickLinkListArray[i].onmouseout = function () {
        // Subtract class from parent LI element on mouseout;
        this.parentNode.className = this.parentNode.className.replace(/\s*hoverSelect/g, ""); 
      };
      quickLinkListArray[i].onclick = function () {
        hideList();
        return true;
      }
    }
    quickLinkListArray[i].onfocus = function () {
      if (this.blur) this.blur(); // Deselect element on blur;
    };
  }
};

addLoadEvent(quicklinks.setQuickLinks); // Add quicklinks.setQuickLinks method to onload event;