/**
*
*	Menu
*
*/

/*{{{*/
function Menu(id){
    this.menu = $(id);
    this.listeRub = $$(this.menu, 'li');
    this.setStyle();
}

Menu.prototype = {
    setStyle: function(){
    	var n = this.listeRub.length;
    	for(var i=0; i<n; i++ ){
	    if($$(this.listeRub[i], 'a').length == 1){
		$$(this.listeRub[i], 'a')[0].innerHTML = "<span class=\"sansStyle\">" + $$(this.listeRub[i], 'a')[0].innerHTML + "</span>";
	    	$$(this.listeRub[i], 'a')[0].className = 'b' + (i + 1);
	    } else {
		this.listeRub[i].innerHTML = "<span class=\"sansStyle\">" + this.listeRub[i].innerHTML + "</span>";
	    	this.listeRub[i].className = 'b' + (i + 1) + '_hover';
	    }
	};
   }
}

Menu.prototype.constructor = Menu;
/*}}}*/

/**
*
*	ChampRecherche
*
*/

/*{{{*/
function ChampRecherche(idConteneur){
    this.widget = $(idConteneur);
    this.setVariables();
    this.setStyle();
    this.setComportement();
}

ChampRecherche.prototype = {
    setVariables: function(){
    	this.corps = $$(this.widget, 'div', 0);
	this.boutValider = $$(this.corps, 'input', 0);
	this.objet = $('objet');
	this.code = $('code');
	this.boutKML = $('bKML');
	this.rubrique = $$(this.corps, 'input', 1).value;
    },

    setStyle: function(){
	this.boutValider.className = 'sansStyle';
	$('labelRecherche').innerHTML = "<img src=\"Media/images/icone/loupe.png\" alt=\"loupe\" title=\"Rechercher\" />";

    },

    setComportement: function(){
    	this.objet.onchange = function(that){
	    return function(){
	    	chaine = 'action=getCR&objet=' + this.value;
	    	if(this.value != 0){
		    new Moteur(chaine, that.code);
		    that.code.className = '';
		} else {
		    that.code.className = 'sansStyle';
		};
	    	return false;
	    };
	}(this);
    	this.code.onchange = function(that){
	    return function(){
	    	var url = '?rub=' + that.rubrique + '&objet=' + that.objet.value + '&code=' + this.value;
	    	document.location = url;
	    	return false;
	    };
	}(this);
	this.boutKML.onclick = function(that){
	    return function(){
	    	alert('Télécharger un fichier KML');
	    	return false;
	    };
	}(this);
    }
}

ChampRecherche.prototype.constructor = ChampRecherche;
/*}}}*/

/**
*
*	FicheActivite
*	Données s'affichant sur le carte pour une animation
*
*/

/*{{{*/
function FicheActivite(idFiche){
    this.widget = $(idFiche);
    this.setVariables();
    this.setStyle();
    this.setComportement();
}

FicheActivite.prototype = {
    setVariables: function(){
    	this.titre = $$(this.widget, 'h4', 0);
    	this.boutFermer = $$(this.titre, 'a', 0);
	this.conteneur = $$(this.widget, 'div', 0);
	var listeItems = $$(this.conteneur, 'li');
	this.listeDocs = new Array();
	var n = listeItems.length;
	if(n > 0){
	    for(var i=0; i<n; i++){
	    	if(listeItems[i].id.indexOf('blocDoc') != -1){
		    this.listeDocs.push($$(listeItems[i], 'a', 0));
		};
	    };
	};
    },

    setStyle: function(){
	this.boutFermer.className = 'b_cacher_C';
    },

    setComportement: function(){
    	this.boutFermer.onclick = function(that){
	    return function(){
	    	if(this.className == 'b_montrer_C'){
		    this.className = 'b_cacher_C';
		    that.conteneur.className = 'corps';
		} else {
		    this.className = 'b_montrer_C';
		    that.conteneur.className = 'sansStyle';
		};
	    	return false;
	    };
	}(this);
	var n = this.listeDocs.length;
	for(var i=0; i<n; i++){
	    this.listeDocs[i].onclick = function(that){
	    	return function(){
			// on construit le substratVisualisateur
		    if($('substratVisualisateur')){
		    	var substratVisu = $('substratVisualisateur');
		    } else {
		    	var substratVisu = document.createElement('div');
		    	substratVisu.id = 'substratVisualisateur';
		    	document.body.appendChild(substratVisu);
		    };
			// on y balance la réponse (médialisateur) du serveur via AJAX
		    var tmp = this.id.split('_');
		    this.href = "javascript: return false;";
		    var lsTypes = new Array(1,2,6);
		    if(lsTypes.in_array(tmp[1])){
			var chaine = 'action=visuMedia&type_doc=' + tmp[1] + '&id_doc=' + tmp[2];
			new Moteur(chaine, substratVisu);
		    } else {
		    	var chaine = "engine.php?action=getDoc&id_doc=" + tmp[2];
			document.location = chaine;
		    };
		    return false;
		};
	    }(this);
	};
    }
}

