
/*

bool OMNI_add_event_listener() -- Attach an event listener to an object

object element -- The object to attach the listener to

string event_type -- The name of the event type (e.g. click, mouseover)

string listener -- Name of the listener function

bool useCapture -- useCapture parameter to addEventListener

*/

function OMNI_add_event_listener( element, event_type, listener, useCapture ) {

  var return_value = false;

  // The UA supports it, so use the DOM method
  if ( element.addEventListener ) {

    element.addEventListener( event_type, window[ listener ], useCapture );

    return_value = true;

  }
  // end if

  // Use the traditional method
  else {

    element[ "on" + event_type.toLowerCase() ] = window[ listener ];

    return_value = true;

  }
  // end else

  return return_value;

}
// end OMNI_add_event_listener


/*

void OMNI_preload_rollovers() -- Preload images for navigation elements that have a rollover effect

*/

function OMNI_preload_rollovers() {

  var rollover_states = [ 'hover', 'link' ];

  var rollover_state;

  var image_file_basename;

  var image_file_ext;

  var matches;

  var current_nav_link;

  var nav_links;

  nav_links = document.getElementById( 'OMNI_body' ).getElementsByTagName( 'a' );

    // Loop through each 'a' element in the current set of navigation elements
  for ( nl_index = 0 ; nl_index < nav_links.length ; ++nl_index ) {

    current_nav_link = nav_links[ nl_index ];

    // The link is not a rollover button
    if ( ! current_nav_link.className.match( /(^| )(OMNI_button)( |$)/ ) ) {

      continue;

    }
    // end if


    OMNI_add_event_listener( current_nav_link, 'mouseover', "OMNI_button_event", false );

    OMNI_add_event_listener( current_nav_link, 'mouseout', "OMNI_button_event", false );


    // Parse the image file basename and extension
    matches = current_nav_link.firstChild.src.match( "/buttons/[^/]+/([^/]+)\\.([^/]+)$" );


    // Preload the image associated with each state a navigation element can take
    for ( rs_index = 0 ; rs_index < rollover_states.length ; ++rs_index ) {

      rollover_state = rollover_states[ rs_index ];

      // The image has already bene loaded
      if ( window[ "button_" + current_nav_link.id + "_" + rollover_state ] ) {

        continue;

      }
      // end if

      window[ "button_" + current_nav_link.id + "_" + rollover_state ] = new Image();

      window[ "button_" + current_nav_link.id + "_" + rollover_state ].src = ( "ui/standard/images/buttons/" + rollover_state + "/" + matches[1] + "." + matches[2] );

    }
    // end for

  }
  // end for


  return;

}
// end OMNI_preload_rollovers


/*

void OMNI_button_event() -- Implement a rollover effect on a navigation element

object event -- The event object generated by the UA in response to user action

*/

function OMNI_button_event( event ) {

  // Normalize the event object for cross-browser purposes
  event = ( event ? event : window.event );

  var new_state;

  var matches = event.type.match( /mouse(.+)/ );

  // Map DOM event type names (mouseover, mouseout) to CSS style names (hover, link)
  if ( matches[1] == 'over' ) {

    new_state = 'hover';

  }
  // end if

  else if ( matches[1] == 'out' ) {

    new_state = 'link';

  }
  // end else if


  // Change the source of the image that is the content of the navigation element the user is acting upon to the source of the image corresponding to the new state of the element
  this.firstChild.src = window[ "button_" + this.id + "_" + new_state ].src;


  return;

}
// end OMNI_button_event


/*

void OMNI_loading_complete() -- Onload handler

*/

function OMNI_loading_complete() {

  // Execute specific onload logic for the current page if defined
  if ( window.loading_complete ) {

    window.loading_complete();

  }
  // end if

  window.OMNI_preload_rollovers();

  return;

}
// end OMNI_loading_complete


OMNI_add_event_listener( window, "load", "OMNI_loading_complete", false );
