/*--------------------------------------------------------
* debug function
* so MHU knows what's going on >:)
*--------------------------------------------------------*/
//id's of the .net controls
var imageid = 'mapctrl_Image1';
var txtmaxxid = 'mapctrl_txtmaxx';
var txtminxid = 'mapctrl_txtminx';
var txtmaxyid = 'mapctrl_txtmaxy';
var txtminyid = 'mapctrl_txtminy';
var txtactionid = 'mapctrl_txtaction';
var txtactivetoolid = 'mapctrl_txtactivetool';
var txtidentxid = 'mapctrl_txtnewmaxx';
var txtidentyid = 'mapctrl_txtnewmaxy';
var txtnewmaxxid = 'mapctrl_txtnewmaxx';
var txtnewminxid = 'mapctrl_txtnewminx';
var txtnewmaxyid = 'mapctrl_txtnewmaxy';
var txtnewminyid = 'mapctrl_txtnewminy';

function debug(msg)
{
    if(false){
        var debugdiv = document.getElementById('debugdiv');
        var str = debugdiv.innerHTML;
        str =  msg + "<br/>" + str;
        debugdiv.innerHTML = str;
    }
}
    
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent; 
		}
	}
	else if (obj.x) 
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/********************************************
*Get's mouse position (should be called onmousemove)
*Sets _mousex and _mousey (global variables for the mouse position to the map)
*********************************************/
function getMousePosition(ev) {
//gets the position of the mousecursor
    var x;
    var y;
    if(!_panning){
        var imagetop = findPosY(_mapimage);
        var imageleft = findPosX(_mapimage);
    }
    else{
        var imagetop = 0;
        var imageleft =0;
    }
    if (!isIE) {
        x = ev.pageX;
        y = ev.pageY;
        
    }
    if (isIE) {
        var xpos = ev.clientX;
        var ypos = ev.clientY;
        x = parseInt(xpos) + (document.body.scrollLeft);
        y = parseInt(ypos) + (document.body.scrollTop);
        //20060212_BTR:
        imageleft = imageleft;
        imagetop = imagetop + 1;
    }
    
    _xpos =x - imageleft;
    _ypos =y  - imagetop;
   
   //20060212_BTR: for debugging click-coordinates:
   /*if(getAction()=="identify")
   {
   alert(_xpos);
   alert(_ypos);
   }*/
   
    debug('x='+x);
    debug('y='+y);
    
    debug('imageleft='+imageleft);
    debug('imagetop='+imagetop);
    debug('_xpos='+_xpos);
    debug('_ypos='+_ypos);
    return true;
    
}


function startBox(e)
{ 
    var ev;
    if (!e){
         ev = window.event
    }
    else{
        ev = e;
    }
    
    
    getMousePosition(ev);
    if((getAction()=="zoomin") || (getAction()=="zoomout")){
        
        _box = document.getElementById('drawbox');
        var xpos = _xpos;//event.x;
        var ypos = _ypos;//event.y;
        //alert('sla');
        _box.style.display = 'block';
        _box.style.top = ypos + 'px';
        _box.style.left = xpos + 'px';
        _box.style.height = 0 + 'px';
        _box.style.width = 0 + 'px';
        _xstart = xpos;
        _ystart = ypos;
        _showbox = true;
             
        
    }
    if(getAction()=="pan"){
        _panning=true;
        
        getMousePosition(ev);
        _xstart = _xpos;
        _ystart = _ypos;
        
        
        
    }
  
}

