var gProtocol;
var gServer;
var gInstance;
var gSession;
var gDrawType;
var gWidth;
var gHeight;
var activeTool;
var maptipX;
var maptipY;
var maptips = "Off";
var activeTool;
var lastTool;
var zoomActive = 0;

function initializeMap(protocol, server, instance, session, drawtype, width, height) {
  gProtocol = protocol;
  gServer = server;
  gInstance = instance;
  gSession = session;
  gDrawType = drawtype;
  gWidth = width;
  gHeight = height;
}

function setActiveTool(tool, obj) {
	if (tool == activeTool) { return }
	activeTool = tool;
	obj.className = "iconSelect";
	if (lastTool) {
		lastTool.className = "iconStyle";
		}
	lastTool = obj;
	if ((activeTool == "ZoomIn") || (activeTool == "ZoomOut")) {
 	  MapImage.style.cursor = "crosshair";
	} else if ((activeTool == "MapTip") || (activeTool == "Identify")) {
	  MapImage.style.cursor = "help";
    } else if (activeTool == "Pan") {
      MapImage.style.cursor = "Move";
   }
}
function iconPopup(obj) {
  if (obj.className == "iconSelect") { return; }
	obj.className = "iconPopup";
}
function iconNormal(obj) {
 if (obj.className == "iconSelect") { return; }
	obj.className = "iconStyle";
}
function doMap() {
 if (activeTool == "ZoomIn") {
 	zoomOn();
 } else if (activeTool == "ZoomOut") {
   zoomOut();
 } else if (activeTool == "Pan") {
   PanOn();
 } else if (activeTool == "Identify") {
   identifyWindow();  
 }
}

function drawMap(left, top, right, bottom) {
 ReloadSplash.style.visibility = 'visible';	
 MapImage.filters.item(0).Apply();
 MapImage.src = gProtocol + "://" + gServer + "/" + gInstance + "/" + gSession + "/DrawMap(" + gDrawType + "," + left + "," + top +  "," + right + "," + bottom + "," + gWidth + "," + gHeight + ",IE)";
}

function drawMapAt(DrawLocation) {
 ReloadSplash.style.visibility = 'visible';	
 MapImage.filters.item(0).Apply();
 MapImage.src = gProtocol + "://" + gServer + "/" + gInstance + "/" + gSession + "/DrawMap(" + DrawLocation + "," + gWidth + "," + gHeight + ",IE)";
}

function ZoomWindow() {
	this.el = ZoomBox; 
	this.grabx = null;   
	this.graby = null;
	this.left = null;
	this.top = null;
}

var ZBInitialX = 0
var ZBInitialY = 0
function zoomOn() {
	if (activeTool != "ZoomIn") return;
	
	ZoomChCount = 0;

	ZB = new ZoomWindow();
	var X = window.event.clientX + mapBody.scrollLeft;
	var Y = window.event.clientY + mapBody.scrollTop;
	
	ZB.grabx = X;
	ZB.graby = Y;
	ZB.left = X;
	ZB.top = Y;
	
	ZBInitialX = window.event.offsetX;
	ZBInitialY = window.event.offsetY;

	ZoomBox.style.width = 1;
	ZoomBox.style.height = 1;
	
	ZoomBox.style.top = Y;
	ZoomBox.style.left = X;
    
	zoomActive = 1;
	
	window.event.returnValue = false;
	window.event.cancelBubble = true;
}

function ZoomSize() {

var X = window.event.clientX + mapBody.scrollLeft;
var Y = window.event.clientY + mapBody.scrollTop;

maptipX = window.event.offsetX;
maptipY = window.event.offsetY;
boxX = X;
boxY = Y;
maptips = "On"
if (zoomActive != 1) {
		return;
	}
	ZoomChCount++;
	if ((Y - ZB.graby) >= 0) {
		if ((Y - ZB.graby) >= 2) {
			ZoomBox.style.height = (Y - ZB.graby) - 2;
		} else {	
			ZoomBox.style.height = (Y - ZB.graby);
		}
	} else {
		ZoomBox.style.top = Y + 1;
		ZoomBox.style.height = ZB.graby - Y; 
	}
	if ((X - ZB.grabx) >= 0) {
		if ((X - ZB.grabx) >= 2) {
			ZoomBox.style.width = (X - ZB.grabx) - 2;
		} else {
			ZoomBox.style.width = (X - ZB.grabx);
		}
	} else {
		ZoomBox.style.left = X + 1;
		ZoomBox.style.width = ZB.grabx - X;
	}
	if (ZoomChCount > 2) {
	  ZoomBox.style.visibility = "visible";
	}
}

