/**
 * exsample: 	var pattern = { label:[[x,y],[x,y]] };
 * 				new AnimationXY($("#object"),pattern).On("label",50);
 * depend: jquery
 */
function AnimationXY(cs,animation_pattern){
	this._cs = cs;
	var bgpos = (cs.css("backgroundPosition")
				|| cs.css("background-position-x")+" "+cs.css("background-position-y")
				).split(" ");
	this._x = parseInt(bgpos[0]);
	this._y = parseInt(bgpos[1]);
	
	this._patterns = animation_pattern;
	this._scene = this._frame = 0;
	this._repeat_cn = this._repeat = 0;
	this._time_id = null;
}

AnimationXY.prototype = {
	On: function(scene,interval,repeat){
		this._repeat=repeat||0;
		this.Off();
		this._scene=scene;
		var This=this;
		var len = This._patterns[This._scene].length;
		this._time_id = setTimeout(function _Local(){
			(function(){
				var ptn = This._patterns[This._scene][This._frame]||[0,0];
				This._x = ptn[0]; This._y = ptn[1];
				This._repeat_cn += This._frame+1>=len?1:0;
				This._frame = (This._frame+=1)>=len?0:This._frame;
				This._cs.css("backgroundPosition",[(-This._x),"px ",(-This._y),"px"].join(""));
			})();
			This._time_id=setTimeout(_Local,interval);
			if(This._repeat>0&&This._repeat_cn==This._repeat)This.Off();
		},interval);
		return this;
	},
	Off: function(repeat){
		repeat=repeat||0;
		this._repeat_cn=0;
		if(repeat==0){
			if(this._time_id)
				clearTimeout(this._time_id);
			this._time_id=null;
		}else{
			if(this._repeat <= 0)
				this._repeat=repeat;
			else this._repeat_cn=this._repeat-repeat;
		}
	}
};