//-- locatienet functions
//-- author: Jelle-Jan van Veelen

var mouseX;
var mouseY;

//-- retrieve mouse position
function getMousePos(e) {
    if (!e) {
        var e = window.event || window.Event;
    }
    
    if('undefined'!=typeof e.pageX) {
        mouseX = e.pageX;
        mouseY = e.pageY;
    } else {
        mouseX = e.clientX + document.body.scrollLeft;
        mouseY = e.clientY + document.body.scrollTop;
    }
    
    draw();
}

function setMouseCapture() {
    if (window.Event && document.captureEvents) {
        document.captureEvents(Event.MOUSEMOVE);
    }
    document.onmousemove = getMousePos;
}
addOnLoadFunction('setMouseCapture');

//-- show tooltip in map
function tooltip(_obj, _name, _adr, _url) {
    
    if (!md) {
        tt      = document.getElementById('tooltip');
        ttname  = document.getElementById('tooltipname');
        ttadr   = document.getElementById('tooltipadr');
        tturl   = document.getElementById('tooltipurl');
        
        ttname.innerHTML = _name;
        ttadr.innerHTML  = _adr;
        tturl.innerHTML  = '<a href="' + _url + '">meer informatie</a> &raquo;';
        
        containerend = document.getElementById('outercon').offsetLeft + document.getElementById('outercon').offsetWidth;
        
        if (mouseX + 200 > containerend) {
            newX = containerend - 225;
        } else {
            newX = mouseX;
        }
        
        newY = mouseY; 
        
        with (tt.style) {
            visibility  = "visible";
            left = newX + "px";
            top  = newY + "px";
        }
		
		// delayHideTooltip();
    }
}

var hideTooltipId;
function delayHideTooltip() {
	if(hideTooltipId) clearTimeout(hideTooltipId);

	hideTooltipId = setTimeout('hideTooltip()',1500);
}

//-- hide tooltip
function hideTooltip() {
    document.getElementById('tooltip').style.visibility = "hidden";
}


var md = false;
var startX = 0;
var startY = 0;
var d, dl, dt, dw, dh;

//-- draw rectangle
function draw() {
    if (!d) d = document.getElementById('knel');
    
    if (!dl && !dt && !dw && !dh) {
        dl = document.getElementById('outercon').offsetLeft + 338 - 5;
        dt = 179 + 45;
        dw = 621;
        dh = 362;
    }
    
    if (md) {
        
        if (startX <= mouseX) {
            divleft     = startX;
            divwidth    = (mouseX - startX);
        } else {
            divleft     = mouseX;
            divwidth    = (startX - mouseX);
        }
        
        if (startY <= mouseY) {
            divtop      = startY;
            divheight   = (mouseY - startY);
        } else {
            divtop      = mouseY;
            divheight   = (startY - mouseY);
        }
        
        divleft -= dl;
        divtop  -= dt;
        
        
        if (divleft + divwidth > dw) {
            divwidth = dw - (divleft + 5);
        }
        
        if (divtop + divheight > dh) {
            divheight = dh - (divtop + 5);
        }
        
        if (divleft < 0) {
            divwidth = (startX - dl > 0) ? startX - dl : 0;
            divleft = 0;
        }
        
        if (divtop < 0) {
            divheight = (startY - dt > 0) ? startY - dt : 0;
            divtop = 0;
        }
        
        with (d.style) {
            left    = divleft + "px";
            top     = divtop + "px";
            width   = divwidth + "px";
            height  = divheight + "px";
        }
        
    } 
}

//-- start drawing
function start() {
    hideTooltip();
    if (!d) d = document.getElementById('knel');
    with (d.style) {
        visibility  = "visible";
        width       = "0px";
        height      = "0px";
    }
    md = true;
    startX = mouseX;
    startY = mouseY;
    return true;
}

//-- stop drawing
function stop() {
    if (!d) d = document.getElementById('knel');
    with (d.style) {
        visibility = "hidden";
    }
    
    md = false;
    
    calculateNewCoords();
}

//-- calculate new coordinates
function calculateNewCoords() {
    
    boxw = parseInt(d.style.width);
    boxh = parseInt(d.style.height);
    boxl = parseInt(d.style.left);
    boxt = parseInt(d.style.top);
    
    if (boxw >= 5 && boxh >= 5) {
    
        showZandlopert();
        
        newx = (((boxl + (boxw / 2)) / 621) * (coordright - coordleft)) + coordleft;
        newy = coordtop - (((boxt + (boxh / 2)) / 362) * (coordtop - coordbottom));
        
        if ((boxh / boxw) < (362 / 621)) {
            newr = (((boxw / 621) * 362) / 362) * radius;
        } else {
            newr = (boxh / 362) * radius;
        }
        
        //alert('x = ' + parseInt(newx) + ' (' + coordleft + ',' + coordright + ')\n' + 'y = ' + parseInt(newy) + ' (' + coordtop + ',' + coordbottom + ')\n' + 'r = ' + parseInt(newr));
        window.location.href = "/kaart/?x=" + parseInt(newx) + "&y=" + parseInt(newy) + "&radius=" + parseInt(newr);
        
    } 
}

function showZandlopert() {
    document.getElementById('zandlopert').style.visibility = 'visible';
}