﻿String.format = function() {
    if( arguments.length == 0 )
        return null;

    var str = arguments[0];
    for(var i=1;i<arguments.length;i++) {
        var re = new RegExp('\\{' + (i-1) + '\\}','gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
} 
/*初始化Tip的容器*/
function enableTooltips() {
    var h;
    if (!document.getElementById || !document.getElementsByTagName) return;
    h = document.createElement("span");
    h.id = "btc";
    h.setAttribute("id", "btc");
    h.style.position = "absolute";
	h.style.display='none';
    document.getElementsByTagName("body")[0].appendChild(h);
}

function CreateEl(t, c) {
    var x = document.createElement(t);
    x.className = c;
    x.style.display = "block";
    return (x);
}

function setOpacity(el) {
    el.style.filter = "alpha(opacity:95)";
    el.style.KHTMLOpacity = "0.95";
    el.style.MozOpacity = "0.95";
    el.style.opacity = "0.95";
}

function showTooltip(el,e) {
	var tooltip, t, s;
	t = el.getAttribute("message");
    if (t == null || t.length == 0) return;
	var tipcontent = "<table border=0 cellspacing=0 cellpadding=4 width=100%>";
	tipcontent = tipcontent + "<tr><td class='TipCol'>索引号：</td><td width=180px>{0}</td><td class='TipCol'>主题信息：</td><td width=180px>{1}</td></tr>";
	tipcontent = tipcontent + "<tr><td class='TipCol'>发文单位：</td><td>{2}</td><td class='TipCol'>生成日期：</td><td>{3}</td></tr>";
	tipcontent = tipcontent + "<tr><td class='TipCol'>文号：</td><td>{4}</td><td class='TipCol'>主题词：</td><td>{5}</td></tr>";
	tipcontent = tipcontent + "<tr><td class='TipCol'>文件名称：</td><td colspan=3>{6}</td></tr>";
	tipcontent = tipcontent + "</table>";
	t=t.split("&");
    
	//el.removeAttribute("message");
    tooltip = CreateEl("span", "tooltip");

    s = CreateEl("span", "top");
    s.innerHTML = String.format(tipcontent,t[0],t[1],t[2],t[3],t[4],t[5],t[6]);
    tooltip.appendChild(s);
    document.getElementById("btc").appendChild(tooltip);
	Locate(e);
}

function hideTooltip(e) {
    var d = document.getElementById("btc");
    if (d.childNodes.length > 0) d.removeChild(d.firstChild);
}

function Locate(e) {
    var posx = 0, posy = 0;
    if (e == null) e = window.event;
    if (e.pageX || e.pageY) {
        posx = e.pageX; posy = e.pageY;
    }
    else if (e.clientX || e.clientY) {
        if (document.documentElement.scrollTop) {
            posx = e.clientX + document.documentElement.scrollLeft;
            posy = e.clientY + document.documentElement.scrollTop;
        }
        else {
            posx = e.clientX + document.body.scrollLeft;
            posy = e.clientY + document.body.scrollTop;
        }
    }
    document.getElementById("btc").style.top = (posy + 10) + "px";
    document.getElementById("btc").style.left = (posx - 20) + "px";
	document.getElementById("btc").style.display='';
}
window.onload = function() { enableTooltips() }; 
