//-----------------------------------\\
//        PROJECT BlueStorm          \\
//	  BluEMotion 				     \\
//        version 1.0                \\
//-----------------------------------\\

var BlueMotion = {
	Name: "Project BlueStorm: BluEMotion",
	Version: "1.0",
	Comment: "Beta 1"
}


var BlueMotionTimeline = new Class;
	BlueMotionTimeline.prototype.object = null;

	BlueMotionTimeline.prototype.FPS = 25;
	BlueMotionTimeline.prototype.currentFrame = 0;
	BlueMotionTimeline.prototype.currentSubFrame = 0;
	BlueMotionTimeline.prototype.currentKeyFrame = 0;
	BlueMotionTimeline.prototype.keyFrames = new Array();
	BlueMotionTimeline.prototype.steps = new Array();
	BlueMotionTimeline.prototype.backBuffer = new Array();

	BlueMotionTimeline.prototype.onKeyFrame = null;
	BlueMotionTimeline.prototype.onStart = null;
	BlueMotionTimeline.prototype.onEnd = null;
	BlueMotionTimeline.prototype.onFrame = new Array();

	BlueMotionTimeline.prototype.addKeyFrame = function(i,style){ 
													if (isString(style)){
														var tmp = new Class;
														style = style.replace(/ /gim,"");
														var sarr = style.split(';');
														sarr.walk(function(v,i){ 
															var arr = v.split(":");
															tmp[arr[0]] = arr[1];
														} );
													} else { tmp = style; }
													this.keyFrames[i] = tmp;
												}
												
	BlueMotionTimeline.prototype.addKeyframe = BlueMotionTimeline.prototype.addKeyFrame;
												
	BlueMotionTimeline.prototype.assignObject = function(obj,leavekf){ 
													this.object = obj; 
													if (!leavekf){ this.keyFrames = new Array(); }
													if (obj.className){	ClassToStyle($CSSClass('.'+obj.className),obj); }
													this.addKeyFrame(0,obj.style); 
												}
	BlueMotionTimeline.prototype.assign = BlueMotionTimeline.prototype.assignObject;
	BlueMotionTimeline.prototype.grab = BlueMotionTimeline.prototype.assignObject;
	BlueMotionTimeline.prototype.step = function(){ 
	
													// WEJSCIE NA KEYFRAME
													if (this.keyFrames[this.currentFrame]){
													
														if (this.onKeyFrame){ this.onKeyFrame(this.currentFrame); }
													
														this.backBuffer = new Array();
													
														this.currentSubFrame=0; 
														this.currentKeyFrame=this.currentFrame; 
														ApplyCSSStyle(this.keyFrames[this.currentFrame],this.object);

														var nextKF = this.keyFrames.next(this.currentFrame);
														this.steps = new Array();
														for (s in nextKF[1]){
															if (!nextKF[1][s].isFunction() && s!='prototype' && parseInt(nextKF[1][s])){
																var nowval = parseFloat(this.keyFrames[this.currentFrame][s]);
																if (!nowval){ nowval = parseFloat(this.object.style[s]); }
																if (!nowval){ nowval = 0; if (s=='Alpha'){ nowval = 100; } }
																var nextval = parseFloat(nextKF[1][s]);
																var diff = nextval - nowval;
																var framesdiff = nextKF[0] - this.currentFrame; 
																this.backBuffer[s] = nowval;
																this.steps[s] = diff / framesdiff;
															}
														}
													}
													
													if (this.onEveryFrame){ this.onEveryFrame(this.currentFrame); }
													if (this.onFrame[this.currentFrame]){ this.onFrame[this.currentFrame](); }
													
													// WYKONANIE KROKU
													var evalcode = '';
													for (s in this.steps){
														if (!this.steps[s].isFunction() && s!='prototype'){
															if (!this.backBuffer[s]){ this.backBuffer[s] = 0; }
															this.backBuffer[s] = parseFloat(this.backBuffer[s])+parseFloat(this.steps[s]);
															
															if (s!='Alpha' && s!='Display'){
																evalcode += "this.object.style['"+s+"'] = this.backBuffer['"+s+"']; ";
															}
															
															if (s=='Alpha'){
																var alpha = Math.floor(this.backBuffer['Alpha']);
																if (document.all){ evalcode += 'this.object.style.filter="alpha(style=0, opacity='+alpha+')"'; }
																else {
																	if (alpha<100){ alpha = parseFloat('0.'+alpha); } else { alpha = 100; } 
																	evalcode += "this.object.style.MozOpacity = "+alpha+"; "; 
																	}
															}
															
															if (s=='Display'){
																if (this.backBuffer['Display']=='1'){ evalcode += "this.object.style.display = 'none';"; }
																if (this.backBuffer['Display']=='2'){ evalcode += "this.object.style.display = '';"; }
															}
															
														}
													}
													eval(evalcode);

													this.currentFrame++; 
													this.currentSubFrame++;
													
													if (this.currentFrame==this.keyFrames.length && this.onEnd){ this.onEnd(); }
												  }
												  
	BlueMotionTimeline.prototype.fire = function(){
												  	var THIS = this;
												  	var tr = new TimeRun;
												  	tr.delay = 1000/this.FPS;
												  	tr.times = this.keyFrames.length;
												  	tr.func = function(){ THIS.step(); }
												  	
													this.currentFrame = 0;
													this.currentSubFrame = 0;
													this.currentKeyFrame = 0;
												  	
												  	if (this.onStart){ this.onStart(); }
												  	tr.fire();
												  }
												  
	BlueMotionTimeline.prototype.start = BlueMotionTimeline.prototype.fire;
