function toolTip() 
{
	toolTip.hideAllTips = function()
	{
		var arrSpans = document.getElementsByTagName("span");
		for(var i = 0; i < arrSpans.length; ++i)
		{
			var eltSpan = arrSpans[i];
			if (eltSpan.className == "ToolTipVisible")
				eltSpan.className = "ToolTip";
		}
		return true;
	};

	toolTip.setPosition = function(element, modulename)
	{
		if (typeof(s_iE)== "undefined")
		{
			s_x=document;
			s_ua=navigator.userAgent.toLowerCase();
			s_iE=s_x.all&&s_ua.indexOf("msie")!=-1?1:0;
		}
    var offsetTrail = element;
    var offsetLeft = 0;
    var offsetTop = 0;
		var pxDifference = s_iE==1?20:10;
    while (offsetTrail)
		{
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    var visibleTop = 0;
    var visibleBottom = 0;
    var spans = element.getElementsByTagName("span");
    var i;
    if (window.innerHeight)
		{
			visibleTop = window.pageYOffset;
			visibleBottom = window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		{
			visibleTop = document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			visibleTop = document.body.scrollTop;
			visibleBottom = document.body.offsetHeight;
		}
    for (i = 0; i < spans.length; i++)
		{
			if (spans[i].className == "ToolTipBox" && spans[i].style)
			{
				if ((((offsetTop - visibleTop) + spans[i].offsetHeight) > (visibleBottom-pxDifference)) &&
					((offsetTop - visibleTop) > spans[i].offsetHeight))
					spans[i].style.top = "-" + (spans[i].offsetHeight) + "px";
				else
				{
					if(modulename != 'marketmovers')
						spans[i].style.top = s_iE==1?"14px":"13px";
					else
						spans[i].style.top = "-" + (spans[i].offsetHeight) + "px";
				}
				// The 230 is to accomodate for the chart
				if ((offsetLeft + spans[i].offsetWidth) > (document.body.offsetWidth - 230) && (offsetLeft - spans[i].offsetWidth) > 0)
					{
						if(modulename != 'marketmovers')
							spans[i].style.left = ((element.offsetWidth - spans[i].offsetWidth)) + "px";
						else
							spans[i].style.left = "-1px";/*((element.offsetWidth - spans[i].offsetWidth)) + "px";*/
					}
				else
					spans[i].style.left = "-1px";
			}
		}
	};

	toolTip.setup = function(modulename)
	{
		var arrSpans = document.getElementsByTagName("span");
		for(var i = 0; i < arrSpans.length; ++i)
		{
			var eltSpan = arrSpans[i];
			if (eltSpan.className == "ToolTip")
			{
				eltSpan.onmouseover = function() 
				{ 
					if (window.document.getElementById("box_"+this.getAttribute("rnd")).innerHTML.indexOf("Loading...")!= -1 && window.document.getElementById("box_"+this.getAttribute("rnd")).innerHTML.indexOf("zawya_loading_img2.")!= -1)
					{	
						//var url = "/zscripts/xml.cfm?companyid="+this.getAttribute("companyid")+"&origin="+this.getAttribute("origin")+"&relatedCompany";
						switch(this.getAttribute("relatedtype")){
							case "Company":
								var url = "/zscripts/xml.cfm?companyid="+this.getAttribute("relatedid")+"&origin="+this.getAttribute("origin")+"&relatedCompany&ric="+this.getAttribute("ric");			
								break;
							case "Officer":
								var url = "/zscripts/xml.cfm?officerid="+this.getAttribute("relatedid")+"&origin="+this.getAttribute("origin")+"&relatedOfficer";
								break;
							case "Disclosures":
								var url = "/zscripts/xml.cfm?announcementid="+this.getAttribute("relatedid")+"&origin="+this.getAttribute("origin")+"&relatedDisclosures";
						}
						ajaxCaller.getPlainText(url,loadDataBox,this.getAttribute("rnd"));
					}
					this.className = "ToolTipVisible";
					if(modulename != 'marketmovers')
						toolTip.setPosition(this);
					else
						toolTip.setPosition(this,modulename);
					return true;
				};
				eltSpan.onmouseout = function() 
				{
					this.className = "ToolTip";
					return true;
				};	
			}
		}
	};
} 

toolTip();


function loadDataBox(text,callingContext,headers)
{
	if (text.replace(/ /g,"") != "")
	{
		var boxMainElts = window.document.getElementsByName("toolTip_"+callingContext)
		var elt = -1;
		for (var i=0;i<boxMainElts.length;i++)
		{
			if (boxMainElts[i].className == "ToolTipVisible")
				elt = i;
			boxMainElts[i].className = "ToolTip";
		}
		var boxElts = window.document.getElementsByName("box_"+callingContext);
		for (var i=0;i<boxElts.length;i++)
			boxElts[i].innerHTML = text;
		if (elt != -1)
		{
			toolTip.setPosition(boxMainElts[elt]);
			boxMainElts[elt].onmouseover();
		}
	}
}