﻿registerNameSpace('KalCode');

KalCode.Animation = function(element_id, duration){
	//variables
	this.element_id = element_id;
	
	
	//parameters
	this.paremeters = new Array();
	this.timer = null;
	this.interval = 0;
	this.stepCount = 0;

	this.fps = 0;
	this.steps = 1406;
	this.duration = duration;
	this.onComplete = null;
	this.onAnime = null;
	
	if (this.fps > 0)
	{
		this.interval = Math.floor(1000 / this.fps);
		this.steps = parseInt((this.duration + (this.interval - 1)) / this.interval);
	}
	else if (this.steps > 0)
		this.interval = parseInt(this.duration / this.steps);
		
this.addParameter = function(start_value, end_value, css_property){
	var val = parseInt((end_value - start_value)/this.steps);
	var params = {start:start_value, end:end_value, property:css_property, unit_value:val}
	this.paremeters[this.paremeters.length] = params;
};
this.stepAnimation = function(){
	++this.stepCount;
	this.animate();
	if (this.stepCount < this.steps)
	{
		this.start(true);
		
	}
	else if (this.onComplete)
		this.onComplete();
		
	
};
this.animate = function(){
	for(i=0;this.paremeters.length >i; i++){
		var start = this.paremeters[i].start;
		var end = this.paremeters[i].end;
		var property = this.paremeters[i].property;
		var unit_value = this.paremeters[i].unit_value;
		var result = parseInt(start + (this.stepCount * unit_value));
	
		if(property == 'backgroundPosition'){
			$(this.element_id).style.backgroundPosition = result + 'px 1px';
		
			
		}
		else
		{
			try{setStyle(this.element_id, property, result + 'px');}catch(e){
				setStyle(this.element_id, property, result);
			}
		}
		
		if(this.onAnime)
			this.onAnime();
	}
	
	
};
this.start = function(is_next){
	var self = this;
	if(is_next == true)
		this.timer = setTimeout(function() { self.stepAnimation(); }, this.interval);
	else
		this.stepAnimation();
};

}


