// détermine le navigateur de l'utilisateur
// (4+ = versions 4 et ultérieures)
var nn4 = (document.layers); // Netscape 4.xx
var dom = (document.getElementById); // IE 5+, Netscape 6+, Mozilla
var iex = (document.all); // IE 4+

// le constructeur de la classe Calque admet 2 arguments :
// - l'id du layer
// - si le layer est visible ou pas (false/true) :
//   si cet argument est omis, le layer est rendu visible
function Calque(id) {
  this.id = id;
  this.x = null;
  this.y = null;
  this.layer = null;
  this.style = null;
  if (arguments.length == 2) this.visible = arguments[1];
  else this.visible = true;
  if (nn4) {
     if (document.layers[id]) {
        this.layer = document.layers[id];
        this.style = document.layers[id];
     }
  }
  else if (dom) {
     if (document.getElementById(id)) {
        this.layer = document.getElementById(id);
        this.style = document.getElementById(id).style;
     }
  }
  else if (iex) {
     if (document.all[id]) {
        this.layer = document.all[id];
        this.style = document.all[id].style;
     }
  }
  if (this.style) this.z = this.style.zIndex;
  else this.z = null;
}

// permet de masquer le calque
Calque.prototype.masquer = function() {
  if (this.style) {
     this.style.visibility = nn4 ? "hide" : "hidden";
     this.visible = false;
  }
}

// permet de réafficher le calque
Calque.prototype.montrer = function() {
  if (this.style) {
     this.style.visibility = nn4 ? "show" : "visible";
     this.visible = true;
  }
}

// déplace le calque de dx horizontalement et de dy verticalement
Calque.prototype.deplacer = function(dx, dy) {
  if (this.style) {
     this.x = parseInt(this.style.left) + dx;
     this.y = parseInt(this.style.top) + dy;
     this.style.left = this.x;
     this.style.top = this.y;
  }
}

// déplace le calque vers le point (x, y)
Calque.prototype.deplacer_vers = function(x, y) {
  if (this.style) {
     this.x = x;
     this.y = y;
     this.style.left = x+"px";
     this.style.top = y+"px";
  }
}

// affecte le contenu en argument au calque
Calque.prototype.changer_contenu = function(valeur) {
  if (this.style) {
     if (nn4) {
        this.layer.document.write(valeur);
        this.layer.document.close();
     }
     else this.layer.innerHTML = valeur;
  }
}

function GestionCalques() {
  this.calques = new Array(); // tableau des calques
  this.calque_cour = -1; // calque en cours ou dernier calque traité
}

// détermine quel indice de this.calques correspond à l'id
// si aucun calque n'a cette id, on en crée un nouveau
GestionCalques.prototype.cherche_id = function(id) {
  this.calque_cour = -1;
  for (var i=0;i<this.calques.length;i++) {
     if (this.calques[i].id == id) {
        this.calque_cour = i;
        break;
     }
  }
  if (this.calque_cour == -1) {
     this.calques[this.calques.length] = new Calque(id);
     this.calque_cour = this.calques.length - 1;
  }
}

// cache le calque d'id "id"
GestionCalques.prototype.masquer = function() {
  if (arguments.length == 1) this.cherche_id(arguments[0]);
  this.calques[this.calque_cour].masquer();
}

// réaffiche le calque d'id "id"
GestionCalques.prototype.montrer = function() {
  if (arguments.length == 1) this.cherche_id(arguments[0]);
  this.calques[this.calque_cour].montrer();
}

// déplace un calque vers le point (x, y)
// un 3e argument facultatif indique l'id du calque à déplacer
GestionCalques.prototype.deplacer_vers = function(x, y) {
  if (arguments.length == 3) this.cherche_id(arguments[2]);
  this.calques[this.calque_cour].deplacer_vers(x, y);
}

// déplace un calque de dx horizontalement et de dy verticalement
// un 3e argument facultatif indique l'id du calque à déplacer
GestionCalques.prototype.deplacer = function(dx, dy) {
  if (arguments.length == 3) this.cherche_id(arguments[2]);
  this.calques[this.calque_cour].deplacer(dx, dy);
}

// change le contenu du calque
// un 2e argument facultatif indique l'id du calque à déplacer
GestionCalques.prototype.changer_contenu = function(valeur) {
  if (arguments.length == 2) this.cherche_id(arguments[1]);
  this.calques[this.calque_cour].changer_contenu(valeur);
}