//var mouseOffset = null;
//var iMouseDown = false;
//var lMouseState = false;
//var dragObject = null;
//var widthSize = null;
//var DragDrops = [];
//var curTarget = null;
//var lastTarget = null;
//var dragHelper = null;
//var tempDiv = null;
//var rootParent = null;
//var rootSibling = null;

//var containerNumber = 0;
//var itemNumber = 0;

//Number.prototype.NaN0 = function() { return isNaN(this) ? 0 : this; }

function onDrop(sender, e) {
    var container = e.get_container();
    var item = e.get_droppedItem();
    var position = e.get_position();

    //alert( String.format( "Container: {0}, Item: {1}, Position: {2}", 
    //       container.id, item.id, position ) );

    var stronaBoxId = parseInt(item.getAttribute("StronaBoxId"));
    var columnNo = parseInt(container.getAttribute("NrKolumny"));
    var row = position;

    BtnReloadDragClick(stronaBoxId, columnNo, row);
    //   WidgetService.MoveWidgetInstance( instanceId, columnNo, row );
}

//function CreateDragContainer() {
//    var cDrag = DragDrops.length;
//    DragDrops[cDrag] = [];

//    var main = document.getElementById('MainContainer');

//    for (var i = 0; i < main.childNodes.length; i++) {
//        var col = main.childNodes[i];

//        if (col.nodeName != '#text') {
//            if (col.id.indexOf('ColContainer') > -1) {
//                for (var j = 0; j < col.childNodes.length; j++) {
//                    var colDrop = col.childNodes[j];

//                    if (colDrop.nodeName != '#text') {
//                        if (colDrop.id.indexOf('DropContainer') > -1) {
//                            DragDrops[cDrag].push(colDrop);
//                        }
//                    }
//                }
//            }
//        }
//    }
//}
//function getPosition(e) {
//    var left = 0;
//    var top = 0;
//    while (e.offsetParent) {
//        left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth)).NaN0() : 0);
//        top += e.offsetTop + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth)).NaN0() : 0);
//        e = e.offsetParent;
//    }


//    left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth)).NaN0() : 0);
//    top += e.offsetTop + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth)).NaN0() : 0);

//    return { x: left, y: top };

//}
//function mouseCoords(ev) {
//    if (ev.pageX || ev.pageY) {
//        return { x: ev.pageX, y: ev.pageY };
//    }

//    return {
//        x: ev.clientX + document.documentElement.scrollLeft,
//        y: ev.clientY + document.documentElement.scrollTop
//    };
//}

//function getMouseOffset(target, ev) {
//    ev = ev || window.event;

//    var docPos = getPosition(target);
//    var mousePos = mouseCoords(ev);
//    return { x: mousePos.x - docPos.x, y: mousePos.y - docPos.y };
//}

//function writeHistory(message) {
//    historyDiv = document.getElementById('History1');
//    historyDiv.appendChild(document.createTextNode(message));
//    historyDiv.appendChild(document.createElement('BR'));

//    historyDiv.scrollTop += 50;
//}

//function mouseMove(ev) {
//    ev = ev || window.event;

//    var target = ev.target || ev.srcElement;
//    var mousePos = mouseCoords(ev);

//    // mouseOut event - fires if the item the mouse is on has changed
//    if (lastTarget && (target !== lastTarget)) {
//        if (lastTarget.nodeName != '#document') {
//            // reset the classname for the target element
//            var origClass = lastTarget.getAttribute('origClass');
//            if (origClass) lastTarget.className = origClass;
//        }
//    }

//    var flag = 0;
//    var dragObj;
//    while (1) {
//        if (target.nodeName == '#document') {
//            break;
//        }
//        else {
//            dragObj = target.getAttribute('DragObj');
//            if (target.getAttribute('DragObj') != null && target.id.indexOf('Item') == -1) {
//                target = target.parentNode;
//            }
//            else {
//                break;
//            }
//        }
//    }

//    //var dragObj = target.getAttribute('DragObj');

//    // if the mouse was moved over an element that is draggable
//    if (dragObj != null) {

//        // mouseOver event - Change the item's class if necessary
//        if (target != lastTarget) {
//            var oClass = target.getAttribute('overClass');
//            if (oClass) {
//                target.setAttribute('origClass', target.className);
//                target.className = oClass;
//            }
//        }

//        // if the user is just starting to drag the element
//        if (iMouseDown && !lMouseState) {
//            // mouseDown target
//            curTarget = target;

//            // Record the mouse x and y offset for the element
//            rootParent = curTarget.parentNode;
//            rootSibling = curTarget.nextSibling;

//            mouseOffset = getMouseOffset(target, ev);

//            // We remove anything that is in our dragHelper DIV so we can put a new item in it.
//            for (var i = 0; i < dragHelper.childNodes.length; i++) dragHelper.removeChild(dragHelper.childNodes[i]);

