/*
 * 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 test_flash_frame(b)
{
    var e=document.getElementById('flash_frame');
    if(!e) return;
    for(var i=0;i<e.flashes.length;i++) {
	var pos=findPos(e.flashes[i]);
	var y=parseInt(b.style.top,10);
	if(y>pos[1]+e.flashes[i].offsetHeight) continue;
	if(y+b.offsetHeight<pos[1]) continue;

	var h=y+b.offsetHeight;
	if(h>pos[1]+e.flashes[i].offsetHeight) h=pos[1]+e.flashes[i].offsetHeight;
	if(y<pos[1]) y=pos[1];
	e.style.top=y+'px';
	e.style.height=(h-y)+'px';
	var x=parseInt(b.style.left,10);
	var w=x+b.offsetWidth;
	if(w>pos[0]+e.flashes[i].offsetWidth) w=pos[0]+e.flashes[i].offsetWidth;
	if(x<pos[0]) x=pos[0];
	e.style.left=x;
	e.style.width=(w-x)+'px';
	e.style.display='block';
	break;
    }
}

function hide_flash_frame()
{
    var e=document.getElementById('flash_frame');
    if(!e) return;
    e.style.display='none';
}

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);
    test_flash_frame(b);
    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";
    }
    hide_flash_frame();
}

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);
    test_flash_frame(b);
}

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

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.className=="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);
