/*
 * Methodes utilitaires.
 * ---------------------
 */

/* Ouverture/Fermeture d'un sous-menu par rapport à son ID */
function openClose(id, onOpen) {
	var elem = document.getElementById(id);
	if (elem.className == "open") {
		elem.className = "close";
	} else {
		elem.className = "open"
		if (onOpen) {
			eval(onOpen);
		}
	}
}
/* Retourne la largeur de la page selon le navigateur */
function getScreenWidth() {
		if ( document.width ) {
			return document.width;
		}
		return document.body.clientWidth;
}
/* Retourne la hauteur de la page selon le navigateur */
function getScreenHeight() {
		if ( document.height ) {
			return document.height;
		}
		return document.body.clientHeight;
}

/* Calcul de la position gauche de l'élément dans la page */
function offsetLeft(obj)
{
	obj = obj.parentNode;
	var curleft = 0;
	while (obj.offsetParent)
	{
		curleft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return curleft;
}

/* Calcul de la position top de l'élément dans la page */
function offsetTop(obj)
{
	obj = obj.parentNode;
	var curtop = obj.offsetHeight;
	while (obj.offsetParent)
	{
		curtop += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return curtop;
}

/* Recherche d'un tag enfant (1 niveau) selon sa class */
function findChildByClassName(elem, className) {
	var list = elem.childNodes;
	for (i=0; i<list.length; i++) {
		if( list.item(i).className == className) {
			return list.item(i);
		}
	}
	return null;
}

/* Recherche d'un tag parent selon sa class */
function findParentByClassName(elem, className) {
	var parent = elem.parentNode;
	while (parent != null) {
		if (parent.className == className) {
			return parent;
		}
		parent = parent.parentNode;
	}
	return null;
}

/* Creation d'un objet XMLHttpRequest selon le navigateur */
function getXMLHttpRequest() {
	var xhr = null; 
	 
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	return xhr;
}


/* Renvoit l'objet HTML associé à la POPUP */
function method_popup_htmlObject() {
	if (!this.obj) {
		this.obj = document.getElementById(this.id);
	}
	return this.obj;
}

/* Affiche la popup */
function method_popup_show() {
	this.htmlObject().style.visibility = "hidden";
	this.htmlObject().style.display = "block";
	this.replace();
	if (this.href) {
		// Lance le téléchargement si neccessaire
		this.download();
	}
	this.htmlObject().style.visibility = "visible";
}

/* Cache la popup */
function method_popup_hide() {
	this.htmlObject().style.display = "none";
}

function method_popup_click() {
	if (this.htmlObject().style.display == "block") {
		this.hide();
	} else {
		this.show();
	}
}

/* Repositionne la popup lors d'une redimension de la page */
function method_popup_replace() {
	var x, y;
	if (this.type=="CENTRE") {
		x = Math.round( (getScreenHeight()-this.htmlObject().offsetHeight)/2 );
		y = Math.round( (getScreenWidth()-this.htmlObject().offsetWidth)/2 );
	} else if (this.type=="PARENT") {
		x = offsetTop(this.htmlObject()) + this.posx;
		y = offsetLeft(this.htmlObject()) + this.posy;
	} else { // PAGE
		x = this.posx;
		y = this.posy;
	} 
	this.htmlObject().style.top = x + "px";
	this.htmlObject().style.left = y + "px";
}

/* Mise à jours du contenu de la popup */
function method_popup_update(html) {
	var contenu = findChildByClassName(this.htmlObject(), "body");
	if (contenu) {
		contenu.innerHTML = html;
		this.replace();
	}
}
/* Téléchargement du contenu de la popup */
function method_popup_download() {
	var popup = this;
	
	this.update("<h2>Chargement...</h2><div class='contenu'>Veuillez patienter pendant le chargement...</div>");
	
	var xhr = getXMLHttpRequest();
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4 && xhr.status == 200) {
			popup.update(xhr.responseText);
		}
	};
	xhr.open("GET", this.href, true);
	xhr.send(null);
}

