addPageEventListener = function(target_element, event_name, func) {
  if (document.addEventListener) {
	  target_element.addEventListener(event_name, func, true);
  } else {
	  target_element.attachEvent("on" + event_name, func);
  }
  return func;
};
 
var nav_mouseout_timer = null;
 
function onLoad() {
  
  // this array contains all the ids of elements the user may click to launch the form overlay
  var popup_form_elements = $A(["slide_container", "main_cta"]);
  popup_form_elements.each(function(elem) {
    if ($(elem)) {
    addPageEventListener($(elem), "click", 
      function() {
        openOverlay('form');
      });
    }
  });
  
  addPageEventListener($("all_nav"), "mouseover",
    function() {
      // alert("mouseover");
      clearTimeout(nav_mouseout_timer);
      $("all_nav_options").style.display = "block";
  });
  
  addPageEventListener($("all_nav"), "mouseout",
    function() {
      // alert("mouseout");
      nav_mouseout_timer = setTimeout(function() {$("all_nav_options").style.display = "none";}, 1000);
  });
    
  
  
  // If there's a div named "slideshow_container, then the slideshow fires up.
  if ($('slideshow_container')) {
    
    // config file must be on same server: this is a security requirement of Ajax
    config_file_url = "config/slideshow.json";
    new Ajax.Request(config_file_url, {
      method: 'get',
      onSuccess: function(transport) {
        slideshow = new SlideShow(transport.responseText);
  	    slideshow.start();
      }
    });
  }
}
 
 
function togglePulldown(id) {
  var pulldown = $(id);
  pulldown.style.display = (pulldown.style.display == "block") ? "none" : "block";
    
}
 
function closeOverlay() {
  $("iframe").src = "";
  $("form").style.display = "none";
  $("overlay").style.display = "none";
}

function openOverlay(show) {
  
  var show_url = "http://michaelharrison.ws/projects/ocr/form.html";
  var newDivWidth = 560;
  var headerText = "Enter Your Contact Information";
  if (show == "repayment_calculator") {
    show_url = "http://www.ed.gov/offices/OSFAP/DirectLoan/RepayCalc/dlentry1.html";
    headerText = "Repayment Calculator";
  } else if (show == "income_calculator") {
    show_url = "http://www.labor.state.ny.us/workforceindustrydata/cen/calc1.asp?reg=cny";
    newDivWidth = 805;
    headerText = "Income Calculator";
  }  
  
  var main_coords = Element.cumulativeOffset($('body'));
  var main_width  = Element.getWidth($('body'));
  
  var overlay_is_initialized = false;
  
  // if the div and overlay already exist, initialize objects just show them. 
  if ($("form") && $("overlay") && $("iframe")) {
    
    overlay_is_initialized = true;
    
    var newDiv = $("form");
    var iFrame = $("iframe");
    var overlay = $("overlay");
    var controlBarText = $("control_bar_text");
    
  } else {
    
    var newDiv = document.createElement("div");
    newDiv.id="form";
    var iFrame = document.createElement("iframe");
    iFrame.id = "iframe";
    var overlay = document.createElement("div");
    overlay.id="overlay";  
    var controlBar = document.createElement("div");
    controlBar.id  = "control_bar";
    var controlBarText = document.createElement("span");
    controlBarText.id="control_bar_text";
    
    var closeControl = document.createElement("div");
    closeControl.innerHTML = "x";
    closeControl.id = "close_control";
    
    controlBar.appendChild(closeControl);
    controlBar.appendChild(controlBarText);
    
    newDiv.style.height = "500px";
    iFrame.style.height = "471px";
    
    
    addPageEventListener(closeControl, "click", function() { closeOverlay(); });
    
    newDiv.appendChild(controlBar);
    newDiv.appendChild(iFrame);
  
  }
  
  // The next four lines of code find the number of pixels by which the screen has been scrolled down.
  // That amount, kept in the variable dsoctop, is then added to the offset of newDiv. This makes sure
  // that the top of the Div appears on the screen, whether the user has scrolled down or not.
  // Scroll detection code from JavaScript Kit: http://www.javascriptkit.com/javatutors/static2.shtml
  var theobject=document.all? document.all.test : document.getElementById("test");
  var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
  var dsoctop=document.all? iebody.scrollTop : pageYOffset;
  
  newDiv.style.top  = "" + (Element.cumulativeOffset($("content_box"))[1] + dsoctop + 1) + "px";
  
  iFrame.src = show_url;
  newDiv.style.left = "" + (main_coords[0] + (main_width - newDivWidth) / 2) + "px";
  newDiv.style.width = "" + newDivWidth + "px";
  iFrame.style.width = "" + (newDivWidth -0) + "px";
  controlBarText.innerHTML = headerText;  
  
  overlay.style.left = "" + main_coords[0] + "px";
  overlay.style.height = "" + (Element.cumulativeOffset($('footer'))[1] + 
    Element.getHeight($('footer'))) +"px";
  
  
  // If overlay elements already exist, just show them.
    
  if (overlay_is_initialized) {
    newDiv.style.display = "block";
    overlay.style.display = "block";
  } else {
    document.body.appendChild(newDiv);
    document.body.appendChild(overlay);
  }

}