//            // Make a copy of the current item and put it in our drag helper.
//            dragHelper.appendChild(curTarget.cloneNode(true));
//            dragHelper.style.display = 'block';

//            // set the class on our helper DIV if necessary
//            var dragClass = curTarget.getAttribute('dragClass');
//            if (dragClass) {
//                dragHelper.firstChild.className = dragClass;
//                //dragHelper.style.width = widthSize;
//            }

//            // disable dragging from our helper DIV (it's already being dragged)
//            dragHelper.firstChild.removeAttribute('DragObj');

//            var dragConts = DragDrops[dragObj];
//            ////writeHistory(dragConts);

//            curTarget.setAttribute('startWidth', parseInt(curTarget.offsetWidth));
//            curTarget.setAttribute('startHeight', parseInt(curTarget.offsetHeight));
//            curTarget.style.display = 'none';
//            //writeHistory(curTarget.id + ':' + curTarget.offsetHeight + ':' + curTarget.style.height);

//            // loop through each possible drop container
//            for (var i = 0; i < dragConts.length; i++) {
//                with (dragConts[i]) {
//                    var pos = getPosition(dragConts[i]);

//                    setAttribute('startWidth', parseInt(offsetWidth));
//                    setAttribute('startHeight', parseInt(offsetHeight));
//                    setAttribute('startLeft', pos.x);
//                    setAttribute('startTop', pos.y);
//                }

//                // loop through each child element of each container
//                for (var j = 0; j < dragConts[i].childNodes.length; j++) {
//                    with (dragConts[i].childNodes[j]) {
//                        if ((nodeName == '#text') || (dragConts[i].childNodes[j] == curTarget)) continue;

//                        var pos = getPosition(dragConts[i].childNodes[j]);

//                        // save the width, height and position of each element
//                        setAttribute('startWidth', parseInt(offsetWidth));
//                        setAttribute('startHeight', parseInt(offsetHeight));
//                        setAttribute('startLeft', pos.x);
//                        setAttribute('startTop', pos.y);
//                    }
//                }
//            }
//        }
//    }

//    // If we get in here we are dragging something
//    if (curTarget) {
//        // move our helper div to wherever the mouse is (adjusted by mouseOffset)

//        dragHelper.style.top = mousePos.y - mouseOffset.y + 'px';
//        dragHelper.style.left = mousePos.x - mouseOffset.x + 'px';

//        var dragConts = DragDrops[curTarget.getAttribute('DragObj')];
//        var activeCont = null;

//        var xPos = mousePos.x; // - mouseOffset.x + (parseInt(curTarget.getAttribute('startWidth')) / 2);
//        var yPos = mousePos.y; // - mouseOffset.y + (parseInt(curTarget.getAttribute('startHeight')) / 2);
//        ////writeHistory(xPos + ' ' + yPos);
//        // check each drop container to see if our target object is "inside" the container
//        for (var i = 0; i < dragConts.length; i++) {
//            with (dragConts[i]) {
//                if ((parseInt(getAttribute('startLeft')) < xPos) &&
//					(parseInt(getAttribute('startTop')) < yPos) &&
//					((parseInt(getAttribute('startLeft')) + parseInt(getAttribute('startWidth'))) > xPos) &&
//					((parseInt(getAttribute('startTop')) + parseInt(getAttribute('startHeight'))) > yPos)) {

//                    activeCont = dragConts[i];

//                    // exit the for loop
//                    break;
//                }
//            }
//        }

//        // Our target object is in one of our containers.  Check to see where our div belongs
//        if (activeCont) {
//            // beforeNode will hold the first node AFTER where our div belongs
//            var beforeNode = null;

//            // loop through each child node (skipping text nodes).
//            for (var i = activeCont.childNodes.length - 1; i >= 0; i--) {
//                with (activeCont.childNodes[i]) {
//                    if (nodeName == '#text') continue;

//                    // if the current item is "After" the item being dragged
//                    if (
//						curTarget != activeCont.childNodes[i] &&
//						((parseInt(getAttribute('startLeft')) + parseInt(getAttribute('startWidth'))) > xPos) &&
//						((parseInt(getAttribute('startTop')) + parseInt(getAttribute('startHeight'))) > yPos)) {
//                        beforeNode = activeCont.childNodes[i];
//                    }
//                }
//            }

//            // the item being dragged belongs before another item
//            if (beforeNode) {
//                if (beforeNode != curTarget.nextSibling) {
//                    activeCont.insertBefore(curTarget, beforeNode);
//                }

//                // the item being dragged belongs at the end of the current container
//            } else {
//                if ((curTarget.nextSibling) || (curTarget.parentNode != activeCont)) {
//                    activeCont.appendChild(curTarget);
//                }
//            }

