////////////////////////////////////////////
//////// Auteur : Matthieu DAMBRUNE ////////
////////////// Nom : InfoBox ///////////////
////////////// Version : 0.6 ///////////////
////////////////////////////////////////////




/////////////////////////////////////////////////////// Paramètres ///////////////////////////////////////////////////////
// Activation du fondu -> true (activer) / false (désactiver)
	var WantFade = false;
// Activation du suivi du curseur par l'infoBox -> true (statique) / false (dynamique)
	var Fixed = false;
// Vitesse de fondu -> de 1 (lent) à 25 (instantané)
	var FadeSpeed = 3;
// Opacité maximum -> de 1 (invisible) à 100 (opaque)
	var OpacityMax = 70;
// Position verticale(Y) et horizontale(X) de l'infoBox par rapport au curseur -> de 5 à l'infini
	var BoxPosX = 20;
	var BoxPosY = 20;


/////////////////////////////////////////////////// Fonctions Internes ///////////////////////////////////////////////////
///////// Récupération de l'id d'une balise /////////
function getId (id) {
	return document.getElementById(id);
}
/////////////////////////////////////////////////////


///////// Chargement des Evenements - javascript non intrusif /////////
function loadEvent(event) {
	var oldOnload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = event;
	} 
	else {
	    window.onload = function() { 
							if (oldOnload) {
								oldOnload();
							}
							event();
						}
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




//////////////////////////////////////////////////// Script d'infobox ////////////////////////////////////////////////////
///////// Variables /////////
// Objet
	var FadeBox;
// Variable de sécurité
	var flag = false;  
// Opacité de l'infoBox
	var opacity = 0;
/////////////////////////////




///////// Arrêt de la boucle d'apparition de l'infoBox /////////
function stopFade() {
	clearInterval(FadeBox);
}
////////////////////////////////////////////////////////////////




///////// Apparition de la l'infobox en fondu /////////
function show(info) {	
	if (flag == false) {
		stopFade();
		
		if (!WantFade) {
			getId("infoBox").style.filter = "alpha(opacity=1)";
			getId("infoBox").style.opacity = 1;
		}
		
		getId("infoBox").style.visibility = "visible";
		getId("infoBox").innerHTML = info;
		FadeBox = setInterval("fadeShow()", 10);
		
		flag = true;
	}
}
//////////////////////////////////////////////////////


///////// Augmentation de l'opacité de l'infobox /////////
function fadeShow() {
	if (document.selection) {	// Si on est sous IE
		if ((WantFade) && (opacity < OpacityMax)) {
			opacity = opacity + FadeSpeed;
			getId("infoBox").style.filter = "alpha(opacity="+ opacity +")";
		}
		else stopFade();
	}
	else {
		if ((WantFade) && (opacity < (OpacityMax/100))) {
			opacity = opacity + (FadeSpeed/100);
			getId("infoBox").style.opacity = opacity;
		}
		else stopFade();
	}
	return opacity;
}
//////////////////////////////////////////////////////////




///////// Disparition de la l'infobox en fondu /////////
function hide() {
	if (flag == true) {
		stopFade();
		
		FadeBox = setInterval("fadeHide()", 10);
		
		flag = false;
	}
}
////////////////////////////////////////////////////////


///////// Reduction de l'opacité de l'infobox /////////
function fadeHide() { 
	if ((WantFade) && (opacity > 0)) {
		if (document.selection) {	// Si on est sous IE
			opacity = opacity - FadeSpeed;
			getId("infoBox").style.filter = "alpha(opacity="+ opacity +")";
		}
		else {
			opacity = opacity - (FadeSpeed/100);
			getId("infoBox").style.opacity = opacity;
		}
	}
	else {
		stopFade();
		getId("infoBox").style.visibility = "hidden";
		getId("infoBox").innerHTML = "";
	}
	return opacity;
}
///////////////////////////////////////////////////////




///////// Recherche de la position idéale de l'infoBox à chaque mouvement /////////
(Fixed) ? document.onmousemove = static : document.onmousemove = dynamic
///////////////////////////////////////////////////////////////////////////////////


///////// Affichage de l'infobox dans sa position idéale /////////
function dynamic(box) {
	if (flag) {
		var infoBox = getId("infoBox");
	// Dimensions de l'infoBox
		var boxWidth = infoBox.offsetWidth;
		var boxHeight = infoBox.offsetHeight;
	// Dimensions de la fenêtre du navigateur du client
		var screenWidth = document.documentElement.clientWidth;
		var screenHeight = document.documentElement.clientHeight;
	// Taille des scrolls
		var scrollHeight = document.documentElement.scrollTop;
		var scrollWidth = document.documentElement.scrollLeft;
	// Dimension de la page
		var pageHeight = screenHeight + scrollHeight;
		var pageWidth = screenWidth + scrollWidth;
	

	// Position du curseur dans la page
		if (document.selection) {	// Si on est sous IE
			var cursorPosX = event.x + scrollWidth;
			var cursorPosY = event.y + scrollHeight;
		}
		else {
			var cursorPosX = box.pageX;
			var cursorPosY = box.pageY;
		}
		
	// Verification de la position de l'infoBox par rapport à la fenêtre du navigateur
		if (((cursorPosX+BoxPosX+boxWidth+5) < pageWidth)) {
			infoBox.style.left = cursorPosX+BoxPosX + "px"
				
		// Positionnement par rapport au curseur: bas/droite	
			if ((cursorPosY+BoxPosY+boxHeight+5) < pageHeight) {	
				infoBox.style.top = cursorPosY+BoxPosY + "px";
			}
		// Positionnement par rapport au curseur: haut/droite
			else {	
				((BoxPosY+boxHeight+5) < cursorPosY-scrollHeight) ? (infoBox.style.top = cursorPosY-BoxPosY-boxHeight + "px") : (infoBox.style.top = scrollHeight+5 + "px")
			}
		}
		else {
			infoBox.style.left = cursorPosX-BoxPosX-boxWidth + "px";
				
		// Positionnement par rapport au curseur: bas/gauche
			if ((cursorPosY+BoxPosY+boxHeight+5) < pageHeight) {
				infoBox.style.top = cursorPosY+BoxPosY+ "px";
			}
		// Positionnement par rapport au curseur: haut/gauche
			else {	
				((BoxPosY+boxHeight+5) < cursorPosY-scrollHeight) ? (infoBox.style.top = cursorPosY-BoxPosY-boxHeight + "px") : (infoBox.style.top = scrollHeight+5 + "px")
			}
		}
	}
}
//////////////////////////////////////////////////////////////////


///////// Affichage de l'infobox dans une position prédéfinie /////////
function static() {
	

}
///////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////