/**************************************************
 * based on www.youngpup.net
 **************************************************/

var Drag = {
 obj : null,
 init : function(o, oRoot, minX, maxX, minY, maxY, finX, finY, misc)
  {
   o.onmousedown	= Drag.start;
   o.root = oRoot && oRoot != null ? oRoot : o ;
   
   if (isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
   if (isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";

   o.minX	= typeof minX != 'undefined' ? minX : null;
   o.minY	= typeof minY != 'undefined' ? minY : null;
   o.maxX	= typeof maxX != 'undefined' ? maxX : null;
   o.maxY	= typeof maxY != 'undefined' ? maxY : null;
   o.finX	= typeof finX != 'undefined' ? finX : null;
   o.finY	= typeof finY != 'undefined' ? finY : null;
   o.misc	= typeof misc != 'undefined' ? misc : null;
   
   o.z = parseInt(o.root.style.zIndex);
   o.top = o.root.offsetTop;
   o.hauteur = o.root.offsetHeight;

   //o.root.onDragStart	= new Function();
   //o.root.onDragEnd	= new Function();
   //o.root.onDrag		= new Function();
  },

 start : function(e)
  {
   var o = Drag.obj = this;
   e = Drag.fixE(e);
   var y = parseInt(o.root.style.top);
   var x = parseInt(o.root.style.left);
   o.x_init = x;
   o.root.style.zIndex = 1001;
   o.root.style.opacity = 0.8;
		
   //o.root.onDragStart(x, y);
   o.lastMouseX	= e.clientX;
   o.lastMouseY	= e.clientY;

   if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
   if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
   if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
   if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;

   document.onmousemove	= Drag.drag;
   document.onmouseup	= Drag.end;

   return false;
  },

 drag : function(e)
  {
   e = Drag.fixE(e);
   var o = Drag.obj;

   var ey	= e.clientY;
   var ex	= e.clientX;
   var y = parseInt(o.root.style.top);
   var x = parseInt(o.root.style.left);
   var nx, ny;

   if (o.minX != null) ex = Math.max(ex, o.minMouseX);
   if (o.maxX != null) ex = Math.min(ex, o.maxMouseX);
   if (o.minY != null) ey = Math.max(ey, o.minMouseY);
   if (o.maxY != null) ey = Math.min(ey, o.maxMouseY);

   nx = x + (ex - o.lastMouseX);
   ny = y + (ey - o.lastMouseY);

   Drag.obj.root.style.left = nx + "px";
   Drag.obj.root.style.top = ny + "px";
   Drag.obj.lastMouseX	= ex;
   Drag.obj.lastMouseY	= ey;
   
   //Drag.obj.root.onDrag(nx, ny);
   return false;
  },

 end : function()
  {
   var o = Drag.obj;
   var nx = parseInt(o.root.style.left);
   var ny = parseInt(o.root.offsetTop);
   if (o.misc == 'tdb')
    {
     //controle x
     //colonnes : 0-395;406-830
     //récupération du noeud de la nouvelle colonne
     //colonne gauche->droite
     if (o.x_init <= 395 && nx >= 406) { var new_colonne = o.root.parentNode.nextSibling.nextSibling; }
     //colonne droite->gauche
     if (nx < 0 && o.root.parentNode.previousSibling.previousSibling) { var new_colonne = o.root.parentNode.previousSibling.previousSibling; }
     if (new_colonne)
      {
       o.root.parentNode.removeChild(o.root); //retrait du bloc
       new_colonne.appendChild(o.root); //placement à la fin de la colonne
      }
     o.root.style.left = "0px";
     
     //controle y
     var parent = o.root.parentNode;
     liste_enfant = parent.childNodes
     for (i=0; i< liste_enfant.length; i++)
      {
       if (liste_enfant[i].offsetTop > ny)
        {
         parent.removeChild(o.root);
         parent.insertBefore(o.root, liste_enfant[i]);
         ny = liste_enfant[i].offsetHeight;
         i = liste_enfant.length + 1;
        }
      }
     o.root.style.top = ny + "px";
    }
   //controle y

   o.root.style.zIndex = Drag.obj.z;
   o.root.style.opacity = 1;
   if (o.finX != null) Drag.obj.root.style.left = o.finX + "px";
   if (o.finY != null) Drag.obj.root.style.top = o.finY + "px";
   if (o.misc == 'tdb')
    {
     rang = '';
     liste_enfant = document.getElementById('td_g').childNodes
     for (i=0; i< liste_enfant.length; i++) { rang += liste_enfant[i].id+','+i+',g;' }
     liste_enfant = document.getElementById('td_d').childNodes
     for (i=0; i< liste_enfant.length; i++) { rang += liste_enfant[i].id+','+i+',d;' }
     document.getElementById('eframe').src='ifz_profil_ordre.php?rang='+rang;
    }
   
   document.onmousemove = null;
   document.onmouseup   = null;
   //Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style.left),parseInt(Drag.obj.root.style.top));
   Drag.obj = null;
  },

 fixE : function(e)
  {
   if (typeof e == 'undefined') e = window.event;
   if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
   if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
   return e;
  }
  
};