// Copyright 2007 Keonnected
// All rights reserved.

/**
 * Info bulle personnalisé pour Keonet 1.5
 *
 * @version 1.2 	
 * @date 2007.08.28
 *
 * @author helder.deazevedo@keonnected.com (Helder de Azevedo)
 * @author david.studer@keonnected.com (David Studer)
 * @author julien.stuby@keonnected.com (Julien Stuby)
 */

window.infobulle = document.getElementById("bulle");
window.infobulle.content = document.getElementById("infobullecontent");

/*__________________________

	MOVE THE INFOBULLE
____________________________*/

window.infobulle.move = function(e,element) {
	
	/* 
		we intercept the position of the mouse and 
		set the infobulle position with globals iframe position and mouse position 
	*/
	
	if (e) {
		var x = e.clientX;
		var y = e.clientY;
	} else {
		var x = element.ownerDocument.parentWindow.event.clientX;
		var y = element.ownerDocument.parentWindow.event.clientY;
	}
	
	var scrollY = window.pageYOffset || document.documentElement.scrollTop || 0; 
	var scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
	
	x = this.framex+x+scrollX+2;
	y = this.framey+y+scrollY+2;
	
	if (x + this.offsetWidth > window.document.body.offsetWidth + scrollX) x = window.document.body.offsetWidth + scrollX - this.offsetWidth;
	//window.pageYOffset
	if (y + this.offsetHeight > window.document.body.offsetHeight + scrollY) y = window.document.body.offsetHeight + scrollY - this.offsetHeight;
	
	this.style.left=x+"px";
	this.style.top=y+"px";

}

/*_______________________

	HIDE INFO BULLE
________________________*/

window.infobulle.startHide = function() {
	
	this.timeout = window.setTimeout("window.infobulle.style.visibility = 'hidden'; window.clearInterval(window.infobulle.rafraichissement)", 100);
	 
}

window.infobulle.correction = function() {
	
	var scrollY = window.pageYOffset || document.documentElement.scrollTop || 0; 
	var scrollX = window.pageXOffset || document.documentElement.scrollLeft || 0;
	
	if (this.offsetLeft + this.offsetWidth > window.document.body.offsetWidth + scrollX) this.style.left=(window.document.body.offsetWidth + scrollX - this.offsetWidth)+"px";
	if (this.offsetTop + this.offsetHeight > window.document.body.offsetHeight + scrollY) this.style.top=(window.document.body.offsetHeight + scrollY - this.offsetHeight)+"px";
}


/*__________________________

	SHOW THE INFOBULLE
____________________________*/

window.showInfoBulle = function(e, text, element) {
	
	if (this.frameElement) {
		
		/* 
			If we are on the a child iframe 
			we save and calculate position of the iframe
			And we go to the next parent iframe
		*/
		
		if (element.framex>0) { 
			element.framex+=getMasterX(this.frameElement,0); 
		} else { 
			element.framex=getMasterX(this.frameElement,0); 
		} 
		
		if (element.framey>0) { 
			element.framey+=getMasterY(this.frameElement,0); 
		} else { 
			element.framey=getMasterY(this.frameElement,0); 
		}
		
		this.parent.showInfoBulle(e, text, element);
	
	} else {
		
		/* If we are on the root frame */
		/* we save the global frames position */
		
		if (element.framex>0) {
			window.infobulle.framex = element.framex;
			element.framex = 0;
		} else {
			window.infobulle.framex = 0;
		}
		
		if (element.framey>0) {
			window.infobulle.framey = element.framey;
			element.framey = 0;
		} else {
			infobulle.framey = 0;
		}

		/* we set the event function for hide and move infobulle */
		
		element.onmousemove = function(e) { window.infobulle.move(e,this); }
		element.onmouseout = function(e) { window.infobulle.startHide(this); this.onmousemove = null; this.onmouseout = null; clearTimeout(window.infobulle.timeoutShow)}
		
		/* we destroy the curent timeout and set the infobulle's content and visibility */
		
		window.clearTimeout(window.infobulle.timeout);
		window.infobulle.style.visibility = 'hidden';
		window.infobulle.content.innerHTML = text;
		
		/* we refresh the position of infobulle (firefox position bug)  */
		window.infobulle.rafraichissement = window.setInterval("window.infobulle.correction();", 100);
		
		window.infobulle.timeoutShow = window.setTimeout('window.infobulle.style.visibility = "visible"', 100);
	
	}
}

window.infobulle.onmouseover = function() {
	
	window.clearTimeout(window.infobulle.timeout);
	
}

window.infobulle.onmouseout = function() {
	
	window.infobulle.startHide()
	
}

