if(!window.RD){
	window.RD = {};
}

if(!RD.XHTML){
	RD.XHTML = {};
}

RD.XHTML.Animation = function(aArgs){
	this.init(aArgs);
};

RD.XHTML.Animation.TYPE = {
	MOVE:0
	,OPACITY:1
	/*
	,SWAP:2
	//,ROTATE:3 -- currently not supported
	*/
};

RD.XHTML.Animation.STATUS = {
	ERROR:0
	,ON:1
	,OFF:2
	,PAUSED:3
};

RD.XHTML.Animation.prototype.init = function(aArgs){
	/*
	this._pauseAmount = aArgs.pauseAmount?aArgs.pauseAmount:0;
	this._pauseTime = 0;
	*/

	this._callId = null;
	this._status = RD.XHTML.Animation.STATUS.OFF;
	this._statusMessage = String.empty;
	this._alertErrors = (aArgs.alertErrors != null)?aArgs.alertErrors:false;
	this._intervalAmount = aArgs.intervalAmount?aArgs.intervalAmount:33;
	this._startTime = aArgs.startTime?aArgs.startTime:0;
	this._repeatCount = aArgs.repeatCount?aArgs.repeatCount:0;
	this._fill = aArgs.fill?aArgs.fill:"remove";
	this._currentRepeatCount = null;
	this._currentTime = 0;
	this._effectTypes = [];
	
	this._changeAmount = (aArgs.changeAmount != null)?aArgs.changeAmount:[1];
	this._changeStyleProperty = aArgs.changeStyleProperty?aArgs.changeStyleProperty:["left"];
	this._startValue = (aArgs.startValue != null)?aArgs.startValue:[0];
	this._endValue = (aArgs.endValue != null)?aArgs.endValue:[0];
	this._changeNode = aArgs.changeNode;
	this._callback = aArgs.callback;
	
	if(typeof(this._changeAmount) != "object"){
		this._changeAmount = [this._changeAmount];
	}
	if(typeof(this._changeStyleProperty) != "object"){
		this._changeStyleProperty = [this._changeStyleProperty];
	}
	if(typeof(this._startValue) != "object"){
		this._startValue = [this._startValue];
	}
	if(typeof(this._endValue) != "object"){
		this._endValue = [this._endValue];
	}

	for(var n=0;n<this._changeStyleProperty.length;n++){
		switch(this._changeStyleProperty[n]){
			case "opacity":
				this._effectTypes.push(RD.XHTML.Animation.TYPE.OPACITY);
				break;
			default:
				this._effectTypes.push(RD.XHTML.Animation.TYPE.MOVE);
				break;
		}
	}
	
	if(this._changeNode){
		if(typeof(this._changeNode) == "string"){
			this._changeNode = $(this._changeNode);
		}
	}
	
	if(!this._changeNode){
		this._status = RD.XHTML.Animation.STATUS.ERROR;
		this._statusMessage = "Change node does not exist";
	}
	
	var vTimerObj = RD.XHTML.Timer.getManager();
	this._timerId = vTimerObj.addCallback(this);
};

RD.XHTML.Animation.prototype.setStyleProperty = function(aProperty, aValue){
	switch(aProperty){
		default:
			if(typeof(aValue) != "string"){
				aValue = aValue.toString() + "px";
				break;
			}
			break;
	}
	
	this._changeNode.style[aProperty] = aValue;
};

RD.XHTML.Animation.prototype.start = function(){
	if(this._status == RD.XHTML.Animation.STATUS.ERROR){
		if(this._statusMessage.length > 0 && this._alertErrors){
			alert(this._statusMessage)
		}
	}
	else{
		if(this._currentRepeatCount != null && this._repeatCount != "indefinate"){
			this._currentRepeatCount++;
		}
		else{
			this._currentRepeatCount = 0;
		}
		
		this._status = RD.XHTML.Animation.STATUS.ON;
		this._currentValue = this._startValue.duplicate();
		this._currentTime = 0;
		
		this.animate(true);
		
		if(this._timerId){
			var vTimerObj = RD.XHTML.Timer.getManager();
			this._callId = vTimerObj.setInterval(this._timerId, "animate", this._intervalAmount);
		}
	}
};


