var theX, theY;
var x1, y1, x2, y2;
var zleft,zright,ztop,zbottom;
var mtop,mleft,mwidth,mheight,mborder;
var mapDivId;
var activeTool;
var dragging = false;
var panning = false;
var waitingForResponse = false;

function setMapDivProperties(top, left, width, height, border, mapId) {
	//mtop = top+border;
	//mleft = left+border;
	//mwidth = width-(2*border);
	//mheight = height-(2*border);
	mtop = top-(2*border);
	mleft = left-(2*border);
	mwidth = width-(2*border);
	mheight = height-(2*border);
	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:500px; height:460px; 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";
					document.images["idTool"].src = "images/identify.gif";
					//document.images["XTool"].src = "images/palantir.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;
						case "identify":
							document.images["idTool"].src = "images/identifyD.gif";
							break;
										 
						
						default:
					}
				}
	}

function setActiveTool(tool){

  		activeTool = tool;
  		
  		var layer = document.getElementById(mapDivId);
  		if(activeTool == "zoomin" || activeTool == "zoomout") {
  		        selectXL = false;
        		layer.onmousedown= startDragging;
    			layer.onclick = null;
    			layer.style.cursor = "crosshair";
    		}
     		else if(activeTool == "pan") {
     		        selectXL = false;
        		layer.onmousedown= startMapDragging;
     			layer.onclick = null;
     			layer.style.cursor = "move";
     		}
    		else if(activeTool == "identify") {
    		        selectXL = false;
    			layer.onmousedown= pointClick;
    			layer.onclick= null;
    			layer.style.cursor = "crosshair";
    		}
    		
    		else if(activeTool == "xlookselect") {
    			layer.onmousedown= pointClickXL;
    			layer.onclick= null;
    			layer.style.cursor = "crosshair";
    			loadXL();
    		}    		
    		
    		
    		 
	}

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;
		var layer = document.getElementById(mapDivId);
 
		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 pointClickXL(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) {
		getXY(e);
		x2 = theX;
		y2 = theY;
		
		if (x2 < mleft) x2 = mleft;
		if (x2 > mleft + mwidth) x2 = mleft + mwidth;
		if (y2 < mtop) y2 = mtop;
		if (y2 > mtop + mheight) y2 = mtop + mheight; 
		var dx = x2-x1;
		var dy = y2-y1;
				
		var cLeft = - dx;
		var cTop = - dy;
		var cRight = mwidth;
		var cBottom = mheight;
		if (dx>0) {
			cLeft = 0;
			cRight = mwidth - dx;
		}
		if (dy>0) {
			cTop = 0;
			cBottom = mheight - dy;
		}
	
		moveLayer(mapDivId,dx ,dy);
		panClipLayer(mapDivId,cLeft,cTop,cRight,cBottom);
		
	}
	return false;
}

function stopDragging(e){
	if (dragging) {
		dragging = false;
		
		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);
			
		
		thispt = convertPixelToMap(theX, theY);
	//alert(top.parent.document.newpoint[0] + "  " + document.newpoint[1]);
				
		//alert(thispt[0] + "    " + thispt[1]);

		document.onmousemove = null;
		document.onmouseup = null;
		
		var ixOffset = x1-x2;
		var iyOffset = y2-y1;
		document.getElementById('theImage').onload = resetAfterPan;
		pan(ixOffset, iyOffset);
		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) {
	  var layer;
	    document.writeln('<div id="' + name + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px;' + '; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
	    document.writeln(content);
	    document.writeln('</div>');
}

function createMapContainer(name, content, itop, ileft, iwidth, iheight, border, mapclass){
	if(!isIE){
		iwidth = iwidth - (2*border);
		iheight = iheight - (2*border);
	}
	document.writeln('<div id="' + name + '" class="' + mapclass + '" style="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 createXLooKContainer(name, content, itop, ileft, iwidth, iheight, border, mapclass){

	//var xleft;
	//xleft == ileft + 600;

	if(!isIE){
		iwidth = iwidth - (2*border);
		iheight = iheight - (2*border);
	}
	document.writeln('<div id="' + name + '" class="' + mapclass + '" style="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;
		layer.width	= newWidth;
		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;
	
}


