/**
 * wrapper for loading-icon while loading content into page
 * @param listenerName
 * @param _htmlElementId
 * @param _renderer
 * @return
 */

function ContentRenderListener(listenerName,_htmlElementId,_renderer){
	this.base = Listener;
	this.base(listenerName);
	this.htmlElementId = _htmlElementId;
	this.renderer = _renderer;
	this.fade = 1.0;
};

ContentRenderListener.faders = {};
ContentRenderListener.prototype = new Listener();
ContentRenderListener.prototype.timeout=null;

/* called on update */
ContentRenderListener.prototype.notify = function(object){
	this.renderer.render(object,this.htmlElementId);
};

/* notify About Incoming Change. */
ContentRenderListener.prototype.notifyAboutIncomingChange = function(){
	var textBox = $(this.htmlElementId);
	if(textBox){
		var loadingDiv = $(this.htmlElementId+"_Loading");
		if(loadingDiv){
			setDisplay(this.htmlElementId+"_Loading",1);				
		}else{
			var parent = textBox.parentNode;	
			var imgTag =  document.createElement("img");
			imgTag.setAttribute("src",window["contextPath"]+"/images/loading.gif");
		
			var divTag = document.createElement("div");
			divTag.setAttribute("id",this.htmlElementId+"_Loading");
			divTag.setAttribute("class","loading");
		
			divTag.setAttribute("className","loading");
			
			divTag.appendChild(imgTag);
			parent.appendChild(divTag);
			
			setDisplay(this.htmlElementId+"_Loading",1);
		}
	}else{
		alert("no html element found with id:" + this.htmlElementId);
	}
};

ContentRenderListener.prototype.fadeOut = function(){
	var divLoading = document.getElementById(this.htmlElementId+"_Loading");
	this.fade = this.fade - 0.10;
	divLoading.style.opacity = this.fade;
	ContentRenderListener.faders[this.htmlElementId] = this; 
	if (this.fade > 0.0){
		ContentRenderListener.faders[this.htmlElementId].timeout = setTimeout("if (ContentRenderListener.faders['"+this.htmlElementId+"']) ContentRenderListener.faders['"+this.htmlElementId+"'].fadeOut();",50);
	}else{
		setDisplay(this.htmlElementId+"_Loading",0);
 		divLoading.style.opacity = 1.0;
 		this.fade = 1.0;
 		clearTimeout(ContentRenderListener.faders[this.htmlElementId].timeout);
 		delete ContentRenderListener.faders[this.htmlElementId]; 
	}
};

ContentRenderListener.prototype.notifyAboutIncomingChangeStop = function(){
	var divLoading = document.getElementById(this.htmlElementId+"_Loading");
  	if(divLoading) this.fadeOut();
};
