// JavaScript Document
//require bmp.js please insert into document.
document.write("<style type=\"text/css\">");
document.write(".fobj { position:absolute; }");
document.write("</style>");

var arrObjFree = new Array();

function objFree(obj,marginMode) {
	if (marginMode == undefined) marginMode = 0;
	this.bMargin = marginMode;
	this.obj = obj;
	if (this.bMargin == 1) {
		//this.obj.className = "fobj";
		x = this.obj.style.marginLeft;
		y = this.obj.style.marginTop;
		if (x == "") this.x = 0;
		else this.x = parseInt(x);
		if (y == "") this.y = 0;
		else this.y = parseInt(y);
		this.interval = 50;
		this.obj.style.marginLeft = this.x + "px";
		this.obj.style.marginTop = this.y + "px";
	}
	else if (this.bMargin == 2) {
		//this.obj.className = "fobj";
		posi = this.obj.style.backgroundPosition;
		arr = null;
		if (posi != "") arr = posi.split(" ");
		if (posi == "") this.x = 0;
		else this.x = parseInt(arr[0]);
		if (posi == "") this.y = 0;
		else this.y = parseInt(arr[1]);
		this.interval = 50;
		this.obj.style.backgroundPosition = this.x + "px " + this.y + "px";
	}
	else {
		this.obj.className = "fobj";
		x = obj.style.left;
		y = obj.style.top;
		if (x == "") this.x = 0;
		else this.x = parseInt(x);
		if (y == "") this.y = 0;
		else this.y = parseInt(y);
		this.interval = 50;
		this.obj.style.left = this.x + "px";
		this.obj.style.top = this.y + "px";
	}
	this.inv = null;
	this.breaking = false;
	this.callback = null;
	this.callbackEnd = null;
	arrObjFree.push(this);
	this.nID = arrObjFree.length - 1;
}

objFree.prototype.changeObj = function(obj,marginMode) {
	if (marginMode == undefined) marginMode = false;
	this.bMargin = marginMode;
	this.obj = obj;
	if (this.bMargin > 0) {
		this.obj.className = "fobj";
		x = this.obj.style.marginLeft;
		y = this.obj.style.marginTop;
		if (x == "") this.x = 0;
		else this.x = parseInt(x);
		if (y == "") this.y = 0;
		else this.y = parseInt(y);
		this.interval = 50;
		this.obj.style.marginLeft = this.x + "px";
		this.obj.style.marginTop = this.y + "px";
	}
	else {
		this.obj.className = "fobj";
		x = this.obj.style.left;
		y = this.obj.style.top;
		if (x == "") this.x = 0;
		else this.x = parseInt(x);
		if (y == "") this.y = 0;
		else this.y = parseInt(y);
		this.interval = 50;
		this.obj.style.left = this.x + "px";
		this.obj.style.top = this.y + "px";
	}
	this.inv = null;
	this.breaking = false;
	this.callback = null;
	this.callbackEnd = null;
	//arrObjFree.push(this);
	//this.nID = arrObjFree.length - 1;
}

objFree.prototype.setPosition = function(x,y) {
	this.x = x;
	this.y = y;
	this.obj.style.left = x + "px";
	this.obj.style.top = y + "px";
}

objFree.prototype.setIntervalRate = function(n) {
	this.interval = n;
}

objFree.prototype.setLayer = function(n) {
	this.obj.style.zIndex = n;
}

objFree.prototype.moveTo = function(x,y,timeUsage,callback,callbackEnd) {
	//alert("move to: " + x + ":" + y);
	this.beginX = this.x;
	this.beginY = this.y;
	this.dstX = x;
	this.dstY = y;
	d = new Date();
	this.timeBegin = d.getTime();
	this.timeEnd = this.timeBegin + timeUsage;
	this.timeUsage = timeUsage;
	this.xUse = x - this.x;
	this.yUse = y - this.y;
	if (callback == undefined) callback = null;
	this.callback = callback;
	if (callbackEnd == undefined) callbackEnd = null;
	this.callbackEnd = callbackEnd;
	this.moveBegin();
}

objFree.prototype.moveNoSkip = function(x,y,timePerFrame,callBack,callBackEnd) {
	this.beginX = this.x;
	this.beginY = this.y;
	this.dstX = x;
	this.dstY = y;
	this.perFrame = timePerFrame;
}

objFree.prototype.moveToBreak = function(x,y,timeUsage,callback,callbackEnd) {
	this.beginX = this.x;
	this.beginY = this.y;
	this.dstX = x;
	this.dstY = y;
	d = new Date();
	this.timeBegin = d.getTime();
	this.timeEnd = this.timeBegin + timeUsage;
	this.timeUsage = timeUsage;
	this.xUse = x - this.x;
	this.yUse = y - this.y;
	if (callback = undefined) callback = null;
	this.callback = callback;
	if (callbackEnd == undefined) callbackEnd = null;
	this.callbackEnd = callbackEnd;
	this.moveBreakBegin();
}