RD.XHTML.Animation.prototype.end = function(){
	this._status = RD.XHTML.Animation.STATUS.OFF;
	//alert(this._callId)
	if(this._timerId && this._callId){
		var vTimerObj = RD.XHTML.Timer.getManager();
		vTimerObj.clearInterval(this._timerId, this._callId);
	}
	
	if(this._fill != "freeze"){
		this._status = RD.XHTML.Animation.STATUS.ON;
		this._currentValue = this._startValue.duplicate();
		this._currentTime = 0;
		
		this.animate(true);

		this._status = RD.XHTML.Animation.STATUS.OFF;
	}
	this.animateCallback();
};

RD.XHTML.Animation.prototype.animate = function(aInit){
	var vEnd = true;
	
	if((this._currentTime >= this._startTime/* || aInit*/) && this._status == RD.XHTML.Animation.STATUS.ON){
		var vTempEnd = true;
		for(var n=0;n<this._changeStyleProperty.length;n++){
			switch(this._effectTypes[n]){
				case RD.XHTML.Animation.TYPE.OPACITY:
					vTempEnd = this.opacity(n, aInit);
					break;
				case RD.XHTML.Animation.TYPE.MOVE:
					vTempEnd = this.move(n, aInit);
					break;
				default:
					vTempEnd = this.move(n, aInit);
					break;
			}
			
			if(!vTempEnd){
				vEnd = false;
			}
		}
		
		if(vEnd){
			this.end();
		}
	}
	
	this._currentTime += this._intervalAmount;
	
	if(!vEnd){
		this.animateCallback();
	}
	else{
		if(this._repeatCount == "indefinate" || (isNull(this._currentRepeatCount) || this._currentRepeatCount < this._repeatCount)){
			this.start();
		}
	}
};

RD.XHTML.Animation.prototype.animateCallback = function(){
	if(this._callback){
		if(typeof(this._callback) == "string"){
			eval(this._callback)(this);
		}
		else{
			this._callback(this);
		}
	}
}

RD.XHTML.Animation.prototype.opacity = function(aIndex, aInit){
	var vEnd = true;
	var vValue = this._currentValue[aIndex];
	if(aInit){
		vEnd = false;
	}
	else{
		vValue += this._changeAmount[aIndex];
		
		if((this._changeAmount[aIndex] > 0 && vValue >= this._endValue[aIndex]) || (this._changeAmount[aIndex] < 0 && vValue <= this._endValue[aIndex])){
			vValue = this._endValue[aIndex];
		}
		else{
			vEnd = false;
		}
	}

	this._currentValue[aIndex] = vValue;
	this.setOpacity(vValue);

	return vEnd;
};

RD.XHTML.Animation.prototype.move = function(aIndex, aInit){
	var vEnd = true;
	var vValue = this._currentValue[aIndex];
	if(aInit){
		vEnd = false;
	}
	else{
		vValue += this._changeAmount[aIndex];
		
		if((this._changeAmount[aIndex] > 0 && vValue >= this._endValue[aIndex]) || (this._changeAmount[aIndex] < 0 && vValue <= this._endValue[aIndex])){
			vValue = this._endValue[aIndex];
		}
		else{
			vEnd = false;
		}
	}

	this._currentValue[aIndex] = vValue;
	this.setStyleProperty(this._changeStyleProperty[aIndex], this._currentValue[aIndex]);

	return vEnd;
};

RD.XHTML.Animation.prototype.getOpacity = function(){
	vOpacityPercent = 0;
	
	if(this._changeNode.filters){
		var vOpacityDegree = this._changeNode.style.filter.toLowerCase().split("(opacity=");
		vOpacityDegree = vOpacityDegree[1].substring(0, vOpacityDegree[1].length-1);
		vOpacityPercent = parseInt(vOpacityDegree, 10);
	}
	else if(this._changeNode.style.KhtmlOpacity != null){
		vOpacityPercent =  parseInt(this._changeNode.style.KhtmlOpacity, 10) * .01;
	}
	else if(this._changeNode.style.MozOpacity != null){
		vOpacityPercent =  parseInt(this._changeNode.style.MozOpacity, 10) * .01;
	}
	else{
		vOpacityPercent =  parseInt(this._changeNode.style.opacity, 10) * .01;
	}
	
	return vOpacityPercent;
};

RD.XHTML.Animation.prototype.setOpacity = function(vOpacityPercent){
	vOpacityPercent = (vOpacityPercent > 0)?vOpacityPercent:0;

	this._changeNode.style.filter="alpha(opacity=" + vOpacityPercent + ")";

	this._changeNode.style.opacity = vOpacityPercent * 0.01;
	this._changeNode.style.KhtmlOpacity = vOpacityPercent * 0.01;
	this._changeNode.style.MozOpacity = vOpacityPercent * 0.01;
};