FicheActivite.prototype.constructor = FicheActivite;
/*}}}*/

/**
*
*	FormulaireBilan
*	Formulaire validant le Bilan BIDULL
*/

/*{{{*/
function FormulaireBilan(idBox){
    this.widget = $(idBox);
    this.setVariables();
    this.setStyle();
    this.setComportement();
}

FormulaireBilan.prototype = {
    setVariables: function(){
    	this.boutValider = $$($$(this.widget, 'li', $$(this.widget, 'li').length - 1), 'a', 0);
    	this.selTxt = $$($$(this.widget, 'li', 5), 'select', 0);
    	this.wGenre = new BoiteSelecteurSimple($$(this.widget, 'li', 4).id);
    },

    setStyle: function(){
    	this.widget.className = 'bilan_item';
    	$$(this.widget, 'textarea', 0).className = 'desc';
    	$$(this.widget, 'textarea', 1).className = 'desc';
	this.boutValider.className = 'b_valider';
    	$$(this.widget, 'li', 7).className = 'sansStyle';
    },

    setComportement: function(){
    	this.boutValider.onclick = function(that){
	    return function(){
	    	if(confirm("Vous êtes sûr ?")){
	    	    var substratForm = document.createElement('li');
		    substratForm.id = 'substrat';
		    that.widget.appendChild(substratForm);
		    // construction de la chaine des valeurs
		    var id = '&id=' + that.widget.id.split('_')[1];
		    var lsValue = $$(that.widget, 'input');
		    var  valeurs = '&npi='+lsValue[0].value;
		    valeurs += '&npe='+lsValue[1].value;
		    valeurs += '&nhd='+lsValue[2].value;
		    valeurs += '&nhc='+lsValue[3].value;
		    valeurs += '&genre='+lsValue[4].value;
		    var lsTxt = $$(that.widget, 'textarea');
		    valeurs += '&bilan='+encodeURIComponent(lsTxt[0].value);
		    valeurs += '&rem='+encodeURIComponent(lsTxt[1].value);
		    var action = "action=valCRBid"+id+valeurs;
		    new Moteur(action, substratForm);
		};
	    	return false;
	    };
	}(this);
	this.selTxt.onchange = function(that){
	    return function(){
	    	if(this.value == 'B'){
    		    $$(that.widget, 'li', 6).className = '';
		    $$(that.widget, 'li', 7).className = 'sansStyle';
		} else {
    		    $$(that.widget, 'li', 7).className = '';
		    $$(that.widget, 'li', 6).className = 'sansStyle';
		};
	    	return true;
	    };
	}(this);
    }
}

FormulaireBilan.prototype.constructor = FormulaireBilan;
/*}}}*/

/**
*
*	ListeDocuments
*	Formulaire validant les documents BIODIV
*/

/*{{{*/
function ListeDocuments(box){
    this.box = box;
    this.setVariables();
    this.setComportement();
}

ListeDocuments.prototype = {
    setVariables: function(){
	this.widget = $$(this.box, 'ul', 0);
	var liste = $$(this.widget, 'li');
	this.boutAjout = $$(this.box, 'a', $$(this.box, 'a').length -1);
	var n = liste.length;
	this.listeDocs = new Array();
	for(var i=0; i<n-1 ; i++){
	    if(liste[i].id.indexOf('doc_') !== -1) this.listeDocs.push(new FormulaireDocument(liste[i]));
	};
    },

    setComportement: function(){
	this.boutAjout.onclick = function(that){
	    return function(){
	    	var chaine = "action=getFDocV&id=" + that.widget.id.split('_')[1];
		if($('substrat')){
		    var substratLD = $('substrat');
		} else {
		    var substratLD = document.createElement('div');
		    substratLD.id = 'substrat';
		    substratLD.className = 'sansStyle';
		    document.body.appendChild(substratLD);
		};
		new Moteur(chaine, substratLD);
	    	return false;
	    };
	}(this);
    },
    
    getNbrDocs: function(){
	return this.listeDocs.length;
    }
}

