/*Javascript for Bubble Tooltips by Alessandro Fulciniti
http://pro.html.it - http://web-graphics.com */

var btc;
var liveReq = new XMLHttpRequest;
var processURI = "/live-search.php";
var posx, posy;

function enableTooltips(id) {
	var links,i,h;
	if (!document.getElementById || !document.getElementsByTagName) return;
	AddCss();
	
	btc = document.createElement("span");
	btc.id = "btc_big";
	btc.setAttribute("id", "btc_big");
	btc.style.position = "absolute";
	btc.style.display = "block";
	document.getElementsByTagName("body")[0].appendChild(btc);
	
	h=document.createElement("span");
	h.id="btc";
	h.setAttribute("id","btc");
	h.style.position="absolute";
	document.getElementsByTagName("body")[0].appendChild(h);

	if (id == null) {
		links = document.getElementsByTagName("a");
	} else {
		links = document.getElementById(id).getElementsByTagName("a");
	}
	for (i = 0; i < links.length; ++i) {
	    Prepare(links[i]);
	}
}

function Prepare(el){
	var tooltip,t,b,s,l;
	t=el.getAttribute("title")
	if(t == null || t.length == 0) return;
	if (t.substr(0, 4) == "ID: ") {
		t = t.substr(4);
		el.tooltip = t;
		el.title = t;
		el.onmouseover=showAJAXTooltip;
		el.onmouseout=hideAJAXTooltip;
	} else {
		el.removeAttribute("title");
		tooltip=CreateEl("span","tooltip");
		s=CreateEl("span","top");
		var oDiv=document.createElement("div");
		s.appendChild(oDiv);
		oDiv.innerHTML = t;
		tooltip.appendChild(s);
		b=CreateEl("b","bottom");
		/*l=el.getAttribute("href");
		if(l.length>28) l=l.substr(0,25)+"...";
		b.appendChild(document.createTextNode(l));*/
		tooltip.appendChild(b);
		setOpacity(tooltip);
		el.tooltip=tooltip;
		el.onmouseover=showTooltip;
		el.onmouseout=hideTooltip;
		el.onmousemove=Locate;
	}
}

function showAJAXTooltip(evt){
	liveReq = new XMLHttpRequest;
	if(liveReq && liveReq.readyState < 4) {
		liveReq.abort();
	}
	
	hideTooltip(evt);
	if (!this.title && this.tooltip) {
		this.title = this.tooltip;
	}
	posx = 0;
	posy = 0;
	if (evt == null) evt = window.event;
	if (evt.pageX || evt.pageY) {
	    posx = evt.pageX;
		posy = evt.pageY;
	} else if (evt.clientX || evt.clientY) {
	    if (document.documentElement.scrollTop) {
	        posx = evt.clientX + document.documentElement.scrollLeft;
	        posy = evt.clientY + document.documentElement.scrollTop;
	    } else {
	        posx = evt.clientX + document.body.scrollLeft;
	        posy = evt.clientY + document.body.scrollTop;
	    }
    }
	
	liveReq.onreadystatechange = liveReqProcessTooltip;
	
	if(processURI.indexOf('?') != -1) {
		liveReq.open("GET", processURI + "&p=" + escape(this.title), true);
	} else {
		liveReq.open("GET", processURI + "?p=" + escape(this.title), true);
	}
	
	this.removeAttribute("title");
	this.title = "";

	liveReq.send(null);
}

function hideAJAXTooltip(e){
	if(liveReq && liveReq.readyState < 4) {
		liveReq.abort();
	}
	
	while (btc.childNodes.length > 0) btc.removeChild(btc.firstChild);
}

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 CreateEl(t,c){
var x=document.createElement(t);
x.className=c;
x.style.display="block";
return(x);
}

function AddCss(){
var l=CreateEl("link");
l.setAttribute("type","text/css");
l.setAttribute("rel","stylesheet");
l.setAttribute("href","/bt.css");
l.setAttribute("media","screen");
document.getElementsByTagName("head")[0].appendChild(l);
}

