/*
  copyright 2005 © Daniel Isenhower (aka gummyAvenger)
  send me an email if you want to use this (I may have an updated version for you)
  daniel@gummyavenger.com
  
  ease functionality is kind of sketchy at the moment...
*/

var slide_timeouts = Array();

function slide(obj_id, x, y, fps, seconds, ease) {
  
  var obj = document.getElementById(obj_id);
  /*var start_x = Math.round(obj.style.left.replace(/px/gi, ''));
  var start_y = Math.round(obj.style.top.replace(/px/gi, ''));*/
  var start_x = Math.round(obj.offsetLeft);
  var start_y = Math.round(obj.offsetTop);
  var current_x = start_x;
  var current_y = start_y;
  var end_x = x;
  var end_y = y;
  var mspf = (1/fps) * 1000; // milliseconds per frame
  
  if (ease) { // percentage of remaining distance
    distance_to_move = 100 / (fps * seconds);
  } else { // percentage of total distance
    distance_to_move = 100 / (fps * seconds);
  }
  
  if (slide_timeouts[obj_id]) {
    clearTimeout(slide_timeouts[obj_id]);
  }
  
  doSlide(obj_id, start_x, start_y, current_x, current_y, end_x, end_y, distance_to_move, ease, mspf);
}

/*
  will treat distance_to_move as a percentage of remaining distance if ease == 1
  will treat distance_to_move as a percentage of total distance if ease == 0
*/
function doSlide(obj_id, start_x, start_y, current_x, current_y, x, y, distance_to_move, ease, mspf) {
  
  var obj = document.getElementById(obj_id);
  var distance_x;
  var distance_y;
  var new_x;
  var new_y;
  
  if (ease) {
    distance_x = x - current_x;
    distance_y = y - current_y;
  } else {
    distance_x = x - start_x;
    distance_y = y - start_y;
  }
  
  new_x = current_x = current_x + (distance_to_move/100) * distance_x;
  new_y = current_y = current_y + (distance_to_move/100) * distance_y;
  
  obj.style.left = Math.round(new_x) + "px";
  obj.style.top = Math.round(new_y) + "px";
  
  
  if (Math.round(new_x) != x || Math.round(new_y) != y) {
    
    slide_timeouts[obj_id] = setTimeout('doSlide("' + obj_id + '", ' + start_x + ', ' + start_y + ', ' + current_x + ', ' + current_y + ', ' + x + ',' + y + ', ' + distance_to_move + ', ' + ease + ', ' + mspf + ')', mspf);
  }
}