var objMove = function(id){
	this.init.apply(this,arguments);
};

objMove.prototype={
	_id			: null,
	_obj		: null,
	_acceleX	: null,
	_acceleY	: null,
	_acceleLength : 20,
	_mouseStat	: null,
	_x			: 0,
	_y			: 0,
	_i			: 0,
	_sx			: 0,
	_sy			: 0,
	_tx			: 0,
	_ty			: 0,
	_pX			: 0,
	_pY			: 0,
	_pW			: 0,
	_pH			: 0,
	_rightButton : null,
	_leftButton : null,
	_moving : null,
	_localFunc : null,
	init:function(id, rb, lb, x, y, w, h, localFunc){
		this._id = id;
		if(rb && lb){
			this._rightButton	= rb;
			this._leftButton	= lb;
			addEvent(rb, "mouseover",	this.bind(this.moveRStart));
			addEvent(lb, "mouseover",	this.bind(this.moveLStart));
			addEvent(rb, "mouseout",	this.bind(this.moveStop));
			addEvent(lb, "mouseout",	this.bind(this.moveStop));
		}
		this._localFunc		= localFunc;

		this._obj		= getObj(this._id);

		if(this._obj.style.left) this._x = Number(this._obj.style.left.match(/[^a-zA-Z]+/));
		if(this._obj.style.top)  this._y = Number(this._obj.style.top.match(/[^a-zA-Z]+/));

		if(!isNaN(x + y + w + h) && x != null && y != null && w != null && h != null){
			this._pX = x;
			this._pY = y;
			this._pW = w;
			this._pH = h;
		} else{
			var parent = this._obj.parentNode;
			document.body.appendChild(this._obj);
			this._pX = -this._obj.offsetWidth;
			this._pY = 0;
			this._pW = this._obj.offsetWidth;
			this._pH = 0;
			parent.appendChild(this._obj);
			var i = 0;
			var cObj = this._obj;
			
			var childs = searchAllChild(this._obj);
			for(var i=0; i<childs.length; i++){
				this._pW = Math.max(this._pW, childs[i].offsetLeft + childs[i].offsetWidth - this._obj.offsetWidth);
//				this._pW = Math.max(this._pW, childs[i].offsetLeft + 30 - this._obj.offsetWidth);
				this._pX = -this._pW;
			}
		}
		
		this.moveAcceleInit();
		this.moveAcceleRun();
	},
	moveAcceleInit:function(){
		this._acceleX = new Array(this._acceleLength);
		this._acceleY = new Array(this._acceleLength);
		for(var i=0; i<this._acceleLength; i++){
			this._acceleX[i] = 0;
			this._acceleY[i] = 0;
		}
	},
	moveToTarget:function(obj){
		var l = -obj.offsetLeft;
		var t = 0;
		this.moveOnce(l, t);
		
	},
	moveOnce:function(x, y){
		if(!this._moving){
			this._moving = true;
			this.moveAcceleInit();
			/*
			if(x > 0 && this._x + x > this._pX + this._pW
			|| x < 0 && this._x + x < this._pX
			|| y > 0 && this._y + y > this._pY + this._pH
			|| y < 0 && this._y + y < this._pY){
			} else{
			*/
				this._obj.style.position = "relative";
				this._tx	= x - this._x;
				this._ty	= y - this._y;
				this._sx	= this._x;
				this._sy	= this._y;
				this._i = 0;
				this.moveOnceRun();
//			}
/*			
			if(this._tx * 2 > 0 && this._sx + this._tx * 2 > this._pX + this._pW){
				this._rightButton.style.visibility = "hidden";
			} else{
				this._rightButton.style.visibility = "visible";
			}
			if(this._tx * 2 < 0 && this._sx + this._tx * 2 < this._pX){
				this._leftButton.style.visibility = "hidden";
			} else{
				this._leftButton.style.visibility = "visible";
			}
*/
		}
	},
	moveOnceRun:function(){
		this._x = (this._sx + this._tx * Math.pow(this._i / 10, 1.5));
		this._y = (this._sy + this._ty * Math.pow(this._i / 10, 1.5));
		this._x = Math.min(this._x, this._pX + this._pW);
		this._x = Math.max(this._x, this._pX);
		this._obj.style.left	= this._x + "px";
		this._obj.style.top		= this._y + "px";
		if(this._i++ >= 10){
			this._moving = false;
		} else{
			setTimeout(this.bind(this.moveOnceRun), 20);
		}
		if(this._localFunc) this._localFunc(this);
	},
	moveRStart:function(){
		this._mouseStat = 1;
	},
	moveLStart:function(){
		this._mouseStat = -1;
	},
	moveStop:function(){
		this._mouseStat = 0;
	},
	moveAccele:function(x, y){
		this._acceleX.push(x);
		this._acceleY.push(y);
		this._acceleX.shift();
		this._acceleY.shift();
	},
	moveAcceleRun:function(){
		if(!this._moving){
			switch(this._mouseStat){
				case -1	:	this.moveAccele(40, 0);		break;
				case 1	:	this.moveAccele(-40, 0);	break;
				default	:	this.moveAccele(0, 0);		break;
			}
					
			var x = 0;
			var y = 0;
			for(var i=0; i<this._acceleLength; i++){
				x += this._acceleX[i] / this._acceleLength;
				y += this._acceleY[i] / this._acceleLength;
			}
	
			this._x += Math.floor(x);
			this._y += Math.floor(y);
	
			if(this._leftButton){
				if(this._x + x * 2 >= this._pX + this._pW){
					this._leftButton.style.visibility = "hidden";
				} else{
					this._leftButton.style.visibility = "visible";
				}
			}
			
			if(this._rightButton){
				if(this._x + x * 2 <= this._pX){
					this._rightButton.style.visibility = "hidden";
				} else{
					this._rightButton.style.visibility = "visible";
				}
			}
	
			this._x = Math.min(this._x, this._pX + this._pW);
			this._x = Math.max(this._x, this._pX);
			this._obj.style.left	= this._x + "px";
			this._obj.style.top		= this._y + "px";
		}
		if(this._localFunc) this._localFunc(this);
		setTimeout(this.bind(this.moveAcceleRun), 20);
	},
	bind:function(method,arg){
		var _this=this;var _arg=(arg)?arg:[];
		return function(){
			method.apply(_this,_arg);
		}
	}
};

function searchAllChild(obj){
	var parentObj	= obj;
	var objs		= new Array();
	obj = obj.firstChild;
	var i = 0;
	while(obj && obj != parentObj){
		if(obj.firstChild){
			obj = obj.firstChild;
		} else if(obj.nextSibling){
			obj = obj.nextSibling;
		} else{
			while(obj != parentObj && !obj.nextSibling){
				obj = obj.parentNode;
				if(++i >= 10000) break;
			}
			obj = obj.nextSibling;
		}
		if(obj && obj.nodeType == 1) objs.push(obj);
		if(++i >= 10000) break;
	}
	return objs;
}
