//set up the RD object
if(!window.RD){
	window.RD = {};
}

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

RD.XHTML.infoRollover = function() {
	if(!RD.XHTML.infoRollover.Manager){
		RD.XHTML.infoRollover.Manager = this;
	}
	
	return RD.XHTML.infoRollover.Manager;
};

RD.XHTML.infoRollover.Manager = null;
RD.XHTML.infoRollover.getManager = function(){
	return new RD.XHTML.infoRollover();
};

RD.XHTML.infoRollover.prototype.init = function(){
	var vImages = $$(document, "img");
	var vImage;
	var vInfo;
	for(var n=0;n<vImages.length;n++){
		vImage = vImages[n];
		vInfo = vImage.getAttribute("info");

		if(vInfo != null && vInfo.length > 0){
			vImage.onmouseover = function(evt){
				var vSrc = this.getAttribute("origSrc");
				if(!vSrc){
					vSrc = this.src;
				}
				
				this.src = vSrc.replace("-out", "-over");
				
				if(RD.XHTML.fixPng){
					RD.XHTML.fixPng(this);
				}

				RD.XHTML.infoRollover.getManager().showInfo(evt, "over");
			};
			vImage.onmouseout = function(evt){
				var vSrc = this.getAttribute("origSrc");
				if(!vSrc){
					vSrc = this.src;
				}
				this.src = vSrc.replace("-over", "-out");
				
				if(RD.XHTML.fixPng){
					RD.XHTML.fixPng(this);
				}
				
				RD.XHTML.infoRollover.getManager().showInfo(evt, "out");
			};
		}
	}
}

RD.XHTML.infoRollover.prototype.showInfo = function(evt, aAction){
	evt = evt?evt:window.event;
	var vTarget = evt.target?evt.target:evt.srcElement;
	
	if(vTarget){
		var vInfo = vTarget.getAttribute("info");
		var vInfoDiv = $("divInfo");

		if(vInfo && vInfoDiv){
			
			vInfoDiv.style.display = "block";
			vInfoDiv.firstChild.nodeValue = vInfo;
			if(!this._aniInfo){
				this._aniInfo = new RD.XHTML.Animation({
					changeNode:vInfoDiv
					,changeStyleProperty:["left", "width", "opacity"]
					,intervalAmount:33
					,startTime:0
					,fill:"freeze"
					,callback:this.onAnimate
				});
				
				this._animatingInfo = {
					type:String.empty
					,eventTarget:vTarget
				}
			}
			
			var vInfoStatus = this._aniInfo._status;
			if(vInfoStatus == RD.XHTML.Animation.STATUS.ON){
				this._aniInfo.end();
				if(this._animatingInfo.eventTarget == vTarget){
					this._aniInfo._startValue = this._aniInfo._currentValue.duplicate();
				}
			}
			
			var vPoint = RD.XHTML.GetAbsPosition(vTarget);

			var vStartRect = new RD.XHTML.Rect(
				vPoint.x - vTarget.offsetWidth-6
				,vPoint.y
				,200
			);

			if(aAction == "over" && this._animatingInfo.type != "over"){
				this._animatingInfo.type = aAction;
				
				this._aniInfo._changeAmount = [-30,30,15];
				this._aniInfo._endValue = [vStartRect.x-vStartRect.width,vStartRect.width,100];
				
				if(vInfoStatus != RD.XHTML.Animation.STATUS.ON || this._animatingInfo.eventTarget != vTarget){
					this._aniInfo._startValue = [vStartRect.x,0,0];
				}

				vInfoDiv.style.left = vStartRect.x + "px";
				vInfoDiv.style.top = vStartRect.y + "px";
				
				this._animatingInfo.eventTarget = vTarget;
				this._aniInfo.start();
			}
			else if(aAction == "out" && this._animatingInfo.type != "out"){
				this._animatingInfo.type = aAction;
				//vStartRect.x = parseInt(vInfoDiv.style.left, 10);
				this._aniInfo._changeAmount = [30,-30,-15];
				this._aniInfo._endValue = [vStartRect.x,0,0];
				
				if(vInfoStatus != RD.XHTML.Animation.STATUS.ON || this._animatingInfo.eventTarget != vTarget){
					this._aniInfo._startValue = [vStartRect.x-vStartRect.width,vStartRect.width,100];
				}
				
				this._animatingInfo.eventTarget = vTarget;
				this._aniInfo.start();
			}

			
		}
	}
};

RD.XHTML.infoRollover.prototype.onAnimate = function(aAnimate){
	if(aAnimate._status == RD.XHTML.Animation.STATUS.OFF && aAnimate.getOpacity() == 0){
		aAnimate._changeNode.style.left = "-1000px";
	}
};

RD.XHTML.infoRollover.getManager().init();