// アニメーション制御

var animeCtrl = new AnimationControler();

function AnimationControler() {
	this.interval = 20;
	this.timer = null;
	this.objList = new Array();
	this.endHandler = null;
	this.clearObjList = new Array();

	// ターゲット・オブジェクトの追加
	this.addObject = function(elm, startp, endp, time) {
		var obj = new Object;
		obj.elm = elm;
		obj.position = clone(startp);
		obj.end = endp;
		obj.time = time;
		obj.elm.style.display = "none";
		this.objList.push(obj);
		if (! this.timer) {
			this.timer = window.setTimeout("animeCtrl.animate()", this.interval);
		}
	}

	// アニメーションの実行
	this.animate = function() {
		var newList = new Array();
		for (var ix = 0; ix < animeCtrl.objList.length; ix++) {
			var obj = animeCtrl.objList[ix];
			if (ix == 0) {
				animeCtrl.dispObj(obj);
				if (obj.time >= 0) {
					animeCtrl.moveObj(obj);
					newList.push(obj);
				} else {
					if (animeCtrl.endHandler) {
						setTimeout(animeCtrl.endHandler, 0);
					}
					this.clearObjList.push(obj);
				}
			} else {
				newList.push(obj);
			}
		}
		animeCtrl.objList = newList;
		if (animeCtrl.objList.length > 0) {
			animeCtrl.timer = window.setTimeout("animeCtrl.animate()", animeCtrl.interval);
		} else {
			animeCtrl.timer = null;
		}
	}
	
	this.clearObj = function() {
		for (var ix = 0; ix < this.clearObjList.length; ix++) {
			var obj = this.clearObjList[ix];
			obj.elm.style.display = "none";
		}
		this.clearObjList = new Array();
	}

	// ターゲットの表示
	this.dispObj = function(obj) {
		obj.elm.style.display = "block";
		obj.elm.style.top = obj.position.getTop();
		obj.elm.style.left = obj.position.getLeft();
	}

	// 位置の移動
	this.moveObj = function(obj) {
		if (obj.time <= 1) {
			obj.position.x = obj.end.x;
			obj.position.y = obj.end.y;
		} else {
			var x = obj.end.x - obj.position.x;
			var y = obj.end.y - obj.position.y;
			obj.position.x += (x / obj.time);
			obj.position.y += (y / obj.time);
		}
		obj.time--;
	}
}

// 位置オブジェクト
function Position(y, x) {
	this.x = x;
	this.y = y; 
	this.getTop = function() {
		return this.y + "px";
	}
	this.getLeft = function() {
		return this.x + "px";
	}
	this.equals = function(obj) {
		return ( (this.x == obj.x) && (this.y == obj.y) );
	}
}
