var aff = (navigator.appName == 'Microsoft Internet Explorer') ? 'block' : 'table-row';

window.onload = function() {
   if (document.getElementById('dateDebut')) {
      var champs = [];
      champs.push('dateDebut');
      champs.push('dateFin');
      initChampsDate(champs);
   }
   
   var liste = document.getElementById('tableListe');
   if (!liste) {
      return;
   }
   var trs = liste.getElementsByTagName('tr');
   var divs = null;
   for (var i = 1; i < trs.length; i++) {
      if (trs[i].className.indexOf('trLot') == -1) {
         trs[i].onmouseover = function () {
            this.className += "Over";
         };
         trs[i].onmouseout = function () {
            this.className = this.className.replace(/\s*Over\s*/gi, '');
         };
      }
      divs = trs[i].getElementsByTagName('div');
      for (var j = 0; j < divs.length; j++) {
         if (divs[j].className.indexOf('divInfo') == 0) {
            new InfoBulle(divs[j].parentNode);
         }
      }
   }
   var imgs = liste.getElementsByTagName('img');
   for (i = 0; i < imgs.length; i++) {
      if (imgs[i].className.indexOf("pouce") == 0) {
         imgs[i].onmouseover = pouceOver;
         imgs[i].onmouseout  = pouceOut;
      }
      else if (imgs[i].className.indexOf("imgEntite") == 0) {
         imgs[i].onmouseover = entiteOver;
         imgs[i].onmouseout  = entiteOut;
      }
   }
   var as = liste.getElementsByTagName('a');
   for (i = 0; i < as.length; i++) {
      if (as[i].id.indexOf('plus_') == 0) {
         as[i].onclick = afficherLots;
         as[i].onclick();
         as[i].onmouseover = pouceOver;
         as[i].onmouseout  = pouceOut;
      }
   }
};
var pouceOver = function () {
   var couleur = this.className.substring(this.className.lastIndexOf(' ') + 1);
   var table = document.getElementById('tableListe');
   var div = document.getElementById('pouce_' + couleur);
   div.style.display = 'block';
   div.style.top = (table.offsetTop + (this.parentNode.offsetTop - div.offsetHeight - 5)) + "px";
   div.style.left = (table.offsetLeft + this.parentNode.offsetLeft + 10) + "px";
};
var pouceOut = function () {
   var couleur = this.className.substring(this.className.lastIndexOf(' ') + 1);
   var div = document.getElementById('pouce_' + couleur);
   div.style.display = 'none';
};
var entites = [];
var entiteOver = function () {
   if (this.hasAttribute('title')) {
      entites[this.src] = this.title;
      this.removeAttribute('title');
   }
   var table = document.getElementById('tableListe');
   var div = document.getElementById('infoBulle_entites');
   div.style.display = 'block';
   div.style.top = (table.offsetTop + (this.parentNode.offsetTop - div.offsetHeight - 5)) + "px";
   div.style.left = (table.offsetLeft + this.parentNode.offsetLeft + 10) + "px";
   div.innerHTML = entites[this.src];
};
var entiteOut = function () {
   document.getElementById('infoBulle_entites').style.display = 'none';
};
var afficherLots = function () {
   var idOffre = this.id.substr(this.id.indexOf('_') + 1);
   var trs = document.getElementById('tableListe').getElementsByTagName('tr');
   for (var i = 0; i < trs.length; i++) {
      if (trs[i].className.indexOf('lot_' + idOffre) != -1) {
         trs[i].style.display = trs[i].style.display == 'none' ? aff : 'none';
      }
   }
}
function InfoBulle(div) {
   this.cellule = div;
   this.infoBulle = null;
   this.table = document.getElementById('tableListe');
   
   var objet = this;
   this.cellule.onmouseover = function (e) { objet.over(e); };
   this.cellule.onmouseout  = function () { objet.out(); };
   this.creerInfoBulle();
}
InfoBulle.prototype.creerInfoBulle = function () {
   this.infoBulle = document.createElement('div');
   this.infoBulle.className = 'infoBulle';
   var tr = this.cellule.parentNode;
   if (tr.className != '') {
      this.infoBulle.className += " " + tr.className.substr(2);
   }
   this.infoBulle.innerHTML = '<b>Intitul&eacute; :</b><br>';
   if (document.getElementById(this.cellule.className)) {
      this.infoBulle.innerHTML = '<b>'
         + document.getElementById(this.cellule.className).innerHTML
         + ' :</b><br>'
   }
   this.infoBulle.innerHTML += this.cellule.getElementsByTagName('div')[0].innerHTML;
   if (this.cellule.className == 'intituleArchGrand' && this.cellule.parentNode.getElementsByTagName('td')[0].getElementsByTagName('a').length > 0) {
      this.infoBulle.innerHTML += '<div class="legende">Avis &agrave; lots multiples, cliquez sur <img src="images/plus.gif"> pour les consulter</div>';
   }
   if (this.cellule.getElementsByTagName('div')[0].className.indexOf('modifie') != -1) {
      this.infoBulle.innerHTML += '<div class="legende"><img src="images/caution.jpg" style="margin-right: 5px;" align="left">Cet avis a &eacute;t&eacute; modifi&eacute;. Veuillez consulter celui qui est <b>non barr&eacute;</b>.';
   }
   document.body.appendChild(this.infoBulle);
};
InfoBulle.prototype.over = function (e) {
   var objet = this;
   this.afficher(e);
   this.cellule.onmousemove = function (e) { objet.afficher(e); };
};
InfoBulle.prototype.out = function () {
   this.cacher();
};
InfoBulle.prototype.afficher = function (e) {
   if (!e && window.event) {
      e = window.event;
   }
   this.infoBulle.style.display = 'block';
   var x = e.clientX + 10;
   if (x + this.infoBulle.offsetWidth > getViewportWidth()) {
      x = e.clientX - 10 - this.infoBulle.offsetWidth;
   }
   var y = this.table.offsetTop + (this.cellule.offsetTop - this.infoBulle.offsetHeight - 5);
   this.infoBulle.style.left = x + "px";
   this.infoBulle.style.top = y + "px";
};
InfoBulle.prototype.cacher = function () {
   this.infoBulle.style.display = 'none';
   this.cellule.onmousemove = null;
};
function getViewportHeight () {
   if (window.innerHeight) {
      return window.innerHeight;
   }
   if (document.documentElement.clientHeight) {
      return document.documentElement.clientHeight;
   }
   if (document.body) {
      return document.body.clientHeight;
   }
   return 0;
}
function getViewportWidth () {
   var w = 0;
   if (window.innerWidth) {
      w = window.innerWidth;
   }
   if (document.documentElement.clientWidth) {
      var w2 = document.documentElement.clientWidth;
      if (!w || w2 && w2 < w) {
         w = w2;
      }
      return w;
   }
   if (document.body) {
      return document.body.clientWidth;
   }
   return 0;
}
function setDate(champ, date) {
   champ.value = date;
}
