var theX, theY;
var x1, y1, x2, y2;
var zleft,zright,ztop,zbottom;
var mtop,mleft,mbroder,mwidth,mheight;
var mapDivId;
var activeTool;
var dragging = false;
var panning = false;
var waitingForResponse = false;

var dragcount = 0;
var dragthrottle = 1;

function setMapDivProperties(top, left, width, height, border, mapId) {
	mtop = top+border;
	mleft = left+border;
	mwidth = width;
	mheight = height;
	mborder = border;
	mapDivId = mapId;
}

function createZoomBoxDivs(){
	createLayer("zoomboxTop",mleft,mtop,mwidth,mheight,false,"");
	createLayer("zoomboxBottom",mleft,mtop,mwidth,mheight,false,"");
	createLayer("zoomboxLeft",mleft,mtop,mwidth,mheight,false,"");
	createLayer("zoomboxRight",mleft,mtop,mwidth,mheight,false,"");
	setLayerBackgroundColor("zoomboxTop","blue");
	setLayerBackgroundColor("zoomboxBottom","blue");
	setLayerBackgroundColor("zoomboxLeft","blue");
	setLayerBackgroundColor("zoomboxRight","blue");
}

function createIdResultDiv(){
	  document.writeln('<div id="IdResult" style="position:absolute; overflow:auto; left:0px; top:0px; width:700px; height:700px; background-color:White; border-style:ridge;visibility:hidden;">');
	  document.writeln('</div>');
		
}

function ButtonOut(id, tool, flatimage, downimage)
	{
		var imgsrc = flatimage;
		if (activeTool == tool)
		{
			imgsrc = downimage;
		}
		var imgObj = document.getElementById(id);
		if(imgObj != null)
			imgObj.src = imgsrc;
	}
	
	function highlightTool(tool){

				if ((tool!=null) && (tool!=""))
				{
					document.images["zoomInTool"].src = "images/zoomin.gif";
					//document.images["zoomOutTool"].src = "images/zoomout.gif";
					document.images["panTool"].src = "images/pan.gif";
					
					switch (tool)
					{
						case "zoomin":
					//		document.images["zoomInTool"].src = "images/zoominD.gif";
							break;
						case "zoomout":
					//		document.images["zoomOutTool"].src = "images/zoomoutD.gif";
							break;
						case "pan":
							document.images["panTool"].src = "images/panD.gif";
							break;
						 
						
						default:
					}
				}
	}

function setActiveTool(tool){

  		activeTool = tool;
  		
  		var layer = document.getElementById(mapDivId);
  		if(activeTool == "zoomin" || activeTool == "zoomout") {
        		layer.onmousedown = startDragging;
    			layer.onclick = null;
    			layer.style.cursor = "crosshair";
    		}
     		else if(activeTool == "pan") {
        		layer.onmousedown= startMapDragging;
     			layer.onclick = null;
			if (isIE) {
				layer.style.cursor = "hand";
			} else {
     				layer.style.cursor = "pointer";
			}
     		}
    		 
}
function startDragging(e){
	panning=false;
	if(!dragging){
		dragging = true;
		getXY(e);
		x1=theX;
		y1=theY;
		x2=x1+1;
		y2=y1+1;
		
		showZoomBox(x1,y1,x2,y2);	
		
		document.onmousemove = updateDragging;
		document.onmouseup = stopDragging;
		
	}
	return false;
}

function startMapDragging(e) {
        dragging = false;
        if (!panning) {
                panning = true;
                getXY(e);
                x1=theX;
                y1=theY;
                x2=x1+1;
                y2=y1+1;
                document.onmousemove = updateMapDragging;
                document.onmouseup = stopMapDragging;
        
        }            
        return false;
}

function pointClick(e) {
	getXY(e);
	var x1 = theX - mleft;
	var y1 = theY - mtop;
	identify(x1, y1);
	return false;
}

function updateDragging(e){
	if (dragging) {
		getXY(e);
		x2=theX;
		y2=theY;
		//var inside = true;
		if (x2 < mleft) {
			x2 = mleft;
			//inside = false;
		}
		if (x2 >  mleft + mwidth ) {
			x2 = mleft + mwidth ;
			//inside = false;
		}
		if (y2 < mtop ) {
			y2 = mtop;
			//inside = false;	
		}
		if (y2 > mtop + mheight) {
			y2 = mtop +  mheight ;
			//inside = false;
		}
		 setClip();
		
	}
	return false
	

}


function updateMapDragging(e) {

        if (panning) {

                dragcount++;

                if (dragcount > dragthrottle) {
                getXY(e);
                x2 = theX;
                y2 = theY;
                var dx = parseInt(x2 - x1);
                var dy = parseInt(y2 - y1);

                minx = parseFloat(minx) - parseFloat(dx * resolution);
                maxy = parseFloat(maxy) + parseFloat(dy * resolution);

                x1 = x2;
                y1 = y2;
	
		dragObjects(dx, dy);

                dragcount=0;
                }
		return false;
        }
}

function stopDragging(e){
	if (dragging) {
		dragging = false;
		scale = "";

		getXY(e);
		document.onmousemove = null;
		document.onmouseup = null;
		hideZoomBox();
		setClip();
		// adjust for offsets
		zleft -= mleft;
		zright -= mleft;
		zbottom -= mtop;
		ztop -= mtop;
		zoom(zleft,zbottom,zright,ztop);
		return false;
	}
}

