/**
 *	ÀÌ¹ÌÁö¸¦ ²Ù¹Ò
 *	
 * need : 
 * 	http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.2/prototype.js
 
±âº» »ç¿ë¹ý
<div style="width: 180; overflow: hidden;">
	<div id="container" style="position: relative; width: 1000;">
		³»¿ë
	</div>
</div>

//ÀÚµ¿À¸·Î ¿òÁ÷ÀÌ±â 2008/10/22
targetMoving.startAutoMoving();
targetMoving.stopAutoMoving();

//ÄÁÅ×ÀÌ³Ê, distanceX, distanceY
var targetMoving = new TargetMoving($('container'), 180, 90, 30, 0.4);
targetMoving.left();
targetMoving.right();
targetMoving.top();
targetMoving.bottom();


*/
 
 
var TargetMoving = Class.create({
	
	//¿òÁ÷ÀÓ ID
	id : null,
	
	//¿òÁ÷ÀÏ div content, ¹Ýµå½Ã 
	container : null,
	
	//¿òÁ÷ÀÏ °Å¸®
	distanceX : 0,
	distanceY : 0,
	
	//ÇöÀç À§Ä¡
	currentX : 0,
	currentY : 0,
	
	//¿òÁ÷ÀÏ À§Ä¡
	targetX : 0,
	targetY : 0,
	
	//ÇöÀç ¾Ö´Ï¸ÞÀÌ¼Ç ÁßÀÎÁö
	isAni : false,	
	
	initialize: function(container, distanceX, distanceY, intervalTime, spead) {
		this.container = container;
		this.distanceX = distanceX;
		this.distanceY = distanceY;
		
		if(intervalTime == null)
			intervalTime = 30;
		
		if(spead == null)
			spead = 0.4;
		
		this.intervalTime = intervalTime;
		this.spead = spead;
		
		if(this.container.style.left == '')
			this.container.style.left = 0;

		if(this.container.style.top == '')
			this.container.style.top = 0;			
		
	},
	
	
	left: function() {
		if(this.isAni == true)
			return;
			
		if(0 <= this.targetX )
			return false;
	
		this.currentX = parseInt(this.container.style.left);
		this.targetX = this.currentX + this.distanceX;						

			
		this.startAni();
	},	
	
	right: function() {
		if(this.isAni == true)
			return; 
		
		if(this.targetX <= -(this.container.getWidth() - this.distanceX))
			return false;
			
		this.currentX = parseInt(this.container.style.left);		
		this.targetX = this.currentX - this.distanceX;
							
		this.startAni();
	},	
	top: function() {
		if(this.isAni == true)
			return; 
			
		if(0 <= this.targetY)
			return false;				
	
		this.currentY = parseInt(this.container.style.top);
		this.targetY = this.currentY + this.distanceY;
		

		
		this.startAni();	
	},	
	bottom: function() {
		if(this.isAni == true)
			return;
			
		if(this.targetY <= -(this.container.getHeight() - this.distanceY))
			return false;
			
		this.currentY = parseInt(this.container.style.top);
		this.targetY = this.currentY - this.distanceY;
		
		this.startAni();	
	},	
	
	
	startAni: function() {
		this.isAni = true;
		
		var cThis = this;
		this.id = setInterval(function(){cThis.moveTarget()}, this.intervalTime);		
	},
	
	
	moveTarget: function() {			
		this.currentX += this.spead * (this.targetX - parseInt(this.container.style.left));
		this.container.style.left = this.currentX;
		
		this.currentY += this.spead * (this.targetY - parseInt(this.container.style.top));
		this.container.style.top = this.currentY;		
				
		if( (Math.abs(this.currentX - this.targetX) < 2) && (Math.abs(this.currentY - this.targetY) < 2) ) {
			clearInterval(this.id);
			this.container.style.left = this.targetX;
			this.container.style.top = this.targetY;
			this.isAni = false;
		}		
	},
	
	//¹æÇâ
	containerId : null, 
	containerDirection : 'right',
	
	//ÀÚµ¿À¸·Î ¿òÁ÷ÀÓ
	startAutoMoving: function(sec) {
		var cThis = this;
		this.containerId = setInterval(function(){cThis.autoMove()}, sec);		
	},
	
	stopAutoMoving: function() {
		clearInterval(this.containerId);
	},
	
	autoMove: function() {
		if(this.containerDirection == 'right') {
			if(this.right() == false)
				this.containerDirection = 'left';		
		} else {
			if(this.left() == false)
				this.containerDirection = 'right';
		}
		
	}	
});
