///////////////
// CONSTANTS //
///////////////

// Direction values.
var right = 0;
var left = 1;

// Timeout between movements.
var speed = 100;

// Pixels to go past edge.
var extra = 100;

// Images.
var imageright = "images/stp1.gif";
var imageleft = "images/stp2.gif";

// X increment.
var dx = 10;

// Flip Flop counter.
var ffx = 10;

// Determine browser type.
var nsBrowser = (document.layers) ? 1 : 0;
var ieBrowser = (document.all) ? 1 : 0;
var mzBrowser = (!document.all && document.getElementById) ? 1 : 0; 

///////////////
// VARIABLES //
///////////////

// Image position variables.
var doc_width = 800;
var doc_height = 600;

// X values.
var dir_x;

// Flip Flop counter.
var flipper = 0;

// Current image.
var currentImage = imageright;

// set width and height according to browser used
if (nsBrowser)
{
  doc_width = self.innerWidth;
  doc_height = self.innerHeight;
}
else if (ieBrowser)
{
  doc_width = document.body.clientWidth;
  doc_height = document.body.clientHeight;
}
else if (mzBrowser)
{
  doc_width = document.body.clientWidth;
  doc_height = document.body.clientHeight;
}
else
{
  doc_width = document.body.clientWidth;
  doc_height = document.body.clientHeight;
}

// Testing.
//document.write('<IMG SRC="' + imageright + '" width="550"><br>');
//document.write('<IMG SRC="' + imageleft + '" width="550"><br>');


// Initialize starting positions.
xp = Math.random()*(doc_width-50);
yp = Math.random()*doc_height;

if (nsBrowser)
{
  document.write('<div style="position:absolute;top:15px;left:15px"><div style="position:relative">');
  document.write('<img id="dot" src="'+ imageright +'" style="position:absolute;top:15px;left:15px;filter:alpha(opacity=90)" width="150">');
}
else if (ieBrowser)
{
  document.write('<div id="dot" style="POSITION: absolute; VISIBILITY: visible; TOP: 15px; LEFT: 15px;">');
  document.write('<img id="santa" IMG SRC="' + imageright + '" width="150">');
  document.write('</div>');
}
else if (mzBrowser)
{
  document.write('<div id="dot" style="position:absolute;top:15px;left:15px">');
  document.write('<img id="santa" img src="'+ imageright + '" style="filter:-moz-opacity:0.9)" width="150">');
  document.write('</div>'); 
}
else
{
  document.write('<div id="dot" style="POSITION: absolute; VISIBILITY: visible; TOP: 15px; LEFT: 15px;">');
  document.write('<img id="santa" IMG SRC="' + imageright + '" width="150">');
  document.write('</div>');
}


function flipflop() {

    if (currentImage == imageright)
    {
        currentImage = imageleft;
    }
    else
    {
        currentImage = imageright;
    }
}

// Netscape main animation function
// (Modified this so image will bounce around,
//  doesn't work for more than one image yet, LES)
function LES_go_NS() {

    // Series of if's for y-direction
    if (yp > doc_height-40) {
      dir_y = up;
      doc_width = self.innerWidth;
      doc_height = self.innerHeight;
    }
    if (yp < 0) {
      dir_y = down;
      doc_width = self.innerWidth;
      doc_height = self.innerHeight;
    }
    if (dir_y == 1) {
      yp -=  dy;
    } else {
        yp +=  dy;
    }
    // Series of if's for x-direction
    if (xp > doc_width-50) {
      dir_x = left;
      doc_width = self.innerWidth;
      doc_height = self.innerHeight;
    }
    if (xp < 0) {
      dir_x = right;
      doc_width = self.innerWidth;
      doc_height = self.innerHeight;
   }
    if (dir_x == 1) {
      xp -=  dx;
    } else {
        xp +=  dx;
    }
    //document.layers.dot.top = yp;
    //document.layers.dot.left = xp;
document.getElementById("dot").top = yp;
document.getElementById("dot").left = xp;

  setTimeout("LES_go_NS()", speed);
}



///////////////////////////////////////
// IE main animation function
///////////////////////////////////////
function LES_go_IE()
{
    // Change direction based on edge detection.
    if (xp > doc_width+extra)
    {
      // Went past end, so change direction and get a new y!

      // Change direction.
      dir_x = left;

      // Change y position.
      yp = Math.random()*doc_height;
    }

    if (xp < -extra)
    {
      // Went past beginning, so change direction and get a new y!

      // Change direction.
      dir_x = right;

      // Change y position.
      yp = Math.random()*doc_height;
    }

    // Increment x based on direction.
    if (dir_x == left)
    {
      xp -=  dx;
    }
    else
    {
      xp +=  dx;
    }

    // Increment flip flop.
    ++flipper;
    if (flipper > 10)
    {
        flipper = 0;

        flipflop();

        // Flip image.
        var imgChg = document.getElementById('dot');
        imgChg.innerHTML = '<img id="santa" img src="'+ currentImage +'" style="filter:-moz-opacity:0.9)" width="150">';
    }

    document.all.dot.style.pixelTop = yp;
    document.all.dot.style.pixelLeft = xp;

    setTimeout("LES_go_IE()", speed);
}

function LES_go_MZ()
{
    // Change direction based on edge detection.
    if (xp > doc_width+extra)
    {
      // Went past end, so change direction and get a new y!

      // Change direction.
      dir_x = left;

      // Change y position.
      yp = Math.random()*doc_height;
    }

    if (xp < -extra)
    {
      // Went past beginning, so change direction and get a new y!

      // Change direction.
      dir_x = right;

      // Change y position.
      yp = Math.random()*doc_height;
    }

    // Increment x based on direction.
    if (dir_x == left)
    {
      xp -=  dx;
    }
    else
    {
      xp +=  dx;
    }

    // Increment flip flop.
    ++flipper;
    if (flipper > 10)
    {
        flipper = 0;

        flipflop();

        // Flip image.
        var imgChg = document.getElementById('dot');
        imgChg.innerHTML = '<img id="santa" img src="'+ currentImage +'" style="filter:-moz-opacity:0.9)" width="150">';
    }

    document.getElementById("dot").style.top = yp;
    document.getElementById("dot").style.left = xp;

    setTimeout("LES_go_MZ()", speed);
}

// Decide what function to run based on browser.
if (nsBrowser)
{
  LES_go_NS();
}
else if (ieBrowser)
{
  LES_go_IE();
}
else if (mzBrowser)
{
  LES_go_MZ();
}
else
{
  LES_go_IE();
}

// End -->
