var the_timeout;

function doneMoving() 
{
    // pages can override this method for any post-moving stuff they want
}

// get the position of an element (Cross browser)
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}   

function moveDiv(elementId,anchor_one, anchor_two, 
                       horizontal_step_size, vertical_step_size)
{
  var the_style = getStyleObject(elementId);
  if (the_style)
  {

    // start point
    var first_points = anchor_one.split(":");
    var first_left = parseInt(first_points[0]);
    var first_top = parseInt(first_points[1]);

    // end point
    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;
    }

    // FF needs "px", IE doesn't
    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;
    
    // check to see if we're done yet
    if ((parseInt(new_left) == parseInt(second_left)) && 
            (parseInt(new_top) == parseInt(second_top)))
    {
      doneMoving();
    } 
    else 
    {
      var new_anchor_one = new_left + ":" + new_top;

      var timeout_string = "moveDiv('" +
        elementId + "', '" + new_anchor_one + "', '" +
        anchor_two + "', " + horizontal_step_size + "," + 
        vertical_step_size + ");";

      the_timeout = setTimeout(timeout_string, 40);
    }
  }
}

//  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;
}

// the DIV is about to be moved past the end point of the segment
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;
    }
}