﻿//titleMessage
//CRI.静静的黎明
var TitleMessage = 
{
	_title : null,

	_boxEntry : null,

	_shadow : null,

	_msgEntry : null,

	_frmEntry : null,
  
	_timber : null,

	init: function()
	{
		if(this._msgEntry == null)
		{
			var tbl = this._msgEntry = document.createElement("table");
			
			tbl.cellPadding = tbl.cellSpacing = tbl.border = "0";
			this._msgEntry = this._msgEntry.insertRow(0);
			this._msgEntry = this._msgEntry.insertCell(0);
			with(this._msgEntry)
			{
				style.cssText = "white-space:nowrap;background-color:#ffffe1;padding:5px;font:12px verdana;border:1px solid #666666;position:relative;z-index:999";				
			}
			this._msgEntry.table = tbl;
		}

		if(this._boxEntry == null)
		{
			this._boxEntry = document.createElement("div");
						
			with(this._boxEntry)
			{
				style.cssText = "position:absolute;z-index:999";
			}
		}

		if(this._shadow == null)
		{
			this._shadow = document.createElement("div");
			with(this._shadow)
			{
				style.cssText = "width:0px;position:absolute;background-color:#666666;filter:alpha(opacity=40);-moz-opacity:0.4;z-index:998";
			}
		}

		if(this._frmEntry == null)
		{
			this._frmEntry = document.createElement("iframe");
			with(this._frmEntry)
			{
				frameBorder = "0";
				src = "about:blank";
				style.cssText = "overflow:hidden;position:absolute;z-index:0;border:1px;width:0px;height:0px;";
			}
		}
	
		this._boxEntry.appendChild(this._frmEntry);
		this._boxEntry.appendChild(this._msgEntry.table);

		
		document.onmouseover =  TitleMessage.capture;	
	},

	capture : function(e)
	{
		var srcEle = (e == null)? event.srcElement : e.target;

		if(srcEle == null) return;

		var title = srcEle.title;
		

		if(title != null && title != "")
		{
			srcEle.titleMsg = title;
			srcEle.title = "";
		}		
		if(srcEle.titleMsg != this._title)
		{
			clearTimeout(this._timber);

			this._title = srcEle.titleMsg;	
			var pageX = (e == null)? event.clientX : e.clientX;		
			var pageY = (e == null)? event.clientY : e.clientY;

			if(this._title != null && this._title != "")
			{
				this._title = this._title.replace(/'/g, "&#39;");
				this._title = this._title.replace(/"/g, "&quot;");				
				this._title = this._title.replace(/\r\n/g, "<br/>");			
				this._timber = setTimeout("TitleMessage._show('" + this._title +"'," + pageX + "," + pageY + ")", 200);				
			}
			else
			{
				TitleMessage._hidden();
			}
		}		
	},

	_dropShadow : function(x, y, width, height)
	{
		var width = this._msgEntry.offsetWidth, height = this._msgEntry.offsetHeight;
		with(this._shadow)
		{
			var d = 3;
			style.width = (width - d) + "px";
			style.height = (height - d) + "px";
			style.top = (y + 2 * d) + "px";
			style.left = (x + 2 * d) + "px";			
		}

		this._frmEntry.style.width = width + "px";
		this._frmEntry.style.height = height + "px";

		document.body.appendChild(this._shadow);
	},

	_show : function(msg, x, y)
	{
		function getScrollTop()		
		{
			if(document.documentElement && document.documentElement.scrollTop)
				return document.documentElement.scrollTop;
			else if(document.body && document.body.scrollTop)	
				return document.body.scrollTop;
			else
				return 0;
		}
		function getScrollLeft()
		{
			if(document.documentElement && document.documentElement.scrollLeft)
				return document.documentElement.scrollLeft;
			else if(document.body && document.body.scrollLeft)	
				return document.body.scrollLeft;
			else
				return 0;			
		}

		this._msgEntry.innerHTML = msg;
		
		document.body.appendChild(this._boxEntry);

		var boxWidth = this._msgEntry.scrollWidth, boxHeight = this._msgEntry.scrollHeight;
		
		if(x + boxWidth + 15 > document.body.clientWidth)	
			x = x - boxWidth - 10;
		else
			x += 10;

		if(x < 10)
			x = (document.body.clientWidth - boxWidth) / 2;
		if(x < 10)
			x = 10;

		if(y + boxHeight + 15 > document.body.clientHeight)
			y = y - boxHeight - 10;
		else
			y += 15;

		var scrollTop = getScrollTop(), scrollLeft = getScrollLeft();

		this._boxEntry.style.top = (scrollTop + y) + "px";
		this._boxEntry.style.left = (scrollLeft + x) + "px";
		this._dropShadow(scrollLeft + x, scrollTop + y);	
	},
	
	_hidden : function()
	{	
		clearTimeout(this._timber);

		if(this._boxEntry.parentNode)
			this._boxEntry.parentNode.removeChild(this._boxEntry);
		if(this._shadow.parentNode)
			this._shadow.parentNode.removeChild(this._shadow);
	}
};
TitleMessage.init();


