
///////////////////////////////////////////////////////////////////////
// zoom box
///////////////////////////////////////////////////////////////////////

function setZoomBoxEvents() {
    document.getElementById(eventLayerId).onmousedown = zoomBoxDown;
    document.getElementById(eventLayerId).onmousemove = doNothing;
    document.getElementById(eventLayerId).style.cursor = "crosshair";
}

function zoomBoxDown(e) {
    if (isLeftClick(e)) {
        
        getMapPixels(e);
    
        startX=currentX;
        startY=currentY;
        currentX=startX+1;
        currentY=startY+1;
    
        clipZoomBox();
        showLayer(zoomBoxId);

		document.getElementById(eventLayerId).onmousemove = zoomBoxMove;
		document.onmouseup = zoomBoxUp;
        actionInProgress = true;
        return false;
    }
	else{panDown(e);}
}

function zoomBoxMove(e) {

    if (actionInProgress) {
        getMapPixels(e);
        clipZoomBox();
        return false;
    }
	else{panMove(e);}
}
function doNothing(e) {

}
function zoomBoxUp(e) { 

    if (actionInProgress) {
        getMapPixels(e);
        actionInProgress = false;
        if ((Math.abs(currentX-startX) < 2) 
            && (Math.abs(currentY-startY) < 2)) {
            // the zoom box is very small, do a zoomIn
            
            getMapXY(currentX,currentY);
        
            var widthHalf = Math.abs(maxx - minx) / 2;
            var heightHalf = Math.abs(maxy - miny) / 2;
            newMinx = mapX - widthHalf;
            newMaxx = mapX + widthHalf;
            newMaxy = mapY + heightHalf;
            newMiny = mapY - heightHalf;
    
            addPercent(-50);

            refreshMap(newMinx,newMiny,newMaxx,newMaxy); 
        } else {
        
            zminx = Math.min(startX,currentX);
            zmaxx = Math.max(startX,currentX);
            zminy = Math.max(startY,currentY);
            zmaxy = Math.min(startY,currentY);

            var width = Math.abs(maxx - minx);
            var height = Math.abs(maxy - miny);
            newMaxy = (height/mapHeight) * (mapHeight - zmaxy) + miny;
            newMaxx = (width/mapWidth) * zmaxx + minx;
            newMinx = (width/mapWidth) * zminx + minx;
            newMiny = (height/mapHeight) * (mapHeight - zminy) + miny;
          
            refreshMap(newMinx,newMiny,newMaxx,newMaxy);
        }
        
        document.getElementById(eventLayerId).onmousemove = doNothing;
        document.onmouseup = doNothing;

        return true;
    }
}

function clipZoomBox() {    

    if ((startX != currentX) && (startY != currentY)) {

        zminx = Math.min(startX,currentX);
        zmaxx = Math.max(startX,currentX);
        zminy = Math.max(startY,currentY);
        zmaxy = Math.min(startY,currentY);
        clipBox(zoomBoxId,zminx,zminy,zmaxx,zmaxy,positionLayerId);
    }
}

///////////////////////////////////////////////////////////////////////
// select (set geographic constraint)
///////////////////////////////////////////////////////////////////////

function setExtentEvents() {
    document.getElementById(eventLayerId).onmousedown = extentDown;
    document.getElementById(eventLayerId).onmousemove = doNothing;
    document.getElementById(eventLayerId).style.cursor = "crosshair";
}

function extentDown(e) {
    
    if (isLeftClick(e)) {
        getMapPixels(e);
    
        startX=currentX;
        startY=currentY;
        currentX=startX+1;
        currentY=startY+1;
    
        clipExtentBox();
        showLayer(extentBoxId);
        actionInProgress = true;
		document.getElementById(eventLayerId).onmousemove = extentMove;
		document.onmouseup = extentUp;
        return false;
    }
}

function extentMove(e) {

    if (actionInProgress) {
        getMapPixels(e);
        clipExtentBox();
        return false;
    }
}

function extentUp(e) { 

    if (actionInProgress) {
        actionInProgress = false;
        getMapPixels(e);
    
        if ((Math.abs(currentX-startX) < 2) 
            && (Math.abs(currentY-startY) < 2)) {
            // the extent box is very small, it's not valid
            alert(extentAlertMsg);
            hideLayer(extentBoxId);
        } else {
            
            zminx = Math.min(startX,currentX);
            zmaxx = Math.max(startX,currentX);
            zminy = Math.max(startY,currentY);
            zmaxy = Math.min(startY,currentY);    
            newMaxy = getMapY(zmaxy);
            newMaxx = getMapX(zmaxx);
            newMinx = getMapX(zminx);
            newMiny = getMapY(zminy);
            clipExtentBox();
            setExtentValue(newMinx,newMiny,newMaxx,newMaxy);
        }
		document.getElementById(eventLayerId).onmousemove = doNothing;
		document.onmouseup = doNothing;
        return true;
    }
}

