/*
 * 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);

    if(decodeURIComponent) {
	text=decodeURIComponent(text);
    } else {
	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];
    newY-=b.offsetHeight;
    newX+=dx;
    newY+=dy;
    b.style.top = newY + 'px';
    b.style.left = newX + 'px';
}

var hide_sdb=0;
var active_card=null;

function dsdb(e,title,body)
{
    if(active_card==e) {
	hide_sdb=0;
	return;
    } else if(active_card) {
	hsdb_real(active_card);
    }
    active_card=e;
    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);
    var b=document.getElementById("service_bubble");
    display_bubble(pos,b,-5,-5);
    b.active_card=e;
    b.onmouseover=function() {if(active_card==this.active_card) hide_sdb=0;}
    b.onmouseout =function() {if(active_card) hsdb(active_card);}
    var c=document.getElementById("service_bubble_z");
    display_bubble(pos,c,-5+b.offsetWidth/2,10);
    c.active_card=e;
    c.onmouseover=function() {if(active_card==this.active_card) hide_sdb=0;}
    c.onmouseout =function() {if(active_card) hsdb(active_card);}
    
    if(ie) {
	e=document.getElementById('select_countries2');
	if(e) e.style.visibility="hidden";
    }
}

function hsdb_real(e)
{
    hide_sdb=0;
    active_card=null;
    e.style.backgroundColor="transparent";
    b=document.getElementById("service_bubble");
    b.style.visibility="hidden";
    b.onmouseover=null;
    b.onmouseout=null;
    b=document.getElementById("service_bubble_z");
    b.style.visibility="hidden";
    b.onmouseover=null;
    b.onmouseout=null;
    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 hsdb(e)
{
    if(!hide_sdb) {
	hide_sdb=1;
	setTimeout(function() {if(hide_sdb) hsdb_real(active_card)},100);
    }
}

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

function hipdg(e,c)
{
/*    e.style.backgroundColor="transparent";*/
    if(c) e.style.backgroundColor=c;
    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);