/* Définition des attributs supplémentaire */
function method_popup_set(ident, type, posx, posy, href) {
	if (this.ident != ident) {
		this.ident = ident;
		this.type = type ? type : "CENTRE";
		this.posx = posx ? posx : 0;
		this.posy = posy ? posx : 0;
		this.href = href;
		if (!this.href) {
			this.update(document.getElementById(ident).innerHTML);
		}
	}
}

/* Creation d'un objet Popup */
function ScriptPopup(id) {
	/* Definition des attributs : */
	this.id = id;
	
	/* Définition des méthodes */
	this.show = method_popup_show;
	this.hide = method_popup_hide;
	this.click = method_popup_click
	this.replace = method_popup_replace;
	this.update = method_popup_update;
	this.download = method_popup_download;
	this.set = method_popup_set;
	this.htmlObject = method_popup_htmlObject;
}

/* Tableau gérant un cache de popup */
var internalPopup = new ScriptPopup("ScriptPopup");

/* Creation d'une popup en passant par le cache */
function popup(id, position, posx, posy, href) {
	internalPopup.set(id, position, posx, posy, href);
	return internalPopup;
}

function hidePopup() {
	internalPopup.hide();
}

previousOnResize = window.onresize;
/* Repositionnement automatique de la popup */
function resizePopup() {
	internalPopup.replace();
	if (previousOnResize) {
		previousOnResize();
	}
}
// window.onresize = resizePopup;


/* Ouverture/Fermeture d'un sous-menu par rapport à son ID */
function hideShow(/* ... */) {

	for (var i=0; i<hideShow.arguments.length; i++) {
		
		var elem = document.getElementById(hideShow.arguments[i]);
		if (elem.style.display=='none') {
			elem.style.display = '';
		} else {
			elem.style.display='none';
		}
	}
}

/* Téléchargement du contenu d'une partie du site */
function ajax_open(id, href) {
	// timestamp permettant d'éviter la mise en cache :
	var timestamp = "timestamp=" + new Date().getTime();
	if (href.indexOf('?')>=0) {
		href += '&' + timestamp;
	} else {
		href += '?' + timestamp;
	}
	
	var element = document.getElementById(id);

	var lg = document.forme.REF00LIN.value;
	var msg = "Chargement en cours ...";
	if (lg == "CO")
		msg = "In corsu di caricamentu ...";
	else if (lg == "IT")
		msg = "Sta caricando ...";
	else if (lg == "DE")
		msg = "Loading ...";
	else if (lg == "EN")
		msg = "Loading ...";
	else if (lg == "EN")
		msg = "Loading ...";
	
	element.innerHTML = "<div id='info'><h3>" + msg + "</h3></div>";
	var xhr = getXMLHttpRequest();
/*
	xhr.onreadystatechange = function(){
		// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
		if(xhr.readyState == 4) {

// readyState n'est géré qu'en mode asynchrone.
			if (xhr.status == 200) {
				element.innerHTML = xhr.responseText;
			} else {

				var msg = "Erreur de chargement";
				if (lg == "CO")
					msg = "Errore di caricamentu";
				else if (lg == "IT")
					msg = "Errore di caricamento";
				else if (lg == "DE")
					msg = "Loading error";
				else if (lg == "EN")
					msg = "Loading error";
				else if (lg == "EN")
					msg = "Loading error";

				element.innerHTML = "<div id='erreur'><h3>" + msg + " : " + xhr.status + "</h3></div>";
			}
		}

	}*/
	
	// true : mode asynchrone, false : mode synchrone.
	xhr.open("GET", href, false);
	xhr.send(null);
	
	if (xhr.status == 200) {
		element.innerHTML = xhr.responseText;
	} else {

			var msg = "Erreur de chargement";
			if (lg == "CO")
				msg = "Errore di caricamentu";
			else if (lg == "IT")
				msg = "Errore di caricamento";
			else if (lg == "DE")
				msg = "Loading error";
			else if (lg == "EN")
				msg = "Loading error";
			else if (lg == "EN")
				msg = "Loading error";
			
			element.innerHTML = "<div id='erreur'><h3>" + msg + " : " + xhr.status + "</h3></div>";
	}
}
