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

function enableTooltips(id, tagname){
var links,i,h;

if (tagname == null){tagname = '*';}

if(!document.getElementById || !document.getElementsByTagName) return;
AddCss();
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('*');
else links=document.getElementById(id).getElementsByTagName('*');

if(id==null) links=document.getElementsByTagName(tagname);
else links=document.getElementById(id).getElementsByTagName(tagname);

for(i=0;i<links.length;i++){
    Prepare(links[i]);
    }
}

function Prepare(elt){
var tooltip,t,b,s,l, l_break, l_pos, el;
var l_t = new Array();
var l_s = new Array();
sub_imgs=elt.getElementsByTagName("img"); /*target element with picture: picture should have the title tag*/
if (sub_imgs.length == 1){ /*only one picture for a title tags*/
   el=sub_imgs[0];
}else{
   el=elt; 
}

t=el.getAttribute("title");
/* we dont need it anymore
if(t==null || t.length==0) t="link:";
*/
if(t==null || t.length==0) t="";

el.removeAttribute("title");
el.removeAttribute("alt");
tooltip=CreateEl("span","tooltip");
s=CreateEl("span","top");
bubble_test = el.getAttribute("bubble1");
if (bubble_test == null || bubble_test.length==0){
   s.appendChild(document.createTextNode(t));
}
tooltip.appendChild(s);
tooltip.onmouseover=hideTooltip; // pokud ztratime udalost (napr. kdyz ajax prepise obsah), nechame zmizet bublinu prejezdem mysi pres bublinu
t_all = t


l_break = false;
l_pos = 1;
while (l_break == false)
{
   l_t[l_pos] = el.getAttribute("bubble"+l_pos);
   el.removeAttribute("bubble"+l_pos);
   if(l_t[l_pos]!=null && l_t[l_pos].length > 0){
      l_s[l_pos]=CreateEl("span","line");
      l_s[l_pos].appendChild(document.createTextNode(l_t[l_pos]));
      tooltip.appendChild(l_s[l_pos]);
      t_all = t_all+l_t[l_pos];
      l_pos++;
   }else{
      l_break = true;
   }
}

t_img=el.getAttribute("bubbleimage");
if(t_img!=null && t_img.length > 0)
{
   el.removeAttribute("bubbleimage");
	i1=CreateEl("div","image");
	ipul = CreateEl("div", "line");
	ipul.innerHTML = '&nbsp;';
	i2=document.createElement('img');
	i2.setAttribute('t_src', t_img);
	
	i1.appendChild(ipul);
	i1.appendChild(i2);
	tooltip.appendChild(i1);
	el.image_div = i2;
	t_all = t_all+t_img;
}



b=CreateEl("b","bottom");
/*
l=el.getAttribute("href");
if(l.length>30) l=l.substr(0,27)+"...";
b.appendChild(document.createTextNode(l));
*/

if(t_all!=null && t_all.length > 0)
{
tooltip.appendChild(b);
setOpacity(tooltip);
el.tooltip=tooltip;
el.onmouseover=showTooltip;
el.onmouseout=hideTooltip;
el.onmousemove=Locate;
}
}


function showTooltip(e){
document.getElementById("btc").appendChild(this.tooltip);
if(this.image_div)
	this.image_div.setAttribute('src', this.image_div.getAttribute('t_src'));
Locate(e);
}

function hideTooltip(e){
var d=document.getElementById("btc");
if(d.childNodes.length>0) d.removeChild(d.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","./lib/bubbles/bt.css");
l.setAttribute("media","screen");
document.getElementsByTagName("head")[0].appendChild(l);
}

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-126)+"px";
}
