
var mtDragDropObject = false;

// inicializace drag-drop udalosti 
function mtDragDrop(objectName, hDragDrop, hDragEnd, hDragStart) {
    if ((o = mtGetObject(objectName)) && o.style) {
        o.style.cursor = 'move';
        if (o.style.position != 'relative')
            o.style.position = 'absolute';
        // handlers 
	    o.onmousedown = mtDragDropMouseDown;
        if (hDragDrop)
            o.ondragdrop = hDragDrop;
        if (hDragEnd)
            o.ondragend = hDragEnd;
        if (hDragStart)
            o.ondragstart = hDragStart;
    }
}

function mtDragDropMouseDown(e) {
    e = mtGetEventObject(e);

    mtDragDropObject = this;

    this.style.zIndex = 999;
    if (!this.style.left)
        this.style.left = (this.offsetLeft ? this.offsetLeft : 0) + 'px';
    if (!this.style.top)
        this.style.top = (this.offsetTop ? this.offsetTop : 0) + 'px';
    
    // mouse position 
    this.mtDragDropPageX = mtGetPageX(e);
    this.mtDragDropPageY = mtGetPageY(e);
    // object position 
    this.mtDragDropLeft = parseInt(this.style.left);
    this.mtDragDropTop = parseInt(this.style.top);

    if (this.ondragstart)
        this.ondragstart(e);

    // IE 
    mtAddEvent('onmousemove', mtDragDropMouseMove);
    mtAddEvent('onmouseup', mtDragDropMouseUp);
    // Mozilla 
    mtAddEvent('mousemove', mtDragDropMouseMove);
    mtAddEvent('mouseup', mtDragDropMouseUp);

    return mtStopEvent(e);
}

function mtDragDropMouseMove(e) {
    e = mtGetEventObject(e);

    if (o = mtDragDropObject) {
        var moveX = mtGetPageX(e) - o.mtDragDropPageX;
        var moveY = mtGetPageY(e) - o.mtDragDropPageY;
        if (o.ondragdrop) {
            o.ondragdrop(e, moveX, moveY);
        } else {
            o.style.left = (o.mtDragDropLeft + moveX) + 'px';
            o.style.top  = (o.mtDragDropTop + moveY) + 'px';
        }
    }
    return mtStopEvent(e);
}

function mtDragDropMouseUp(e) {
    e = mtGetEventObject(e);

    // IE 
    mtRemoveEvent('onmousemove', mtDragDropMouseMove);
    mtRemoveEvent('onmouseup', mtDragDropMouseUp);
    // Mozilla 
    mtRemoveEvent('mousemove', mtDragDropMouseMove);
    mtRemoveEvent('mouseup', mtDragDropMouseUp);

    if (o = mtDragDropObject) {
        if (o.ondragend)
            o.ondragend(e);
        o.style.zIndex = 100;
        o = null;
    }
    return mtStopEvent(e);
}


/* End of file. */