function clipExtentBox() {    

    if ((startX != currentX) && (startY != currentY)) {

        zminx = Math.min(startX,currentX);
        zmaxx = Math.max(startX,currentX);
        zminy = Math.max(startY,currentY);
        zmaxy = Math.min(startY,currentY);
        clipBox(extentBoxId,zminx,zminy,zmaxx,zmaxy,positionLayerId);
    }
}

///////////////////////////////////////////////////////////////////////
// zoom in
///////////////////////////////////////////////////////////////////////

function setZoomInEvents() {
    document.getElementById(eventLayerId).onmousedown = zoomInDown;
    document.getElementById(eventLayerId).onmousemove = doNothing;
    document.onmouseup = null;
    document.getElementById(eventLayerId).style.cursor = "crosshair";
}

function zoomInDown(e) {

    if (isLeftClick(e)) {
        getMapPixels(e);
        getMapXY(currentX,currentY);
    
        var widthHalf = Math.abs(maxx - minx) / 2;
        var heightHalf = Math.abs(maxy - miny) / 2;
        newMinx = mapX - widthHalf;
        newMaxx = mapX + widthHalf;
        newMaxy = mapY + heightHalf;
        newMiny = mapY - heightHalf;
    
        addPercent(-50);
        refreshMap(newMinx,newMiny,newMaxx,newMaxy); 
        return false;
    }
}

///////////////////////////////////////////////////////////////////////
// zoom out
///////////////////////////////////////////////////////////////////////

function setZoomOutEvents() {
    document.getElementById(eventLayerId).onmousedown = zoomOutDown;
    document.getElementById(eventLayerId).onmousemove = doNothing;
    document.onmouseup = doNothing;
    document.getElementById(eventLayerId).style.cursor = "crosshair";
}
function zoomOutDown(e) {
    if (isLeftClick(e)) {
        
        getMapPixels(e);
    
        startX=currentX;
        startY=currentY;
        currentX=startX+1;
        currentY=startY+1;
    
        clipZoomBox();
        showLayer(zoomBoxId);

        document.getElementById(eventLayerId).onmousemove = zoomOutMove;
        document.onmouseup = zoomOutUp;
        actionInProgress = true;
        return false;
    }
}

function zoomOutMove(e) {

    if (actionInProgress) {
        getMapPixels(e);
        clipZoomBox();
        return false;
    }
}

function zoomOutUp(e) { 

    if (actionInProgress) {
        getMapPixels(e);
    
        if ((Math.abs(currentX-startX) < 2) 
            && (Math.abs(currentY-startY) < 2)) {
            // the zoom box is very small, do a zoomOut
            
            getMapXY(currentX,currentY);
        
            var widthHalf = Math.abs(maxx - minx) / 2;
            var heightHalf = Math.abs(maxy - miny) / 2;
            newMinx = mapX - widthHalf;
            newMaxx = mapX + widthHalf;
            newMaxy = mapY + heightHalf;
            newMiny = mapY - heightHalf;
            addPercent(75);
            refreshMap(newMinx,newMiny,newMaxx,newMaxy);
        } else {
            zminx = getMapX(Math.min(startX,currentX));
            zmaxx = getMapX(Math.max(startX,currentX));
            zminy = getMapY(Math.max(startY,currentY));
            zmaxy = getMapY(Math.min(startY,currentY));
           percentX = (maxx-minx)/(zmaxx-zminx);
           percentY = (maxy-miny)/(zmaxy-zminy);
           percent = (percentX+percentY)/2;
				
            widthH = (maxx-minx)/2;
            heightH = (maxy-miny)/2;
            cx = zminx + widthH;
            cy = zminy + heightH;

            newMinx = cx - percent * widthH;
            newMiny = cy - percent * heightH;
            newMaxx = cx + percent * widthH;
            newMaxy = cy + percent * heightH;
            //alert(newMinx+", "+newMiny+", "+newMaxx+", "+newMaxy);
            refreshMap(newMinx,newMiny,newMaxx,newMaxy);
        }
        actionInProgress = false;
        document.getElementById(eventLayerId).onmousemove = doNothing;
        document.onmouseup = doNothing;

        return true;
    }
}