objFree.prototype.moving = function() {
	d = new Date();
	timeUsed = d.getTime() - this.timeBegin;
	if (d.getTime() >= this.timeEnd) {
		this.x = this.dstX;
		this.y = this.dstY;
		this.obj.style.left = this.x + "px";
		this.obj.style.top = this.y + "px";
		clearInterval(this.inv);
		if (this.callback != null) this.callback();
		if (this.callbackEnd != null) this.callbackEnd();
		this.callback = null;
		return;
	}
	ats = timeUsed / this.timeUsage;
	pX = Math.round(this.xUse * ats);
	pY = Math.round(this.yUse * ats);
	this.x = this.beginX + pX;
	this.y = this.beginY + pY;
	this.obj.style.left = this.x + "px";
	this.obj.style.top = this.y + "px";
	if (this.callback != null) this.callback();
	//setTimeout(this.moving,this.interval);
}

objFree.prototype.movingM = function() {
	d = new Date();
	timeUsed = d.getTime() - this.timeBegin;
	if (d.getTime() >= this.timeEnd) {
		this.x = this.dstX;
		this.y = this.dstY;
		this.obj.style.marginLeft = this.x + "px";
		this.obj.style.marginTop = this.y + "px";
		clearInterval(this.inv);
		if (this.callback != null) this.callback();
		if (this.callbackEnd != null) this.callbackEnd();
		this.callback = null;
		return;
	}
	ats = timeUsed / this.timeUsage;
	pX = Math.round(this.xUse * ats);
	pY = Math.round(this.yUse * ats);
	this.x = this.beginX + pX;
	this.y = this.beginY + pY;
	this.obj.style.marginLeft = this.x + "px";
	this.obj.style.marginTop = this.y + "px";
	if (this.callback != null) this.callback();
	//setTimeout(this.moving,this.interval);
}

objFree.prototype.movingB = function() {
	d = new Date();
	timeUsed = d.getTime() - this.timeBegin;
	if (d.getTime() >= this.timeEnd) {
		this.x = this.dstX;
		this.y = this.dstY;
		this.obj.style.backgroundPosition = this.x + "px " + this.y + "px";
		//this.obj.style.marginTop = this.y + "px";
		clearInterval(this.inv);
		if (this.callback != null) this.callback();
		if (this.callbackEnd != null) this.callbackEnd();
		this.callback = null;
		return;
	}
	ats = timeUsed / this.timeUsage;
	pX = Math.round(this.xUse * ats);
	pY = Math.round(this.yUse * ats);
	this.x = this.beginX + pX;
	this.y = this.beginY + pY;
	this.obj.style.backgroundPosition = this.x + "px " + this.y + "px";
	//this.obj.style.marginTop = this.y + "px";
	if (this.callback != null) this.callback();
	//setTimeout(this.moving,this.interval);
}

objFree.prototype.movingBreak = function() {
	d = new Date();
	timeNow = d.getTime();
	timeUsed = timeNow - this.timeBegin;
	if (timeNow >= this.timeEnd) {
		this.x = this.dstX;
		this.y = this.dstY;
		this.obj.style.left = this.x + "px";
		this.obj.style.top = this.y + "px";
		clearInterval(this.inv);
		if (this.callbackEnd != null) this.callbackEnd();
		return;
	}
	degNow = timeUsed / this.timeUsage * 90;
	rad = degNow * Math.PI / 180;
	y = Math.sin(rad) * this.timeUsage;
	this.x = this.beginX + Math.round(y / this.timeUsage * this.xUse);
	this.y = this.beginY + Math.round(y / this.timeUsage * this.yUse);
	this.obj.style.left = this.x + "px";
	this.obj.style.top = this.y + "px";
	if (this.callback != null) this.callback();
}

objFree.prototype.moveBegin = function() {
	if (this.bMargin == 1) this.inv = setInterval("arrObjFree[" + this.nID + "].movingM()",this.interval);
	else if (this.bMargin == 2) this.inv = setInterval("arrObjFree[" + this.nID + ".movingB()",this.interval);
	else this.inv = setInterval("arrObjFree[" + this.nID + "].moving()",this.interval);
}

objFree.prototype.moveBreakBegin = function() {
	this.inv = setInterval("arrObjFree[" + this.nID + "].movingBreak()",this.interval);
}

objFree.prototype.destroy = function() {
	arrObjFree.slice(this.nID,1);
}

