
function setMapDivProperties(top, left, width, height, border, mapId) {
	mtop = top+border;
	mleft = left+border;
	mwidth = width-(2*border);
	mheight = height-(2*border);
	mborder = border;
	mapDivId = mapId;
}

function createZoomBoxDivs() {
	createLayer("zoombox_top",mleft,mtop,mwidth,mheight,false,"");
	createLayer("zoombox_bottom",mleft,mtop,mwidth,mheight,false,"");
	createLayer("zoombox_left",mleft,mtop,mwidth,mheight,false,"");
	createLayer("zoombox_right",mleft,mtop,mwidth,mheight,false,"");
	/*
    setLayerBackgroundColor("zoomboxTop","blue");
	setLayerBackgroundColor("zoomboxBottom","blue");
	setLayerBackgroundColor("zoomboxLeft","blue");
	setLayerBackgroundColor("zoomboxRight","blue");
	*/
}

function modifyZoomBoxDivs(){
    modifyLayer('zoombox_top',mleft,mtop,mwidth-20,mheight-20,false,"");
	modifyLayer('zoombox_bottom',mleft,mtop,mwidth-20,mheight-20,false,"");
	modifyLayer('zoombox_left',mleft,mtop,mwidth-20,mheight-20,false,"");
	modifyLayer('zoombox_right',mleft,mtop,mwidth-20,mheight-20,false,"");
}

function createIdResultDiv() {
    return;
}

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";
					
					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 = $(mapDivId);
	if(activeTool == "zoomin" || activeTool == "zoomout") {
		lat_long_display();
		debug("zoom - activated");
		layer.onmousedown = startDragging;
		layer.onclick = null;
		debug("assign-onclick");
		layer.style.cursor = "crosshair";
	}
	else if(activeTool == "pan") {
		debug("pan - activated");
		layer.onmousedown= startMapDragging;
		layer.onclick = null;
		layer.style.cursor = "move";
	}
	else if(activeTool == "identify") {
		layer.onmousedown= pointClick;
		layer.onclick= null;
		layer.style.cursor = "crosshair";
	}
	else if(activeTool == "print") {
		// get current settings to create a print for 8.5 x 11 (portrait first)
		debug("EM Print");
		em_display_dialog_print();
	}
	else if (activeTool == "hotspot") {
		debug("HotSpots");
		//em_testGetPoints();
		em_getPoints();
	} else if (activeTool == "usr_point_add") {
		bMouseHelp_prev = $('fld_lat_lon_visible').checked;
		$('fld_lat_lon_visible').checked = true;
    	Element.removeClassName('mouse_help', 'hide');
    	Element.addClassName('mouse_help','show');
		layer.onmousedown=usr_point_click_select;
		layer.onclick=null;
		layer.style.cursor = "crosshair";
	} else {
		// no tool
		layer.onmousedown=null;
		layer.onclick=null;
		layer.style.cursor="default";
	}
}

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) {
		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
		info("Z values ---");
		
		debug("zleft="+zleft);
		zleft -= mleft;
		debug('zleft('+zleft+') -= mleft('+mleft+')');
		
		debug('zright='+zright);
		zright -= mleft;
		debug('zright('+zright+') -= mleft('+mleft+')');
		
		debug('zbottom='+zbottom);
		zbottom -= mtop;
		debug('zbottom('+zbottom+') -= mtop('+mtop+')');
		
		debug('ztop='+ztop);
		ztop -= mtop;
		debug('ztop('+ztop+') -= mtop('+mtop+')');
		/*if (activeTool == "zoomout") {
			alert('ZoomOut > '+'zleft='+zleft+',zleft('+zleft+') -= mleft('+mleft+'), '
			+'zright='+zright+',zright('+zright+') -= mleft('+mleft+'), '
			+'zbottom='+zbottom+',zbottom('+zbottom+') -= mtop('+mtop+'), '
			+'ztop='+ztop+',ztop('+ztop+') -= mtop('+mtop+')');
		}*/
		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>');
}

/* added 20060120 jknab@jreen.com */
function modifyLayer (name, inleft, intop, width, height, visible, content) {
    // $(name)
	  $(name).style.top = intop;
	  $(name).style.left = inleft;
	  $(name).style.width = width;
	  $(name).style.height = height;
	  $(name).style.zIndex = 33;
	  $(name).style.visibility = (visible ? show : hide);
	  $(name).style.overflow = 'hidden';
	  $(name).style.position = 'absolute';
	  updateContent(name, content);
}

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 getLayer(name) {
	if ($(name)!=null) {
		return $(name).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('zoombox_top',left,top,right,bottom);
	clipLayer("zoombox_left",left,top,right,bottom);
	clipLayer("zoombox_right",left,top,right,bottom);
	clipLayer("zoombox_bottom",left,top,right,bottom);
	showLayer("zoombox_top");
	showLayer("zoombox_left");
	showLayer("zoombox_right");
	showLayer("zoombox_bottom");
}
function hideZoomBox()
{
	window.scrollTo(0,0);
	hideLayer("zoombox_top");
	hideLayer("zoombox_left");
	hideLayer("zoombox_right");
	hideLayer("zoombox_bottom");
}

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 = show;
}

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("zoombox_top",zleft,ztop,zright,ztop+lSize);
		clipLayer("zoombox_left",zleft,ztop,zleft+lSize,zbottom);
		clipLayer("zoombox_right",zright-lSize,ztop,zright,zbottom);
		clipLayer("zoombox_bottom",zleft,zbottom-lSize,zright,zbottom);
	}
	return false;
}

var isMoving = false;
var idx, idy;
var idLayer;

function startMove(e, divId) {
	idLayer = $(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;
	
}