/*
 * GLOBAL VARIABLES
 ******************************************************/
var mouseDrag = false;    // TRUE when mouse is pressed
var maction;

var enableRightMousePan = top.enableRightMousePan;
var rightMouseButton = false;

var downX, downY;
var upX, upY;
var moveX, moveY;

var rBoxMinW = 10;  // Minimal width until to show refBox; below threshold switches to refCross
var rOffs = 13;     // Offset of refCross Image, adapt to Image size and refbox border

if (document.all) {
    var zBorder = 0;
} else {
    var zBorder = 4;
}

var isIE4 = (document.all) ? true : false;

/*
 * DEFINE MOUSE ACTIONS, CALLED AS 'ONLOAD' SCRIPT
 ******************************************************/
function startUp() {
    document.onmousedown = doMouseDown;
    document.onmouseup   = doMouseUp;
    document.onmousemove = doMouseMove;
    document.oncontextmenu = disableContextMenu;
    
    // ENABLES ACTIONS FOR KEYBOARD KEYS
    // comment out if not wanted
    if (document.all) document.onkeydown = kp;
    document.onkeypress = kp; 
    
    // ENABLES ACTIONS FOR MOUSE WHEEL
	document.onmousewheel = omw;
}


/*
 * FUNCTIONS TO GET MOUSE POSITIONS
 ******************************************************/
// For MouseDown
function getDownXY(e) {
    if (document.all) {
        downX = event.clientX;
        downY = event.clientY;
    } else {
        downX = e.pageX;
        downY = e.pageY;
    }
	//alert(downX+" / "+downY);
    // subtract offsets from left and top
	if((downX < anX) || (downX > (anX+ mapW)) || (downY < anY) || (downY > (anY + mapH))){
	    downX = 0;
	    downY = 0;
	} else{
		downX = downX - offsX;
	    downY = downY - offsY;
	}
	//alert("downX-Y"+downX+" / "+downY); 
}


// For MouseUp
function getUpXY(e) {
	if (document.all) {
		upX = event.clientX;
		upY = event.clientY;
	} else {
		upX = e.pageX;
		upY = e.pageY;
	}
	// subtract offsets from left and top and don't go outside of map
	upX = upX - offsX;
	upY = upY - offsY;
	if((upX < anX) || (upX > (anX + mapW)) || (upY < anY) || (upY > (anY + mapH))){
	    upX = 0;
	    upY = 0;
	}
	//alert("getUpX-Y"+upX+" / "+upY); 
}

// For MouseMove
function getMoveXY(e) {
    if (document.all) {
        moveX = event.clientX;
        moveY = event.clientY;
    } else {
        moveX = e.pageX;
        moveY = e.pageY;
    }
    // subtract offsets from left and top
    /*moveX = Math.min(moveX - offsX, mapW);
    moveY = Math.min(moveY - offsY, mapH); */

    if((moveX < anX) || (moveX > (anX + mapW)) || (moveY < anY) || (moveY > (anY + mapH))){
		moveX = 0;
	    moveY = 0;
	} else {
	    moveX = moveX - offsX;
	    moveY = moveY - offsY;
	}
	//alert(moveX + " / " + moveY);           
}

function zoombox_apply(minx, miny, maxx, maxy) {
	//zoomin(imgbox);
	var imgbox = minx + "+" + miny + "+" + maxx + "+" + maxy;
	//var imgxy  = minx + "+" + miny;
	var mapurl = 'index.phtml?imgbox='+imgbox+'&CMD=ZOOM_IN&MAINMAP_x&MAINMAP_y';
	location = mapurl;
}


/*
 * BASIC MOUSE FUNCTIONS: DOWN, UP, MOVE
 ******************************************************/

function doMouseDown(e) {
    e = (e)?e:((event)?event:null);
    
    // Disable right mouse context menu
    document.oncontextmenu = disableContextMenu;
    
    if (enableRightMousePan) {
        if (e.button == 2) {
            rightMouseButton = true;
            setCursor(true);
        } else {
            rightMouseButton = false;
        }
    }
    
    // ENABLES ACTIONS FOR KEYBOARD KEYS
    // comment out if not wanted
    if (document.all) document.onkeydown = kp;
    document.onkeypress = kp; 
    
    mouseDrag = true;
    getDownXY(e);
	if((downX == 0) && (downY == 0)){
		hideLayer('zoombox');
		mouseDrag = false;
		//alert("zoombox hidden");
	} else {
    	return false;
	}
}


