/*
* Copyright 2009 under GNU/GPL v2.0
*/

function AnimationClass() {
	
	this.fadeOut = function(element, callback) {
		var callbackCalled = false;
		animate({node: element, styles: ['-moz-opacity', 'opacity'], startValue: 1.0, endValue: 0.0, time: 400, onAnimation: function() {
			if(callback) {
				if(!callbackCalled)
					callback();
				callbackCalled = true;
			}
		}});
		animate({node: element, styles: ['filter'], valuePrefix: 'alpha(opacity=', valueSuffix: ')', startValue: 100, endValue: 0, time: 400, onAnimation: function() {
			if(callback) {
				if(!callbackCalled)
					callback();
				callbackCalled = true;
			}
		}});
	};
	
	this.fadeIn = function(element, callback) {
		var callbackCalled = false;
		animate({node: element, styles: ['-moz-opacity', 'opacity'], startValue: 0.0, endValue: 1.0, time: 400, onAnimation: function() {
			if(callback) {
				if(!callbackCalled)
					callback();
				callbackCalled = true;
			}
		}});
		animate({node: element, styles: ['filter'], valuePrefix: 'alpha(opacity=', valueSuffix: ')', startValue: 0, endValue: 100, time: 400, onAnimation: function() {
			if(callback) {
				if(!callbackCalled)
					callback();
				callbackCalled = true;
			}
		}});
	};
	
	var animate = function(opts) {
		if(opts.value != 0 && !opts.value || opts.value==null)
			opts.value = opts.startValue;
		if(!opts.stepValue && opts.stepValue!=0 || !opts.interval && opts.interval!=0)
			if(opts.time){
				opts.interval = 50;
				if(opts.time>0)				
					opts.stepValue = (opts.endValue-opts.startValue)/(opts.time/opts.interval);
				else
					opts.stepValue = opts.endValue-opts.startValue;
			}
		opts.value = (opts.value*100 + opts.stepValue*100)/100;
		if(opts.value > opts.endValue && opts.stepValue > 0 || opts.value < opts.endValue && opts.stepValue < 0){
			opts.value = opts.endValue;
			for(i in opts.styles){
				try{
					var finalValue = opts.value;
					if(opts.valueSuffix)
						finalValue = finalValue + opts.valueSuffix;
					if(opts.valuePrefix)
						finalValue = opts.valuePrefix + finalValue;
					opts.node.style[opts.styles[i]] = finalValue;
				}catch(e){}
			}
			if(opts.onAnimation)
				opts.onAnimation();
			return;
		}
		for(i in opts.styles){
			try{
				var finalValue = opts.value;
				if(opts.valueSuffix)
					finalValue = finalValue + opts.valueSuffix;
				if(opts.valuePrefix)
					finalValue = opts.valuePrefix + finalValue;
				opts.node.style[opts.styles[i]] = finalValue;
			}catch(e){}
		}
		setTimeout(function(){
			animate(opts);
		}, opts.interval);
	};
}

var Animation = new AnimationClass();

