var dragging, ixOffset, iyOffset, xStart, xEnd, yStart, yEnd;
var reloadTimer=0;
function refreshMap(){

	var newWidth = getMapWidth();
	var newHeight = getMapHeight();

	if ( newWidth!=mWidth || newHeight!=mHeight){
	    mHeight = newHeight;
	    mWidth = newWidth;

	    // use a timer to slow down the number of requests sent to server
	    window.clearTimeout(reloadTimer);
	    reloadTimer = window.setTimeout("getMap()",1000);

	    // hide pin
	    hidePin();
	 }
}

//get the Map Image width
function getMapWidth () {
	var mapFrameWidth = MapFrame.window.innerWidth;

	if (mapFrameWidth == null) {
		mapFrameWidth = MapFrame.document.body.clientWidth;
	}

    return mapFrameWidth;
}

 //get the Map Image height
function getMapHeight () {
	var mapFrameHeight = MapFrame.window.innerHeight;

	if (mapFrameHeight == null) {
		mapFrameHeight = MapFrame.document.body.clientHeight;
	}
	return  mapFrameHeight;
}

function startUpEventHandler() {
	//Add code to handle the onmousedown, onmousemove and onmouseup events
	MapFrame.document.onmousedown = mapTool;
	MapFrame.document.onmousemove = getMouse;
	MapFrame.document.onmouseup = chkMouseUp;
	// MapFrame.document.mapForm.theMapImage.onmousedown = mapTool;
	//MapFrame.document.mapForm.theMapImage.onmousemove = getMouse;
//	MapFrame.document.mapForm.theMapImage.onmouseup = chkMouseUp;
}


function startPan(e){
	dragging = true;
	// remember where the pan started from
	xStart = e.clientX;
	yStart = e.clientY;
}

function panMap(e){
	//move the map image with the mouse pointer
	if(dragging){
	    MapFrame.document.getElementById("theMapDiv").style.left = e.clientX - xStart;
	    MapFrame.document.getElementById("theMapDiv").style.top = e.clientY - yStart;
	    return false;
	}
}

function panMpPin (e){
	//move the pin with the mouse pointer
	var theDiv = MapFrame.document.getElementById("mpPin").style;
	theDiv.left = e.clientX;
	theDiv.top = e.clientY - 16;
	theDiv.visibility = "visible";
    return false;
}

function stopPan(e){

	var xDistance, yDistance, pixelX, xOffset, yOffset;

	// stop the dragging
	dragging = false;

	// get current mouse coordinates
	xEnd = e.clientX;
	yEnd = e.clientY;

	//calculate the size of the map
	xDistance = Math.abs(minx-maxx);  //difference in x coords (map units)
	yDistance = Math.abs(miny-maxy);  //difference in y coords (map units)

	// calculate a single pixel size in map units
	pixelX = xDistance / mWidth;  // note: scale is the same in the x and y directions

	/// get the magnitude and direction of the pan movement
	ixOffset = xEnd - xStart;
	iyOffset = yStart - yEnd;  // notice the change in positive direction of the y axis

	// convert pan offsets from pixels to map units
	xOffset = pixelX * ixOffset;
	yOffset = pixelX * iyOffset;

	// finally calculate the new extents
	minx = minx - xOffset;
	maxx = maxx - xOffset;
	maxy = maxy - yOffset;
	miny = miny - yOffset;

	// send request to server using the new extents
	getMap();
}

function panByButton(buttonMode) {

    var dx = 0;
    var dy = 0;
    var pixelX, xDistance;

    // hide the pin
    hidePin();

    //calculate a single pixel size in map units
    xDistance = Math.abs(minx-maxx);
	pixelX = xDistance / mWidth;

    switch(buttonMode) {
        case 'east':
            dx = mWidth/3;
            break
        case 'north_east':
            dx = mWidth/3;
            dy = mHeight/3;
            break
        case 'north':
            dy = mHeight/3;
            break
        case 'north_west':
            dx = -(mWidth/3);
            dy = mHeight/3;
            break
        case 'west':
            dx = -(mWidth/3);
            break
        case 'south_west':
            dx = -(mWidth/3);
            dy = -(mHeight/3);
            break
        case 'south':
            dy = -(mHeight/3);
            break
        case 'south_east':
            dx = mWidth/3;
            dy = -(mHeight/3);
            break
    }

    // calculate the new extents
	minx = parseFloat(minx) + (pixelX * dx);
	maxx = parseFloat(maxx) + (pixelX * dx);
	maxy = parseFloat(maxy) + (pixelX * dy);
	miny = parseFloat(miny) + (pixelX * dy);

    // send request to server using the new extents
	getMap();
}