function doMouseUp(e) {
	e = (e)?e:((event)?event:null);
	//alert (rightMouseButton);
	
	mouseDrag = false;
	getUpXY(e);
	//alert(upX +" / "+ upY);
	if((upX == 0) && (upY == 0)){
		hideLayer('zoombox');
		//alert("zoombox hidden");
		return true;
	} else {
		// Click in main map
		maction = document.F_layers.CMD.value;
		//alert(maction);
		if (rightMouseButton) {
			maction = 'ZOOM_IN';
		}
		if (maction == 'RECENTER'){
			//var diffX = upX - downX;
			//var diffY = upY - downY;
			//minx = (Math.min(downX,upX))- anX;
			//miny = (Math.min(downY,upY))- anY;
			minx = downX - anX;
			miny = downY - anY;
			//alert("RECENTER "+minx+" / "+miny+" / "+downX+" / "+downY);
			var mapurl = 'index.phtml?MAINMAP_x='+minx+'&MAINMAP_y='+miny+'&CMD=RECENTER';
				location = mapurl;
		} else if (maction == 'ZOOM_OUT'){
			minx = (Math.min(downX,upX))- anX;
			miny = (Math.min(downY,upY))- anY;
			//alert("ZOOM_OUT "+minx+" / "+miny+" / "+downX+" / "+downY);
			var mapurl = 'index.phtml?MAINMAP_x='+minx+'&MAINMAP_y='+miny+'&CMD=ZOOM_OUT';
			location = mapurl;
		} else if (maction == 'QUERY'){
			minx = (Math.min(downX,upX))- anX;
			miny = (Math.min(downY,upY))- anY;
			//alert("QUERY "+minx+" / "+miny+" / "+downX+" / "+downY);
			if(GetQlayer == ''){
				var mapurl = 'index.phtml?MAINMAP_x='+minx+'&MAINMAP_y='+miny+'&CMD=QUERY&Qlayer=';
			} else {
				var mapurl = 'index.phtml?MAINMAP_x='+minx+'&MAINMAP_y='+miny+'&CMD=QUERY&Qlayer='+GetQlayer;
			}
			location = mapurl;
		} else if (maction == 'ZOOM_IN'){
			minx = (Math.min(downX,upX))- anX;
			miny = (Math.min(downY,upY))- anY;
			maxx = (Math.max(downX,upX)) - anX;
			maxy = (Math.max(downY,upY))- anY;
			if ((downX == upX) && (downY == upY)){
				//alert("ZOOM_IN "+minx+" / "+miny+" / "+downX+" / "+downY);
				//alert("ZOOM_IN "+minx+" / "+downX+" / "+upX+" / "+anX);
				//alert("ZOOM_IN "+miny+" / "+downY+" / "+upY+" / "+anY);
				//alert("ZOOM_IN "+maxx+" / "+downX+" / "+upX+" / "+anX);
				//alert("ZOOM_IN "+maxy+" / "+downY+" / "+upY+" / "+anY);
				var mapurl = 'index.phtml?MAINMAP_x='+minx+'&MAINMAP_y='+miny+'&CMD=ZOOM_IN';
				location = mapurl;
			} else {
				//alert("ZOOM_IN "+minx+" / "+miny+" / "+maxx+" / "+maxy);
				zoombox_apply(minx, miny, maxx, maxy);
			}  
		}
		return false; 
	}   
}


function doMouseMove(e) {
	e = (e)?e:((event)?event:null);
	getMoveXY(e);
	
	/* 	* Draw a zoombox when mouse is pressed and zoom-in or select function are active
		* move map layer when pan function is active
		* do nothing for all others                                                      */
	
	// Actions in MAIN MAP
	if ((moveX == 0) && (moveY == 0)){
		//alert("moveX == 0");
		hideLayer('zoombox');
		return true;
	} else {
		maction = document.F_layers.CMD.value;
		if (rightMouseButton) {
			maction = 'ZOOM_IN';
		}
		// Show coordinates in status bar
		//var mouseString = getCoords(moveX, moveY);
		//window.status = mouseString;
		// Grad
	    //koordinatenumrechnung(moveX, moveY);
		
		// Show coordinates 
        displayCoordinates(moveX, moveY);
		   
		switch (maction) {
			//# zoom-in, select
			case 'ZOOM_IN':
				startZoomBox(e, moveX, moveY);
				break;
			
			//# zoom-out, identify
			case 'ZOOM_OUT':
				hideLayer('zoombox');
				break;
			
			//# pan with drag
			case 'RECENTER':
				hideLayer('zoombox');
				//startPan(e, moveX, moveY);
				break;
		}
	    return false;
	}
}


/* 
 * AUXILIARY FUNCTIONS
 ******************************/
// DISPLAY MAP COORDINATES FOR MOUSE MOVE
function displayCoordinates() {
	mouseX = moveX - KoorAnX;
	mouseY = moveY - KoorAnY;
	
	var x_geo = Math.round(minx_geo + ((mouseX/mapW) * xdelta_geo));
    var y_geo = Math.round(maxy_geo - ((mouseY/mapH) * ydelta_geo));
	
    var rfactor = 4;
    px = roundN(x_geo, rfactor);
    py = roundN(y_geo, rfactor);
	//window.status = px +" "+ py;
	
	koordinatenumrechnung(px, py)
}

//
// Round to a specified decimal
//
function roundN(numin, rf) {
    return ( Math.round(numin * Math.pow(10, rf)) / Math.pow(10, rf) );
} 

/*
 * FUNCTIONS FOR ZOOM BOX && PAN MOVING MAP
 ******************************************************/