///////////////////////////////////////////////////////////////////////
// pan
///////////////////////////////////////////////////////////////////////

function setPanEvents() {
    document.getElementById(eventLayerId).onmousedown = panDown;
    document.getElementById(eventLayerId).style.cursor = "move";
    document.getElementById(eventLayerId).onmousemove = doNothing;
}

function panDown(e) {

    if (isLeftClick(e) || currentMode=="zoomBox") {
        getMapPixels(e);
        startX=currentX;
        startY=currentY
        currentX=startX+1;
        currentY=startY+1;
        actionInProgress = true;
		document.getElementById(eventLayerId).onmousemove = panMove;
		document.onmouseup = panUp;
        return false;
    }
}

function panMove(e) {

    if (actionInProgress) {
        getMapPixels(e);

        if (currentX >= 0 && currentX <= mapWidth  
            && currentY >= 0 && currentY <= mapHeight) {
        
            xMove = currentX-startX;
            yMove = currentY-startY;
            cLeft = -xMove;
            cTop = -yMove;
            cRight = mapWidth;
            cBottom = mapHeight;
            if (xMove>0) {
                cLeft = 0;
                cRight = mapWidth - xMove;
            }
            if (yMove>0) {
                cTop = 0;
                cBottom = mapHeight - yMove;
            }
            clipLayer(mapImageId,cLeft,cTop,cRight,cBottom);
            positionLayer(mapImageId,xMove,yMove,positionLayerId);
        }
        return false;
    }
}

function panUp(e) { 
    
    if (actionInProgress) {
        actionInProgress = false;

        // show the latest mouse move
        panMove(e);
        
        if ((Math.abs(currentX-startX) < 2) && (Math.abs(currentY-startY) < 2)) {
            // the pan move is very small, do a recenter
            getMapXY(currentX,currentY);
            var widthHalf = Math.abs(maxx - minx) / 2.0;
            var heightHalf = Math.abs(maxy - miny) / 2.0;
            newMinx = mapX - widthHalf;
            newMiny = mapY - heightHalf;
            newMaxx = mapX + widthHalf;
            newMaxy = mapY + heightHalf;

            refreshMap(newMinx,newMiny,newMaxx,newMaxy);

        } else {
    
            var width = Math.abs(maxx - minx);
            var height = Math.abs(maxy - miny);
            var xOffset = (width/mapWidth) * (currentX-startX);
            var yOffset = (height/mapHeight) * (startY-currentY);
            newMaxy = maxy - yOffset;
            newMaxx = maxx - xOffset;
            newMinx = minx - xOffset;
            newMiny = miny - yOffset;
            
            // otherwise the map jumps around
            //document.getElementById(mapImageName).src="images/other/transparent.gif";
			
			//for pan, we send new image to 'non-active mapimage,' and then hide active image
            if(activeMapImage==1){
				tempImgName=mapImageName;
				mapImageName=mapImageName2;
				mapImageName2=tempImgName;
				
				
				tempImgId=mapImageId;
				mapImageId=mapImageId2;
				mapImageId2=tempImgId;
				
				activeMapImage=2;
				}
			else{
				tempImgName=mapImageName;
				mapImageName=mapImageName2;
				mapImageName2=tempImgName;
				
				tempImgId=mapImageId;
				mapImageId=mapImageId2;
				mapImageId2=tempImgId;
				activeMapImage=1;
				}
            refreshMap(newMinx,newMiny,newMaxx,newMaxy);
        }
            
		document.getElementById(eventLayerId).onmousemove = doNothing;
		document.onmouseup = doNothing;
        return true;
    }
}

// fixed pan move
// from current extent move xPercent to the East and yPercent to the North
// negative percent values move to the West and the South
function pan(xPercent,yPercent) {

    // move %
    valueX = xPercent/100;
    valueY = yPercent/100;
    
    var width = Math.abs(maxx-minx);
    var height = Math.abs(maxy-miny);
    
    newMinx = minx + valueX*width;
    newMiny = miny + valueY*height;
    newMaxx = maxx + valueX*width;
    newMaxy = maxy + valueY*height;

    refreshMap(newMinx,newMiny,newMaxx,newMaxy);
}

///////////////////////////////////////////////////////////////////////
// custom map click
///////////////////////////////////////////////////////////////////////

function setCustomMapClickEvents() {
    document.getElementById(eventLayerId).onmousedown = customMapClickDown;
    document.getElementById(eventLayerId).onmousemove = null;
    document.onmouseup = null;
    if (navigator.appName.indexOf("Microsoft Internet Explorer")>=0)
        document.getElementById(eventLayerId).style.cursor = "hand";
    else
        document.getElementById(eventLayerId).style.cursor = "pointer";
}

