/** 
 *	arnaud@2exvia.com 
 *	27/08/2009 16:08:51
 *
 */

/**
 * Remplace document.getElementById
 */ 
function $(id) {  
	if (id == undefined) return false;
	return (document.getElementById(id.toString())) ? document.getElementById(id.toString()) : false; 
}

/**
 *  INFOBULLE
 */ 
function initInfobulle(){
	var infobulle = document.createElement('div');
	infobulle.setAttribute('id','divInfobulle');
	infobulle.style.left = "0px";
	infobulle.style.zIndex = "100";
	infobulle.className = 'infoBulle';

	var _body = document.getElementById('body');
	_body.appendChild(infobulle);
}

function afficheInfobulle(event, sTexte) {
	deplaceInfobulle(event);
	$('divInfobulle').style.display = 'block';
	$('divInfobulle').innerHTML = sTexte;
};
function deplaceInfobulle(event) {
	$('divInfobulle').style.left = Number(event.clientX + 15) + 'px';
	$('divInfobulle').style.top = Number(event.clientY + document.body.scrollTop + document.documentElement.scrollTop) + 'px';
};
function masqueInfobulle() {
	$('divInfobulle').style.display = 'none';
	$('divInfobulle').innerHTML = "";
};


// ============================================================================
// récupère la largeur de la zone visible du document
function getWidth(){
	// IE
	if (document.all) {
		return document.documentElement.clientWidth;
	// FF
	}else{
		return window.innerWidth;
	}
}

// ============================================================================
// récupère la hauteur de la zone visible du document
function getHeight(){
	// IE
	if (document.all) {
		return document.documentElement.clientHeight;
	// FF
	}else{
		return window.innerHeight;
	}
}

// ============================================================================
// récupère le top de la zone visible du document
function getTop(){
	if (typeof(window.pageYOffset) != 'undefined'){
     return window.pageYOffset;
	}else if (typeof(document.documentElement.scrollTop) != 'undefined' && document.documentElement.scrollTop > 0){
     return document.documentElement.scrollTop;
	}else if (typeof(document.body.scrollTop) != 'undefined'){
     return document.body.scrollTop;
	}
}

// ============================================================================
// récupère le left de la zone visible du document
function getLeft(){
	if (typeof(window.pageYOffset) != 'undefined'){
     return window.pageXOffset;
	}else if (typeof(document.documentElement.scrollLeft) != 'undefined' && document.documentElement.scrollLeft > 0){
     return document.documentElement.scrollLeft;
	}else if (typeof(document.body.scrollLeft) != 'undefined'){
     return document.body.scrollLeft;
	}
}




// *********************************************************************************
// 																*** POPUP ****
// *********************************************************************************

	// ============================
	// On créé la popup
	var largeur;
	var hauteur;
	
	function showPopup(message,plargeur,phauteur){
		if($('popup_container')) hidePopup();
	
		var opacity = 100;
		
		if(largeur == "undefined"){
			largeur = 400;
		}else{
			largeur = plargeur;
		}
		if(hauteur == "undefined"){
			hauteur = 400;
		}else{
			hauteur = phauteur;
		}
		
		// Body
		var _body = document.getElementById('body');
		// Conteneur
		var container = document.createElement("div");
		// voile
		var voile = document.createElement("div");
		// popup
		var popup = document.createElement("div");
		
		// Croix de fermeture
		var croix = document.createElement("img");
		
		croix.setAttribute("src","../medias/images/fermer.png");
		
		
		// ID'S
		container.setAttribute("id","popup_container");
		voile.setAttribute("id","popup_voile");
		popup.setAttribute("id","popup_message");
		
		// Dimensions de la zone visible
		var W = getWidth();
		var H = getHeight(); 
		
		W = (W<largeur)?largeur:W;
		H = (H<hauteur)?hauteur:H;

		// hauteur = (hauteur<H)?hauteur:H;

		// Récupère le top & left
		var T = getTop();
		var L = getLeft();
		
		// STYLES
		// IE
		if (document.all) {
			container.style.setAttribute("cssText","z-index:10000;position:fixed; left: 0px; top:0px; width: "+W+"px; height: "+H+"px;");
			voile.style.setAttribute("cssText","width: "+W+"px; height: "+H+"px; background: #000000; opacity: "+ opacity/100 +"; MozOpacity: "+ opacity/100 +"; KhtmlOpacity: "+ opacity/100 +"; filter: alpha(opacity="+ opacity +");");
			popup.style.setAttribute("cssText","display:block;position:absolute;padding:26px 0px 26px 26px;background-color:#000000;font-family:Trebuchet ms;font-size:12px;color:#000000;");
			croix.style.setAttribute("cssText","z-index:1;position:absolute;top:0px;left:"+(W-32)+"px;");
		// FF
		}else{
			container.setAttribute("style","z-index:10000;position:fixed; left: 0px; top:0px; width: "+W+"px; height: "+H+"px;");
			voile.setAttribute("style"," width: "+W+"px; height: "+H+"px; background: #000000; opacity: "+ opacity/100 +"; MozOpacity: "+ opacity/100 +"; KhtmlOpacity: "+ opacity/100 +"; filter: alpha(opacity="+ opacity +");");
			popup.setAttribute("style","display:block;position: absolute;-moz-border-radius:0px;-webkit-border-radius:0px;padding:26px 0px 26px 26px;background-color:#000000;font-family:Trebuchet ms;font-size:12px;color:#000000");
			croix.setAttribute("style","z-index:1;position:absolute;top:0px;left:"+(W-48)+"px;");
		}
		
		popup.style.width = largeur+"px";
		popup.style.height = hauteur+"px";
		popup.style.left = ((W-largeur)/2)+"px";
		popup.style.top = ((H-hauteur)/2)+"px";
		
		// Ajout au DOM
		
		container.appendChild(croix);
		container.appendChild(voile);
		
		container.appendChild(popup);
		_body.appendChild(container);
		popup.innerHTML = message;
		
		voile.onclick = hidePopup;
		popup.onclick = hidePopup;
		croix.onclick = hidePopup;
	}


	// ============================
	// Ferme la popup
	function hidePopup() {
		var _body = document.getElementById('body');
		var container = document.getElementById('popup_container');
		_body.removeChild(container);
	}


	// =====================================================
	// On ajuste le voile quand on redimensionne la fenetre
	window.onresize = function(){
		if(document.getElementById('popup_container')){
			var W = getWidth();
			var H = getHeight(); 
			
			document.getElementById('popup_container').style.width = W+"px";
			document.getElementById('popup_container').style.height = H+"px";
			document.getElementById('popup_voile').style.width = W+"px";
			document.getElementById('popup_voile').style.height = H+"px";
			
			document.getElementById('popup_message').style.left = ((W-largeur)/2)+"px";
			document.getElementById('popup_message').style.top = ((H-hauteur)/2)+"px";
		}
	}
	

	// =================================================
	// On ajuste le voile quand on scrolle la fenetre
	window.onscroll = function(){
		if(document.getElementById('popup_message')){
			if(hauteur > getHeight()){
				var T = getTop();
				document.getElementById('popup_message').style.top = -T+"px";
			}
			if(largeur > getWidth()){
				var L = getLeft();
				document.getElementById('popup_message').style.left = -L+"px";
			}
		}
	}