// DRAG ZOOM BOX (ZOOM IN, SELECT)
function startZoomBox(e, moveX, moveY) {
	if((moveX == 0) && (moveY == 0)){
		return true;
	//} else if (moveX < 0 || moveY < 0) {
	//	return true;
	} else {
	    if (mouseDrag == true) {
	        showLayer('zoombox');
			// Kontroll X Rechts
			if (downX > (mapW + anX)) {
				downX = mapW + (anX - rand);
			}
			// Kontroll X Links
			if (downX < anX) {
				downX = anX + rand;
			}
			// Kontroll Y Unten
			if (downY > (mapH + anY)) {
				downY = mapH + (anY - rand);
			}
			// Kontroll Y Oben
			if (downY < anY) {
				downY = anY + rand;
			}
	        var boxL = Math.min(moveX, downX);
	        var boxT = Math.min(moveY, downY);
	        var boxW = Math.abs(moveX - downX);
	        var boxH = Math.abs(moveY - downY);
	        var theZoomBox = document.getElementById("zoombox");
	
	        theZoomBox.style.left   = boxL;
	        theZoomBox.style.top    = boxT;
	        theZoomBox.style.width  = boxW;
	        theZoomBox.style.height = boxH;
	    }
	    return false;
	}
}

// DRAG ZOOM BOX (ZOOM IN, SELECT)
function startPan(e, moveX, moveY) {
    if (mouseDrag == true) {

        var mapL = moveX - downX;
        var mapT = moveY - downY;

        var theMapImg = document.getElementById("mapimg");

        theMapImg.style.left = mapL;
        theMapImg.style.top  = mapT;
    }
    return false;
}

// SET DIV LAYER VISIBLE - HIDDEN
function showLayer(elementID) {
    var theZoomBox = document.getElementById(elementID);
    theZoomBox.style.visibility = "visible";
}

function hideLayer(elementID) {
    var theZoomBox = document.getElementById(elementID);
    theZoomBox.style.visibility = "hidden";
}

/* 
 * AUXILIARY FUNCTIONS
 ******************************/
// GET MAP COORDINATES FOR MOUSE MOVE
function getCoords(mouseX, mouseY) {
    var x_geo = Math.round(minx_geo + ((mouseX/mapW) * xdelta_geo));
    var y_geo = Math.round(maxy_geo - ((mouseY/mapH) * ydelta_geo));

    var mapCoords = 'X: ' + x_geo + '  Y: ' + y_geo;
    return  mapCoords;

}


/*
 * KEYBOARD FUNCTIONS
 * original script taken from http://ka-map.maptools.org/
 ******************************************************************/
function kp(e) {
    e = (e)?e:((event)?event:null);
    if(e) {
        var charCode=(e.charCode)?e.charCode:e.keyCode;
        //alert(charCode);
        switch(charCode){
          case 63232://safari up arrow
          case 38://up arrow
            arrowpan('n');
            break;
          case 63233://safari down arrow
          case 40://down arrow
            arrowpan('s');
            break;
          case 63234://safari left arrow
          case 37:// left arrow
            arrowpan('w');
            break;
          case 63235://safari right arrow
          case 39://right arrow
            arrowpan('e');
            break;
          case 63276://safari pageup
          case 33://pageup
            gofwd();
            break;
          case 63277://safari pagedown
          case 34://pagedown
            goback();
            break;
          case 63273://safari home (left)
          case 36://home
            zoomfullext();
            break;
          case 63275://safari end (right)
          case 35://end
            slideBy(-viewportWidth/2,0);
            break;
          case 43: //zoom in
            zoompoint(2, '');
            break;
         case 45: // zoom out
            zoompoint(-2, '');
            break;
          default:
            b=false;
        }
    }
}



/*
 * MOUSEWHEEL FUNCTIONS (zoom in/out)
 * only works with IE
 ******************************************************************/
function omw(e) {
    e = (e)?e:((event)?event:null);
    if(e) {
        wD = e.wheelDelta;
        //alert(wD);
        if (wD >= 120) {
            //zoompoint(2, '');
			var mapurl = 'index.phtml?CMD=ZOOM_IN';
			location = mapurl;
            return false;
        } else if (wD <= -120) {
            //zoompoint(-2, '');
			var mapurl = 'index.phtml?CMD=ZOOM_OUT';
			location = mapurl;
            return false;
        }
    }
}


/*
function wheel(e)
{
  var wheelDelta;
  if (!e)
    e=window.event;
  if (e.wheelDelta)
    wheelDelta=e.wheelDelta;
  else if (e.detail)
    wheelDelta=e.detail*-1;
  if (wheelDelta>0)
	myZoomIn();
  else if (wheelDelta<0)
        myZoomOut();
}
*/



function disableContextMenu(e) {
    e = (e)?e:((event)?event:null);
    return cancelEvent(e);
}


function cancelEvent(e)
{
    e = (e)?e:((event)?event:null);
    e.returnValue = false;
    if (e.preventDefault) e.preventDefault();
    return false;
}
