/*
 * Javascript for service directory
 */

function findPos(obj)
{
    var curleft = 0;
    var curtop  = 0;
    if (obj.offsetParent)
    {
	while (obj.offsetParent)
	{
	    curleft += obj.offsetLeft;
	    curtop  += obj.offsetTop;
	    obj = obj.offsetParent;
	}
    }
    else {
	if (obj.x) curleft += obj.x;
	if (obj.y) curtop  += obj.y;
    }
    return(new Array(curleft,curtop));
}

function changetext(e,text)
{
    var bnode=0;
    for(var node=e.firstChild;node;node=node.nextSibling) {
	if(bnode) e.removeChild(bnode);
	bnode=node;
    }
    if(bnode) e.removeChild(bnode);

    text=unescape(text);
/*
    Note: in konqueror, following doesn't work, because unicode characters is not parsed in regexp - only in string.
    at=text.replace(/ /g,'\u00a0').replace(/(^|[^\u00a0])\u00a0/g,'\$1 ').split("\n");	// Unicode non-breakable space ...
*/
    at=text.replace(new RegExp('\r','g'),'').replace(/ /g,'\u00a0').replace(new RegExp('(^|[^\u00a0])\u00a0','g'),'\$1 ').split("\n");	// Unicode non-breakable space ...
    for(var i=0;i<at.length;i++) {
	var t=document.createTextNode(at[i]);
	e.appendChild(t);
	var b=document.createElement("br");
	e.appendChild(b);
    }
    if(b) e.removeChild(b);
}

function display_bubble(pos,b,dx,dy)
{
    b.style.display="block";
    b.style.visibility="visible";

    var newX = pos[0];
    var newY = pos[1];
    newX-=b.offsetWidth;
    newY-=b.offsetHeight/2;
    newX+=dx;
    newY+=dy;
    b.style.top = newY + 'px';
    b.style.left = newX + 'px';
}

function dsdb(e,title,body)
{
    cardUsed++;
    cardOut=null;
    e.style.backgroundColor="#e5e5e3";
    b=document.getElementById("bubble_title");
    changetext(b,title);
    b=document.getElementById("bubble_body");
    changetext(b,body);

    pos=findPos(e);
    b=document.getElementById("service_bubble");
    display_bubble(pos,b,0,20);
    b=document.getElementById("service_bubble_z");
    display_bubble(pos,b,13,20);
    
    if(ie) {
	e=document.getElementById('select_countries2');
	if(e) e.style.visibility="hidden";
    }
}

function hsdb(e)
{
    e.style.backgroundColor="transparent";
    b=document.getElementById("service_bubble");
    b.style.visibility="hidden";
    b=document.getElementById("service_bubble_z");
    b.style.visibility="hidden";
    e=document.getElementById('splash_popup');
    if((!e)||(e.style.visibility == 'hidden')||(e.style.display!='block')) {
	e=document.getElementById('select_countries2');
	if(e) e.style.visibility="visible";
    }
}

function dipdg(e)
{
/*    e.style.backgroundColor="#e5e5e3";*/
    b=document.getElementById("ipdg_bubble");
    pos=findPos(e);
    b.style.display="block";
    display_bubble(pos,b,b.offsetWidth-50,-b.offsetHeight/2+7);
}

function hipdg(e)
{
/*    e.style.backgroundColor="transparent";*/
    b=document.getElementById("ipdg_bubble");
    b.style.visibility="hidden";
}

function randomPop()
{
    if(!cardUsed) {
	r=Math.floor(Math.random()*cards);
	e=document.getElementById("servicedir");
	i=0;
	for(el=e.firstChild;el;el=el.nextSibling) {
	    if((el.nodeName=="DIV")&&(el.cssClass="sd_title")) {
		for(elm=el.firstChild;elm;elm=elm.nextSibling) {
		    if((elm.nodeName=="DIV")&&(elm.onmouseover)) {
			if(i==r) {
			    elm.onmouseover();
			    cardOut=elm;
			    setTimeout('if(cardOut) hsdb(cardOut);cardOut=null;',4000);
			}
			i++;
		    }
		}
	    }
	}
    }
}

var cardOut;
var cardUsed=0;
setTimeout('randomPop();',4000);