function customMapClickDown(e) {

    if (isLeftClick(e)) {
        getMapPixels(e);
        getMapXY(currentX,currentY);
        customMapClick(mapX,mapY);
        return false;
    }
}

///////////////////////////////////////////////////////////////////////
// map helper methods
///////////////////////////////////////////////////////////////////////

function getMapPixels(e) {
    if (navigator.appName.indexOf("Netscape")>=0) {
        currentX=e.pageX;
        currentY=e.pageY;
    } else {
        currentX=event.clientX + document.body.scrollLeft;
        currentY=event.clientY + document.body.scrollTop;
    }
    // subtract offsets from page left and top
    var layerPositioner = document.getElementById(positionLayerId);
    currentX = currentX-getAbsX(layerPositioner);
    currentY = currentY-getAbsY(layerPositioner);
    
    // we don't want to go outside our defined area
    currentX = Math.max(0,currentX);
    currentX = Math.min(currentX,mapWidth);
    currentY = Math.max(0,currentY);
    currentY = Math.min(currentY,mapHeight);
}

// convert mouse click xy's into map coordinates
function getMapXY(xIn,yIn) {
    var unitsPerPixelX = (maxx-minx) / mapWidth;
    mapX = unitsPerPixelX * xIn + minx;
    var unitsPerPixelY = (maxy-miny) / mapHeight;
    mapY = unitsPerPixelY * (mapHeight - yIn) + miny;
}

// returns true if event was caused by a left mouse click
function isLeftClick(e) {
    var leftClick = false;
    if (e && e.which == 1) { // Netscape
        leftClick = true;
    } else if (event && event.button == 1) { // IE
        leftClick = true;
    }
    return leftClick;
}

// get the pixel coordinates over the image
function addPercent(value) {

    // add %
    value = 1 + value/100;
    
    var width = Math.abs(newMaxx-newMinx);
    var height = Math.abs(newMaxy-newMiny);
    var cx = newMinx + width/2;
    var cy = newMiny + height/2;
    
    newMinx = cx - width*value/2;
    newMiny = cy - height*value/2;
    newMaxx = cx + width*value/2;
    newMaxy = cy + height*value/2;
}
function getMapX(x) {
	var pixelX = (maxx-minx) / mapWidth;
	var cx = Math.round((pixelX * x + minx)*d)/d;
	return cx;
}

function getMapY(y) {
	var pixelY = (maxy-miny) / mapHeight;
	var cy = Math.round((pixelY * (mapHeight - y) + miny)*d)/d;
	return cy;
}

function getPixelX(cx) {
	var pixelX = (maxx-minx) / mapWidth;
	var x = Math.round((cx-minx)/pixelX);
	return x;
}

function getPixelY(cy) {
	var pixelY = (maxy-miny) / mapHeight;
	var y = mapHeight - Math.round((cy-miny)/pixelY);
	return y;
}


///////////////////////////////////////////////////////////////////////
// locator map
///////////////////////////////////////////////////////////////////////

function setLocatorMapEvents() {
    document.getElementById("theLocatorMap").onmousedown = locatorDown;
    if (navigator.appName.indexOf("Microsoft Internet Explorer")>=0)
        document.getElementById("theLocatorMap").style.cursor = "hand";
    else
        document.getElementById("theLocatorMap").style.cursor = "pointer";
}

function locatorDown(e) {
    getLocatorPixels(e);
    getLocatorMapXY(currentX,currentY);

    var widthHalf = Math.abs(maxx - minx) / 2;
    var heightHalf = Math.abs(maxy - miny) / 2;
    newMinx = locatorMapX - widthHalf;
    newMiny = locatorMapY - heightHalf;
    newMaxx = locatorMapX + widthHalf;
    newMaxy = locatorMapY + heightHalf;

    refreshMap(newMinx,newMiny,newMaxx,newMaxy); 
}

function getLocatorPixels(e) {
    if (navigator.appName.indexOf("Netscape")>=0) {
        currentX=e.pageX;
        currentY=e.pageY;
    } else {
        currentX=event.clientX + document.body.scrollLeft;
        currentY=event.clientY + document.body.scrollTop;
    }
    // subtract offsets from page left and top
    var locatorLayerPositioner = document.getElementById(positionLayerId);
    currentX = currentX-getAbsX(locatorLayerPositioner);
    currentY = currentY-getAbsY(locatorLayerPositioner);
}

