
/*
if (document.all)
{
  var detect = navigator.userAgent.toLowerCase();
  var browser,thestring;
  var version = 0;

  if (checkIt('msie'))
  {
    browser = "IE "
    browser += detect.substr(place + thestring.length,3);
    document.title = browser + ' - ' + document.title;
  }
}

function checkIt(string)
{
  place = detect.indexOf(string) + 1;
  thestring = string;
  return place;
}*/


/*****************************************************************************************/
// Applicatif : Splashbox (modele)

var appli_coordonnees = new Array;
var appli_visibility  = new Array;




// Constructeur/classe de l'outil
function splashBox(id)
{
  this.offsetTop  = 0;
  this.offsetLeft = 0;
  this.id     = id;
  this.obj
  this.setId  = newId;
  this.init   = init;
  this.toggleVisibility  = toggleVisibility;
  this.attachToAnchor 	 = attachToAnchor;
  this.setBoxEvents	     = setBoxEvents;
} // end of class "appli_box()"



/**
 * Cherche les coordonnées d'un objet
 *
 */
function getPosition(obj, scrollCompensation)
{
  var coordonnees = new Array;
  coordonnees['x'] = 0;
  coordonnees['y'] = 0;

  if (scrollCompensation == null) {
      scrollCompensation = true;
  }

  if (!obj.id) {
    obj.id = newId('anonymousElement');
  }
  var objId = obj.id;

   if (obj.offsetParent) {
      var ox = obj;
      var oy = obj;
      coordonnees['x'] = ox.offsetLeft;
      while ((ox = ox.offsetParent)) {
        coordonnees['x'] += ox.offsetLeft;
      }
      coordonnees['y'] = oy.offsetTop;
      while ((oy = oy.offsetParent)) {
        coordonnees['y'] += oy.offsetTop;
      }
    } else if (obj.x) {
      coordonnees['x'] += obj.x;
      coordonnees['y'] += obj.y;
    }



    /**
     * Attention : si le lien est dans un calque scrollé,
     * on doit ajouter la distance de scroll
     */
    /*var winOffset = getScrollOffset();
    coordonnees['x'] -= winOffset.x;
    coordonnees['y'] -= winOffset.y;*/

    if (obj.parentNode && scrollCompensation) {
        var scrollParent = obj.parentNode;
        while (scrollParent && scrollParent.nodeType == 1 && scrollParent.nodeName != 'HTML') {
            if (scrollParent.nodeType == 1 && (scrollParent.scrollLeft || scrollParent.scrollTop)) { // && (scrollParent.scrollLeft > 0 || scrollParent.scrollTop > 0)
                coordonnees['x'] -= scrollParent.scrollLeft;
                coordonnees['y'] -= scrollParent.scrollTop;
                DBG_msg('getPosition: objet en cours : ' + scrollParent.nodeName + ', id=' + scrollParent.id + ', class=' + scrollParent.className, 'info');
                DBG_msg('getPosition: correction avec parent scrollé => scrollParent.x=' + scrollParent.scrollLeft + ', scrollParent.y=' + scrollParent.scrollTop, 'info');
            }

            if (scrollParent.parentNode) {
                scrollParent = scrollParent.parentNode;
            }
        }
    }

  DBG_msg('getPosition: x=' + coordonnees['x'] + ', y=' + coordonnees['y'], 'info');

  return coordonnees;
} // end of function "getPosition()"




// Retourne un objet en utilisant la méthode appropriée
function getObj(name, win)
{
    if (!win || win == null) win = window;

  if (document.getElementById) {
    this.obj = win.document.getElementById(name);
    this.style = win.document.getElementById(name).style;

  } else if (document.all) {
    this.obj = win.document.all[name];
    this.style = win.document.all[name].style;

  } else if (document.layers) {
     this.obj = win.document.layers[name];
     this.style = win.document.layers[name];
  }
}



// Retourne un identifiant unique
function newId(id)
{
  var i = 1;
  var new_id = id + '-' + i;
  while (document.getElementById(new_id)) {
    i++;
    new_id = id + '-' + i;
  }
  return new_id;
} // end of function "newId()"



// Va chercher l'élément ayant déclenché l'action
function getTarget(e)
{
  var targ;
  if (!e && window.event) {
    var e = window.event;
    window.event.cancelBubble = true;
  }

  if (e.target) {
    targ = e.target;
  } else if (e.srcElement) {
    targ = e.srcElement;
  }

  // Contourne un bug de safari considérant le texte comme la cible
  if (targ.nodeType == 3) {
    targ = targ.parentNode;
  }
  return targ;

} // end of function "getTarget()"



