// -----------------------------
// RQTooltip
// -----------------------------
var RQTooltip = 
{
	divalt : null,
	visible : false,
	zIndex	: 1000,
	m_isFadingOut:false,
	timer : null,
	voidImg: "Data/Img/tooltipLoader.gif",
	yDest:0,
	show : function(attr,obj,opts)
	{	
		var x		= $(obj).offset().left;
		if( opts.dx) x+=opts.dx;
		x-=25;		// ?
		var y		= $(obj).offset().top;
		var contentDiv=this.divalt.find(".inner");
		contentDiv.html( attr);
		var hAlt = this.divalt.height();
		if( hAlt<40) hAlt=40;
		y-=hAlt;
		p = {left:x,top:y,opacity:1.0};

		if (!this.visible)
		{
			this.visible = true;
			this.divalt.css({left:x,top:y,opacity:0,display:"block"});
			p = {opacity:1.0};
		}

		if (this.timer)
		{
			self.clearTimeout(this.timer);
			this.timer = null;
		}
		if(this.m_isFadingOut)
		{
			this.divalt.stop();
			this.m_isFadingOut=false;
		}

		this.divalt.animate(p,{ queue:false, duration:200 });


		// load images within the div
		var thisObj = this;
		contentDiv.find("img").map(function(index){ $(this).load(RQDelegate(thisObj,RQTooltip.cbImgLoaded)).attr('src',$(this).attr('alt'));    });

		// reposition on screen overflow
		this.repositionTooltip(y);

	}
	,
	repositionTooltip:function(y)
	{
		var yBottom = $(document).scrollTop()+$(window).height();
		var tipH = this.divalt.height();
		if( y+tipH > yBottom)
		{	
			this.divalt.animate({top:yBottom-tipH-5},{ queue:false, duration:200 });
		}

	}
	,
	cbImgLoaded:function()
	{
		this.repositionTooltip(this.yDest);
	}
	,
	hide : function()
	{
		this.timer = self.setTimeout(RQDelegate(this,this.doHideAlt), 500);
	}
	,
	doHideAlt:function()
	{	this.timer = null
		this.m_isFadingOut= true;
		this.divalt.animate({opacity:0},1000,"linear",RQDelegate(this,this.cbHideAltEnd));
	}
	,
	cbHideAltEnd:function()
	{
		this.visible = false;
		this.m_isFadingOut = false;
		this.timer = null;
		if( this.divalt)
			this.divalt.hide();

	}
	,
	// clicking the tooltip results on hiding it completely
	onDivClick:function()
	{
		this.cbHideAltEnd()
	}
	,
	// strPath: a jquery path of elements to which assign a tooltip
	// tipDivId: the id of the tooltip div. Default is 'tooltip'
	M_install : function(strPath,tipDivId)
	{
		if( ! tipDivId) 
			tipDivId='tooltip';
		if(this.divalt		= $('#'+tipDivId))
		{	this.divalt.css({position:"absolute", display:"none"}).click(RQDelegate(this,this.onDivClick) );
			this.divalt.css({zIndex:this.zIndex});
		}
		$(strPath).each(
						function()
						{   
							var attr	= $(this).attr("title");
							if( attr != undefined)
							{
								if( attr=='#')
								{	var tipDiv=$("#"+$(this).attr("id")+"_info");
									//clear imgs
									//tipDiv.find("img").map(function(index){ $(this).attr('alt',$(this).attr('src'));$(this).attr('src',RQTooltip.voidImg) });
									attr = tipDiv.html();											
								
								}
								else
									attr="<span>"+attr+"</span>";
								
								$(this).mouseover	( function(){RQTooltip.show(attr,$(this),{dx:$(this).width()/3} )});  
								$(this).mouseout	( function(){RQTooltip.hide();} );  

								$(this).attr("title", "");
							
							} 
						}
					);
	}
	,
	changeLabel : function()
	{
	}
	,
	rollover : function(){this.show();}
	,
	rollout : function(){this.show();}
}