function stopZoomIn() {


	// calculate new current extents
	//calculate the size of the map
	var xDistance = Math.abs(minx-maxx);
	var yDistance = Math.abs(miny-maxy);

	//calculate a single pixel size in map units
	var uppCurrent = xDistance / mWidth;

	// calc center of zoombox in pixels
	var zxCenter = (parseInt(zleft) + parseInt(zright))/2;  // (pixels)
	var zyCenter = (parseInt(ztop) + parseInt(zbottom))/2;   // (pixels)

	// check for a very small zoom box
	var zoomBoxWidth = Math.abs(zright-zleft);   // (pixels)
	var zoomBoxHeight = Math.abs(ztop-zbottom); //  (pixels)

	if (zoomBoxWidth < 5) {
	    zleft = zleft- mWidth/4;
	    zright = zright + mWidth/4;
	    zoomBoxWidth = Math.abs(zright-zleft);
	}
	if (zoomBoxHeight < 5) {
	    ztop = ztop + mHeight/4;
	    zbottom = zbottom - mHeight/4;
	    zoomBoxHeight = Math.abs(ztop-zbottom);
	}

	// the new Center
    var xCenter = (parseFloat(maxx) + parseFloat(minx)) /2;
    var yCenter = (parseFloat(maxy) + parseFloat(miny)) /2;
	xCenter = xCenter + parseFloat((zxCenter - (mWidth/2))*uppCurrent);
	yCenter = yCenter - parseFloat((zyCenter - (mHeight/2))*uppCurrent);


	var xDistanceNew = mWidth * (zoomBoxWidth / mWidth) * uppCurrent;  // (map units)
	var yDistanceNew = mHeight * (zoomBoxHeight/ mHeight) * uppCurrent;  // (map units)

	var uppNewX = xDistanceNew / mWidth;
	var uppNewY = yDistanceNew / mHeight;

	// recalculate the distance dependent on the upps
	if (uppNewY > uppNewX) {
	    // hold on yDistance
	    xDistanceNew = mWidth * uppNewY;

	}
	else {
	    // hold on xDistance
	    yDistanceNew = mHeight * uppNewX;
	}

	// finally new extents
	minx = xCenter - (xDistanceNew/2);
	maxx = xCenter + (xDistanceNew/2);
	miny = yCenter - (yDistanceNew/2);
	maxy = yCenter + (yDistanceNew/2);

	//send new request for map
	getMap();
	return true;
}



function stopZoomOut() {
	// calculate upp of the map
    var xDistanceMap = parseFloat(maxx) - parseFloat(minx);
    var upp = xDistanceMap / mWidth;

	// calc center of zoombox in pixels
	var zxCenter = (parseInt(zleft) + parseInt(zright))/2;  // (pixels)
	var zyCenter = (parseInt(ztop) + parseInt(zbottom))/2;   // (pixels)

	// the new Center
	// center coords of the requested map
    xCenter = (parseFloat(maxx) + parseFloat(minx)) /2;
    yCenter = (parseFloat(maxy) + parseFloat(miny)) /2;
	xCenter = xCenter + parseFloat((zxCenter - (mWidth/2))*upp);
	yCenter = yCenter - parseFloat((zyCenter - (mHeight/2))*upp);

	// calc width of Zoombox in pixels
	var zoomBoxWidth = Math.abs(zright-zleft); //width of zoombox (pixels)
	var zoomBoxHeight = Math.abs(ztop-zbottom); //height of zoombox (pixels)
	if (zoomBoxWidth < 10) { zoomBoxWidth = mWidth/2; }
	if (zoomBoxHeight < 10) { zoomBoxHeight = mHeight/2; }

	var xDistanceNew = mWidth * (mWidth / zoomBoxWidth) * upp;  // (map units)
	// now calculate the new units-per-pixel
	upp = xDistanceNew / mWidth;
	var yDistanceNew = mHeight * upp;

	// finally new extents
	minx = xCenter - (xDistanceNew/2);
	maxx = xCenter + (xDistanceNew/2);
	miny = yCenter - (yDistanceNew/2);
	maxy = yCenter + (yDistanceNew/2);

	//send request to server using the new extents
	getMap();
	return true;
}

function fullExtent() {
    // delete all icons to display
    aObjectIDsToShow = new Array();
    aSymbols = new Array();
    bRewriteMap = true;
	//set initial extent
	minx = top.startMinx;
	maxx = top.startMaxx;
	miny = top.startMiny;
	maxy = top.startMaxy;

	//send request to server using the new extents
	getMap();

	// reset objectsearch
	if (top.AlphaFrame) {
	    top.AlphaFrame.resetObjectSearch();
	}

}

function getMapXY(xPixel,yPixel) {

	var thePoint = new Array();

	// calculate upp of the map
	var upp = Math.abs(minx-maxx) / mWidth;

    // center coords of the requested map
    var xCenter = (parseFloat(maxx) + parseFloat(minx)) /2;
    var yCenter = (parseFloat(maxy) + parseFloat(miny)) /2;

    minxMap = parseFloat(minx);
    minyMap = yCenter - (upp * mHeight) /2;

	// consider the change in Y origin
	yPixel = mHeight - yPixel;

	thePoint[0] = minxMap + parseFloat(xPixel * upp); // location of x coord (map units)
	thePoint[1] = minyMap + parseFloat(yPixel * upp);  // location of y coord (map units)

	return thePoint;
}


