/**
version 2.0
Tom Truyens
**/

var isClick = false;
var mouseStartX = -1;
var mouseStartY = -1;

var minX = 0;
var maxX = 0;
var minY = 0;
var maxY = 0;

var globaldrag = 0;

var imgLayer = "region";

var selLayer = "selection";

var dragactief = 0;

function preload() {
    document.getElementById(imgLayer).style.visibility = "visible";
}

function initDragZoom() {

    preload();

    var theDiv = document.getElementById(imgLayer);
    minX = findPosX(theDiv);
    maxX = minX + parseInt(theDiv.offsetWidth);
    minY = findPosY(theDiv);
    maxY = minY + parseInt(theDiv.offsetHeight);


}

function mouseDown(e) {

    if (!e) var e = window.event;

    if (globaldrag != 0) {

        var rightclick;
        if (e.which) rightclick = (e.which == 3);
        else if (e.button) rightclick = (e.button == 2);
        if (rightclick) return true;


        var posx = 0;
        var posy = 0;
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX + document.body.scrollLeft
                    + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop
                    + document.documentElement.scrollTop;
        }

        mouseStartX = posx;
        mouseStartY = posy;

        isClick = ((mouseStartX >= minX) && (mouseStartX <= maxX)) && ((mouseStartY >= minY) && (mouseStartY <= maxY));

        if (isClick) {

            dragactief = 1;
            showSelection(mouseStartX, mouseStartY);
            document.getElementById(selLayer).style.visibility = "visible";
        }
    }
    return false;
}

function mouseMove(e) {

    if (!e) var e = window.event;

    if (globaldrag != 0) {

        var posx = 0;
        var posy = 0;
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX + document.body.scrollLeft
                    + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop
                    + document.documentElement.scrollTop;
        }


        document.getElementById("dragdrop").style.visibility = "visible";
        document.getElementById("dragdrop").style.left = posx + 5+"px";
        document.getElementById("dragdrop").style.top = posy + 15+"px";

        if (dragactief == 1) {
            showSelection(posx, posy);
            dragactief = 1;
            return false;
        }
    }
    return true;
}

function mouseUp(e) {

    if (!e) var e = window.event;

    if (globaldrag != 0) {

        var rightclick;
        if (e.which) rightclick = (e.which == 3);
        else if (e.button) rightclick = (e.button == 2);
        if (rightclick) return true;

        var posx = 0;
        var posy = 0;
        if (e.pageX || e.pageY) {
            posx = e.pageX;
            posy = e.pageY;
        }
        else if (e.clientX || e.clientY) {
            posx = e.clientX + document.body.scrollLeft
                    + document.documentElement.scrollLeft;
            posy = e.clientY + document.body.scrollTop
                    + document.documentElement.scrollTop;
        }
        var criteriapart = "";

        if (isClick) {

            var x1 = mouseStartX;
            var x2 = posx;

            x0 = mouseStartX - minX;
            y0 = mouseStartY - minY;
            x1 = posx - minX;
            y1 = posy - minY;

            var mouseMoved = (x1 != x0) || (y1 != y0);

            document.getElementById(selLayer).style.visibility = "hidden";

            if (x1 < 0) x1 = 0;
            if ((x1 + minX) > maxX) x1 = maxX - minX;
            if (y1 < 0) y1 = 0;
            if ((y1 + minY) > maxY) y1 = maxY - minY;

            submitDZ(x0,y0,x1,y1);
            mouseStartX = -1;
            mouseStartY = -1;
            //dragactief = 0;
        }
        isClick = false;
    }
    return true;
}

function showSelection(mouseX, mouseY) {

    var x1 = mouseStartX;
    var x2 = mouseX;
    if (x1 > x2) {
        var a = x2;
        x2 = x1;
        x1 = a;
    }
    if (x1 < minX) x1 = minX;
    if (x2 > maxX) x2 = maxX;

    var y1 = mouseStartY;
    var y2 = mouseY;
    if (y1 > y2) {
        a = y2;
        y2 = y1;
        y1 = a;
    }
    if (y1 < minY) y1 = minY;
    if (y2 > maxY) y2 = maxY;

    document.getElementById(selLayer).style.left = x1 + "px";
    document.getElementById(selLayer).style.width = (x2 - x1) + "px";
    document.getElementById(selLayer).style.top = y1 + "px";
    document.getElementById(selLayer).style.height = (y2 - y1) + "px";
}


function disabledrag() {
    globaldrag = 0;
}

function enabledrag() {
    globaldrag = 1;

    var theDiv = document.getElementById(imgLayer);

    theDiv.onmousedown = mouseDown;
    theDiv.onmousemove = mouseMove;
    theDiv.onmouseup = mouseUp;
    if (document.addEventListener)
        document.addEventListener('mouseup', mouseUp, true);

    if (document.images) {
        //document.images['dragimage'].src = "images/electrabel/dragzoomAct.gif";
    }

}


/**************************************************
 * finding x and y
 **************************************************/
function findPosX(obj) {

    var curleft = 0;
    if (document.getElementById || document.all) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (document.layers)
        curleft += obj.x;

    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (document.getElementById || document.all) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (document.layers)
        curtop += obj.y;
    return curtop;
}




