    var moveState = false;
    var moveObj, moveObjTitle;
    var x0, y0;
    var objX0, objY0;
    var moveObjZ = 10000;
    var prevPosition = "";
    var browser = (
        !!(window.attachEvent && !window.opera) ? 'IE' :
        !!window.opera ? 'Opera' :
        navigator.userAgent.indexOf('AppleWebKit/') > -1 ? 'WebKit' :
        navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1 ? 'Gecko' : ''
    )
    var gadgetBT = 0

    function cursorPosition(event) {
        var x = y = 0;
        if (document.attachEvent != null) { // Internet Explorer & Opera
            x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
            y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
        }
        if (!document.attachEvent && document.addEventListener) { // Gecko
            x = event.clientX + window.scrollX;
            y = event.clientY + window.scrollY;
       }
        return {x:x, y:y};
    }

    function initMove(th, obj, event) {
        var event = event || window.event;
        moveObj = document.getElementById(obj);
        moveObjTitle = th
        moveObj.style.zIndex = moveObjZ++;
//        prevPosition = moveObj.style.position
//        moveObj.style.position = "absolute";
        var posCursor = cursorPosition(event);
//        var posObj = getAbsPos(moveObj);
        var posObj = offset(moveObj);
        x0 = posCursor.x;
        y0 = posCursor.y;
        objX0 = posObj.x;
        objY0 = posObj.y;
//        alert(objY0)
        moveState = true;
//        document.getElementById('txt').value = x0 + " - " + y0 + " - " + objX0 + " - " + objY0 + " - " + moveState
        return false
    }
    
    function destroyMove()
    {
//        moveObj.style.position = prevPosition;
        prevPosition = ""
        moveState = false;
        moveObj = "";
        moveObjTitle = "";
    }

    document.onmouseup = function() {
        destroyMove();
//        document.getElementById('txt').value = x0 + " - " + y0 + " - " + objX0 + " - " + objY0 + " - " + moveState
    }

    document.onmousemove = function(event) {
        moveHandler(event);
        if (moveState) return false
    }

    function moveHandler(event) {
        if (moveState)
        {
            var event = event || window.event;
            var posCursor = cursorPosition(event);
            moveObj.style.left = (objX0 + posCursor.x - x0) + 'px';
            moveObj.style.top = (objY0 + posCursor.y - y0) + 'px';
            setGadgetBorderTop(objY0 + posCursor.y - y0)
//            document.getElementById('txt').value = objX0 + " - " + objY0 + " - " + posCursor.x + " - " + posCursor.y + " - " + x0 + " - " + y0 + " - " + obj.style.left + " - " + obj.style.top
            return false
        }
    }

	function getAbsPos(p)
	{
		var s = { x:0, y:0 }
		while (p)
		{
			s.x += p.offsetLeft
			s.y += p.offsetTop
			p = p.offsetParent
		}
		return s
    }

    function offset(elem) {
        var top = 0
        var left = 0
        if (elem.getBoundingClientRect && browser == 'Opera') {
//            var doc = elem.ownerDocument
            var box = elem.getBoundingClientRect();
            add(box.left /*+ Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft(*/,
            box.top /*+ Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)*/);
//            add(-doc.documentElement.clientLeft, -doc.documentElement.clientTop);
        }
        else {
            while (elem) {
                add(elem.offsetLeft, elem.offsetTop)
                elem = elem.offsetParent
            }
        }
        return { x: left , y: top};

        function add(l, t) {
            left += parseInt(l, 10) || 0;
            top += parseInt(t, 10) || 0;
        }
    }

    function getScrollTop() {
        return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop)
    }

    function setGadgetBorderTop(ot) {
        gadgetBT = ot - getScrollTop()
    }
