/*
 * Necessary Start-up Functions
 * "included" last among the standard JS files
 */

Event.observe(window, 'load', function() {
  // Build Menu
  initMenu();
  
  // Attach custom action to all form elements
  initFormElements();
  
  // Convert anchor links to onclick functions
  initAnchors();
});

var menuIds = new Array("menu");

function initMenu() {
  for (var i = 0; i < menuIds.length; i++) {
    var ulTags = $(menuIds[i]).select("ul");
    ulTags.each( function(ulTag) {
      var p = $(ulTag.parentNode);
      p.observe('mouseover', function(event) {
        this.select("ul")[0].style.visibility = "visible";
        //this.select("ul")[0].style.display = "block";
      });
      p.observe('mouseout', function(event) {
        this.select("ul")[0].style.visibility = "hidden";
        //this.select("ul")[0].style.display = "none";
      });
    });
  }
}

function initFormElements() {
  $$('input').each( function(e) {
    if (e.readAttribute('type') == 'text' || e.readAttribute('type') == 'password') {
      e.observe('focus', function(event) { this.addClassName('focusedElement'); });
      e.observe('blur', function(event) { this.removeClassName('focusedElement'); });
    }
    e.observe('change', formElementChanged);
  });
  
  $$('textarea').each( function(e) {
    e.observe('focus', function(event) { this.addClassName('focusedElement'); });
    e.observe('blur', function(event) { this.removeClassName('focusedElement'); });
    e.observe('change', formElementChanged);
  });
}

function initAnchors() {
  $$('a').each( function(e) {
    // Special case to ignore anchor
    if (e.className == 'ignore') return;
    
    var href = e.readAttribute('href');
    e.setAttribute('href', '#');
    e.setAttribute('oldHref', href);
    e.observe('click', function(event) {
      goTo(href);
      return false;
    });
  });
}