ListeDocuments.prototype.constructor = ListeDocuments;
/*}}}*/

/**
*
*	FormulaireDocument
*	Formulaire validant les documents BIODIV
*/

/*{{{*/
function FormulaireDocument(doc){
    this.widget = doc;
    this.setVariables();
    this.setStyle();
    this.setComportement();
}

FormulaireDocument.prototype = {
    setVariables: function(){
    	this.listeBouts = $$(this.widget, 'a');
	if(this.listeBouts.length > 1){
	    this.formInfo = $$(this.widget, 'div', 0);	    
	    this.formModif = $$(this.widget, 'ul', 0);
	    this.selURI = new SelTypeURI($$(this.formModif, 'li', 2));
	    this.selEsp = new SelecteurEspeces($$(this.formModif, 'li', 3));
	    this.bEff = this.listeBouts[0];
	    this.bMod = this.listeBouts[1];
	    this.bVal = $$($$(this.formModif, 'li', $$(this.formModif, 'li').length -1), 'a', 0);
	    this.bAnn = $$($$(this.formModif, 'li', $$(this.formModif, 'li').length -1), 'a', 1);
	    this.pBarre = $$($$(this.formModif, 'li', $$(this.formModif, 'li').length -1), 'span', 2);
	    this.selType = new SelecteurType($$(this.formModif, 'li', 0));
	};
    },

    setStyle: function(){
    	this.widget.className = 'doc_item';
	if(this.listeBouts.length > 1){
	    this.formInfo.className = 'doc_item';	    
	    this.formModif.className = 'sansStyle';
    	    this.bEff.className = 'effacer';
    	    this.bMod.className = 'modifier';
    	    this.bVal.className = 'b_valider';
    	    this.bAnn.className = 'b_annuler';
	};
    },

    setComportement: function(){
    	if(this.bEff){
    	    this.bEff.onclick = function(that){
	    	return function(){
		    if(that.widget.id.indexOf('docProv_') == -1){
	    	    	if(confirm("Vous êtes sûr ?")){
		    	    var substratMess = document.createElement('span');
			    substratMess.id = 'substrat';
			    that.widget.appendChild(substratMess);
			    var chaine = "action=delDoc&id=" + that.widget.id.split('_')[1];
			    new Moteur(chaine, substratMess);
			};
		    } else {
		    	var doc = $$(that.widget, 'input', $$(that.widget, 'input').length - 1);
		    	if(doc && doc.name == 'id_activite' && doc.value != '')	$('lsDoc_' + doc.value).removeChild(that.widget);
		    };
	    	    return false;
	    	};
	    }(this);
	};
    	if(this.bMod){
    	    this.bMod.onclick = function(that){
	    	return function(){
		    that.formInfo.className = 'sansStyle';
	    	    that.formModif.className = 'doc_item';
	    	    return false;
	    	};
	    }(this);
	};
    	if(this.bVal){
    	    this.bVal.onclick = function(that){
	    	return function(){
		    if($$(that.formModif, 'input', 0).value == ''){
		    	alert('Le titre est vide !');
			return false;
		    };
		    alert("Vous vous apprêtez à mettre un document sur le serveur. Selon son 'poids', cela peut prendre plusieurs minutes. Soyez patient, un message vous préviendra de la fin de la procédure !");
		    that.pBarre.className = 'progressBar';
		    that.pBarre.innerHTML = '0 %';
		    that.selEsp.selectionne();
		    // mise en place de l'iframe substrat
		    var iframeH = document.createElement('iframe');
		    iframeH.className = 'sansStyle';
		    iframeH.src = 'about:blank';
		    iframeH.name = 'iframeH';
		    iframeH.id = 'substrat';
		    that.widget.appendChild(iframeH);
		    
		    // mise en place de l'iframe substrat de la barre d'upload
		    if($('iframePB')) that.widget.removeChild($('iframePB'));
		    var iframeHPB = document.createElement('iframe');
		    iframeHPB.className = 'sansStyle';
		    iframeHPB.src = 'about:blank';
		    iframeHPB.name = 'iframeHPB';
		    iframeHPB.id = 'substratPB';
		    that.widget.appendChild(iframeHPB);

		    var formRPb = document.createElement('form');
		    formRPb.method = 'post';
		    formRPb.action = 'engine.php';
		    formRPb.className = 'sansStyle';
		    formRPb.target = 'iframeHPB';

		    var identUP = $$($$(that.widget, 'form', 0), 'input', 0).value;

		    formRPb.innerHTML = "<input type=\"hidden\" name=\"action\" value=\"getProgress\" /><input type=\"hidden\" name=\"id\" value=\"" + identUP + "\" /><input type=\"hidden\" name=\"UPLOAD_IDENTIFIER\" value=\"" + identUP + "\" />";
		    that.widget.appendChild(formRPb);
		    
		    formH = $$(that.widget, 'form', 0); 
		    formH.submit();

		    fct = function(){
			formRPb.submit();
		    };
		    setTimeout('fct()', 2000);
	    	    return false;
	    	};
	    }(this);
	};
    	if(this.bAnn){
    	    this.bAnn.onclick = function(that){
	    	return function(){
		    that.formInfo.className = 'doc_item';
	    	    that.formModif.className = 'sansStyle';
	    	    return false;
	    	};
	    }(this);
	};
    },


    updateResume: function(ident){
    	tmp = $$(this.formInfo, 'span');
	tmpM = $$(this.formModif, 'input');
	tmp[2].innerHTML = $$(this.formModif, 'option', $$(this.formModif, 'select', 0).value).innerHTML;
	tmp[3].innerHTML = tmpM[0].value;
	// especes
	var tmp2 = $$($$(this.formModif, 'select', 2), 'option');
	var tmp3 = $$($$(this.formModif, 'select', 1), 'option');
	var n = tmp2.length;
	var n2 = tmp3.length;
	var lsEspeces = new Array();
	var identifiants = new Array();

	if(ident && ident != 'init' && ident.length > 0){
		// construction du tableau new array pur pouvoir lui appliquer in_array
	    var m= ident.length;
	    for(var j=0; j<m; j++){		
		identifiants.push(ident[j]);
	    };

		// mise à jour du champ "espèces"
	    for(var j=0; j<n; j++){
	    	if(identifiants.in_array(tmp2[j].value.split('_')[1]) === true) lsEspeces.push(tmp2[j].innerHTML);
	    };
	    for(var j=0; j<n2; j++){
	    	if(identifiants.in_array(tmp3[j].value.split('_')[1])) lsEspeces.push(tmp3[j].innerHTML);
	    };
	} else if(ident && ident == 'init'){	// cas nécessitant la réinitialisation de la liste des espèces
	} else {			// cas de la copie directe de la liste de sélection (arrivée)
	    for(var i=0; i<n; i++){
		if(tmp2[i].selected && tmp2[i].value != -1) lsEspeces.push(tmp2[i].innerHTML);
	    };
	};
	tmp[5].innerHTML = (lsEspeces.length > 0) ? lsEspeces.join(', ') : 'Aucune';

	this.formInfo.className = 'doc_item';
	this.formModif.className = 'sansStyle';
    }
}

