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

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

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

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

// Write a generic redirect message into the current document:
function Redirect_write() {
  var url = this.url;
  if ( window.location == url ) return;
  var secs = this.secs;
  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 " + secs + " seconds..." );
  document.writeln( "</p>" );
}
Redirect.prototype.write = Redirect_write;
