// source: http://yermej.dyndns.org/movers.js


function Mover(session_id,biDir, iL, iR) {
	// Get window limits 
	var minY = document.body.scrollTop;
	var minX = document.body.scrollLeft;
	var szY = document.body.clientHeight;
	var szX = document.body.clientWidth;
	
	// Add direction and random current position 
	this.curY = Math.round(Math.random() * szY) + minY;
	this.curX = Math.round(Math.random() * szX) + minX;
	
	this.loopMove = false;
	
	this.ttl  = 10;
	this.dest = Array();
	
	this.dirY = 1;
	this.dirX = 1;

	//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);
	this.bi = biDir;
	if (biDir) {
		this.imgL = new Image();
		this.imgL.src = iL;
		this.imgR = new Image();
		this.imgR.src = iR;
		this.imgElem.src = this.imgR.src;
	} else {
		this.imgElem.src = iL;
	}
	// place image element at initial position
	this.imgElem.style.left = this.curY+'px';
	this.imgElem.style.top = this.curX+'px';
	this.imgElem.style.display = "block";
	
	
	this.remove = function () {
		imgElem = document.getElementById(this.imgElem.id);
		document.getElementsByTagName('body')[0].removeChild(imgElem)
	}	
	// Random move image
	this.moveOnce = function () {
		var speed = 8;
		var minY = document.body.scrollTop;
		var minX = document.body.scrollLeft;
		var maxY = document.body.clientHeight + minY;
		var maxX = document.body.clientWidth + minX;

		var h = this.imgElem.height;
		var w = this.imgElem.width;

		var rY = Math.round(Math.random() * 3);
		var rX = Math.round(Math.random() * 3);
		var coinToss = Math.round(Math.random());

		this.curY = this.curY + this.dirY*speed;// * rY;
		this.curX = this.curX + this.dirX*speed;// * rX;
	
		if (this.curY > maxY - h) {
			this.dirY *= -1;
			this.curY = maxY - h;
		} else if (this.curY < minY) {
			this.dirY *= -1;
			this.curY = minY;
		}
		
		if (this.curX > maxX - w) {
			this.imgElem.style.zIndex = coinToss * 2;
			this.dirX *= -1;
			this.curX = maxX - w;
			if (this.bi) { this.imgElem.src = this.imgL.src; }
		} else if (this.curX < minX) {
			this.imgElem.style.zIndex = coinToss * 2;
			this.dirX *= -1;
			this.curX = minX;
			if (this.bi) { this.imgElem.src = this.imgR.src; }
		}
		
		this.imgElem.style.top = this.curY;
		this.imgElem.style.left = this.curX;
	}

	// Random move image
	this.moveRandom = function () {
		var speed = 8;
		var minY = document.body.scrollTop;
		var minX = document.body.scrollLeft;
		var maxY = document.body.clientHeight + minY;
		var maxX = document.body.clientWidth + minX;

		var h = this.imgElem.height;
		var w = this.imgElem.width;

		var rY = Math.round(Math.random() * 3);
		var rX = Math.round(Math.random() * 3);
		var coinToss = Math.round(Math.random());

		this.curY = this.curY + this.dirY * rY;
		this.curX = this.curX + this.dirX * rX;
	
		if (this.curY > maxY - h) {
			this.dirY *= -1;
			this.curY = maxY - h;
		} else if (this.curY < minY) {
			this.dirY *= -1;
			this.curY = minY;
		}
		
		if (this.curX > maxX - w) {
			this.imgElem.style.zIndex = coinToss * 2;
			this.dirX *= -1;
			this.curX = maxX - w;
			if (this.bi) { this.imgElem.src = this.imgL.src; }
		} else if (this.curX < minX) {
			this.imgElem.style.zIndex = coinToss * 2;
			this.dirX *= -1;
			this.curX = minX;
			if (this.bi) { this.imgElem.src = this.imgR.src; }
		}
		
		this.imgElem.style.top = this.curY;
		this.imgElem.style.left = this.curX;
	}

	// Image follower
	this.follow = function (position) {
		var minY = document.body.scrollTop;
		var minX = document.body.scrollLeft;
		var maxY = document.body.clientHeight + minY;
		var maxX = document.body.clientWidth + minX;
		
		//var h = this.imgElem.height;
		//var w = this.imgElem.width;
		//alert(i+"="+ position.x+ position.y)
		this.imgElem.style.top  = maxY - this.imgElem.height - position.y;
		this.imgElem.style.left = maxX - this.imgElem.width - position.x;
		
		var result = new Object;
		result.x = maxX - this.imgElem.width - position.x;
		result.y = maxY - this.imgElem.height - position.y;

 		return result;
	}
	
	this.place = function (position) {
		this.imgElem.style.top  = position.y;
		this.imgElem.style.left = position.x;
		
		var result = new Object;
		result.x = position.x;
		result.y = position.y;

 		return result;
	}		
	
	this.moveTo = function (new_pos) {
		
		var old_pos = { x:parseInt(this.imgElem.style.left)
		 	  		  , y:parseInt(this.imgElem.style.top)
				 	  };
		var direction = {x:'W', y:'N'};
		
		
		if(old_pos.x < new_pos.x) {
			direction.x = 'E';
		}
		if(old_pos.y < new_pos.y) {
			direction.y = 'S';
		}
		
		// alert(direction.x)
		// alert(direction.x+','+direction.y+':'+old_pos.x+','+old_pos.y+'->'+new_pos.x+','+new_pos.y);
		loop_moveTo(this, new_pos, direction);
	}
	
	return this;
}



loop_moveTo = function(obj, new_pos, direction) {
	var step = 10;
	var old_pos = { x:parseInt(obj.imgElem.style.left)
	 	  		  , y:parseInt(obj.imgElem.style.top)
			 	  };
	
	var x_done = false;
	var y_done = false;
	
	//alert('new_pos ='+new_pos.x+','+new_pos.y+'old_pos ='+old_pos.x+','+old_pos.y);
	if(direction.x == 'E'){
		old_pos.x < new_pos.x?obj.imgElem.style.left = old_pos.x+step+'px':x_done=true;
	} else {
		old_pos.x > new_pos.x?obj.imgElem.style.left = old_pos.x-step+'px':x_done=true;
	}
	if(direction.y == 'S'){
		old_pos.y < new_pos.y?obj.imgElem.style.top  = old_pos.y+step+'px':y_done=true;
	} else {
		old_pos.y > new_pos.y?obj.imgElem.style.top  = old_pos.y-step+'px':y_done=true;
	}
		
	if (!(x_done && y_done)){
		//alert(obj.imgElem.style.top+','+obj.imgElem.style.left);
		//clearTimeout
		if (this.loopMove != false){
			clearTimeout(this.loopMove);
		}
		this.loopMove = setTimeout(function(){loop_moveTo(obj, new_pos, direction)}, 33);
	} else {
		//alert(x_done+' '+y_done)
		//alert(direction.x +','+direction.y+' '+obj.imgElem.style.top+','+obj.imgElem.style.left);
	}
	
}