FormulaireDocument.prototype.constructor = FormulaireDocument;
/*}}}*/

/**
*
*	BilanBiodiv
*	Permet de mettre en forme un bilan pour une activité biodiv avec toutes ses composantes js
*
*/

/*{{{*/
function BilanBiodiv(idBilan){
    this.widget = $(idBilan);
    this.setVariables();
    this.setStyle();
    this.setComportement();
    this.register();
}

BilanBiodiv.prototype = {
    setVariables: function(){
	this.titre = $$(this.widget, 'h4', 0);
	this.boutMasquer = $$(this.titre, 'a', 0);
	this.contBilan = $$(this.widget, 'div', 0);
	this.bilan = ($$(this.contBilan, 'fieldset').length > 1) ? new FormulaireBilan($$(this.contBilan, 'ul', 0).id) : false;
	this.documents = new ListeDocuments($$(this.contBilan, 'fieldset', (this.bilan === false) ? 0 : 1));
    },

    setStyle: function(){
	this.widget.className = "ficheAdmin";
	this.titre.className = "header";
	$$(this.titre, 'span', 1).className = 'titre';
	$$(this.titre, 'span', 1).style.color = (this.documents.getNbrDocs() == 0) ? 'red' : 'green';
	this.boutMasquer.className = 'b_montrer_C';
	this.contBilan.className = 'sansStyle';
    },

    setComportement: function(){
    	this.boutMasquer.onclick = function(that){
	    return function(){
		if(this.className == 'b_montrer_C'){
		    this.className = 'b_cacher_C';
		    that.contBilan.className = '';
		} else {
		    this.className = 'b_montrer_C';
		    that.contBilan.className = 'sansStyle';
		};
		return false;
	    };
	}(this);
    },

    register: function(){
    	listeBilans[this.widget.id] = this;	
    }
}

