// -----------------------------
// RQTooltip
// -----------------------------
var RQTooltip = 
{
	divalt : null,
	visible : false,
	zIndex	: 1000,
	m_isFadingOut:false,
	timer : null,
	voidImg: "Data/Img/tooltipLoader.gif",
	isRight: false,
	yDest:0,
	show : function(attr,obj,opts)
	{	
		this.isRight = false;
		var x		= $(obj).offset().left;
		x-=25;		// ?
		x -= parseInt($(".all").css("left"));	// KDVI centering
		if( opts.dx) x+=opts.dx;
		if( $('#column3').get(0))
		{	var wCtn = parseInt($('#column3').css('left'))-50; RQDebug("wCtn="+wCtn+" x="+x); 	// KDVI download PDF
			if( x>wCtn)
			{	RQDebug("isRight");
				this.isRight=true;
				x=wCtn;
				
			}
		}


		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};
		}
		this.onLayoutChanged();

		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);

	}
	// onLayoutChanged: our custom function
	, onLayoutChanged: function()
	{
		// KDVI
		RQDebug("tooltip onLayoutChanged");
		var w=this.divalt.width();
		if( this.isRight)
		{
			this.divalt.find(".shadowBR").css({width: 7,left:w-7});
			this.divalt.find(".arrShadow").css({left:w-57});
			this.divalt.find(".shadowBL").css({left:0, width:w-57});
		}
		else
		{	this.divalt.find(".shadowBL").css({left:0, width:7});
			this.divalt.find(".arrShadow").css({left:7});
			this.divalt.find(".shadowBR").css({left:57, width: w-(7+50)});
		}

	}
	,
	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},500,"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) && attr)
							{
								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
								{	
								
									// kdvi underline
									var regName = new RegExp($(this).text(),"g");
									attr=attr.replace(regName,"<span class=\"und bord\">"+$(this).text()+"</span>");

									var reg = new RegExp("\._\.","g");
									attr="<span>"+attr.replace(reg,"<br />")+"</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();}
}
