/*	Funktioner för popup-beskrivningstexter (onmouseover) som följer muspekaren
	John Eklund 2007

	Om en div (i html-filen) som default har css-positionering för bottom: istället för top:,
	Betraktar Firefox uppenbarligen den som oändligt hög (gardering för ev. avstavning?).
	Det gör att man aldrig kan placera en popupruta som följer muspekaren ÖVER muspekaren,
	då vid clamping i X-led muspekaren hamnar under rutan, vilket betraktas som
	onmouseover för rutan, dvs onmouseout för det man egentligen har pekaren på. Ger flimmer.
	Om div:en istället har top: försvinner problemet. Exempel:
	<div id="box_p13_p14_plan_2a" style="position:absolute; left:0; top:0; z-index:4; visibility:hidden;">
*/

var boxid_global, xoffset_global, yoffset_global, winwidth, winheight;	// Popuptext för kraftverkets olika delar

function truebody() {
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function showbox(boxid, xoffset, yoffset) {
	var xpos, ypos;

	if(xoffset) xoffset_global=xoffset;
	else xoffset_global=12;
	if(yoffset) yoffset_global=yoffset;
	else yoffset_global=4;

	if(boxid_global)	// Ta bort ev. föregående ruta som blivit kvar vid fönsterväxling
		hidebox(boxid_global);

	boxid_global = boxid;

	// Kontrollera nuvarande fönsterstorlek
	if (self.innerHeight) {	// all except Explorer
		winwidth = self.innerWidth + self.pageXOffset;
		winheight = self.innerHeight + self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.clientHeight) {	// Explorer 6 Strict Mode
		winwidth = document.documentElement.clientWidth + document.documentElement.scrollLeft;
		winheight = document.documentElement.clientHeight + document.documentElement.scrollTop;
	}
	else if (document.body) {	// all other Explorers
		winwidth = document.body.clientWidth + document.body.scrollLeft;
		winheight = document.body.clientHeight + document.body.scrollTop;
	}

	/* Spar korrekt storlek för popup-textrutan eftersom Firefox förvanskar värdena när rutan flyttas */
	if(typeof getRefToDiv(boxid_global).realwidth =="undefined")
		getRefToDiv(boxid_global).realwidth = getRefToDiv(boxid_global).offsetWidth;
	if(typeof getRefToDiv(boxid_global).realheight =="undefined")
		getRefToDiv(boxid_global).realheight = getRefToDiv(boxid_global).offsetHeight;

	if (typeof window.event !="undefined") {	// Initiera position för IE. (Firefox bjuder på en automagisk movelayer(event))
		if(xoffset_global<0)
			xpos=xoffset_global-getRefToDiv(boxid_global).realwidth+truebody().scrollLeft+event.clientX;
		else
			xpos=xoffset_global+truebody().scrollLeft+event.clientX;

		if(yoffset_global<0)
			ypos=yoffset_global-getRefToDiv(boxid_global).realheight+truebody().scrollTop+event.clientY;
		else
			ypos=yoffset_global+truebody().scrollTop+event.clientY;

		getRefToDiv(boxid_global).style.left=xpos+"px";
		getRefToDiv(boxid_global).style.top=ypos+"px";
	}

//	movelayer();
	show(boxid);
	document.onmousemove=movelayer;
}

function hidebox(boxid) {
	hide(boxid);
	document.onmousemove="";
}

function movelayer(e)
{
//	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15;
//	var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight);

	var xisclamped, xpos, ypos;

	if(xoffset_global<0)
		xpos=xoffset_global-getRefToDiv(boxid_global).realwidth;
	else
		xpos=xoffset_global;
	if(yoffset_global<0)
		ypos=yoffset_global-getRefToDiv(boxid_global).realheight;
	else
		ypos=yoffset_global;

	if (typeof e != "undefined"){
		xpos+=e.pageX;
		ypos+=e.pageY;
	}
	else if (typeof window.event !="undefined"){	// IE 5 etc...
		xpos+=truebody().scrollLeft+event.clientX;
		ypos+=truebody().scrollTop+event.clientY;
	}

	xisclamped=0;
	getRefToDiv(boxid_global).style.display="";

	if (xpos+ getRefToDiv(boxid_global).realwidth > winwidth-1) {
		getRefToDiv(boxid_global).style.left= winwidth-1 - getRefToDiv(boxid_global).realwidth +"px";
		xisclamped=1;
	}
	else if (xpos<0) {
		getRefToDiv(boxid_global).style.left="0px";
		xisclamped=1;
	}
	else	getRefToDiv(boxid_global).style.left=xpos+"px";

	if(ypos+ getRefToDiv(boxid_global).realheight > winheight) {
		getRefToDiv(boxid_global).style.top= winheight - getRefToDiv(boxid_global).realheight +"px";
		if(xisclamped)
			getRefToDiv(boxid_global).style.display="none";
	}
	else if (ypos<0) {
		getRefToDiv(boxid_global).style.top="0px";
		if(xisclamped)
			getRefToDiv(boxid_global).style.display="none";
	}
	else	getRefToDiv(boxid_global).style.top=ypos+"px";

	return false;
}