// convert mouse click xy's into locator coordinates
function getLocatorMapXY(xIn,yIn) {

    if (themeId.substring(0,1) != 'p') {
        filename = document.getElementById(positionLayerId).src;
        pos = filename.indexOf("_")
        name = filename.substring(pos+1,filename.indexOf(".",pos));
        locatorMinX = 0;
        locatorMinY = 0;
        locatorMaxX = 0;
        locatorMaxY = 0;
        for (i = 0; i < locatorExtents.length; i++) {
            if (locatorExtents[i][4] == name) {
                locatorMinX = locatorExtents[i][0];
                locatorMinY = locatorExtents[i][1];
                locatorMaxX = locatorExtents[i][2];
                locatorMaxY = locatorExtents[i][3];
                break;
            }
        }
    }
        
    var unitsPerPixelX = (locatorMaxX-locatorMinX) / locatorWidth;
    locatorMapX = unitsPerPixelX * xIn + locatorMinX;
    var unitsPerPixelY = (locatorMaxY-locatorMinY) / locatorHeight;
    locatorMapY = unitsPerPixelY * (locatorHeight - yIn) + locatorMinY;
}

// draw extent box on locator map
function drawExtentBox() {
    
    pixelWidth = document.getElementById(positionLayerId).width;
    pixelHeight = document.getElementById(positionLayerId).height;

    areaMap = (maxx-minx)*(maxy-miny);
    areaFullMap = (locatorMaxX-locatorMinX)*(locatorMaxY-locatorMinY);
     
    if (areaMap < areaFullMap) {
        ratio = (Math.max(Math.min(minx,locatorMaxX),locatorMinX) - locatorMinX) / (locatorMaxX-locatorMinX);
        startX = Math.round(ratio * pixelWidth);
        ratio = (Math.max(Math.min(miny,locatorMaxY),locatorMinY) - locatorMinY) / (locatorMaxY-locatorMinY);
        startY = pixelHeight - Math.round(ratio * pixelHeight);
        ratio = (Math.max(Math.min(maxx,locatorMaxX),locatorMinX) - locatorMinX) / (locatorMaxX-locatorMinX);
        endX = Math.round(ratio * pixelWidth);
        ratio = (Math.max(Math.min(maxy,locatorMaxY),locatorMinY) - locatorMinY) / (locatorMaxY-locatorMinY);
        endY = pixelHeight - Math.round(ratio * pixelHeight);
        
        if (endX-startX < 4) {
            centerX = Math.round((endX+startX)/2);
            startX = centerX-2;
            endX = centerX+2;
        }
        if (startY-endY < 4) {
            centerY = Math.round((startY+endY)/2);
            endY = centerY-2;
            startY = centerY+2;
        }
        
        if (startX < 0) startX = 0;
        if (startX > pixelWidth) startX = pixelWidth;
        if (startY < 0) startY = 0;
        if (startY > pixelHeight) startY = pixelHeight;
        if (endX < 0) endX = 0;
        if (endX > pixelWidth) endX = pixelWidth;
        if (endY < 0) endY = 0;
        if (endY > pixelHeight) endY = pixelHeight;
    
        showLayer("theLocatorMap");
        if (startX > 0 || startY < pixelHeight || endX < pixelWidth || endY > 0) {
            clipBox(extentBoxId, startX, startY, endX, endY, positionLayerId);
             showLayer(extentBoxId);
        } else {
             hideLayer(extentBoxId);
        }
    } else {
        showLayer("theLocatorMap");
        hideLayer(extentBoxId);
    }
}
///////////////////////////////////////////////////////////////////////
// click a point
///////////////////////////////////////////////////////////////////////

function setPointEvents() {
    document.getElementById(eventLayerId).onmousedown = pointDown;
    document.getElementById(eventLayerId).onmousemove = pointMove;
    document.onmouseup = doNothing;
    document.getElementById(eventLayerId).style.cursor = "crosshair";
}

function pointMove(e) {
        getMapPixels(e);
        getMapXY(currentX,currentY);
        writeXY(mapX, mapY);
}

function pointDown(e) {

    if (isLeftClick(e)) {
        getMapPixels(e);
        getMapXY(currentX,currentY);
        if (setPointXY(mapX, mapY)) {
            clipBox(pointPositionLayerId, currentX-5, currentY-5, currentX+5, currentY+5, positionLayerId);
            showLayer(pointPositionLayerId);
            return true;
        } else 
            return false;
    }
}

///////////////////////////////////////////////////////////////////////
// zoom to feature
///////////////////////////////////////////////////////////////////////


