
var pixelTolerance= 4;
var idMinX, idMinY, idMaxX, idMaxY;
var idCount = 0;
var selectedId = 1;



var theX, theY;
var x1, y1, x2, y2;
var zleft,zright,ztop,zbottom;
var mtop,mleft,mwidth,mheight,mbroder;
var mapDivId;
var activeTool;
var dragging = false;
var panning = false;
var waitingForResponse = false;
var tempo=new Array();
function setMapDivProperties(top, left, width, height, border, mapId) {
	mtop = top+border;
	mleft = left+border;
	mwidth = width-(2*border);
	mheight = height-(2*border);
	//alert(mwidth);
	//alert(mheight);
	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;
			
		}
		 //alert("Id>>" + id);
		var imgObj = document.getElementById(id);
		if(imgObj != null)
			imgObj.src = imgsrc;
	}
	
	function highlightTool(tool){
				if ((tool!=null) && (tool!=""))
				{
										
					document.images["zoomInTool"].src = "images/extent.gif";
					//document.images["zoomOutTool"].src = "images/zoomout.gif";
					document.images["panTool"].src = "images/pan.gif";
					//document.images["idTool"].src = "images/identify.gif";
					//document.images["zoomRectTool"].src = "images/pointer.gif"; // Added By Srikanta
					
					switch (tool)
					{
						case "zoomin":
							document.images["zoomInTool"].src = "images/extent.gif";
							break;
						/*case "zoomout":
							document.images["zoomOutTool"].src = "images/zoomoutD.gif";
							break;*/
						case "pan":
							document.images["panTool"].src = "images/pan.gif";
							break;
						/*case "identify":
							document.images["idTool"].src = "images/identifyD.gif";
							break;
						 
						case "ZoomRect":
							document.images["zoomRectTool"].src = "images/pointerD.gif";
							break;*/
						
						
						default:
					}
				}
	}

function setActiveTool(tool){

  		activeTool = tool;
  		
  		var layer = document.getElementById(mapDivId);
  		if(activeTool == "zoomin" || activeTool == "zoomout" || activeTool == "ZoomRect") {
  		
        		layer.onmousedown= startDragging;
    			layer.onclick = null;
    			layer.style.cursor = "crosshair";
    		}
     		else if(activeTool == "pan") {
        		layer.onmousedown= startMapDragging;
     			layer.onclick = null;
     			layer.style.cursor = "move";
     		}
    		else if(activeTool == "identify") {
    			layer.onmousedown= pointClick;
    			layer.onclick= null;
    			layer.style.cursor = "crosshair";
    		}
    		 
	}

function startDragging(e){
	panning=false;
	
	if(!dragging){
		dragging = true;
		getXY(e);
		x1=theX;
		y1=theY;
		x2=x1+1;
		y2=y1+1;
		var pointsXY=identify(theX- mleft,theY- mtop);
		window.status="Start co-ordinate   X=" + pointsXY[0] + "  Y=" + pointsXY[1];
		startX=pointsXY[0];
		startY=pointsXY[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) {
        //alert("point click");
	getXY(e);
	var x1 = theX - mleft;
	var y1 = theY - mtop;
	//alert("X1==" + x1 + " Y1==" + y1);
	var pointsXY=identify(x1, y1);
	//window.status="Identify   X=" + pointsXY[0] + "  Y=" + pointsXY[1];
	
	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 ;
			//inside = false;
		}
		if (y2 < mtop ) {
			y2 = mtop;
			//inside = false;	
		}
		if (y2 > mtop + mheight) {
			y2 = mtop  ;
			//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;
		
		var pointsXY=identify(theX-mleft,theY- mtop);
		
		endX=pointsXY[0];
	        endY=pointsXY[1];
		
		window.status="[(" + startX + "," + startY + "),("+ endX + "," +endY + ")]";
		window.parent.tempcord="[(" + startX + "," + startY + "),("+ endX + "," +endY + ")]";
		//alert(window.parent.tempcord);
		zoom(zleft,zbottom,zright,ztop);
		return false;
	}
	
}





function stopMapDragging(e){
	if (panning) {
		panning = false;
		
		getXY(e);
		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="border:none;position:absolute; overflow:hidden; left:' + ileft + 'px; top:' + itop + 'px; width:' + iwidth + 'px; height:' + iheight + 'px;' + ';visibility:visible;">');
	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;
	}
	//window.status="X==" + theX + " " + "Y==" + theY;
	 //tempo=convertPixelToMap(theX-2,theY-2);
	// window.status="X==" + tempo[0] + " " + "Y==" + tempo[1];
	
	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;
}

/*function identify(x1, y1){
	
	var idMins = convertPixelToMap(x1-(pixelTolerance/2), y1+(pixelTolerance/2));
	var idMaxs = convertPixelToMap(x1+(pixelTolerance/2), y1-(pixelTolerance/2));
	idMinX = idMins[0];
	idMinY = idMins[1];
	idMaxX = idMaxs[0];
	idMaxY = idMaxs[1];
	//selectedId = 1;
	//sendIdRequest(true); 
}

function convertPixelToMap(px, py){
	var dx = (maxx - minx)/mwidth;
	var mx = minx + (dx * px);
	var my = miny + (dx * py);
	//alert("mx>>>>>>>= "+mx);
	//alert("my>>>>>>>>= "+my);
	//window.status="mx==" + mx + "  my==" + my;
	var newpoint = new Array();
	newpoint[0] = mx;
	newpoint[1] = my;
	return newpoint;

}
*/

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;
	
}