BilanBiodiv.prototype.constructor = BilanBiodiv;
/*}}}*/

/**
*
*	Widget de fixation de la répartition des genres du public
*	BoiteSelecteur
*
*/

/*{{{*/
function BoiteSelecteurSimple(idBox, nbrCel){
    this.widget = $(idBox);
    this.valeur = $$(this.widget, 'input', 0).value;
    this.tableau = $$(this.widget, 'table', 0);
    this.tagDis = (this.tableau.className == 'disabled')
    this.nbrCel = (nbrCel) ? parseInt(nbrCel) : 10;
    $$(this.widget, 'input', 0).value = (this.valeur == '') ? 50 : this.valeur;
    this.setStyle();
    this.setComportement();
}

BoiteSelecteurSimple.prototype = {
    setStyle: function(){
    	this.tableau.className = 'widgetAxes';
	this.cleanCel();
    },

    setComportement: function(){
	var ligne = $$(this.tableau, 'tr', 0);
	for(var j=0; j < this.nbrCel; j++){
	    $$(ligne, 'td', j).onclick = function(that, nbr){
		return function(){
		    if(!that.tagDis){
		        var tmp = (that.nbrCel - nbr - 1) * that.nbrCel;
			$$(that.widget, 'input', 0).value = tmp;
			that.cleanCel();
		    };
		    return false;
		};
	    }(this, j);

	    $$(ligne, 'td', this.nbrCel + 1).onclick = function(that){
	    	return function(){
		    if(!that.tagDis){
		    	$$(that.widget, 'input', 0).value = 100;
		    	that.cleanCel();
		    };
	    	    return false;
		};
	    }(this);
	};
    },

    detLabel: function(n){
    	return "[" + (100 - n) + "% / " + n + "%]";
    },

    cleanCel: function(){
	var ligne = $$(this.tableau, 'tr', 0);
	for(var j=0; j < this.nbrCel - parseInt($$(this.widget, 'input', 0).value)/this.nbrCel; j++){
	    $$(ligne, 'td', j).className = 'fem';
	};
	for(var j=this.nbrCel - parseInt($$(this.widget, 'input', 0).value)/this.nbrCel; j < this.nbrCel; j++){
	    $$(ligne, 'td', j).className = 'masc';
	};
	$$($$(ligne, 'td', this.nbrCel), 'span', 0).innerHTML = this.detLabel(parseInt($$(this.widget, 'input', 0).value));
	$$($$(ligne, 'td', this.nbrCel), 'span', 0).className = 'axes_etat';
	$$(ligne, 'td', this.nbrCel).className = 'nom';
	$$(ligne, 'td', this.nbrCel + 1).className = 'fem_off';
    }
}

BoiteSelecteurSimple.prototype.constructor = BoiteSelecteurSimple;
/*}}}*/

/**
*
*	Widget de sélection du type d'URI
*	SelTypeURI
*/

/*{{{*/
function SelTypeURI(selecteur){
    this.widget = selecteur;
    this.setVariables();
    this.setStyle();
    this.setComportement();
}

SelTypeURI.prototype = {
    setVariables: function(){
    	this.bLoc = $$(this.widget, 'input', 0);
    	this.bDist = $$(this.widget, 'input', 1);
    	this.sDist = $$(this.widget, 'input', 2);
    	this.sLoc = $$(this.widget, 'input', 3);
    	this.sLeg = $$(this.widget, 'span', 0);
	this.local = true;
    },

    setStyle: function(){
    	if(this.bLoc.checked){
	    this.local = true;
	    this.sLoc.className = '';
	    this.sLeg.className = '';
	    this.sDist.className = 'sansStyle';
	} else {
	    this.sDist.className = '';
	    this.sLeg.className = 'sansStyle';
	    this.sLoc.className = 'sansStyle';
	}
    },

    setComportement: function(){
    	this.bLoc.onclick = function(that){
	    return function(){
	    	this.local = true;
	    	that.sLoc.className = '';
		that.sLeg.className = '';
	    	that.sDist.className = 'sansStyle';
	    	return true;
	    };
	}(this);

    	this.bDist.onclick = function(that){
	    return function(){
	    	this.local = false;
	    	that.sDist.className = '';
		that.sLeg.className = 'sansStyle';
	    	that.sLoc.className = 'sansStyle';
	    	return true;
	    };
	}(this);
    },

    isLocal: function(){
    	return this.local;
    }
}

