function cTooltip(e,p)
{
	//variables
	this.theDiv = null;
	this.theDivClose = null;
	this.caption = null;
	this.align = "left";
	this.fontSize = 10;
	this.clickEventAdded = null;
	this.hideTimeout = 5000;

	this.top = 0;
	this.left = 0;
	this.width = 0;
	this.height = 0;

	if(e)
	{
		this.sourceObject = e.target || e.srcElement;
		
		if(e.type == "click"){
			this.title = this.sourceObject.computedTitle;
			this.sourceObject.title = "";
			this.params = this.title;
			this.autoHide = 0;
		}
		else
		{
			this.title = this.sourceObject.title;
			this.sourceObject.title = "";
			this.params = this.title;
			this.autoHide = 1;
		}

	} else { //manually triggered
		this.autoHide = 0;
		this.params = p;
		this.fixedPosition = 1;

		this.relativeObject = null;
		this.relativePosition = "br"; //br=bottomright, tr=topright, bl, tl.
	}
	//methods
	this.createDiv = tooltipCreateDiv;
	this.styleDiv = tooltipStyleDiv;
	this.destroyDiv = tooltipDestroy;
	this.getParams = tooltipGetParams;

	this.getParams();

	if(e)
	{
		if (e.pageX||e.pageY)
		{
			xMouse = e.pageX;
			yMouse = e.pageY;
		} else {
			de = document.documentElement;
			b = document.body;
			xMouse = e.clientX+(de.scrollLeft||b.scrollLeft) - (de.clientLeft||0);
			yMouse = e.clientY+(de.scrollTop||b.scrollTop) - (de.clientTop||0);
		}

		this.top = yMouse + 3;
		this.left = xMouse + 3;
	}

	if(this.autoHide)
	{
		var temp = this;
		//this.sourceObject.onmouseout = function(){temp.destroyDiv();}
		addEvent(this.sourceObject,"mouseout",function(){temp.destroyDiv();});
		
	}


	if(this.relativeObject)
	{
		if(this.relativePosition == "tl"){
			this.top = getTop(this.relativeObject);
			this.left = getLeft(this.relativeObject);

		}else if(this.relativePosition == "tr"){
			this.top = getTop(this.relativeObject);
			this.left = getLeft(this.relativeObject) + this.relativeObject.offsetWidth;

		}else if(this.relativePosition == "bl"){
			this.top = getTop(this.relativeObject) + this.relativeObject.offsetHeight;
			this.left = getLeft(this.relativeObject);

		}else if(this.relativePosition == "br"){
			this.top = getTop(this.relativeObject) + this.relativeObject.offsetHeight;
			this.left = getLeft(this.relativeObject) + this.relativeObject.offsetWidth;
		}
	}

	this.createDiv();
	this.styleDiv();

	if(this.autoHide == 1 && this.hideTimeout > 0)
	{
		var that = this;
		setTimeout(function(){that.destroyDiv();},this.hideTimeout);
	}

}
			
function tooltipCreateDiv()
{
	this.theDiv = document.createElement("div");
	this.theDiv.style.display = "none";
	
	if(!this.autoHide)
	{
		this.theDivClose = document.createElement("span");
		this.theDivClose.innerHTML = "[X]";	
		this.theDiv.appendChild(this.theDivClose);
	}
	
	text = document.createElement("div");
	text.innerHTML = this.caption;
	text.style.textAlign = this.align;
	this.theDiv.appendChild(text);
	
	document.body.appendChild(this.theDiv);
	
	var temp = this;

	if(this.sourceObject && !this.clickEventAdded)
	{
		addEvent(this.sourceObject,"click",function(){temp.destroyDiv();});
		this.clickEventAdded = 1;
	}
	
	if(!this.autoHide)
	{
		this.theDivClose.onclick = function(){temp.destroyDiv();};					
	}
}

