// lib_init.js - Javascript library that handles starting event listeners

// Requires: lib_core.js
if (!LIB_CORE_LOADED || !LIB_AJAX_LOADED) {
  // Here is where you _could_ do something to alert the user to a problem, but we don't want to do that.
}

/*** Function Declarations ***/

var listOfEvents = "";
// Keeps up with a list of events, and can be used by any JS function.
function logEvent(eType,eFunc) {
  var thisEventAdded = "";
  var thisFunc = eFunc.toString();
  switch(eType) {
    case "reqForm":
      thisEventAdded = "Marking required fields in form '" + thisFunc + "'.\n";
      break;
    case "prepDiv":
      thisEventAdded = "Preparing special DIV methods for '" + thisFunc + "'.\n";
      break;
    default:
      thisFunc = thisFunc.replace(/\n/g,"");
      var funcTest = trim(thisFunc.substr(0, thisFunc.indexOf('(')));
      if (funcTest != "function") { thisFunc = funcTest; }
      thisFunc = thisFunc.replace("function ","");
      thisEventAdded = "Added '" + thisFunc + "' as " + eType + " event.\n";
  }
  listOfEvents = listOfEvents + thisEventAdded;
} // logEvent

// Displays the list of JS events, either in a DIV or as an alert.
function showEvents() {
  var debugDiv = document.getElementById("jsDebug");
  if (debugDiv) {
    debugDiv.innerHTML = debugDiv.innerHTML + "<hr/><b>Javascript Event History:</b><br/>" + listOfEvents.replace(/\n/g,"<br/>") + "<br/>";
  } else {
    alert(listOfEvents);
  }
} // showEvents

// Original code from http://simonwillison.net/2004/May/26/addLoadEvent/
// This appears to work for all Win32-based browsers, but not too well on Mac.
// Also, this is always destroyed by any onload events in the body tag!
// So this will only be used as a backup when all other methods are unavailable.
function addLoadEvent(func) {
  // Store any previously requested events.
  var oldEvents = window.onload;
  if (typeof window.onload != "function") {
    // Assign the first function directly, as this handler starts as an object.
    window.onload = func;
  } else {
    // Re-build the list of events, and push the new event onto the end of the stack.
    window.onload = function() {
      if (oldEvents) { oldEvents(); }
      func();
    }
  }
} // addLoadEvent

// This can be used to attach multiple listeners/handlers for supported event types.
// Please note that the order in which functions are run differs between browsers:
// -IE: onload body event, followed by event listeners (in reverse order).
// -Opera: event listeners (in order), followed by onload body event.
// -FF/NS/Moz: event listeners from head, then onload body event, followed by remaining event listeners.
// Note: onclick event is actually made up of onmousedown and onmouseup events.
function addUIEvent(eType,eFunc) {
  var functionAdded = false;
  try {
    if (window.addEventListener || document.addEventListener) {
      // Non-IE Browsers
      switch(eType) {
        case "onload":
          window.addEventListener('load', eFunc, true);
          break;
        case "onunload":
          window.addEventListener('unload', eFunc, true);
          break;
        case "onclick":
          document.addEventListener('click', eFunc, true);
          break;
        case "onmousedown":
          document.addEventListener('mousedown', eFunc, true);
          break;
        case "onmousemove":
          document.addEventListener('mousemove', eFunc, true);
          break;
      }
      functionAdded = true;
    } else if (window.attachEvent || document.attachEvent) {
      // IE Browsers
      switch(eType) {
        case "onload":
          window.attachEvent('onload', eFunc);
          break;
        case "onunload":
          window.attachEvent('onunload', eFunc);
          break;
        case "onclick":
          document.attachEvent('onclick', eFunc);
          break;
        case "onmousedown":
          document.attachEvent('onmousedown', eFunc);
          break;
        case "onmousemove":
          document.attachEvent('onmousemove', eFunc);
          break;
      }
      functionAdded = true;
    } else {
      // Older Browsers
      switch(eType) {
        case "onload":
          addLoadEvent(eFunc);
          break;
        case "onunload":
          window.onunload = eFunc;
          break;
        case "onclick":
          document.onclick = eFunc;
          break;
        case "onmousedown":
          document.onmousedown = eFunc;
          break;
        case "onmousemove":
          document.onmousemove = eFunc;
          break;
      }
      functionAdded = true;
    }
  } catch(e) { /* Die Quietly */ }
  if (functionAdded) { logEvent(eType,eFunc); }
} // addUIEvent

// This automatically gathers a list of all forms in use on the page.
// Then each form has its' required fields marked via markRequired()
function prepareForms() {
  var formList = document.getElementsByTagName("form"); // Gather all possible forms.
  var thisForm;
  var skipForm;
  for(i=0;i<formList.length;i++) {
    thisForm = formList[i]; // Get a simple handle to the form.
    skipForm = thisForm.getAttribute('noauto'); // If true, do not auto-mark this form as required.
    if (skipForm != "true") {
      markRequired(thisForm); // Mark selected form fields as required.
    }
  }
} // prepareForms

// This automatically gathers a list of all DIV tags with an ID attribute.
// Then each DIV is turned into an object by the same name as the ID.
// Additional methods that control behavior are then available to the DIV.
function prepareDivs() {
  var divList = document.getElementsByTagName("div"); // Gather all possible div's.
  var thisDiv;
  var divID;
  for(i=0;i<divList.length;i++) {
    thisDiv = divList[i]; // Get a simple handle to the div.
    divID = thisDiv.getAttribute('id'); // Check if the DIV has an ID attribute.
    if (divID) {
      // Overload the name of the div as an object that now has additional methods.
      eval("var " + divID + " = new initElement('" + divID + "');");
    }
  }
} // prepareDivs
/*** end function section ***/


/*** Event Handlers (Must be called AFTER all target functions are defined) ***/
/* Commenting these out for the time being, as they're not 100% foolproof yet.
// From declarations above
addUIEvent('onload',prepareForms);
addUIEvent('onload',prepareDivs);
*/

// From 'googlefix' in lib_core.js
addUIEvent('onload',restoreStyles);

// From lib_help.js, include/page_head.html
addUIEvent('onload',showHelpIcons);
addUIEvent('onunload',closeHelpWin);

/*** end handler section ***/

// Set a global flag that indicates this library has been loaded by the browser.
var LIB_INIT_LOADED = true;

// End library
