// déclaration de constantes
var jsTopLeft             = 0;
var jsTopCentered         = 1;
var jsTopRight            = 2;
var jsCenteredLeft        = 3;
var jsCenteredCentered    = 4;
var jsCenteredRight       = 5;
var jsBottomLeft          = 6;
var jsBottomCentered      = 7;
var jsBottomRight         = 8;

var jsNormal = "normal";
var jsMaximise = "maximise";
var jsMinimise = "minimise";
var jsZINDEX = 0;

// constructeur
function CalqueClasse(sCalque, iLargeur, iHauteur, bFixe, objRacine) {
//   PRECONDITION(!isNaN(parseInt(iHauteur)), "la hauteur est invalide");
//   PRECONDITION(!isNaN(parseInt(iLargeur)), "la largeur est invalide");
//   PRECONDITION(this.m_div == null, "Calque déjà existant.");

   // attributs
   this.m_etat = jsNormal;
   this.m_visibilite = false;
   this.m_calque = sCalque;
   
   this.m_hauteur = iHauteur;
   this.m_largeur = iLargeur;
   this.m_gauche = -1000;
   this.m_haut = -1000;
   this.m_racine = (objRacine == null) ? "document" : objRacine;
   this.m_fixe = bFixe;
   this.m_div = null;
   this.m_documentAttache = null;

//   POSTCONDITION(this.m_calque == sCalque, "affectation incorrecte");
//   POSTCONDITION(this.m_hauteur == iHauteur, "affectation incorrecte");
//   POSTCONDITION(this.m_largeur == iLargeur, "affectation incorrecte");

   // prototypage
   this.fixerCalque = fixerCalque;
   this.remplirCalque = remplirCalque;
   this.asgCalque = asgCalque;
   this.deasgCalque = deasgCalque;
   this.asgPosition = asgPosition;
   this.asgPositionFormatee = asgPositionFormatee;
   this.asgDimensions = asgDimensions;
   this.asgTransparence = asgTransparence;
   this.asgEtatCalque = asgEtatCalque;
   this.supprimerCalque = supprimerCalque;
   this.visibleCalque = visibleCalque;
   this.reqEtatCalque = reqEtatCalque;
   this.reqVisibiliteCalque = reqVisibiliteCalque;
   this.reqNomCalque = reqNomCalque;
   this.reqPositionX = reqPositionX;
   this.reqPositionY = reqPositionY;
   this.reqDiv = reqDiv;
   this.reqLargeurCalque = reqLargeurCalque;
   this.reqHauteurCalque = reqHauteurCalque;
   this.reqNavigateur = reqNavigateur;
   this.mettreAvantPlan = mettreAvantPlan;
   this.alternerListesDeroulantes = alternerListesDeroulantes;

   function reqNavigateur() {
      return (document.all != null) ? "ie" : "ns";
   }

   // méthodes
   function fixerCalque(bFixe) {
      if (bFixe) {
         this.m_fixe = true;
      }
      else {
         this.m_fixe = false;
      }
   }

   function remplirCalque(sHTML, objSource, objOuverture, sModeleCalque) {
      this.m_racine = eval(this.m_racine);
      this.m_div = (!this.m_racine.getElementById(this.m_calque)) ? this.m_racine.createElement("div") : this.m_racine.getElementById(this.m_calque);
   this.m_div.style.zIndex = (++jsZINDEX);


      this.m_div.className = (sModeleCalque == null) ? "layDefaut" : sModeleCalque;

      if (sHTML != null) this.m_div.innerHTML = sHTML;

      if (this.m_fixe) {
         this.m_div.style.left   = this.m_gauche;
         this.m_div.style.top    = this.m_haut;
      }
      else {
         if (objOuverture != null) {
            var iGauche = objOuverture.offsetLeft;
            var iHaut = objOuverture.offsetTop;
         }
         else {
            var iGauche = iHaut = 0;
         }

         var iX = (reqNavigateur() == "ie") ? objSource.x : objSource.pageX;
         var iY = (reqNavigateur() == "ie") ? objSource.y : objSource.pageY;
         this.m_div.style.left = (document.body.clientWidth < (parseInt(iGauche) + parseInt(iX) + parseInt(this.m_largeur))) ? document.body.clientWidth - this.m_largeur - 5 + "px" : (parseInt(iGauche) + parseInt(iX)) + "px";
         this.m_div.style.top = (document.body.clientHeight < (parseInt(iHaut) + parseInt(iY) + parseInt(this.m_hauteur))) ? document.body.clientHeight - this.m_hauteur - 5 + "px" : (parseInt(iHaut) + parseInt(iY)) + "px";
      }
      this.m_div.style.width  = this.m_largeur + "px";
      this.m_div.style.height = this.m_hauteur;
      this.m_div.style.visibility = (this.m_visibilite) ? "visible" : "hidden";
      this.m_div.setAttribute("id", this.m_calque);
   }

   function asgCalque(sDocumentAttache) {
      this.m_documentAttache = sDocumentAttache;

      if (this.m_racine.getElementById(this.m_documentAttache) != null)
         this.m_racine.getElementById(this.m_documentAttache).appendChild(this.m_div);
      else
         this.m_racine.getElementsByTagName(this.m_documentAttache)[0].appendChild(this.m_div);

      // ajustement automatique de la taille du calque
      if ((this.m_div.childNodes[0].offsetHeight > this.m_div.offsetHeight) || (this.m_div.childNodes[0].offsetWidth > this.m_div.offsetWidth)) {
         iLargeur = (this.m_div.childNodes[0].offsetWidth > this.m_div.offsetWidth) ? this.m_div.childNodes[0].offsetWidth : this.m_div.offsetWidth;
         iHauteur = (this.m_div.childNodes[0].offsetHeight > this.m_div.offsetHeight) ? this.m_div.childNodes[0].offsetHeight : this.m_div.offsetHeight;
         this.asgDimensions(iLargeur, iHauteur);
      }
   }

   function deasgCalque() {
      if (this.m_racine.getElementById(this.m_documentAttache) != null)
         this.m_racine.getElementById(this.m_documentAttache).removeChild(this.m_div);
      else
         this.m_racine.getElementsByTagName(this.m_documentAttache)[0].removeChild(this.m_div);
   }

   function asgPosition(x, y) {
      this.m_div.style.left = x;
      this.m_div.style.top = y;

      this.m_gauche = x;
      this.m_haut = y;
   }

   function asgDimensions(iLargeur, iHauteur) {
      this.m_div.style.width = iLargeur + "px";
      this.m_div.style.height = iHauteur + "px";

      this.m_largeur = iLargeur;
      this.m_hauteur = iHauteur;
   }

   function asgTransparence(iTransparence) {
      if (reqNavigateur() == "ie")  
         this.m_div.style.filter = "alpha(opacity=" + (100 - iTransparence) + ")";
      else
         this.m_div.style.MozOpacity = 1 - (iTransparence / 100);
   }

   function asgPositionFormatee(iPosition) {
//      PRECONDITION(this.m_div != null, "Calque non initialisé");
//      PRECONDITION(this.m_fixe, "attribut dynamique.");

      var iAppendHeight = parent.document.body.scrollTop;
      switch (iPosition) {
         case jsTopLeft          : 
            this.m_div.style.left   = 0;
            this.m_div.style.top    = 0;
            break;
         case jsTopCentered      :
            this.m_div.style.left   = ((document.body.clientWidth - this.m_div.style.width.replace("px", "")) / 2);
            this.m_div.style.top    = 5;
            break;
         case jsTopRight         : 
            this.m_div.style.left   = document.body.clientWidth - this.m_div.style.width.replace("px", "") - 25;
            this.m_div.style.top    = 5;
            break;
         case jsCenteredLeft     : 
            this.m_div.style.left   = 5;
            this.m_div.style.top    = ((iAppendHeight + this.m_hauteur) / 2);
            break;
         case jsCenteredCentered :
            this.m_div.style.left   = ((document.body.clientWidth - this.m_div.style.width.replace("px", "")) / 2);
            this.m_div.style.top    = ((iAppendHeight + document.body.clientHeight - this.m_hauteur) / 2);
            break;
         case jsCenteredRight    :
            this.m_div.style.left   = (document.body.clientWidth - this.m_div.style.width.replace("px", "")) - 25;
            this.m_div.style.top    = ((iAppendHeight + this.m_hauteur) / 2);
            break;
         case jsBottomLeft       :
            this.m_div.style.left   = 5;
            this.m_div.style.top    = (iAppendHeight + this.m_hauteur - 130);
            break;
         case jsBottomCentered   :
            this.m_div.style.left   = ((document.body.clientWidth - this.m_div.style.width.replace("px", "")) / 2);
            this.m_div.style.top    = (iAppendHeight + document.body.clientHeight - this.m_hauteur);
            break;
         case jsBottomRight      :
            this.m_div.style.left   = (document.body.clientWidth - this.m_div.style.width.replace("px", "")) - 25;
            this.m_div.style.top    = (iAppendHeight + document.body.clientHeight - this.m_hauteur - 5);

            break;
      }
   }

   function visibleCalque(bVisible) {
      if (this.m_div != null) {
         this.m_div.style.visibility = (bVisible) ? "visible" : "hidden";
         this.m_visibilite = bVisible;
      }      
   }

   function supprimerCalque() {
      var iNbInconnus = 0;
      
      if ((this.m_documentAttache != null) && (this.m_div != null)) {

         while (this.m_div.getElementsByTagName("IFRAME")[0] != null) {
            if (this.m_div.getElementsByTagName("IFRAME")[0].id != "") {
               eval("parent." + this.m_div.getElementsByTagName("IFRAME")[0].id + " = null");
               this.m_div.getElementsByTagName("IFRAME")[0].parentNode.removeChild(this.m_div.getElementsByTagName("IFRAME")[0]);
            }         
            else {
               if (iNbInconnus == this.m_div.getElementsByTagName("IFRAME").length)
                  break;
               iNbInconnus++;               
            }            
         }

         this.m_div.innerHTML = "";
         this.m_racine.getElementById(this.m_documentAttache).removeChild(this.m_div);
         this.m_documentAttache = null;
      }
      this.m_div = null;
   }

   function asgEtatCalque(sEtat) {
//      PRECONDITION(sEtat == jsNormal || sEtat == jsMaximise || sEtat == jsMinimise);
      this.m_etat = sEtat;
//      POSTCONDITION(this.m_etat != null);
   }

   function reqNomCalque() {
      return this.m_calque;
   }

   function reqPositionX() {
      return this.m_gauche;
   }

   function reqPositionY() {
      return this.m_haut;
   }

   function reqDiv() {
      return this.m_div;
   }

   function reqHauteurCalque() {
      return this.m_hauteur;
   }

   function reqLargeurCalque() {
      return this.m_largeur;
   }

   function reqVisibiliteCalque() {
      return this.m_visibilite;
   }

   function reqEtatCalque() {
      return this.m_etat;
   }
   
   function mettreAvantPlan() {   
      this.m_div.style.zIndex = (++jsZINDEX);
   }
      
   function alternerListesDeroulantes(bVisible) {
      if (jsListesSelection != null) {
         for (var i = 0; i < jsListesSelection.length; i++) {
            document.getElementById(jsListesSelection[i]).style.visibility = (bVisible) ? "visible" : "hidden";
         }
      }
   }
}