function ZoomCancel() {
	if (zoomActive != 1) {
		return;
	}
	ZoomBox.style.visibility = "hidden";
	zoomActive = 0;
}

function zoomIn() {
    if (zoomActive != 1) {
		return;
	}
	if (ZoomChCount < 2) {
		zoomInCenter();
		return;
	}
	
	var left;
	var top;
	var right;
	var bottom;
	var ZBFinalX = window.event.offsetX;
	var ZBFinalY = window.event.offsetY;
	
	
	if (ZBFinalX > ZBInitialX) {
		left = ZBInitialX;
		right = gWidth - (gWidth - ZBFinalX);
	} 
	else {
		left = ZBFinalX;
		right = gWidth - (gWidth - ZBInitialX);
	}
	if (ZBFinalY > ZBInitialY) {
		top = ZBInitialY;
		bottom = gHeight - (gHeight - ZBFinalY);
	} 
	else {
		top = ZBFinalY;
		bottom = gHeight - (gHeight - ZBInitialY);
	}
	
	drawMap(left, top, right, bottom); 
	ZoomBox.style.visibility = "hidden";
	zoomActive = 0;
	ZoomBox.style.width = 0;
	ZoomBox.style.height = 0;
	ZoomBox.style.top = 0;
	ZoomBox.style.left = 0;
}

function zoomInCenter() {
    var panIncrementX;
	var panIncrementY;
	var left;
	var top;
	var right;
	var bottom;
	var x;
	var y;
	
	
	panIncrementX = Math.round(gWidth / 3);
	panIncrementY = Math.round(gHeight / 3);
	x = ZBInitialX;
	y = ZBInitialY;
	
	x = x - gWidth / 2;
	y = y - gHeight / 2;
	left = 0 + x;
	left = left + panIncrementX;
	top = 0 + y;
	top = top + panIncrementY;
	right = gWidth + x;
	right = right - panIncrementX;
	bottom = gHeight + y;
	bottom = bottom - panIncrementY;
	drawMap(left, top, right, bottom); 
	ZoomCancel();

}

function zoomOut() {
    var panIncrementX;
	var panIncrementY;
	var left;
	var top;
	var right;
	var bottom;
	var x;
	var y;
	
	panIncrementX = Math.round(gWidth / 1.5);
	panIncrementY = Math.round(gHeight / 1.5);
	x = window.event.offsetX;
	y = window.event.offsetY;
	
	x = x - gWidth / 2;
	y = y - gHeight / 2;
	left = 0 + x;
	left = left - panIncrementX;
	top = 0 + y;
	top = top - panIncrementY;
	right = gWidth + x;
	right = right + panIncrementX;
	bottom = gHeight + y;
	bottom = bottom + panIncrementY;
	drawMap(left, top, right, bottom); 
}

function PanInfo() {
	this.grabx = null;   
	this.graby = null;
}

var Panning = 0;
var Loading = 0;
var PanCenterX = 0;
var PanCenterY = 0;

function PanOn() {
	if (activeTool != "Pan") return;
	PanChCount = 0;
	Panning = 1;
	PI = new PanInfo();
	
	PI.grabx = window.event.clientX;
	PI.graby = window.event.clientY;
	
	PanCenterX =  gWidth / 2;
	PanCenterY = gHeight / 2;
	
	window.event.returnValue = false;
}

var cliptop
var clipbottm
var clipleft
var clipright
function PanChange() {
	if (Panning == 0) {
		return;
	}
	
	PanChCount++;	

	obj = MapWrap;
	
	ldiff = window.event.clientX - PI.grabx;
	tdiff = window.event.clientY - PI.graby;
	
	obj.style.left = ldiff;
	obj.style.top = tdiff;

	PanCenterX = gWidth / 2 - ldiff;
	PanCenterY = gHeight / 2 - tdiff;

	cliptop = 0 - (window.event.clientY - PI.graby);
	clipright = gWidth - (window.event.clientX - PI.grabx);
	clipbottom = gHeight + (PI.graby - window.event.clientY);
	clipleft =  0 - (window.event.clientX - PI.grabx);
	
	MapImage.style.clip = "rect(" + cliptop + " " + clipright  + " " +  clipbottom  + " " +  clipleft + ")";
}