SelTypeURI.prototype.constructor = SelTypeURI;
/*}}}*/

/**
*
*	Widget de sélection du type de média
*	SelecteurType
*/

/*{{{*/
function SelecteurType(selecteur){
    this.widget = selecteur
    this.setVariables();
    this.setStyle();
    this.setComportement();
}

SelecteurType.prototype = {
    setVariables: function(){
    	this.selecteur = $$(this.widget, 'select', 0);
	this.listeLabels = $$(this.widget, 'span');
   },

    setStyle: function(){
    	var valeur = this.selecteur.value;
	var n = this.listeLabels.length;
	for(var i=0; i<n; i++){
	    this.listeLabels[i].className = (this.listeLabels[i].id.split('_')[1] == valeur) ? '' : 'sansStyle';
	};
    },

    setComportement: function(){
    	this.selecteur.onchange = function(that){
	    return function(){
		var n = that.listeLabels.length;
		for(var i=0; i<n; i++){
		    that.listeLabels[i].className = (that.listeLabels[i].id.split('_')[1] == this.value) ? '' : 'sansStyle';
		};
		return true;
	    };
	}(this);
    }
}

SelecteurType.prototype.constructor = SelecteurType;
/*}}}*/

/**
*
*	Widget de sélection des espèces
*	SelecteurEspeces
*/

/*{{{*/
function SelecteurEspeces(selecteur){
    this.widget = selecteur;
    this.setVariables();
    this.setStyle();
    this.setComportement();
}

SelecteurEspeces.prototype = {
    default_size: 6,

    setVariables: function(){
	this.conteneur = $$(this.widget, 'div', 0);
	this.boites = $$(this.conteneur, 'div');
	this.boutAdd = $$(this.boites[1], 'a', 0);
	this.boutOte = $$(this.boites[1], 'a', 1);
	this.boutAdEsp = $$(this.boites[3], 'a', 0);
	this.listeDebut = $$(this.boites[0], 'select', 0);
	this.listeFin = $$(this.boites[2], 'select', 0);
	this.nEspece = $$(this.boites[3], 'input', 0);
    },

    setStyle: function(){
    	this.widget.className = 'listeEspeces';
    	this.conteneur.className = 'boites';
	this.boites[0].className = 'boite1';
	$$(this.boites[0], 'select', 0).size = this.default_size;
	this.boites[1].className = 'option';
	this.boites[2].className = 'boite2';
	$$(this.boites[2], 'select', 0).size = this.default_size;
	this.boites[3].className = 'boite3';

	this.boutAdEsp.className = 'b_ajouter1';
	this.boutAdd.className = 'b_ajouter';
	this.boutOte.className = 'b_enlever';
    },

    setComportement: function(){
    	this.boutOte.onclick = function(that){
	    return function(){
	    	var liste = $$(that.listeFin, 'option');
	    	var n = liste.length;
		for(var i=n-1; i>=0; i--){
		    if(liste[i].selected && liste[i].value != -1){
		    	if(that.listeDebut.length == 1 && that.listeDebut[0].value == -1) that.listeDebut.removeChild(that.listeDebut[0]);
		    	that.listeDebut.appendChild(liste[i]);
		    };
		};
		if(liste.length == 0){
		    var tmp = document.createElement('option');
		    tmp.value = -1;
		    tmp.innerHTML = "Ajouter ici";
		    that.listeFin.appendChild(tmp);
		};
	    	return false;
	    };
	}(this);
    	this.boutAdd.onclick = function(that){
	    return function(){
	    	var liste = $$(that.listeDebut, 'option');
	    	var n = liste.length;
		for(var i=n-1; i>=0; i--){
		    if(liste[i].selected && liste[i].value != -1){
		    	if(that.listeFin.length == 1 && that.listeFin[0].value == -1) that.listeFin.removeChild(that.listeFin[0]);
		    	that.listeFin.appendChild(liste[i]);
		    };
		};
		if(liste.length == 0){
		    var tmp = document.createElement('option');
		    tmp.value = -1;
		    tmp.innerHTML = "Ajouter ici";
		    that.listeDebut.appendChild(tmp);
		};
	    	return false;
	    };
	}(this);
    	this.boutAdEsp.onclick = function(that){
	    return function(){
	    	if(confirm("Etes-vous sûr ?")){
	    	    if(that.nEspece.value != '' && that.nEspece != ' '){
		    	var chaine = 'action=newEsp&item=' + encodeURIComponent(that.nEspece.value) + '&id_substrat=' + that.listeFin.id;
		    	new Moteur(chaine, that.listeFin, 1);
		    } else {
		    	alert('Aucune espèce...');
		    };
		};
	    	return false;
	    };
	}(this);
    },
    
    selectionne: function(){
    	var liste = $$(this.listeFin, 'option');
    	var n = liste.length;
	for(var i=0; i<n; i++){
	    liste[i].selected = true;
	};
    }
}

