/*
==============================================================================

  © Copyright 2002-2008 Media Monitors. All Rights Reserved.

  Media Monitors Company Confidential
  Media Monitors, 445 Hamilton Ave. White Plains, NY 10601

  This code and all associated files are the proprietary property of 
  Media Monitors.

  No portion or portions may be used without the express written 
  permission of Media Monitors.

==============================================================================
*/

/*
	Generic tooltip with drop-shadow and ballistic motion

	In each page to use this control, include this stylesheet and JavaScript:
		<script type="text/javascript" src="script/tooltip.js"></script>

	To show/hide tooltip, call it like:
		var toolTip = new ToolTip (myMsg, x, y, true, null, "myTooltipClassName");
		toolTip.show();
		toolTip.moveTo(x, y);
		toolTip.hide();
		etc.
*/


var ToolTip = function (_message, _x, _y, _initiallyHidden, _duration, _class, _owner)
{
	this.generate = function ()
	{
		this.tag.style.top = this.y;
		this.tag.style.left = this.x;
		this.tag.innerHTML = "<table class='StandardToolTipText' cellspacing=0 cellpadding=0><tr><td align=left valign=top>" + this.message.replace(/\n/g, "<br>") + "</td><td align=right valign=top><span id='x' class='StandardToolTipX' onclick=\"" + this.hideCmd + "\">x</span></td></tr></table>";
		var setShadowCmd = "try { top.document.tooltips['" + this.id + "'].setShadow(); } catch (_e) {}";
		setTimeout(setShadowCmd, 10);
	};

	this.setShadow = function ()
	{
		if (parseInt(this.tag.clientWidth) >= parseInt(document.body.clientWidth) - 100)
			this.tag.style.width = 400;

		this.shadow.style.width = this.tag.clientWidth;
		this.shadow.style.height = this.tag.clientHeight;
		this.shadow.style.position = "absolute";
		this.shadow.style.top = this.y + 10;
		this.shadow.style.left = this.x + 10;
		this.shadow.style.backgroundImage = "url(images/halfmask2.png)";
		this.shadow.innerHTML = "&nbsp;";
		this.shadow.style.zIndex = 9999999999998;
		this.shadow.style.display = "inline";
	};

	this.destroy = function ()
	{
		this.hide();
		this.tag = null;
		this.shadow = null;
		top.document.tooltips[this.id] = null;
	};

	this.show = function (_duration)
	{
		if (this.owner)
			this.appendTo(this.owner);
		else
			this.appendToPage();

		clearTimeout(this.moveTimer);
		clearTimeout(this.hideTimer);
		this.duration = _duration ? _duration : this.duration;
		this.x = this.saveX;
		this.y = this.saveY;
		this.shadow.style.display = "inline";
		this.generate();
		if (this.duration > 0)
		{
			clearTimeout(this.hideTimer);
			this.hideTimer = setTimeout(this.hideCmd, this.duration);
		}
	};

	this.showAt = function (_x, _y, _duration)
	{
		if (this.owner)
			this.appendTo(this.owner);
		else
			this.appendToPage();

		clearTimeout(this.moveTimer);
		clearTimeout(this.hideTimer);
		this.duration = _duration ? _duration : this.duration;
		this.x = _x;
		this.y = _y;
		this.shadow.style.display = "inline";
		this.generate();
		if (this.duration > 0)
		{
			clearTimeout(this.hideTimer);
			this.hideTimer = setTimeout(this.hideCmd, this.duration);
		}
	};

	this.hide = function ()
	{
		if (this.y == -2000 && this.x == -2000)
			return;
		//alert("hiding "+this.id);
		clearTimeout(this.moveTimer);
		clearTimeout(this.hideTimer);
		this.saveX = this.x;
		this.saveY = this.y;
		this.y = this.x = -2000;
		this.shadow.style.display = "none";
		this.generate();
	};

	this.setText = function (_message)
	{
		this.message = _message;
		this.generate();
	};

	this.appendToPage = function ()
	{
		if (document && document.body)
			this.appendTo(document.body);
	};

	this.appendTo = function (_obj)
	{
		try
		{
			if (this.owner)
			{
				this.owner.removeChild(this.shadow);
				this.owner.removeChild(this.tag);
			}
		}
		catch (_e)
		{
		}
		if (_obj)
		{
			_obj.appendChild(this.shadow);
			_obj.appendChild(this.tag);
			this.owner = _obj;
		}
	};

	
	this.setToRelative = function ()
	{
		this.tag.style.position = "relative";
		this.shadow.style.position = "relative";
		this.generate();
	}

	this.setToAbsolute = function ()
	{
		this.tag.style.position = "absolute";
		this.shadow.style.position = "absolute";
		this.generate();
	}

	this.moveTo = function (_x, _y)
	{
		clearTimeout(this.moveTimer);
		var deltaX = (_x - this.x) / this.speed;
		var deltaY = (_y - this.y) / this.speed;
		if (Math.abs(deltaX) < this.speed)
			deltaX = (_x - this.x);
		if (Math.abs(deltaY) < this.speed)
			deltaY = (_y - this.y);
		this.x += parseInt(deltaX);
		this.y += parseInt(deltaY);
		this.generate();
		window.status = "{"+this.x+","+this.y+"}";
		if (Math.abs(deltaX) < .5 && Math.abs(deltaY) < .5)
			return;
		var moveCmd = "try { top.document.tooltips['" + this.id + "'].moveTo(" + _x + "," + _y + "); } catch (_e) {}";
		clearTimeout(this.moveTimer);
		this.moveTimer = setTimeout(moveCmd, this.delay);
	};

	// c'tor code:
	
	this.id = "ToolTip" + new Date ().getTime() + parseInt(Math.random() * 100);
	//alert("created "+this.id);

	this.duration = _duration ? _duration : 0;
	this.tag = document.createElement("div");
	this.shadow = document.createElement("div");

	this.message = _message;
	this.x = this.saveX = _x;
	this.y = this.saveY = _y;
	this.speed = 2;
	this.delay = 10;
	if (_owner)
	{
		this.owner = _owner;
	}
	else if (document && document.body)
	{
		this.owner = document.body;
	}
	if (_class)
	{
		this.tag.className = _class;
	}
	else
	{
		//this.tag.className = "StandardToolTip";
		this.tag.style.backgroundColor = "cornsilk";
		this.tag.style.color = "black";
		this.tag.style.fontFamily = "verdana, arial, sans-serif, helvetica";
		this.tag.style.fontSize = "10px";
		this.tag.style.fontWeight = "normal";
	}
	this.tag.id = this.id;
	this.tag.style.position = "absolute";
	this.tag.style.border = "1px silver solid";
	this.tag.style.padding = "7px";
	this.tag.style.zIndex = 9999999999999;

    this.generate();

	if (!top.document.tooltips)
	{
		top.document.tooltips = new Array ();
	}
	top.document.tooltips[this.id] = this;
	this.hideCmd = "try { top.document.tooltips['" + this.id + "'].hide(); } catch (_e) { }";
	//alert(this.hideCmd);

	if (_initiallyHidden)
		this.hide();
	if (this.duration > 0)
	{
		this.hideTimer = setTimeout(this.hideCmd, this.duration);
	}

	return this;
}

var tooltip = new ToolTip ("", -100, -100, true, 0, null);