function liveReqProcessTooltip() {
	if(liveReq.readyState == 4) {
		xmlDoc = liveReq.responseXML;
		if (xmlDoc.childNodes.length > 0 && xmlDoc.getElementsByTagName("ProductName").length > 0) {
			var ProductName = unescape(xmlDoc.getElementsByTagName("ProductName")[0].firstChild.nodeValue);
			if (xmlDoc.getElementsByTagName("CDescription").length > 0) {
				var CDescription = unescape(xmlDoc.getElementsByTagName("CDescription")[0].firstChild.nodeValue);
			} else {
				var CDescription = "";
			}
			if (xmlDoc.getElementsByTagName("ImageURL")[0].childNodes.length > 0) {
				var ImageURL = xmlDoc.getElementsByTagName("ImageURL")[0].firstChild.nodeValue;
			} else {
				var ImageURL = "";
			}
			
			content = document.createElement("span");
			content.id = "bt_content";
			content.setAttribute("id", "bt_content")
			content.className = "tip_big";
			content.setAttribute("class", "tip_big");
			
			topd = document.createElement("span");
			topd.className = "top";
			topd.setAttribute("class", "top");
			topd.style.display = "block";
			
			title = document.createElement("div");
			title.id = "bt_title";
			title.setAttribute("id", "bt_title");
			title.innerHTML = ProductName;
			topd.appendChild(title);
			
			middle = document.createElement("span");
			middle.className = "middle_top";
			middle.setAttribute("class", "middle_top");
			middle.style.display = "block";
			
			if (ImageURL != "") {
				thumb = document.createElement("img");
				thumb.className = "bt_thumbnail";
				thumb.setAttribute("class", "bt_thumbnail");
				thumb.src = ImageURL;
				thumb.setAttribute("src", ImageURL);
				middle.appendChild(thumb);
			}
			
			desc = document.createElement("div");
			desc.className = "bt_description";
			desc.setAttribute("class", "bt_description");
			desc.innerHTML = CDescription;
			middle.appendChild(desc);
			
			bottom = document.createElement("span");
			bottom.className = "bottom";
			bottom.setAttribute("class", "bottom");
			bottom.style.display = "block";
			
			content.appendChild(topd);
			content.appendChild(middle);
			content.appendChild(bottom);
			
			posy -= 25;
			posx += 50;
			
			if (window.innerHeight) {
				screenHeight = window.innerHeight;
				docTop = window.pageYOffset;
			} else if (document.documentElement.clientHeight) {
				screenHeight = document.documentElement.clientHeight;
				docTop = document.documentElement.scrollTop;
			} else if (document.body.clientHeight) {
				screenHeight = document.body.clientHeight;
				docTop = document.body.scrollTop;
			}
			
			hideAJAXTooltip(null);
			
			btc.appendChild(content);
			if (content.offsetHeight > screenHeight || content.offsetHeight > 1500) {
				maxHeight = screenHeight;
				if (maxHeight > 1500) maxHeight = 1500;
				bottom.appendChild(document.createTextNode("<MORE>"));
				bottom.style.height = "20px";
				bottom.style.paddingRight = "15px";
				
				middle.style.overflow = "hidden";
				middle.style.height = maxHeight - bottom.offsetHeight - topd.offsetHeight - 7;
			}
		
			if ((content.offsetHeight + posy + 25) > screenHeight) {
				middle.className = "middle_bottom";
				middle.setAttribute("class", "middle_bottom");
				posy = posy - content.offsetHeight + 35;
			}
			
			if (posy < docTop) {
				middle.className = "middle_top";
				middle.setAttribute("class", "middle_top");
				posy = docTop + 3;
			}
			
			btc.style.left = posx + "px";
			btc.style.top = posy + "px";
			
		}
		
	}
}

function showTooltip(e){
	document.getElementById("btc").appendChild(this.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";
}