// Defini l'identifiant du calque de l'outil
function toggleVisibility(objId, force)
{
  if (window.event) {
    window.event.cancelBubble = true;
  }

  if (document.all) {
      var selectVisibility = '';
      if (force == "visible") {
          var selectVisibility = "hidden";
      }

      var calqueparent;
      var selectlist =  document.getElementsByTagName('select');
      for (var i = 0; i < selectlist.length; i++) {
          calqueparent = selectlist[i].parentNode;
          while (calqueparent.nodeName != 'BODY' && !(calqueparent.nodeName == 'DIV' && calqueparent.id == objId)) {
              if (calqueparent.parentNode) {
                  calqueparent = calqueparent.parentNode;
              }
          }
          DBG_msg('toggleVisibility: noeud ' + calqueparent.nodeName + ', id ' + calqueparent.id, 'info');
          if (selectlist[i].style && calqueparent.id != objId) {
              selectlist[i].style.visibility = selectVisibility;
          } else {
              selectlist[i].style.visibility = force;
          }
      }
  }



  var obj = new getObj(objId).style;
  if (force == "visible") {
    appli_visibility[objId] = 0;
    /*obj.visibility = "visible";*/
    obj.display = "block";
    obj.top  = this.offsetTop + "px";
    obj.left = this.offsetLeft + "px";

  } else {
    /*obj.visibility = "hidden";*/
    obj.display = "none";
  }

  return true;
} // end of function "toggleVisibility()"



// Ajoute le calque a la page
function init(contentObject)
{
  // Ajout du code a la fin de la page (calque des couleurs par exemple)
  var boite = document.createElement('div');
  boite.id  = this.id;

  if (contentObject != null) {
    boite.appendChild(contentObject);
  }
  document.getElementsByTagName("body")[0].appendChild(boite);

  this.obj = document.getElementById(this.id);
  eval('this.obj.onmouseout = function() {appli_visibility["' + this.id + '"] = 0; return false;};');
  eval('this.obj.onmouseover = function() {appli_visibility["' + this.id + '"] = 1; return false;};');
  this.setBoxEvents();

  return true;
} // end of function "init()"




// Action qui font disparaitre l'objet : clic en dehors, touches du clavier, etc.
function setBoxEvents()
{
  if (document.layers && document.captureEvents) {
    document.captureEvents(Event.KEYPRESS | Event.MOUSEDOWN);
  }

  // un clic n'importe ou ailleurs dans la page ferme le popup
  var hideaction = 'function () { if (appli_visibility["' + this.id + '"] == 0) toggleVisibility("' + this.id + '", "hidden");}';

  if (document.addEventListener) {
    eval('document.addEventListener("mousedown", ' + hideaction + ', true);');
    eval('document.addEventListener("keypress", ' + hideaction + ', true);');

  } else if (document.attachEvent) {
    eval('document.attachEvent("onmousedown", ' + hideaction + ');');
    eval('document.attachEvent("onkeypress", ' + hideaction + ');');
  }

  // un clic dans une frame fait aussi dispara?tre la fen?tre
  framelist = document.getElementsByTagName('iframe');
  if (framelist.length > 0) {
    for (var i = 0; i < framelist.length; i++) {
      if (framelist[i].contentWindow) {
        if (document.addEventListener) {
          eval('framelist[i].contentWindow.document.addEventListener("mousedown", ' + hideaction + ', true);');
          eval('framelist[i].contentWindow.document.addEventListener("keypress", ' + hideaction + ', true);');

        } else if (document.attachEvent) {
          eval('framelist[i].contentWindow.document.attachEvent("onmousedown", ' + hideaction + ');');
          eval('framelist[i].contentWindow.document.attachEvent("onkeypress", ' + hideaction + ');');
        }
      }
    }
  }

  return true;
} // end of function "setBoxEvents()"



// Defini l'identifiant du calque de l'outil
function attachToAnchor(anchorId, offsetTop, offsetLeft)
{
  var coordonnees = getPosition(document.getElementById(anchorId));
  this.offsetTop  = coordonnees['y'] + offsetTop;
  this.offsetLeft = coordonnees['x'] + offsetLeft;
} // end of function "attachToAnchor()"


/*****************************************************************************************/
