var isDragged = false;
var draggedTarget = null;

document.onmousedown = function(e) {
    e = (e || event);
    
    //czyszczę zmienną podniesionego obiektu
    draggedTarget = null;

    var target = e.target || e.srcElement;

    if (target.tagName == 'HTML')
        return;

    while (target != document.body && (target.className || "").indexOf("block_drag") == -1) {
        target = target.parentNode;
    }

    if ((target.className || "").indexOf("block_drag") == -1)
        return;

    target = target.parentNode;

    var sx = e.clientX, sy = e.clientY,
        dx = 0, dy = 0, l = 0, t = 0;

    l = parseInt(target.style.left) || target.offsetLeft;
    t = parseInt(target.style.top) || target.offsetTop;

    if (e.preventDefault) {
        e.preventDefault();
    }

    document.onmousemove = function(e) {
        e = (e || event);

        dx = e.clientX - sx;
        dy = e.clientY - sy;
    
        target.style.left = (l + dx) + "px";
        target.style.top  = (t + dy) + "px";
        
        //Poprzez ten warunek zapisuje wartość do zmiennej tylko raz
        if (!isDragged) {
            draggedTarget = target;
        }

        isDragged = true;

        return false;
    }
};

document.onmouseup = function(e) {
    document.onmousemove = null;

    // Przycisk przewijania
    clearInterval(timer);
    
    //zapisz pozycję upuszczenia okna
    if (isDragged) {
        isDragged = false;
        
        if (draggedTarget) {
            var top = parseInt(draggedTarget.style.top);
            var left = parseInt(draggedTarget.style.left);
            
            //Trzeba zapisać to co cookie
            var exp = new Date();
                exp.setTime(exp.getTime() + 2592000000);//30 dni
            
            document.cookie = "uzPos=" + top + ":" + left + "; expires=" + exp.toGMTString();
        }
    }

    return false;
}

// Ubita Ziemia
var timer;
var ubitaZiemia = document.getElementById("ubita_ziemia_data");
var ubitaDown   = document.getElementById("ubita_down");
var ubitaUp = document.getElementById("ubita_up");

var ubitaOH = ubitaZiemia.offsetHeight;

ubitaDown.onclick = function(e) {
    $("#ubita_ziemia_data").animate({ 
        scrollTop : ubitaZiemia.scrollTop + 36
    }, 500);
}

ubitaDown.onmousedown = function(e) {
    clearInterval(timer);
    timer = setInterval(function() {
        $("#ubita_ziemia_data").animate({ 
            scrollTop : ubitaZiemia.scrollTop + 36
        }, 300);
    }, 300);
}

ubitaUp.onmousedown = function(e) {
    clearInterval(timer);
    timer = setInterval(function() {
        $("#ubita_ziemia_data").animate({ 
            scrollTop : ubitaZiemia.scrollTop - 36
        }, 300);
    }, 300);
}

ubitaUp.onclick = function(e) {
    $("#ubita_ziemia_data").animate({ 
        scrollTop : ubitaZiemia.scrollTop - 36
    }, 500);
}