function tooltipStyleDiv()
{
	//var stylestring = "-moz-opacity:.25;opacity:.25;";
	//theDiv.setAttribute("style", stylestring);

	this.theDiv.style.padding = "1px 3px 1px 3px";				
	this.theDiv.style.textAlign = "right";
	//this.theDiv.style.textAlign = this.align;
	this.theDiv.style.verticalAlign = "middle";
	this.theDiv.style.zIndex = "9999";
	this.theDiv.style.border = "1px solid #333399";
	this.theDiv.style.color = "#333";
	this.theDiv.style.background = "#CCCCFF";
	this.theDiv.id = "dynamicDiv" + Math.random();
	this.theDiv.style.fontSize = this.fontSize + "px";
	//overlay.style.filter = "alpha(opacity=25)";
	this.theDiv.style.position = "absolute";
	
	this.theDiv.style.left = "-1500px";
	this.theDiv.style.top = "0px";
	this.theDiv.style.width = "auto";
	this.theDiv.style.height = "auto";
			
	//this.theDiv.innerHTML += this.caption;
	var scrollbarOffset = 0;
	this.theDiv.style.display = "block";	
		
	
	if(this.width)
	{
		if(parseInt(this.left) + parseInt(this.width) + scrollbarOffset > parseInt(document.body.clientWidth))
		{
			mv = (parseInt(this.left)+parseInt(this.width))-parseInt(document.body.clientWidth);
			this.left -= mv;
		}
	}
	else
	{
		if(this.left + this.theDiv.offsetWidth +scrollbarOffset > document.body.clientWidth)
		{
			var mv = (this.left+this.theDiv.offsetWidth)-document.body.clientWidth;
			this.left -= mv;
		}
	}
	
	this.theDiv.style.top = this.top + "px";
	this.theDiv.style.left = this.left + "px";

	
	if(this.width != 0)
	{
		this.theDiv.style.width = this.width + "px";
	}
	else
	{
		this.theDiv.style.width = "auto";
	}
	if(this.height != 0)
	{
		this.theDiv.style.height = this.height + "px";
	}
	else
	{
		this.theDiv.style.height = "auto";
	}
	
	if(this.theDivClose){
		this.theDivClose.style.textAlign = "right";
		this.theDivClose.style.cursor = "pointer";
		//this.theDivClose.style.border = "1px solid #000;";	
	}
	
	this.theDiv.style.display = "block";
}

function tooltipGetParams()
{
	var temp = this.params.split("|");
	if(temp.length == 1)
	{
		this.caption = temp[0];
	}
	else
	{
		for(var i = 0; i < temp.length; i++)
		{
			temp2 = temp[i].split(":");
			switch(temp2[0])
			{
				case "x":
					this.left = temp2[1];
					break;
				case "y":
					this.top = temp2[1];
					break;
				case "w":
					this.width = temp2[1];
					break;
				case "h":
					this.height = temp2[1];
					break;
				case "relativeposition":
					this.relativePosition = temp2[1];
					break;
				case "relativeobjectid":
					this.relativeObject = document.getElementById(temp2[1]);
					break;
				case "caption":
					this.caption = temp2[1];
					break;
				case "align":
					this.align = temp2[1];
					break;
				case "fontsize":
					this.fontSize = temp2[1];
					break;
				case "timeout":
					this.hideTimeout = temp2[1];
					break;
				case "autohide":
					this.autoHide = parseInt(temp2[1]);
					break;
			}
		}
	}
}	

function tooltipDestroy()
{
	removeElement(this.theDiv.id);
	if(this.sourceObject && !this.sourceObject.computedTitle)
	{
		this.sourceObject.title = this.title;
	}
}

//--------------------------------------------------------------------------------------
function spawnTooltip(event,params)
{
	new cTooltip(event,params);
}

function initTooltip()
{
	var elCount = 0;				
	allElements = document.getElementsByTagName("*");

	var c = 0;
	for(var i = 0; i < allElements.length; i++)
	{	
		if(allElements[i].attributes != null)
		{
			for(var j = 0; j < allElements[i].attributes.length; j++)///offensive to IE.
			{	
				c++;
				if(allElements[i].attributes[j].nodeName == "title" &&  allElements[i].attributes[j].nodeValue.length > 0)
				{
					//if(allElements[i].className){alert(allElements[i].className);}
					if(allElements[i].className == "help"){
						addEvent(allElements[i],"click",spawnTooltip);
						allElements[i].computedTitle = allElements[i].title;
						allElements[i].title="";

						if(!allElements[i].id)
						{
							allElements[i].id = Math.random();	
						}
						break;
					}
				}	
			}	
		}
	}
	//alert(c);
}

function removeElement(id)
{
   var Node = document.getElementById(id);
   if(Node){Node.parentNode.removeChild(Node);}
}		