SelecteurEspeces.prototype.constructor = SelecteurEspeces;
/*}}}*/

/**
*	Boite médias
*	@param idBoite		id du conteneur
*	@param type		type du média: Audio, Video, Image, Lien
*	@param substrat		Objet représentant le substrat sur equel se déplacent les fenêtres
*	@param widthImg		largeur de l'image
*	@param heightImg	hauteur de l'image
*/

/*{{{*/
function BoiteMedia(idBoite, type, substrat, widthImg, heightImg){
    this.boite = $(idBoite);
    this.type = type;
    this.substrat = substrat;
    this.widthImg = widthImg;
    this.heightImg = heightImg;
    this.header = $$(this.boite, 'div', 0);
    this.corps = $$(this.boite, 'div', 1);
    this.footer = $$(this.boite, 'div', $$(this.boite, 'div').length -1);
    this.setStyle();
    this.setComportement();
}

BoiteMedia.prototype = {
    tailleMaxImage: 400,
    
    setStyle: function(){
    	this.boite.className = 'medialisateur';
    	this.header.className = 'entete';
	$$(this.header, 'a', 0).className = 'fermer';
	this.header.style.cursor = 'move';
	$$(this.header, 'span', 0).className = 'ico'+this.type;
    	this.footer.className = 'fin';
	// highlight

	var patron = /<b name="([^"]*)">([^<]*)<\/b>/ig;
	$$(this.corps, 'span', 0).innerHTML = $$(this.corps, 'span', 0).innerHTML.replace(patron, '<span class="$1">$2</span>');
	$$(this.corps, 'span', 1).innerHTML = $$(this.corps, 'span', 1).innerHTML.replace(patron, '<span class="$1">$2</span>');

	// habillage du corps
	this.corps.className = 'corps';
	$$(this.corps, 'span', 0).className = 'titre';
	$$(this.corps, 'span', 1).className = 'description';
	// traitement des photos trop grande
	if($$(this.corps, 'a', 0)){
	    this.photo = $$($$(this.corps, 'a', 0), 'img', 0);
	    if(this.widthImg > this.tailleMaxImage){
		this.photo.width = this.tailleMaxImage;
		this.photo.height = this.heightImg * (this.tailleMaxImage / this.widthImg);
	    };
	};
    },

    setComportement: function(){
        $$(this.header, 'a', 0).onclick = function(that){
	    return function() {
		if(that.type == 'Video'){
		    try { $$($('substratVisualisateur'), 'object', 0).stop() }
		    catch (excp){};
		};
	    	document.body.removeChild($('substratVisualisateur'));
	    	return false;
	    };
	}(this);

	// on rend la boite déplaçable dans le substrat
	var tampon = this
	this.header.onmousedown = function(evt){
	    if(tampon.substrat) tampon.substrat.register(evt, tampon.boite);
	};

	this.header.onmouseup = function(evt){
	    if(tampon.substrat) tampon.substrat.deregister();
	};

	//	Cas où la boite média contient une photo
	if($$(this.corps, 'a', 0)){
	    $$(this.corps, 'a', 0).onclick = function(that){	// on la rend cliquable et on créé le widget de visualisation
	    	return function(){
		    var grandePhoto = document.createElement('div');	// grandePhoto est la boite de visualisation de l'image en plein écran
		    grandePhoto.id = 'visuPhoto';
		    var photo = document.createElement('img');
		    photo.src = $$(this, 'img', 0).src;
		    // retaille de l'image si nécessaire
		    var ecranWidth = parseInt(document.body.clientWidth);
		    var ecranHeight = parseInt(document.body.clientHeight);
		    if(parseInt(photo.width) >= ecranWidth){
		    	var facteur = parseInt(photo.height) / parseInt(photo.width);
		    	photo.width = ecranWidth*0.7;
			photo.height = facteur * parseInt(photo.width);
		    };
		    if (parseInt(photo.height) >= ecranHeight) {
		    	var facteur = parseInt(photo.width) / parseInt(photo.height);
		    	photo.height = ecranHeight*0.7;
			photo.width = facteur * parseInt(photo.height);
		    };
		    // on centre la photo
		    photo.style.marginTop = '-' + Math.round(photo.height / 2) + "px";
		    photo.style.marginLeft = '-' + Math.round(photo.width / 2) + "px";
		    grandePhoto.appendChild(photo);
		    grandePhoto.className = 'zoomImage';
		    document.body.appendChild(grandePhoto);

		    grandePhoto.onclick = function(){		// on rend le zoom cliquable (pour donner la possibilité de le fermer)
			document.body.removeChild($('visuPhoto'));
		    };

		    return false;
	    	};
	    }(this);
	};
    }
}

