//
// Javascript to compute elapsed time between "Start" and "Finish" button clicks
//
var time_object;
var numMaxDisplayTime;

function timestamp_class ( this_current_time, this_start_time, this_end_time, this_time_difference )
{
   this.this_current_time       = this_current_time;
   this.this_start_time         = this_start_time;
   this.this_end_time           = this_end_time;
   this.this_time_difference    = this_time_difference;

   this.GetCurrentTime          = GetCurrentTime;
   this.StartTiming             = StartTiming;
   this.GetElapsedTime          = GetElapsedTime;
}

//
// Get current time from date timestamp
//
function GetCurrentTime()
{
   var my_current_timestamp = new Date();		//stamp current date & time
   return my_current_timestamp.getTime();
}

//
// Stamp current time as start time and reset display textbox
//
function StartTiming()
{
   this.this_start_time = GetCurrentTime();                 // stamp current time
}

//
// Stamp current time as stop time, compute elapsed time difference and display in textbox
//
function GetElapsedTime()
{
   this.this_end_time        = GetCurrentTime();                                    // stamp current time
   this.this_time_difference = (this.this_end_time - this.this_start_time) / 1000;	// compute elapsed time

   return ( this.this_time_difference );
}

/*** START OF CODE FROM JSFX_Layer.js ***/

var flagStartTimer,
    timStartTime,
    timCurrentTime,
    numElapsedTime;

if ( !window.JSFX )
	JSFX = new Object();

JSFX.layerNo = 0;

/**********************************************************************************/

JSFX.createElem = function(htmlStr)
   {
   var elem = null;

   var xName = "xLayer"    + JSFX.layerNo++;
   var txt   = "<DIV ID='" + xName +
               "' STYLE=\"position:absolute;" +
               "visibility:hidden\">"         +
               htmlStr                        +
	          "</DIV>";

   document.body.insertAdjacentHTML ( "BeforeEnd", txt );

   elem = document.all[ xName ];

   return elem;
   }

/**********************************************************************************/

JSFX.Layer = function(newLayer)
   {
   if ( !newLayer )
      return;

   if ( typeof newLayer == "string")
      this.elem = JSFX.createElem(newLayer);
	else
      this.elem = newLayer;

   if ( document.layers )
      {
      this.images = this.elem.document.images;
      this.style  = this.elem;
      }
   else
      {
      this.images  = document.images;
      this.style   = this.elem.style;
      }
}

/**********************************************************************************/

/**********************************************************************************/
/*** moveTo (x,y) ***/
JSFX.Layer.prototype.moveTo = function(x,y)
   {
   this.style.left = x + "px";
   this.style.top  = y + "px";
   }

/**********************************************************************************/
/*** show()/hide() Visibility ***/

JSFX.Layer.prototype.show = function()
   {
   this.style.visibility = "visible";
   }

JSFX.Layer.prototype.hide = function()
   {
   this.style.visibility = "hidden";
   }

/**********************************************************************************/
/*** Opacity ***/

if ( document.all )
   {
   JSFX.Layer.prototype.setOpacity = function(pc)
      {
      if ( this.style.filter == "" )
           this.style.filter =  "alpha(opacity=100);";

      this.elem.filters.alpha.opacity = pc;
      }
   }


/******* END OF CODE FROM JSFX.Layer.js ***/


/*** START OF CODE FROM JSFX.Browser.js ***/
JSFX.Browser = new Object();

JSFX.Browser.getCanvasWidth  = function() { return ( document.body.clientWidth  ); }
JSFX.Browser.getCanvasHeight = function() { return ( document.body.clientHeight ); }
JSFX.Browser.getMinX         = function() { return ( document.body.scrollLeft   ); }
JSFX.Browser.getMinY         = function() { return ( document.body.scrollTop    ); }
JSFX.Browser.getMaxX         = function() { return ( document.body.scrollLeft + document.body.clientWidth  ); }
JSFX.Browser.getMaxY         = function() { return ( document.body.scrollTop  + document.body.clientHeight ); }

/*** END OF CODE FROM JSFX.Browser.js ***/

/*** START OF CODE FROM JSFX.Images.js ***/

JSFX.Holiday        = new Object();
JSFX.Holiday.Images = new Array();

JSFX.Holiday.start  = function()
   {
   if ( JSFX.Holiday.theTimer == null )
      {
      JSFX.Holiday.theTimer = setTimeout ( "JSFX.Holiday.animateAll()", 40 );
      }
   }

JSFX.Holiday.animateAll = function()
   {
   numElapsedTime = time_object.GetElapsedTime();
   if ( numElapsedTime < numMaxDisplayTime )
      JSFX.Holiday.theTimer = setTimeout ( "JSFX.Holiday.animateAll()", 40 );

   var sp = JSFX.Holiday.Images;
   var i;
   for ( i = 0; i < sp.length; i++ )
      {
      if ( numElapsedTime < numMaxDisplayTime )
         sp[i].animate();
      else
         {
         sp[i].hide();
         sp[i].state = "off";
         }
      }
   }

JSFX.AddImage = function ( img )
   {
   var myImage = null;
   var htmlStr = "<IMG SRC='" + img + "'>";

   myImage    = new JSFX.Layer ( htmlStr );
   myImage.op = 0;

   myImage.x  = Math.random() * JSFX.Browser.getMaxX();
   myImage.y  = Math.random() * JSFX.Browser.getMaxY();

   myImage.dx = 0;
   myImage.dy = 0;

   myImage.w  = 30;
   myImage.h  = 30;

   myImage.targetX = Math.random()*JSFX.Browser.getMaxX();
   myImage.targetY = Math.random()*JSFX.Browser.getMaxY();
   myImage.state   = "off"
   myImage.animate = JSFX.animateImages;

   myImage.hide         ();
   myImage.setOpacity   ( this.op );
   myImage.moveTo       ( myImage.x, myImage.y );

   if ( flagStartTimer == true )
      {
      flagStartTimer = false;

      timStartTime   = new Date ();
      }

   JSFX.Holiday.Images [ JSFX.Holiday.Images.length ] = myImage;
   JSFX.Holiday.start ();
   }

JSFX.animateImages = function()
   {
   switch ( this.state )
          {
          case "off":
               if ( Math.random() > .99 )
                  {
                  this.state = "up";
                  this.show();
                  }
               break;

          case "on":
               if ( Math.random() > .98 )
                  this.state = "down";
               break;

	      case "up":
		       this.op += 2;
               this.setOpacity ( this.op );
               if ( this.op == 100 )
                  this.state = "on";
               break;

	      case "down":
		       this.op -= 2;
               if ( this.op == 0 )
                  {
                  this.hide();
                  this.state = "off";
                  }
               break;

	      default:
               this.setOpacity ( this.op );
               break;
          }

   m        = this;
   var X    = (this.targetX - m.x);
   var Y    = (this.targetY - m.y);
   var len  = Math.sqrt ( X * X + Y * Y );
   if ( len < 1 )
      len   = 1;

   var dx   = 20 * (X/len);
   var dy   = 20 * (Y/len);
   var ddx  = (dx - this.dx) / 10;
   var ddy  = (dy - this.dy) / 10;

   this.dx += ddx;
   this.dy += ddy;
   m.x     += this.dx;
   m.y     += this.dy;

   m.moveTo ( m.x, m.y );

   if ( Math.random() >.95 )
      {
      this.targetX = Math.random() * ( JSFX.Browser.getCanvasWidth ()                          - 150 );
      this.targetY = Math.random() * ( JSFX.Browser.getCanvasHeight() + JSFX.Browser.getMinY() - 150 );
      }
   }
