﻿// FM : 12/06/2007
// --------------------------------------------------------------------------------
// move(e, theID, fonctionAppel, prefReceiver, limitID)
// e : mouse event
// theID : the ID of the element to drag
// fonctionAppel : fonction à appeler en retour de drop
// prefReceiver : un prefixe permettant de limiter les objets sur lesquels on drop (optionnal)
// limitID : the ID of the boundary element (optionnal)
//
// L'appelant reçoit la liste des objets sur lesquels on a droppé l'objet par une 
// fonction objectHasBeenDropped(x,y,theDraggedObject,receiverList). L'appelant doit implémenter cette fonction
// receiverList contient la liste des id des objets séparés par des ";"
//
// exemples d'appel :
//  <span onmousedown="javascript:move(event,'todrag1','todrag');" id="todrag1">
//  <td onmousedown="javascript:move(event,'todrag2','','receiver');" style=" cursor:move; background-image: url(images/handle.gif)" width="45">
// -----------------------------------------------------------------------------


var newDiv;
var newDivFond;
var dragX = 0;
var dragY = 0;
var deltX = 20;
var deltY = 20;
var nDw = 0;
var nDh = 0;
var extLimX1 = 0;
var extLimY1 = 0;
var extLimX2 = 0;
var extLimY2 = 0;
var extLimID = "";
var prefReceiverID = "";
var receiverList = "";
var theDraggedObject = "";
var callFonctionAppel = "";
var globalDelta = 0;

function getthePosition(inID, inTYPE)
{
 var iVal = 0;
 var oObj = document.getElementById(inID);
 var sType = 'oObj.offset' + inTYPE;
 while (oObj && oObj.tagName != 'BODY') {
  iVal += eval(sType);
  oObj = oObj.offsetParent;
 }
 return iVal;
}

function getthePositionObj(oObj, inTYPE)
{
 var iVal = 0;
 var sType = 'oObj.offset' + inTYPE;
 while (oObj && oObj.tagName != 'BODY') {
  iVal += eval(sType);
  oObj = oObj.offsetParent;
 }
 return iVal;
}