//            setTimeout(function() {
//                var contPos = getPosition(activeCont);
//                activeCont.setAttribute('startWidth', parseInt(activeCont.offsetWidth));
//                activeCont.setAttribute('startHeight', parseInt(activeCont.offsetHeight));
//                activeCont.setAttribute('startLeft', contPos.x);
//                activeCont.setAttribute('startTop', contPos.y);
//            }, 5);

//            // make our drag item visible
//            if (curTarget.style.display != '') {
//                curTarget.style.display = '';
//                curTarget.style.visibility = 'hidden';
//            }
//        } else {

//            // our drag item is not in a container, so hide it.
//            if (curTarget.style.display != 'none') {
//                curTarget.style.display = 'none';
//            }
//        }
//    }

//    // track the current mouse state so we can compare against it next time
//    lMouseState = iMouseDown;

//    // mouseMove target
//    lastTarget = target;

//    if (dragObject) {
//        dragObject.style.position = 'absolute';
//        dragObject.style.top = mousePos.y - mouseOffset.y;
//        dragObject.style.left = mousePos.x - mouseOffset.x;
//    }

//    // track the current mouse state so we can compare against it next time
//    lMouseState = iMouseDown;

//    // this helps prevent items on the page from being highlighted while dragging
//    if (curTarget || dragObject) return false;
//}

//function mouseUp(ev) {
//    if (curTarget) {
//        // hide our helper object - it is no longer needed
//        dragHelper.style.display = 'none';

//        // if the drag item is invisible put it back where it was before moving it
//        if (curTarget.style.display == 'none') {
//            if (rootSibling) {
//                rootParent.insertBefore(curTarget, rootSibling);
//            } else {
//                rootParent.appendChild(curTarget);
//            }
//        }

//        // make sure the drag item is visible
//        curTarget.style.display = '';
//        curTarget.style.visibility = 'visible';

//        BtnReloadDragClick();
//    }
//    curTarget = null;
//    iMouseDown = false;
//}

//function mouseDown(ev) {
//    ev = ev || window.event;
//    var target = ev.target || ev.srcElement;

//    iMouseDown = true;
//    if (target.onmousedown || target.getAttribute('DragObj')) {
//        return false;
//    }
//}

//document.onmousemove = mouseMove;
//document.onmousedown = mouseDown;
//document.onmouseup = mouseUp;

//window.onresize = function() {
//    var container = document.getElementById('MainContainer');
//    if (container) {
//        //SetNewWidth(container);
//        if (widthSize != 'NaNpx' && dragHelper != null) {
//            dragHelper.style.width = widthSize;
//        }
//    }
//}

//window.onload = function() {
//    dragHelper = document.createElement('DIV');
//    dragHelper.style.cssText = 'position:absolute;left:500px; top:500px;';

//    CreateDragContainer();

//    var container = document.getElementById('MainContainer');
//    if (container) {

//        //SetNewWidth(container);
//        if (widthSize != 'NaNpx') {
//            dragHelper.style.width = widthSize;
//        }
//    }

//    document.body.appendChild(dragHelper);

//};

//function GetCurrentPos(main) {
//    var pos = '';
//    for (var i = 0; i < main.childNodes.length; i++) {
//        var col = main.childNodes[i];

//        if (col.nodeName != '#text') {
//            if (col.id.indexOf('ColContainer') > -1) {
//                for (var j = 0; j < col.childNodes.length; j++) {
//                    var colDrop = col.childNodes[j];

//                    if (colDrop.nodeName != '#text') {
//                        if (colDrop.id.indexOf('DropContainer') > -1) {
//                            for (var k = 0; k < colDrop.childNodes.length; k++) {
//                                var item = colDrop.childNodes[k];

//                                if (item.nodeName != '#text') {
//                                    if (item.id.indexOf('Item') > -1) {
//                                        pos = pos + item.id.substr(4);
//                                    }
//                                }
//                            }
//                            pos = pos + ';';
//                        }
//                    }
//                }
//            }
//        }
//    }
//    return pos;
//}

function BtnReloadDragClick(stronaBoxId, kolumna, wiersz) {

    var main = document.getElementById('MainContainer');
    if (main) {

        var pos = stronaBoxId + ";" + kolumna + ";" + wiersz;

        for (var j = 0; j < main.childNodes.length; j++) {
            var hf = main.childNodes[j];

            if (hf.nodeName != '#text') {
                if (hf.id.indexOf('HfCurrentPos') > -1) {
                    hf.value = pos;
                    break;
                }
            }
        }

        for (var i = 0; i < main.childNodes.length; i++) {
            if (main.childNodes[i].nodeName != '#text') {
                if (main.childNodes[i].id.indexOf('BtnReloadDrag') > -1) {
                    main.childNodes[i].click();
                    break;
                }
            }
        }
    }
}
