FSite2.Anim = {};

FSite2.Anim.fps = 25;

FSite2.Anim.animations = new Array();

FSite2.Anim._interval = null;

FSite2.Anim._processAnimations = function()
{
	var i;
	
	for (i = 0; i < FSite2.Anim.animations.length; i++)
		if (FSite2.Anim.animations[i].step())
			i--;
			
	if (i == 0)
	{
		clearInterval(FSite2.Anim._interval);
		FSite2.Anim._interval = null;
	}
}

FSite2.Anim.linear = function(state)
{
	if (typeof state != 'object')
		return;
		state.step = (state.end - state.start) / (state.time / 1000 * FSite2.Anim.fps);
	state.value += state.step;
}

FSite2.Anim.test = function(state)
{
	if (typeof state != 'object')
		return;
	if (state.speed == null)
		state.speed = 10;
	state.value += (state.end - state.value) / state.speed;
}

/*
	object - obiekt, ktory bedzie animowany. Przykladowo podczas animacji styli nalezy ustawic object.style 
	property - wlasciwosc, ktora bedzie animowana. Przykladowo height, width, opacity...
	anim - funkcja animujaca aktualnie dostepa tylko w wersji linera
	animParams - definiuje poczatek i koniec animacji oraz czas w sekundach domyslnie ustawiony na 1 sec.
	valueParams - definiuje precyzje animacji domyslnie ustawiona na 1 oraz szablon animacji np "alpha(opacity=#)","#px"  
*/
FSite2.Animation = function(object, property, anim, animParams, valueParams)
{
	this.object = object;
	this.property = property;
	this.anim = anim;
	this.animParams = {
		time: 1
	};
	this.valueParams = {
		template: '#',
		precision: 1
	};
	var k;
	if (animParams && (typeof animParams == 'object'))
	{
		for (k in animParams)
			this.animParams[k] = animParams[k];
	}
	if (valueParams && (typeof valueParams == 'object'))
	{
		for (k in valueParams)
			this.valueParams[k] = valueParams[k];
	}
	if (!animParams.dontStart)
		this.start();
}

FSite2.Animation.prototype.start = function()
{
	if (typeof this.animParams.onStart == 'function')
		if (this.animParams.onStart() === false)
			return;
			
	if(this.animParams.start == undefined)
		this.animParams.start = parseFloat(this.object[this.property]);
		
	if(this.animParams.value == undefined)
		this.animParams.value = this.animParams.start
		
	this.animParams.lastValue = this.animParams.value;
	
	if(!this.isRunning())
		FSite2.Anim.animations.push(this);
		
	this.setValue();
	
	if (FSite2.Anim._interval == null)
		FSite2.Anim._interval = setInterval(FSite2.Anim._processAnimations, 1000 / FSite2.Anim.fps);
}

FSite2.Animation.prototype.isRunning = function()
{
	for (i = 0; i < FSite2.Anim.animations.length; i++)
		if(FSite2.Anim.animations[i] === this) 
			return true;
		
	return false;
}

FSite2.Animation.prototype.stop = function()
{
	for (i = 0; i < FSite2.Anim.animations.length; i++)
		if(FSite2.Anim.animations[i] === this)
			FSite2.Anim.animations.splice(i, 1);	
}

FSite2.Animation.prototype.step = function()
{
	this.animParams.lastValue = this.animParams.value;
	this.anim(this.animParams);
	if (typeof this.animParams.onStep == 'function')
		if (this.animParams.onStep() === false)
			return true;
			
	return this.setValue();
}

FSite2.Animation.prototype.setValue = function()
{
	var newValue = Math.round(this.animParams.value / this.valueParams.precision) * this.valueParams.precision;

	if (((this.animParams.lastValue <= this.animParams.end) && (newValue >= this.animParams.end) || (this.animParams.lastValue >= this.animParams.end) && (newValue <= this.animParams.end)))
		newValue = this.animParams.end;
	var newValueStr = this.valueParams.template.replace('#', newValue);
	if (this.object[this.property] != newValueStr)
		this.object[this.property] = newValueStr;
		
	if(newValue == this.animParams.end)
	{
		this.stop();
		if (typeof this.animParams.onComplete == 'function')
		{
			var res = this.animParams.onComplete();
			if ((typeof this.animParams.chainAnimation == 'object') &&
				(this.animParams.chainAnimation.constructor === FSite2.Animation) &&
				res !== false)
				this.animParams.chainAnimation.start();
		}
		
		this.animParams.value = undefined;
		this.animParams.lastValue = undefined;		
		return true;
	}
	else
		return false;
}

FSite2.Anim.animatePixels = function(object, styleProperty, anim, animParams)
{
	return new FSite2.Animation(object.style, styleProperty, anim, animParams, {template: '#px', precision: 1});
}

FSite2.Anim.animateOpacity = function(object, anim, animParams)
{
	if (window.navigator.userAgent.indexOf('MSIE') === -1)
		return new FSite2.Animation(object.style, 'opacity', anim, animParams, {precision: 0.01});
	else
	{
		var filterParams = {};
		for (var k in animParams)
			filterParams[k] = animParams[k];
		filterParams.start = Math.round(filterParams.start * 100);
		filterParams.end = Math.round(filterParams.end * 100);
		return new FSite2.Animation(object.style, 'filter', anim, filterParams, {template: 'alpha(opacity=#)'});
	}
}