function stopMapDragging(e){
        if (panning) {
                panning = false;
                getXY(e);
                document.onmousemove = null;
                document.onmouseup = null;
                getMap("");
                return false;
        }
}

function resetAfterPan(){
	moveLayer(mapDivId,0,0);
	clipLayer2(mapDivId,0,0,mwidth,mheight);
	document.getElementById('theImage').onload = null;
}

function createLayer(name, inleft, intop, width, height, visible, content) {
	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;z-index:3;' + 'visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	    document.writeln(content);
	  document.writeln('</div>');
}

function createMapContainer(name, content, itop, ileft, iwidth, iheight, border, mapclass){
	
	document.writeln('<div id="' + name + '" class="' + mapclass + '" style="z-index:2;position:absolute; overflow:hidden; left:' + ileft + 'px; top:' + itop + 'px; width:' + iwidth + 'px; height:' + iheight + 'px;visibility:visible;border-width:' + border +'">');

	document.writeln(content);

	document.writeln('</div>');
}

function getLayer(name) {
	var theObj = document.getElementById(name);
	if (theObj!=null) {
		return theObj.style
	 }  else {
	    return(null);
	 }
}
		

function isVisible(name) {
	  var layer = getLayer(name);
	  if (isNav && layer.visibility == "show")
	    return(true);
	  if (isIE && layer.visibility == "visible")
	    return(true);
	  return(false);
}

function getXY(e) {
	
	if (isNav) {
		theX=e.pageX;
		theY=e.pageY;
	} else {
		theX=event.clientX + document.body.scrollLeft;
		theY=event.clientY + document.body.scrollTop;
	}

	return false;
}

function showZoomBox(left, top, right, bottom){
	clipLayer("zoomboxTop",left,top,right,bottom);
	clipLayer("zoomboxLeft",left,top,right,bottom);
	clipLayer("zoomboxRight",left,top,right,bottom);
	clipLayer("zoomboxBottom",left,top,right,bottom);
	showLayer("zoomboxTop");
	showLayer("zoomboxLeft");
	showLayer("zoomboxRight");
	showLayer("zoomboxBottom");
}

function hideZoomBox()
{
	window.scrollTo(0,0);
	hideLayer("zoomboxTop");
	hideLayer("zoomboxLeft");
	hideLayer("zoomboxRight");
	hideLayer("zoomboxBottom");
	
}


function moveLayer(name, x, y) {		
  	var layer = getLayer(name);		
    layer.left = x + "px";
   	layer.top  = y + "px";
}


function setLayerBackgroundColor(name, color) {		
  	var layer = getLayer(name);		
	layer.backgroundColor = color;
}


function hideLayer(name) {		
  	var layer = getLayer(name);		
  	layer.visibility = "hidden";
}


function showLayer(name) {		
  	var layer = getLayer(name);		
  	layer.visibility = "visible";
}


function clipLayer2(name, clipleft, cliptop, clipright, clipbottom) {		
	  var layer = getLayer(name);		
	  layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
}

function clipLayer(name, clipleft, cliptop, clipright, clipbottom) {		

	var layer = getLayer(name);

	var newWidth = clipright - clipleft;
	var newHeight = clipbottom - cliptop;
	layer.height = newHeight + "px";
	layer.width	= newWidth + "px";
	layer.top	= cliptop  + "px";
	layer.left	= clipleft + "px";

}

function panClipLayer(name, clipleft, cliptop, clipright, clipbottom) {		
	var layer = getLayer(name);
	if (layer!=null) {
		layer.clip = 'rect(' + cliptop + ' ' +  clipright + ' ' + clipbottom + ' ' + clipleft +')';
	} 
	return false;
}
function setClip() {
	var inSize = 3;
	var lSize = parseInt(inSize) - 1;
	if (lSize < 1) lSize = 1;
	var tempX=x1;
	var tempY=y1;
	if (x1>x2) {
		zright=x1;
		zleft=x2;
	} else {
		zleft=x1;
		zright=x2;
	}
	if (y1>y2) {
		zbottom=y1;
		ztop=y2;
	} else {
		ztop=y1;
		zbottom=y2;
	}
	if ((x1 != x2) && (y1 != y2)) {
		clipLayer("zoomboxTop",zleft,ztop,zright,ztop+lSize);
		clipLayer("zoomboxLeft",zleft,ztop,zleft+lSize,zbottom);
		clipLayer("zoomboxRight",zright-lSize,ztop,zright,zbottom);
		clipLayer("zoomboxBottom",zleft,zbottom-lSize,zright,zbottom);
	}
	return false;
}

var isMoving = false;
var idx, idy;
var idLayer;

function startMove(e, divId) {
	idLayer = document.getElementById(divId);
	isMoving = true;
	getXY(e);
	idx = theX - idLayer.offsetLeft;
	idy = theY - idLayer.offsetTop;
	document.onmousemove = updateMove;
	document.onmouseup = stopMove;
}

function updateMove(e) {
	if(!isMoving || (idLayer == null)) return;
	getXY(e);
	idLayer.style.left = theX - idx;
	idLayer.style.top = theY - idy;
	if (idLayer.offsetLeft < 0)
		idLayer.style.left = 0;
	if (idLayer.offsetTop < 0)
		idLayer.style.top = 0;
}

function stopMove() {
	if(!isMoving || (idLayer == null)) return;
	idLeft = idLayer.offsetLeft;
	idTop = idLayer.offsetTop;
	isMoving = false;
	idLayer = null;
	
}