function gettheXY(e)
{
	if (!e) var e = window.event;

	if (e.pageX || e.pageY) 	{
		dragX = e.pageX;
		dragY = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		dragX = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		dragY = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
}

function initExtLimit()
{
    extLimX1 = 0;
    extLimY1 = 0;
    extLimX2 = 0;
    extLimY2 = 0;
    extLimID = "";
}

function setExtLimit(limID)
{
    var theObj = document.getElementById(limID);
    if (theObj)
    {
        extLimID = limID;
        extLimX1 = getthePosition(limID, 'Left');
        extLimY1 = getthePosition(limID, 'Top');
        extLimX2 = extLimX1 + theObj.offsetWidth;
        extLimY2 = extLimY1 + theObj.offsetHeight;
    }
}

function createFond()
{
    newDivFond = document.createElement('DIV'); // create div 
    
    with(newDivFond.style) 
    { 
        zIndex = 99;
        position = "absolute";
        left = "1px"; 
        top = "1px"; 
        width = "99%"; 
        height = "99%";
    }
    document.body.appendChild(newDivFond);
}

function removeFond()
{
    document.body.removeChild(newDivFond);
    newDivFond = null;
}

function move2(e, theID, fonctionAppel, prefReceiver, limitID, scrollDivID)
{
    globalDelta = document.getElementById(scrollDivID).scrollTop;
    rmove(e, theID, fonctionAppel, prefReceiver, limitID);
}

function move(e, theID, fonctionAppel, prefReceiver, limitID)
{
    globalDelta =  0;
    rmove(e, theID, fonctionAppel, prefReceiver, limitID);
}

function rmove(e, theID, fonctionAppel, prefReceiver, limitID)
{
        if (e.preventDefault!=undefined) {e.preventDefault();} ;
        theDraggedObject = theID;
        if (limitID)
            setExtLimit(limitID);
        else
            initExtLimit();
            
        if (prefReceiver)
            prefReceiverID = prefReceiver;
        else    
            prefReceiverID="";
       
        callFonctionAppel = fonctionAppel;
        
        var theObj = document.getElementById(theID);
        var x = getthePosition(theID, 'Left');
        var y = getthePosition(theID, 'Top');
        nDw = theObj.offsetWidth;
        nDh = theObj.offsetHeight;
        y = y - globalDelta;

        gettheXY(e);
        deltX = dragX - x;
        deltY = dragY - y;
        
        createFond();
        
        newDiv = document.createElement('DIV'); // create div 
        newDiv.innerHTML = theObj.innerHTML;        
        
        with(newDiv.style) // CSS 
        { 
            zIndex = 100;
            position = "absolute";
            left = x + "px"; 
            top = y + "px"; 
            width = nDw + "px"; 
            height = nDh + "px"; 
            borderStyle = "dashed"; 
            borderColor = "#999999"; 
            borderWidth = 1 + "px"; 
            backgroundColor = theObj.style.backgroundColor;
            fontFamily = theObj.style.fontFamily;
            fontSize = theObj.style.fontSize;
            fontWeight = theObj.style.fontWeight;
            cursor = "move";
        }
        newDiv.className = theObj.className;
       
        document.body.appendChild(newDiv); // paste
        document.body.onmousemove = deplace;        
        document.body.onmouseup = release;
}



function deplace(e)
{
    gettheXY(e);
    var nX = dragX - deltX;
    if (dragX >= extLimX1 && ((dragX < extLimX2) || (extLimX2==0))) 
        newDiv.style.left = nX + "px";
    var nY = dragY - deltY;
    if (dragY >= extLimY1 && ((dragY < extLimY2) || (extLimY2==0))) 
        newDiv.style.top = nY + "px";
}

function release(e)
{    
    gettheXY(e);
    var finalX = deltX + parseInt(newDiv.style.left.replace("px",""));
    var finalY = deltY + parseInt(newDiv.style.top.replace("px","")) + globalDelta;
    document.body.removeChild(newDiv);
    newDiv = null;
    removeFond();
    document.body.onmousemove = null;        
    document.body.onmouseup = null;
    postrel(finalX,finalY);
}

function cleanDD()
{
    try {
        document.body.removeChild(newDiv);
    } catch (ex) {}
    newDiv = null;
    try {
        removeFond();
    } catch (ex) {}
    document.body.onmousemove = null;        
    document.body.onmouseup = null;
}

function isInside(x,y,obj)
{
    var retV = false;
    var Tl = getthePositionObj(obj, 'Left');
    var Tr = Tl + obj.offsetWidth;
    var Tt = getthePositionObj(obj, 'Top');
    var Tb = Tt + obj.offsetHeight;
    if (x >=  Tl && x <= Tr && y >=  Tt && y <= Tb)
        retV = true;
    return retV;    
}

function addReceiver(oObj)
{
    if ((prefReceiverID.length > 0 && oObj.id.indexOf(prefReceiverID) > -1) || prefReceiverID.length == 0)
    {
        if (receiverList.length > 0) receiverList += ";";
        receiverList += oObj.id; 
    }
}


function checkChild(x,y,oObj)
{
    if (isInside(x,y,oObj)) 
        addReceiver(oObj);

    var fils = oObj.childNodes; 
    var nbFils = fils.length; 
    for(var i = 0; i < nbFils; i++){
     
      if(fils[i].nodeType==1){ 
         checkChild(x,y,fils[i]);
      }
      else
      { 
        if (isInside(x,y,fils[i]))
          addReceiver(fils[i]);  
      }
    }
}

function postrel(x,y)
{
    receiverList = "";
    var oObj;        
    if (extLimID.length > 0)
        oObj = document.getElementById(extLimID);
    else
        oObj = document.body;

    checkChild(x,y,oObj);

    if (receiverList.length > 0)
        callFonctionAppel(x,y,theDraggedObject,receiverList);
}