function moveBox(e)
{
    if(_showbox){
        var ev;
        if (!e){
            ev = window.event
        }
        else{
            ev = e;
        }
        
        getMousePosition(ev);
        var xpos = _xpos;
        var ypos = _ypos;
        var bwidth = 1;
        var bheight = 1;
        var bleft = xpos;
        var btop = ypos;
       
        
        if (xpos<_xstart){
            bwidth = _xstart-xpos;
            bleft = xpos;
        }
        else if (xpos>_xstart){
            bwidth = xpos-_xstart;
            bleft = _xstart;
        }
        
        if (ypos<_ystart){
            bheight = _ystart-ypos;
            btop = ypos;
        }
        else if (ypos>_ystart){
            bheight = ypos-_ystart;
            btop = _ystart;
        }
       
        _box.style.left = bleft + 'px';
        _box.style.top = btop + 'px';  
        _box.style.height = bheight + 'px';
        _box.style.width = bwidth + 'px';
    } 
    if(_panning){
         var ev;
        if (!e){
            ev = window.event
        }
        else{
            ev = e;
        }
        
        getMousePosition(ev);
        
        moveMap();
    
    }  
}
function stopBox(e){
   
    var ev;
    if (!e){
         ev = window.event
    }
    else{
        ev = e;
    }
    
    getMousePosition(ev);
    if(_showbox){
        var xpos = _xpos;
        var ypos = _ypos;
        var imagetop = findPosY(document.getElementById(imageid));
        var imageleft = findPosX(document.getElementById(imageid));
    
		var mapWidth = _mapimage.width;
		var mapHeight = _mapimage.height;
		
        if (xpos<_xstart){
            _xend = _xstart;
            _xstart = xpos;
        }
        else if (xpos>_xstart){
            _xend = xpos;
        }
        else  //_xstart = _xend
        {
        _xstart = xpos - (0.6 * (mapWidth/2));
        _xend = xpos + (0.6 * (mapWidth/2));
        }
        
        if (ypos<_ystart){
            _yend = _ystart;
            _ystart = ypos;
        }
        else if (ypos>_ystart){
            _yend = ypos;
        }
        else  //_ystart = _yend
        {
        _ystart = ypos - (0.6 * (mapHeight/2));
        _yend = ypos + (0.6 * (mapHeight/2));
        }
        
        _showbox = false;
        _box.style.display = 'none';
       
    
    }
    if(_panning){
        _panning  = false;
    }
    if(getAction()=="identify")
    {
        calculateIdentify();
    }
    else{
        calculateNewExtend();    
    }
}

//move the image
function moveMap(){
    if(_panning) {
        
        var image = document.getElementById(imageid)
        var nowX = _xpos;
        var nowY = _ypos;
        var cliptop = 0;
        var clipright = image.width;
        var clipbottom = image.height;
        var clipleft=0;
        var image;
        var cliprect;
        var div;
        
        dwidth = nowX - _xstart;
        dheight = nowY - _ystart;
        
      
        if(dheight>0){
            clipbottom = clipbottom - dheight;
        }
        else{
            cliptop = cliptop - dheight;
        }
        if(dwidth>0){
            clipright = clipright - dwidth;
        }
        else{
            clipleft = clipleft - dwidth;
        }
        
        cliprect = 'rect('+cliptop+' px '+clipright+'px '+clipbottom+'px '+clipleft+'px)';
        
        image.style.position = 'absolute';
        image.style.top = dheight + 'px';
        image.style.left = dwidth + 'px';
        image.style.clip = cliprect;
       
    }
}

function calculateIdentify(){
    var mapWidth = _mapimage.width;
    var mapHeight = _mapimage.height;

        
    //get old rd coordinates
    var rdMaxXold = document.getElementById(txtmaxxid).value.replace(",",".");
    var rdMinXold = document.getElementById(txtminxid).value.replace(",",".");
    var rdMaxYold = document.getElementById(txtmaxyid).value.replace(",",".");
    var rdMinYold = document.getElementById(txtminyid).value.replace(",",".");
    
    
    //calculate delta rd old coordinates
    var rdDeltaX = rdMaxXold - rdMinXold;
    var rdDeltaY = rdMaxYold - rdMinYold;
    
    rdX = parseFloat(rdMinXold) + ((_xpos * rdDeltaX)/mapWidth)
    rdY = parseFloat(rdMaxYold) - ((_ypos * rdDeltaY)/mapHeight)
    
    

    document.getElementById(txtidentxid).value = rdX;
    document.getElementById(txtidentyid).value = rdY;
    document.getElementById(txtactionid).value='identify';
    
   //20060212_BTR: for debugging click-coordinates:
   /*if(getAction()=="identify")
   {
   alert("rdX: " + rdX + "rdY: " + rdY + "_xpos: " + _xpos + "_ypos: " + _ypos);
   }*/
    
    document.forms[0].submit();
    
}

