﻿//	**************************************************************************
//	Jena.ToolTips - Javascript Library
//
//	Written by Robert Swiston
//
//	Copyright 2010, RomiDesigns - All Rights Reserved
//
//	No part of this script may be reproducted or altered without the
//	expressed consent of RomiDesigns or its representative(s)
//	**************************************************************************
var tTipArrowImage = "/App_Support/Images/Controls/ToolTips/Blue_rArrow.png",
	tScroller = {top:0, left:0, x:0, height:0, y:0, width:0, Scroller:null},
	tToolTip = {x:0, y:0};
	
function jenaToolTip(pElement)
{
	var self = this,
		toolTipId = "ToolTipObject";
	
	//clear the existing scroller object and zero all values
	this.clear = function()
	{
		tScroller = {top:0, left:0, x:0, height:0, y:0, width:0, Scroller:null};
	};
	//hide any existing tooltips
	this.close = function()
	{
		jCore("#" + toolTipId).remove();
	};
	//action to habdle the movement of the tooltip when a containing div is scrolled
	this.move = function(pObject)
	{
		if (tScroller.Scroller == null)
		{
			var scroller = jCore(pObject).pos().findAbsPosition(pObject);
			tScroller.x = scroller.x;
			tScroller.y = scroller.y;
			tScroller.Scroller = pObject;
			
			scroller = jCore(pObject).bounds();
			tScroller.height = scroller.height;
			tScroller.width = scroller.width;
		}
		var change = tScroller.top - pObject.scrollTop;
		
		tScroller.top = pObject.scrollTop;
		tScroller.left = pObject.scrollLeft;
		
		var tip = jCore("#" + toolTipId);
		
		if (!tip.isNull)
		{
			tip.show().pos().top((tToolTip.y - tScroller.top) + "px").pos().left((tToolTip.x - tScroller.left) + "px");
			var centerY = (tToolTip.y - tScroller.top) + (tip.bounds().height / 2);
		
			if (centerY < tScroller.y || centerY > (tScroller.height + tScroller.y)) tip.hide();
		}
	};
	//show the current tooltip
	this.show = function(pText)
	{
		var tip = jCore("#" + toolTipId);
			
		if (tip.isNull)
		{
			tip = jCore(body)
				.append("table").padding("0").spacing("0").css(toolTipId).id(toolTipId)
				.append("thead").append("tr").append("td").css("Content").id(toolTipId + "Content").parent()
				.append("td").css("Arrow").append("img").src(tTipArrowImage).parent(4);
		}
		
		tip.find("td>@Content").text(pText).parent(3).show();
		
		var Left = tip.bounds().width,
			Top = (tip.bounds().height / 2) - (pElement.offsetHeight / 2);
			
		tip.pos().left(-(Left + tScroller.left), pElement).pos().top(-(Top + tScroller.top), pElement);
		tToolTip = {x:parseInt(tip.pos().left()), y:parseInt(tip.pos().top())};
		
		if (tScroller.Scroller != null && tScroller.top > 0)
		{
			tToolTip.x += tScroller.left;
			tToolTip.y += tScroller.top;
		}
		
		var Blur = jCore(pElement).attribute("onblur");
		Blur = (Blur === null ? "" : Blur.toString());
		
		if (Blur.indexOf("jTip().close()") < 0)
		{
			Blur = Blur.replace("function anonymous() {", "").replace("}", "") + "jTip().close();";
			pElement.onblur = new Function(Blur);
		}
	};
	
	return self;
};

jTip = function(pElement)
{
	return new jenaToolTip(pElement);
};