function PanGo() {
	if (Panning == 0) {
		return;
	}

	if (PanChCount < 2) {
		PanCancel();
		return;
	}

	Panning = 0;
	Loading = 1;
	var left;
	var top;
	var right;
	var bottom;
	var x;
	var y;
	
	x = PanCenterX - gWidth / 2;
	y = PanCenterY - gHeight / 2;
	
	left = 0 + x;
	top = 0 + y;
	right = gWidth + x;
	bottom = gHeight + y;
	
	drawMap(left, top, right, bottom);
	
}

function PanCancel() {
	if (activeTool != "Pan") {
		return;
	}

	Panning = 0;

	if (Loading != 1) {
		MapWrap.style.left = 0;
		MapWrap.style.top = 0;
		
		cliptop = 0;
		clipright = gWidth;
		clipbottom = gHeight;
		clipleft =  0;
		
		MapImage.style.clip = "rect(" + cliptop + " " + clipright  + " " +  clipbottom  + " " +  clipleft + ")";
	}	
}

function panButton(button) {
    var panIncrementX;
	var panIncrementY;
	var left;
	var right;
	var top;
	var bottom;
	
	panIncrementX = Math.round(gWidth / 3);
	panIncrementY = Math.round(gHeight / 3);
	if (button == "PanNorth") {
		left = 0;
		right = gWidth;
		top = 0 - panIncrementY;
		bottom = gHeight - panIncrementY;
	} else if (button == "PanSouth") {
	    left = 0;
		right = gWidth;
		top = panIncrementY;
		bottom = gHeight + panIncrementY;
	} else if (button == "PanEast") {
	    left = panIncrementX;
		right = gWidth + panIncrementX;
		top = 0;
		bottom = gHeight;
	} else if (button == "PanWest") {
	    left = 0 - panIncrementX;
		right = gWidth - panIncrementX;
		top = 0;
		bottom = gHeight;
	} else if (button == "PanSouthEast") {
	    left = panIncrementX;
		right = gWidth + panIncrementX;
		top = panIncrementY;
		bottom = gHeight + panIncrementY;
	} else if (button == "PanSouthWest") {
	    left = 0 - panIncrementX;
		right = gWidth - panIncrementX;
		top = panIncrementY;
		bottom = gHeight + panIncrementY;
	} else if (button == "PanNorthWest") {
	    left = 0 - panIncrementX;
		right = gWidth - panIncrementX;
		top = 0 - panIncrementY;
		bottom = gHeight - panIncrementY;
	} else if (button == "PanNorthEast") {
	    left = panIncrementX;
		right = gWidth + panIncrementX;
		top = 0 - panIncrementY;
		bottom = gHeight - panIncrementY;
	}
 drawMap(left, top, right, bottom);
}

var subX = 10;
var subY = 10;
var lastX = 0;
var lastY = 0;
function runMapTip() {
  if ((activeTool != "MapTip") || (maptips == "Off")) { return; }
    if ((maptipX == subX) && (maptipY == subY))
		return;
	if ((lastX != maptipX) || (lastY != maptipY)) {
		lastX = maptipX;
		lastY = maptipY;
		return;
	}
	if ((subX != maptipX) || (subY != maptipY)) {
	    subX = maptipX;
		subY = maptipY;
        
	tipRequest = window.location + "/" + gSession + "/MAPTIP(" + maptipX + "," + maptipY + ")";
	maptipSrc.location = tipRequest;
	}
}
function showMapTip() {
   if ((activeTool != "MapTip") || (maptips == "Off")) { return; }
    MapTipPopup.innerHTML = document.frames("maptipSrc").document.all.Results.innerHTML;
	MapTipSplash.style.left = boxX;
	MapTipSplash.style.top = boxY + 20;
	MapTipSplash.style.visibility = "visible";
}
function stopMapTip() {
	if (activeTool != "MapTip") { return; }
	maptips = "Off";
	MapTipSplash.style.visibility = "hidden";
	MapTipPopup.innerHTML = "Test";
}
function identifyWindow() {
 identifyRequest = window.location + "/" + gSession + "/IDENTIFY(" + window.event.offsetX + "," + window.event.offsetY + ")";
 identifyWin = window.open(identifyRequest,"", "menubar=yes,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=500,height=400,top=100,left=100");
}
function afterRefresh() {
 MapWrap.style.left = 0;
 MapWrap.style.top = 0;
 MapImage.style.clip = "rect(" + 0 + " " + gWidth  + " " +  gHeight  + " " +  0 + ")";
 MapImage.filters.item(0).Play();
 ReloadSplash.style.visibility = 'hidden';
}

document.onmouseup=PanGo;
document.onmousemove=PanChange;  