
// Dragged control
var DraggedControl = null;

// Dragged control start pos
var DCPosX = -1, DCPosY = -1;

// Mouse pos on start dragging
var StartMousePosX = -1, StartMousePosY = -1;

// End drag action
var EndDragAction = null;

// ----------------------------
// Function to start drag
// ----------------------------

function StartDrag(Control, event, DragAction)
    {

    if(DraggedControl) 
        {
        // Reset control
        DraggedControl = null;

        // Exec drag action
        eval(DragAction);

        DCPosX = -1;
        DCPosY = -1;
        }
        else
        {
        // Set draged control
        DraggedControl = Control;

        // Store drag action
        EndDragAction = DragAction;

        // Stort start DC position
        DCPosX = Control.style.left;
        DCPosY = Control.style.top;

        // Check coordinates
        if( parseInt(event.x) )
            {
            // Store start mouse position
            StartMousePosX = event.x;
            StartMousePosY = event.y;
            }
            else
            {
            StartMousePosX = event.clientX;
            StartMousePosY = event.clientY;
            }
        }

    }

// ----------------------------
// Get init dragged control position
// ----------------------------

function GetInitDraggedControlPosition()
    {
    return( Array(DCPosX, DCPosY) );
    }

// ----------------------------
// Function to move object with mouse
// ----------------------------

function MoveDragObject(event)
    {

    // Check for current dragging
    if(DraggedControl==null) return;

    // Mouse coordinates
    var mx, my;

    // Check coordinates
    if( parseInt(event.x) )
        {
        // Store start mouse position
        mx = event.x;
        my = event.y;
        }
        else
        {
        mx = event.clientX;
        my = event.clientY;
        }

    // Set new object pos
    DraggedControl.style.left = ( parseInt(DCPosX) + (mx - parseInt(StartMousePosX) ) ) + 'px';
    DraggedControl.style.top  = ( parseInt(DCPosY) + (my - parseInt(StartMousePosY) ) ) + 'px';
    }
