var the_timeout;

var ANCHORS = 25;
var the_coords = new Array();
var the_div    = document.getElementById("coolDiv");
var the_style  = the_div.style;;

function stopFAN() {
    if (!the_style) return false;
    clearTimeout(the_timeout);
    the_style.visibility = 'hidden';
}

// build an array with anchors
function setup(startX, startY)
{
    if (the_div && the_style && Math.random) {
        var maxY, maxX;
        if (window.innerWidth) {
            maxX = window.innerWidth;
            maxY = window.innerHeight;
        } else if (document.body.offsetWidth) {
            maxX = document.body.offsetWidth;
            maxY = document.body.offsetHeight;
        } else {
            return false;
        }
        minX = Math.round(maxX / 20);
        maxX -= (parseInt(the_style.width) + minX);
        minY = Math.round(maxY / 20);
        maxY -= (parseInt(the_style.height) + minY);
        the_coords = buildAnchors(minX,maxX, minY, maxY, startX, startY);
//        alert('Borders: ' + minX + '/' + maxX + ', ' + minY + '/' + maxY);
//        alert('Prepared Path: ' + the_coords);
    }
}

function buildAnchors(minX, maxX, minY, maxY, startX, startY)
{
    var anchors = new Array();
    if (!maxX || !maxY) return anchors;
    if (startX && startY) {
        anchors[0] = new Array(startX, startY);
    } else {
        anchors[0] = new Array(Math.round(maxX/2), Math.round(maxY/2));
    }
    var moveX = Math.round((maxX/ANCHORS) * 2);
    var moveY = Math.round((maxY/ANCHORS) * 2);
    for (x = 1; x <= ANCHORS; x++) {
        // get last coords
        var lastX = anchors[x-1][0]; var lastY = anchors[x-1][1];
        var newX = lastX; var newY = lastY;
        // get horizontal step-width
        var distX = Math.round(moveX * (Math.random()));
        // check boundaries
        if (lastX - distX < minX) {
            newX += distX;
        } else if (lastX + distX > maxX) {
            newX -= distX;
        } else {
            newX = lastX + (Math.round(Math.random()) > 0 ? distX : (distX * -1));
        }
        // get vertical step-width
        var distY = Math.round(moveY * (Math.random()));
        // check boundaries
        if (lastY - distY < minY) {
            newY += distY;
        } else if (lastY + distY > maxY) {
            newY -= distY;
        } else {
            newY = lastY + (Math.round(Math.random()) > 0 ? distY : (distY * -1));
        }
        anchors[x] = new Array(newX, newY);
    }
    // convert anchors to strings
    for (x = 0; x < anchors.length; x++) {
        anchors[x] = anchors[x][0] + ':' + anchors[x][1];
    }
    return anchors;
}


// function getAnchors()
//    this function gets the two anchor points of a segment
//    of the path and then calls moveDiv() to move the
//    DIV along that segment
//
function getAnchors(array_position)
{
  if (array_position < 0) {
      setup();
      array_position = 0;
  }
  var first_anchor = the_coords[array_position];
  var second_anchor = the_coords[array_position+1];
//  window.status = array_position +': Moving from ' + first_anchor + ' to ' + second_anchor + '...';

  array_position++;
  if (array_position == the_coords.length-1)
  {
        var points = the_coords[the_coords.length-1].split(":");
        var startX = parseInt(points[0]);
        var startY = parseInt(points[1]);
        setup(startX, startY);
        array_position = 0;
  }
  moveDiv(array_position, first_anchor, second_anchor, 0, 0);

}

getAnchors(-1);

// function moveDiv()
//   this function moves the DIV along a segment of the path
//   based on the anchor points.  It will keep calling
//   itself with a setTimeout() until the DIV is at the end
//   of the segment
//
function moveDiv(array_position, anchor_one, anchor_two,
                       horizontal_step_size, vertical_step_size)
{
  if (the_style)
  {

    // get the first anchor
    //
    var first_points = anchor_one.split(":");
    var first_left = parseInt(first_points[0]);
    var first_top = parseInt(first_points[1]);

    // get the second anchor
    //
    var second_points = anchor_two.split(":");
    var second_left = parseInt(second_points[0]);
    var second_top = parseInt(second_points[1]);

    // if we don't know the step sizes to move the DIV, figure them out
    //
    if ((horizontal_step_size == 0) && (vertical_step_size == 0))
    {
      horizontal_step_size =
        getStepSize(anchor_one, anchor_two, 0);

      vertical_step_size =
        getStepSize(anchor_one, anchor_two, 1);
   }

    // figure out the new coordinates
    //
    var new_left = first_left + horizontal_step_size;
    var new_top = first_top + vertical_step_size;

    // if we're at the end of the segment, set the coordinates to
    // move the DIV to the end
    //
    if (atEndOfPath(horizontal_step_size, second_left, new_left)
       ||(atEndOfPath(vertical_step_size, second_top, new_top)))
    {
      new_left = second_left;
      new_top = second_top;
    }

    // add the px or don't, depending on the browser
    if (!document.layers)
    {
      new_left = new_left + "px";
      new_top = new_top + "px";
    }

    // now actually move the DIV
    //
    the_style.left = new_left;
    the_style.top = new_top;
    if (the_style.visibility == 'hidden') {
        the_style.visibility = 'visible';
    }

    // if we're at the end of the segment, get new anchors
    // otherwise, call moveDiv() again with a new starting point
    // along the segment
    //
    if ((parseInt(new_left) == parseInt(second_left)) &&
            (parseInt(new_top) == parseInt(second_top)))
    {
      getAnchors(array_position);
    }
    else
    {
      var new_anchor_one = new_left + ":" + new_top;

      var timeout_string = "moveDiv(" +
        array_position + ", '" + new_anchor_one + "', '" +
        anchor_two + "', " + horizontal_step_size + "," +
        vertical_step_size + ");";

      the_timeout = setTimeout(timeout_string, 50);
    }
  }
}

// function getStepSize()
//  this figures out how much to move the DIV each time
//
function getStepSize(anchor_one, anchor_two, position)
{

  var first_points = anchor_one.split(":");
  var first_number = parseInt(first_points[position]);

  var second_points  = anchor_two.split(":");
  var second_number = parseInt(second_points[position]);

  var step_size = Math.round((second_number - first_number)/10);

  return step_size;
}

// function atEndOfPath()
//   if the DIV is about to be moved past the end point of
//   the segment, this will return true.  Otherwise, it will
//   return false.
//
function atEndOfPath(the_step_size, second_number, new_number)
{
    var the_end = false;

    if (((the_step_size > 0) && (new_number > second_number)) ||
        ((the_step_size < 0) && (new_number < second_number)))
   {
     the_end = true;
   }

   return the_end;
}


function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its
    if(document.getElementById && document.getElementById(objectId)) {
    // W3C DOM
    return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
    // MSIE 4 DOM
    return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
    // NN 4 DOM.. note: this won't find nested layers
    return document.layers[objectId];
    } else {
    return false;
    }
} // getStyleObject