BoiteMedia.prototype.constructor = BoiteMedia;
/*}}}*/

/**
*	Boite Globale
*	implémente le substrat où vont se déplacer les fenêtres
*	est rattaché au corps de la page (Body)
*/

/*{{{*/
function BoiteGlobale(){
    this.boite = document.body;
    this.fenetreCourante = 0;
    this.posXinit = 0;
    this.posYinit = 0;
    this.setComportement();
}

BoiteGlobale.prototype = {
    ZIndexMax: 510,
    setComportement: function() {
	var courantSubstrat = this;
	this.boite.onmousemove = function(evt){
	    if(courantSubstrat.fenetreCourante != 0){
		var mousePos = courantSubstrat.getMousePos(evt);
		$(courantSubstrat.fenetreCourante.id).style.left = (mousePos.x - courantSubstrat.posXinit) + 'px';
		$(courantSubstrat.fenetreCourante.id).style.top = (mousePos.y - courantSubstrat.posYinit) + 'px';
	    };
	};
    },

    /**
    *	Débute un mouvement
    *	@param evt vide, événement
    *	@param contenant Objet du DOM devant se déplacer)
    */
    register: function(evt, contenant){
	this.fenetreCourante = contenant;
	this.setWindowFocus(this.fenetreCourante.id);
	var mouseInit = this.getMousePos(evt);
	var windowInit = this.getWindowPos($(this.fenetreCourante.id));
	this.posXinit = mouseInit.x - windowInit.x;
	this.posYinit = mouseInit.y - windowInit.y;
    },
    /**
    *	Termine un mouvement
    */
    deregister: function(){
	windowFin = this.getWindowPos($(this.fenetreCourante.id));
	this.fenetreCourante = 0;
	this.posXinit = 0;
	this.posYinit = 0;
    },

    /**
    *	Fonctions internes de positionnement/localisation
    */

    getMousePos: function(evt){
	if(!evt) var evt = window.event;
	var pX = (evt.pageX) ? evt.pageX : (evt.clientX) ? (evt.clientX + this.boite.scrollLeft - this.boite.clientLeft) : false;
	var pY =  (evt.pageY) ? evt.pageY : (evt.clientY) ? (evt.clientY + this.boite.scrollTop - this.boite.clientTop) : false;
	return {x:pX, y:pY}
    },
    
    getWindowPos: function(fenetre){
	var pWX = 0;
	var pWY = 0;
	fenetre.offsetParent;
	while(fenetre.offsetParent){
	    pWX += fenetre.offsetLeft;
	    pWY += fenetre.offsetTop;
	    fenetre = fenetre.offsetParent;
	};
	pWX += fenetre.offsetLeft;
	pWY += fenetre.offsetTop;
	return {x: pWX, y: pWY};
    },
    
    setWindowFocus: function(fenetre){
	$(fenetre).style.position = 'absolute';
	if(!$(fenetre).style.zIndex | $(fenetre).style.zIndex < this.ZIndexMax){
	    $(fenetre).style.zIndex = (1 + parseInt(this.ZIndexMax));
	    this.ZIndexMax++;
	} else if($(fenetre).style.zIndex > this.ZIndexMax){
	    this.ZIndexMax = parseInt($(fenetre).style.zIndex);
	};
    }
}

BoiteGlobale.prototype.constructor = BoiteGlobale;
/*}}}*/
function selectListeBilans(id){
    return listeBilans[id];
}

function twistKeyBilans(idBilan, idOld, idNew){
    var tmp = listeBilans[idBilan].documents[idOld];
    listeBilans[idBilan].documents[idNew] = tmp;
}