function calculateNewExtend(){
    var action = getAction();
    
    var mapWidth = _mapimage.width;
    var mapHeight = _mapimage.height;
    
    //get old rd coordinates
    var rdMaxXold = document.getElementById(txtmaxxid).value.replace(",",".");
    var rdMinXold = document.getElementById(txtminxid).value.replace(",",".");
    var rdMaxYold = document.getElementById(txtmaxyid).value.replace(",",".");
    var rdMinYold = document.getElementById(txtminyid).value.replace(",",".");
    
    
     
    //calculate delta rd old coordinates
    var rdDeltaX = rdMaxXold - rdMinXold;
    var rdDeltaY = rdMaxYold - rdMinYold;
    
    if(action=="zoomin" || action=="zoomout")
    {
        //calculate delta max x, delta minx, delta miny and delta maxy
        rdDeltaMinX = (_xstart * rdDeltaX)/mapWidth
        rdDeltaMinY = (_ystart * rdDeltaY)/mapHeight
        rdDeltaMaxY = (_yend * rdDeltaY)/mapHeight
        rdDeltaMaxX = (_xend * rdDeltaX)/mapWidth
        
        
        if(action=="zoomin"){
            
            //calculate new rd values
            var rdMinXnew = (parseFloat(rdMinXold) + parseFloat(rdDeltaMinX));
            var rdMaxXnew = (parseFloat(rdMinXold) + parseFloat(rdDeltaMaxX));
            var rdMinYnew = (parseFloat(rdMinYold) + (rdDeltaY-rdDeltaMaxY));
            var rdMaxYnew = rdMaxYold - rdDeltaMinY;
        }
        if(action=="zoomout"){
            //calculate new rd values
            var rdMinXnew = rdMinXold - (parseFloat(rdDeltaX) - parseFloat(rdDeltaMaxX));
            var rdMaxXnew = (parseFloat(rdMaxXold) + parseFloat(rdDeltaMinX));
            
            var rdMinYnew = rdMinYold - rdDeltaMinY;
            var rdMaxYnew = (parseFloat(rdMaxYold) + (rdDeltaY -  rdDeltaMaxY));
            
        }
    }
    if(action=="pan"){
        var pngDeltaX = parseFloat(_xstart) - parseFloat(_xpos);
        var pngDeltaY = parseFloat(_ystart) - parseFloat(_ypos);
        
        var rdMoveDeltaX = (pngDeltaX*rdDeltaX)/mapWidth;
        var rdMoveDeltaY = (pngDeltaY*rdDeltaY)/mapHeight;
        
        
        var rdMinXnew = parseFloat(rdMinXold) + parseFloat(rdMoveDeltaX);
        var rdMaxXnew = parseFloat(rdMaxXold) + parseFloat(rdMoveDeltaX);
        var rdMinYnew = rdMinYold -rdMoveDeltaY;
        var rdMaxYnew = rdMaxYold -rdMoveDeltaY;
        
    }
    
    
    document.getElementById(txtnewmaxxid).value=rdMaxXnew;
    document.getElementById(txtnewminxid).value=rdMinXnew;
    document.getElementById(txtnewmaxyid).value=rdMaxYnew;
    document.getElementById(txtnewminyid).value=rdMinYnew;
    document.getElementById(txtactionid).value='newextend';
    
    
        document.forms[0].submit();
    
   
}
function getAction(){
    var action ="";
    
    if(document.getElementById(txtactivetoolid)){
        action = document.getElementById(txtactivetoolid).value;
    }
    else{
        action="";
    }
    if(action==""){
        action="zoomin";
    }
    return action;
}
function setAction(action){
    document.getElementById(txtactivetoolid).value = action;
}
function setActiveTool(action){
    
    //sets the mousecursor you need and the form values 
    var cursor = "";
    var zmvalue = "";
    document.getElementById('zoominbox').className = 'buttonzoominbox';
    document.getElementById('zoomoutbox').className = 'buttonzoomoutbox';
    document.getElementById('crosshair').className = 'buttoncrosshair';
    document.getElementById('pan').className = 'buttonpan';
    
    if (action == "zoomin"){
        cursor = 'url(images/zoomin.cur),default';
        document.getElementById('zoominbox').className = 'buttonzoominboxsel';
    }
    else if (action == "zoomout") {
        cursor = 'url(images/zoomout.cur),default';
        document.getElementById('zoomoutbox').className = 'buttonzoomoutboxsel';
    }
    else if (action == "identify") {
        cursor = 'crosshair';
        document.getElementById('crosshair').className = 'buttoncrosshairsel';
    }
    else if (action == "pan") {
        cursor = 'move';
        document.getElementById('pan').className = 'buttonpansel';
    }
    else {
        cursor = 'default';      
    }
    document.getElementById('boxdiv').style.cursor = cursor;
    setAction(action);
}
