var theX, theY;
var x1, y1, x2, y2;
var zleft,zright,ztop,zbottom;
var mtop,mleft,mbroder,mwidth,mheight;
var mapDivId = 'mapArea';
var activeTool;
var dragging = false;
var panning = false;
var waitingForResponse = false;

var dragcount = 0;
var dragthrottle = 1;
var width = 0;
var height = 0;

var dragged = false;

function init_vars()
{
        var ms = document.getElementById('map_space');
        var top = ms.style.top.substring(0, ms.style.top.length-2);
        var left = ms.style.left.substring(0, ms.style.left.length-2);
        width = ms.style.width.substring(0, ms.style.width.length-2);
        height = ms.style.height.substring(0, ms.style.height.length-2);

        var ma = document.getElementById('mapArea');
        ma.style.width = width + 'px';
        ma.style.height = height + 'px';
        ma.style.left = left + 'px';
        ma.style.top = top + 'px';

        var mc = document.getElementById('mapContainer');
        mc.style.width = ma.style.width;
        mc.style.height = ma.style.height;
        mc.style.left = ma.style.left;
        mc.style.top = ma.style.top;

        setMapDivProperties(top,left,width,height,1);

        createZoomBoxDivs();
        createIdResultDiv();
        setActiveTool('zoomin');
        highlightTool('zoomin');
}

function setMapDivProperties(top, left, width, height, border, mapId) {
	mtop = parseInt(top)+border;
	mleft = parseInt(left)+border;
	mwidth = width;
	mheight = height;
	mborder = border;
}

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 jumpToPoint(pnt) {
        if (pnt.length == 0) { return; }

        var lat_lon = pnt.split("|");

        var centery = lat_lon[0];
        var centerx = lat_lon[1];

        minx = centerx;
        miny = centery;
        maxx = centerx;
        maxy = centery;
        fixedzoom = "true";
        scale = "13";
        getMap("");
}

function jumpToPointScale(pnt, s) {
        if (pnt.length == 0) { return; }

        var lat_lon = pnt.split("|");

        var centery = lat_lon[0];
        var centerx = lat_lon[1];

        minx = centerx;
        miny = centery;
        maxx = centerx;
        maxy = centery;
        fixedzoom = "true";
        scale = s;
        getMap("");
}

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 = "/mapping_v2006/jsps/COMMON/ts_us/images/zoom_1.gif";
                document.images["panTool"].src = "/mapping_v2006/jsps/COMMON/ts_us/images/pan_1.gif";
                                        
                switch (tool)
                {
                        case "zoomin":
                                document.images["zoomInTool"].src = "/mapping_v2006/jsps/COMMON/ts_us/images/zoom_2.gif";
                                break;
                        case "pan":
                                document.images["panTool"].src = "/mapping_v2006/jsps/COMMON/ts_us/images/pan_2.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;	
			layer.ondblclick = doubleClickPan;

			if (isIE) {
				layer.style.cursor = "hand";
			} else {
     				layer.style.cursor = "pointer";
			}
     		}
    		 
}

function doubleClickPan(e) {
        getXY(e);
        if (parseInt(scale) < max_scale) {
                scale = parseInt(scale) + 1;
                var x = theX - mleft;
                var y = theY - mtop;

                var centerx = parseFloat(minx) + parseFloat(x * resolution);
                var centery = parseFloat(maxy) - parseFloat(y * resolution);
                minx = centerx - parseFloat(mwidth * resolution);
                miny = centery - parseFloat(mheight * resolution);
                maxx = centerx + parseFloat(mwidth * resolution);
                maxy = centery + parseFloat(mheight * resolution);
                fixedzoom = "true";
                getMap("");
        } else {
                alert("Currently at closest zoom " + scale);
        }
        return false;

}

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 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) {
		dragged = true;
                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;
		if (dragged) {
			dragged = false;                        
        	        getMap("");
		}
                return false;
        }
}

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:10;' + '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;
	
}


