// LGPL: Rui Guerra 2007
// 
// This code has been inspired in the work done at:
// http://yermej.dyndns.org/movers.js

//Number of miliseconds per frame
var MILISECS_PER_FRAME = 33
// should be calculated as REFRESH_TIME/MILISECS_PER_FRAME
var POSITIONS_PER_REFRESHTIME = 20
// Time that an image will remain without moving
var TTL = 10

function Mover(session_id, image) {
	
	this.init = function () {
		// Add initial position and direction
		this.curY = -1;
		this.curX = -1;
		
		this.loopMove = false;
		
		this.ttl  = TTL;
	
		//create image element and append to body
		this.imgElem = document.createElement('img');
		this.imgElem.style.position = 'absolute';
		this.imgElem.style.display = 'none';
		this.imgElem.style.zIndex = 0;
		this.imgElem.id = session_id;
		document.getElementsByTagName('body')[0].appendChild(this.imgElem);
		
		// The following code is no longer
		this.imgElem.src = image;
		
		// place image element at initial position
		this.place(this.curX, this.curY)
		this.imgElem.style.display = "block";
	}

	// Changes the location of this image element
	this.place = function (x, y) {
		this.imgElem.style.top  = y+'px';
		this.imgElem.style.left = x+'px';
	}		
	
	// Removes the img element from the DOM
	this.remove = function () {
		imgElem = document.getElementById(this.imgElem.id);
		document.getElementsByTagName('body')[0].removeChild(imgElem)
	}	
	
	// Simulates the movement of an img element to a new_pos
	this.moveTo = function (new_pos) {
		var old_pos = { x:parseInt(this.imgElem.style.left)
		 	  		  , y:parseInt(this.imgElem.style.top)
				 	  };
		
		// When no initial position is given, 
		// it simply places the image at final location
		if (old_pos.x < 0 && old_pos.y < 0){
			this.place(new_pos.x, new_pos.y)
		} else {
			// Calcula the speed that of each coordinate movement
			var step = { x:Math.round((new_pos.x-old_pos.x)/POSITIONS_PER_REFRESHTIME)
			 	  	   , y:Math.round((new_pos.y-old_pos.y)/POSITIONS_PER_REFRESHTIME)
				   	   };

			var imgElem = this.imgElem;

			// Use set interval instead and do not pass the object
			loop_moveTo(imgElem, step, new_pos);
		}
	}
	
	this.init();
	return this;
}


loop_moveTo = function(imgElem, step, new_pos) {
	$('#debug_msg2').html("SCRIPT: ON "+step.x+','+step.y);
				
	var old_pos = { x:parseInt(imgElem.style.left)
	 	  		  , y:parseInt(imgElem.style.top)
			 	  };
	
	var x_done = false;
	var y_done = false;
	
	msg = step.x+','+step.y+'||'+'new_pos ='+new_pos.x+','+new_pos.y+'old_pos ='+old_pos.x+','+old_pos.y;

	
	//arrived to final destination x
	if ((step.x >= 0  && old_pos.x >= new_pos.x) || (step.x <= 0  && old_pos.x <= new_pos.x)) {
		x_done=true;
		imgElem.style.left = new_pos.x+'px';
	} else {
		imgElem.style.left = old_pos.x+step.x+'px';
	}
	//arrived to final destination y
	if ((step.y >= 0  && old_pos.y >= new_pos.y) || (step.y <= 0  && old_pos.y <= new_pos.y)) {
		y_done=true;
		imgElem.style.top = new_pos.y+'px';
	} else {
		imgElem.style.top = old_pos.y+step.y+'px';
	}

	// Keep on calling loop_moveTo until it arrives final destination
	if (!(x_done && y_done)){
		if (this.loopMove != false){
			clearTimeout(this.loopMove);
			$('#debug_msg2').html("STOP MOVE: "+msg);
		}
		this.loopMove = setTimeout(function(){loop_moveTo(imgElem, step, new_pos)}, 33);//MILISECS_PER_FRAME);
	}
}