/**
 *  File:  redirect.js
 *
 *  Timed client-side redirection
 *
 *  Usage:
 *
 *    <script type="text/javascript" src="scripts/redirect.js">
 *    </script>
 *
 *  Insert the <script> element into the <head> or <body> of your document.
 *
 *  Constructor:  Redirect( url )    create a Redirect object
 *
 *  Methods:      setDelay( secs )   set the delay in seconds before redirection
 *                write()            write the redirect into the current document
 *                go()               start the countdown!
 *
 *  Example 1:  redirectTest1.html
 *
 *    <script type="text/javascript" src="scripts/redirect.js">
 *    </script>
 *    <script type="text/javascript">
 *      var redirect = new Redirect( "http://gridshib.globus.org" );
 *      redirect.go();  // redirect NOW!
 *    </script>
 *
 *  Example 2:  redirectTest2.html
 *
 *    <script type="text/javascript" src="scripts/redirect.js">
 *    </script>
 *    <script type="text/javascript">
 *      var redirect = new Redirect( "http://www.ncsa.uiuc.edu" );
 *      redirect.setDelay( 5 );
 *      redirect.go();  // redirect in 5 seconds
 *    </script>
 *
 *  Example 3:  redirectTest3.html
 *
 *    <script type="text/javascript" src="scripts/redirect.js">
 *    </script>
 *    <!-- Put the following script in the <body> of your document -->
 *    <script type="text/javascript">
 *      var redirect = new Redirect( "http://www.uiuc.edu/" );
 *      redirect.setDelay( 8 );
 *      redirect.write();
 *      redirect.go();
 *    </script>
 *
 */

// Redirect constructor:
function Redirect( url ) {
  this.url = url;
  this.delay = 0;
}

// Perform the redirect:
Redirect.prototype.go = function() {
  var url = this.url;
  if ( url ) {
    var delay = this.delay;
    if ( delay == 0 ) {
      window.location.replace( url );
    } else {
      var ms = delay * 1000;
      setTimeout( 'window.location.replace( "' + url + '" )', ms );
    }
  }
}

// Set the delay of the redirect:^M
Redirect.prototype.setDelay = function( secs ) {
  // Try to parse the argument as an integer:
  delay = parseInt( secs );
  // Test for a non-negative integer:
  if ( !isNaN( delay ) && delay >= 0 ) this.delay = delay;
}

// Write a generic redirect message into the current document:
Redirect.prototype.write = function() {
  var url = this.url;
  if ( window.location == url ) return;
  var delay = this.delay;
  document.writeln( "" );
  document.writeln( "<p>" );
  document.writeln( "This page has moved to:" );
  document.writeln( "<\/p>\n<p style=\"margin-left: 1.5em\">" );
  document.writeln( url.link( url ) );
  document.writeln( "<\/p>\n<p>" );
  document.writeln( "You will be auto-redirected to this new page " );
  document.writeln( "in " + delay + " seconds..." );
  document.writeln( "<\/p>" );
}
