/* Fonction d'initialisation des flottants, appel?e normalement par onload() */
function initFlottant()
{
// remplissage du tableau de r?f?rence
  for (i=0;i<flot.length;i++)
  {
    o=document.getElementById(flot[i]);
    oFlottant[i]=new Array();
    oFlottant[i][0]=flot[i];  // id
    oFlottant[i][1]=o;        // objet
    oFlottant[i][2]=false;    // affich? ?
    if(document.getElementById(flot[i]) == '[object]')
	    closeFlottantById(flot[i]);
  }
}

/* fonction de recherche du flottant conteneur de l'objet en r?f?rence */
function searchFlottant(flot)
{
  /* tant que l'objet courant n'est pas un flottant, on cherche un objet parent */
  while (flot && flot.className!="flottant") flot=flot.parentNode;
  
  /* on a trouv? un flottant contenant ... c'est lequel parmi ceux enregistr?s ? */
  for (i in oFlottant) {
    o=oFlottant[i]
    if (o[1]==flot) return o;
  }
  return false;
}

function searchFlottantById(flot)
{
  /* fonction de recherche d'un flottant par son id dans la liste des flottants enregistr?s */
  for (i in oFlottant) 
  {
    o=oFlottant[i];
    //alert(""+o[0]+"=="+flot+"");
    if (o[0]==flot)
    {
    	return o;
    }
  }
  return false;
}

function closeFlottant(o) 
{

  /* fonction de fermeture (invisible) d'un flottant */
  fcloseFlottant=true;            // flag de traitement de fermeture en cours
  flot=searchFlottant(o);         // recherche du flottant ? fermer
  if (flot) {                     // on l'a trouv?, on le ferme
    flot[1].style.display="none";
    flot[2]=false;
  } else alert('pas trouv? !');     // on l'a pas trouv? ... c'est pas normal
}
function closeFlottantById(oid) 
{
  /* fonction de fermeture (invisible) d'un flottant */
  fcloseFlottant=true;            // flag de traitement de fermeture en cours
  flott=searchFlottantById(oid);   // recherche du flottant ? ouvrir
  if (flott)
  {                     // on l'a trouv?, on le ferme
    flott[1].style.display="none";
    flott[2]=false;
  }
  else 
  	alert('pas trouve !!');     // on l'a pas trouv? ... c'est pas normal
}
  /* fonction d'ouverture d'un flottant */
function openFlottant(oid)
{
  fcloseFlottant=false;       		// annulation de traitement de fermeture
  flot=searchFlottantById(oid);   // recherche du flottant ? ouvrir
  if (flot)             					// on l'a trouv? on l'ouvre
  {
    flot[1].style.display="block";
    flot[2]=true;
		var valeur = parseInt(flot[1].style.top,10);
		if(oFlottanty < document.body.scrollTop)
			flot[1].style.top = document.body.scrollTop;
  } 
  else 
  	alert('pas trouv? !!!');     // on l'a pas trouv?, c'est pas normal
}

function reduceFlottant(o)
{
  /* Fonction de r?duction/restauration d'un flottant */
  flot=searchFlottant(o);             // recherche du flottant ? traiter
  if (flot) {
    if (flot[1].style.overflow=="hidden") {   // Il est r?duit ? on le restaure
      flot[1].style.overflow="visible";
      flot[1].style.height="auto";
      o.innerHTML=charcloseFlottant[1];
    }else{                    // sinon on le r?duit
      o.innerHTML=charcloseFlottant[0];
      flot[1].style.overflow="hidden";
      flot[1].style.height=o.parentNode.offsetHeight+"px";
    }
  }else alert('pas trouv?!!!!')           // on l'a pas trouv?, c'est pas normal
}

function startmoveFlottant(o,e)
{
  /* fonction d'initialisation du d?placement d'un flottant */
  omoveFlottant=searchFlottant(o);              // on cherche le flottant ? traiter
  if (omoveFlottant && ! fcloseFlottant) {          // on l'a trouv? et il est pas en cours de fermeture
    margeFlottant[0]=(e.offsetX) ? e.offsetX:e.layerX;    // on stocke la position relative du clic
    margeFlottant[1]=(e.offsetY) ? e.offsetY:e.layerY;
    if (omoveFlottant[1].style.zIndex<maxzFlottant) {   // on place le flottant "au dessus" des autres
      maxzFlottant++;
      omoveFlottant[1].style.zIndex=maxzFlottant;
    }
  }
}
/* fonction de d?placement d'un flottant */
function moveFlottant(e)
{
  if(omoveFlottant) {               // on a un flottant en cours de d?placement ... ouf
    o=omoveFlottant[1];
    x=(e.x) ? e.x:e.pageX;            // on calcule sa position relative
    y=(e.y) ? e.y:e.pageY;
    o.style.left=(x-margeFlottant[0])+"px";   // par rapport ? la position du clic
    o.style.top=(y-margeFlottant[1])+"px";
  }
}

function popup(oid)
{
	flot=searchFlottantById(oid);
  if (flot)             					// on l'a trouv? on l'ouvre
  {
  	if(flot[2] == false)
  	{
		  for (i=0;i<oFlottant.length;i++)
		  {
		    if(document.getElementById(oFlottant[i][0]) == '[object]')
			    closeFlottantById(oFlottant[i][0]);
		  }
			openFlottant(oid);
			//flot=searchFlottantById(oid);
			//alert(flot[1].style.top);
		}
		else
			closeFlottantById(oid);
  } 
  else 
  	alert('pas trouv?');     // on l'a pas trouv?, c'est pas normal
}

// on intercepte le mousedown ... pour que le drag marche m?me si il y a eu des p?pins
window.onmousedown=moveFlottant;