/* function addOnloadEvent()
 * Handle multiple events/functions that needs to happen on page load
 */
function addOnloadEvent( ev ) {
    var init = window.onload;
    window.onload = ( typeof init != 'function' ) ? ev : function() { init(); ev(); };
}

/* function popup()
 * Take complusory parameter: url( url to open in the popup )
 * Take optional parameter: params ( popup window features )
 * By default the popup window will always have a titlebar
 * The popup is placed on top of all other open windows
 */
function popup(url,params) {
  params = ( params ) ? 'titlebar=1,' + params : 'titlebar=1';
  var w = window.open(url,'popup',params);
  w.focus();
}

/* function popupNamed()
 * Take complusory parameter: url( url to open in the popup )
 * Take optional parameter: params ( popup window features )
 * By default the popup window will always have a titlebar
 * The popup is placed on top of all other open windows
 */
function popupNamed(url,wname,params) {
  params = ( params ) ? 'titlebar=1,' + params : 'titlebar=1';
  var w = window.open(url,wname,params);
  w.focus();
}

/* function initScenarios()
 * Dependencies: function createAfterLoad() of the UFO class (ufo.js)
 * Creates 'view scenario' links to view scenarios
 * Clicking on a 'view scenario' link:
 * - temporarily hides the clicked link
 * - creates a div to hold a swf movie created by the UFO class
 */
var oLastDIV;
var oLastA;
function initScenarios() {
  if ( document.getElementById('scenarios') ) {
    var oContainer = document.getElementById( 'scenarios' );
    var aAllDivs = oContainer.getElementsByTagName('DIV');
    // get all the titlebar DIVs
    var aTDivs = new Array();
    for ( var i=0; i<aAllDivs.length; i++ ) {
      if ( aAllDivs[i].className.indexOf('titlebar') > -1 ) aTDivs[aTDivs.length] = aAllDivs[i];
    }

    for ( i=0; i<aTDivs.length; i++ ) {
      // create Anchor link: 'View Scenarios'
      var oAnchor = document.createElement( 'A' );
      oAnchor.apos = i;
      oAnchor.ref = 'scenario' + (i + 1);
      oAnchor.setAttribute( 'href', '#' );
      oAnchor.appendChild( document.createTextNode( 'View Scenario' ) );
      aTDivs[i].getElementsByTagName('H2')[0].appendChild( oAnchor );

      oAnchor.onclick = function() {
        this.style.display = 'none';

        // create div to hold swf
        var oSwfDiv = document.createElement( 'DIV' );
        oSwfDiv.setAttribute( 'id', this.ref );
        if ( aTDivs[this.apos + 1] ) {
          oContainer.insertBefore( oSwfDiv, aTDivs[this.apos + 1] );
        } else {
          oContainer.appendChild( oSwfDiv );
        }

        //reset the oLastDIV and oLastA parameters
        if ( oLastDIV != null ) oContainer.removeChild( oLastDIV );
        if ( oLastA != null ) oLastA.style.display = 'block';
        oLastDIV = oSwfDiv;
        oLastA = this;

        // load swf movie
        var FO = { movie:"../../swf/" + this.ref + ".swf", width:"525", height:"415", majorversion:"6", build:"0", wmode:"transparent" };
        UFO.createAfterLoad(FO, this.ref);

        return false;
      }
    }
  }
}

/* function closeScenario()
 * Called from within the flash movie to hide a scenario
 */
function closeScenario() {
  var oContainer = document.getElementById( 'scenarios' );
  if ( oLastDIV != null ) { oContainer.removeChild( oLastDIV ); oLastDIV = null; }
  if ( oLastA != null ) { oLastA.style.display = 'block'; oLastA = null; }
}

/* function rotateQuotes()
 * hide/show quotes randomly only on pages which has got a container element with the id=rotatequotes
 */
var oLastQuote;
function rotateQuotes() {
  oLastQuote = ( oLastQuote ) ? oLastQuote : document.getElementsByTagName( 'BLOCKQUOTE' )[0];
  // if a blockquote with id 'firstquote' exists
  if ( document.getElementById( 'rotatequotes' ) ) {
    var aQuotes = document.getElementsByTagName( 'BLOCKQUOTE' );
    var iRandom = 0;
    // get a random number which would not display the previous quote, always a different quote
    do {
      iRandom = Math.round( Math.random() * (aQuotes.length - 1) );
    } while ( oLastQuote == aQuotes[iRandom] );
    // hide the last quote and display the new quote
    oLastQuote.style.display = 'none';
    aQuotes[iRandom].style.display = 'block';
    oLastQuote = aQuotes[iRandom];
    // display a differnt quote every 20 seconds
    setTimeout( "rotateQuotes()",20000 );
  }
}

//hide from netscape 4 and ie 4
if ( !document.layers && !( document.all && !document.getElementById ) ) {
  addOnloadEvent( initScenarios );
  addOnloadEvent( rotateQuotes );
}