function getPixelXY(xMap,yMap, llx, lly, urx, ury, iWidth, iHeight) {

	var theLLx ,theLLy , theURx, URy;
	var theWidth, theHeight;

    // are params defined -> else use global variables
	if (llx && lly && urx && ury && iWidth && iHeight){
	    theLLx = llx;
	    theLLy = lly;
	    theURx = urx;
	    theURy = ury;
	    theWidth = iWidth;
	    theHeight = iHeight;
	}
	else {
	    theLLx = parseFloat(minx);
	    theLLy = parseFloat(miny);
	    theURx = parseFloat(maxx);
	    theURy = parseFloat(maxy);
	    theWidth = mWidth;
	    theHeight = mHeight;
	}

	// calculate upp of the map
   var upp = Math.abs(theLLx-theURx) / theWidth;

   // center coords of the requested map
   var xCenter = (parseFloat(theURx) + parseFloat(theLLx)) /2;
   var yCenter = (parseFloat(theURy) + parseFloat(theLLy)) /2;

   var minxMap = parseFloat(theLLx);
   var minyMap = yCenter - (upp * theHeight) /2;

   var thePoint = new Array();
	thePoint[0] = parseInt((xMap - minxMap) / upp);

	// consider the change in Y origin
	yPixel = (yMap - minyMap) / upp;
	thePoint[1] = parseInt(theHeight - yPixel);

	return thePoint;
}

var x1, y1, x2, y2, width, height, zooming, layer;
var zleft, zright, ztop, zbottom;


function getLayer(documentObj, name) {
    // get the layer object called "name"
    var theObj = documentObj.getElementById(name);
    return theObj.style
}

function startZoomBox(e){
	zooming=true;
    //determine where the user has clicked by interogating the event object
	x1=e.clientX;
	y1=e.clientY;

	zleft = x1;
	zright =x1;
	zbottom = y1;
	ztop = y1;
}

function resizeZoomBox(e){
    var layer = getLayer(MapFrame.document,"zoomBox");
    //determine current mouse location
	var mouseX=e.clientX;
	var mouseY=e.clientY;

	//determine if the mouse goes off the map image
	if ((mouseX>mWidth) || (mouseY>mHeight) || (mouseX<=0) ||(mouseY<=0)){
	  stopZoomBox();
	} else {
	  if (zooming) {
	   x2=mouseX;
	   y2=mouseY;
       //determine width and height of zoom box
	   width=x2-x1;
	   height=y2-y1;

	   //determine the direction in which the zoom box is drawn and set the appropriate
	   // left and top properties
	   if (width > 0) {
	     layer.left=x1;
	     zleft=x1;
	     zright = x2;
	   } else {
	     layer.left=x2;
		 zleft=x2;
		 zright = x1;}
	   if (height > 0) {
	     layer.top=y1;
		 ztop=y1;
		 zbottom = y2;
	   } else {
	     layer.top=y2;
		 ztop=y2;
	     zbottom = y1;
	   }

		//Set the width and height of the zoom box
	    layer.width = Math.abs(width);
	    layer.height = Math.abs(height);
        //Make the zoom box visible
		layer.visibility = "visible";

	  }
	}

return false;
}

function stopZoomBox(){
    layer = getLayer(MapFrame.document,"zoomBox");
//Hide the zoom box
	layer.visibility="hidden";
	zooming=false;
}

function displayMapCoords (e) {

    var thePoint = new Array ();
    var thePointX, thePointY;

    // get current mouse coordinates
    x = e.clientX;
    y = e.clientY;

	// convert the maximum x and y values from pixel to map coordinates
	thePoint = getMapXY(x,y);

	thePointX = Math.floor(thePoint[0]);
	thePointY = Math.floor(thePoint[1]);

    window.status = 'X:'+ thePointX +'   Y:'+ thePointY;
}

function displayAdminMapCoords (e) {

    var thePoint = new Array ();
    var thePointX, thePointY;

    // get current mouse coordinates
    x = e.clientX;
    y = e.clientY;

	// convert the maximum x and y values from pixel to map coordinates
	thePoint = getMapXY(x,y);

	thePointX = Math.floor(thePoint[0]);
	thePointY = Math.floor(thePoint[1]);

	AlphaFrame.ResultFrame.document.getElementById("Rechtswert").value = thePointX;
	AlphaFrame.ResultFrame.document.getElementById("Hochwert").value = thePointY;
}

function stopDisplayMapCoords () {
    // clear Status
    window.status = '';
    // if meeting point mode hide pin
    if (toolMode == 5){ MapFrame.document.getElementById("mpPin").style.visibility = "hidden";}
}

function centerCoords () {
	var boundingBox = top.getBoundingBox();
    var minx = Number(boundingBox[0]);
	var miny = Number(boundingBox[1]);
	var maxx = Number(boundingBox[2]);
	var maxy = Number(boundingBox[3]);
	var x = (minx + maxx) / 2;
	var y = (miny + maxy) / 2;
	x = x.toFixed(0);
	y = y.toFixed(0);
	var url = "coords.jsp?x=" + x + "&y=" + y;
	window.open(url,"Koordinaten","width=300,height=100,top=0,left=0");
}

