// sjcMOUSE.js
// map document mouse event handlers
// Copyright (C) 2005 San Joaquin County Community Development Geographic Information Systems, ALL RIGHTS RESERVED
// 01/14/2004 David Bollinger
//


var LEFT_BUTTON = 1;

mouse = new function() {
	this.x = 0;
	this.y = 0;
	this.mode = '';
}

panner = new function() {
	this.x1 = 0;
	this.y1 = 0;
	this.x2 = 0;
	this.y2 = 0;
	this.start = function(x,y) {
		moveLayer("theMap",0,0);
		this.x1 = this.x2 = x;
		this.y1 = this.y2 = y;
	}
	this.drag = function(x,y) {
		this.x2 = x;
		this.y2 = y;
		var xMove = this.x2 - this.x1;
		var yMove = this.y2 - this.y1;
		var cLeft = (xMove>0) ? 0 : -xMove;
		var cRight = (xMove>0) ? iWidth-xMove : iWidth;
		var cTop = (yMove>0) ? 0 : -yMove;
		var cBottom = (yMove>0) ? iHeight-yMove : iHeight;
		clipLayer2("theMap",cLeft,cTop,cRight,cBottom);
		moveLayer("theMap",xMove,yMove);
	}
	this.stop = function() {
	}
	this.doPan = function() {
		mouse.mode='';
		window.scrollTo(0,0);
		saveLastExtent();
		var ixOffset = this.x2 - this.x1;
		var iyOffset = this.y1 - this.y2;
		var muppx = extent.width / iWidth;
		var theY = iHeight - this.y2;
		var muppy = extent.height / iHeight;
		var xOffset = muppx * ixOffset;
		var yOffset = muppy * iyOffset;
		extent.move(-xOffset, -yOffset);
		limitExtent();
		document.theImage.onload = resetPanImage;
		map.request();
	}
}


zoomer = new function() {
	this.x1 = 0;
	this.y1 = 0;
	this.x2 = 0;
	this.y2 = 0;
	this.sort = function() {
		var tx1 = this.x1;
		var ty1 = this.y1;
		this.x1 = Math.min(tx1, this.x2);
		this.y1 = Math.min(ty1, this.y2);
		this.x2 = Math.max(tx1, this.x2);
		this.y2 = Math.max(ty1, this.y2);
	};
	this.start = function(x,y) {
		moveLayer("theMap",0,0);
		this.x1 = this.x2 = x;
		this.y1 = this.y2 = y;
		boxIt(x,y,x,y);
	};
	this.drag = function(x,y) {
		this.x2 = x;
		this.y2 = y;
		boxIt(Math.min(this.x1, this.x2),
			Math.min(this.y1, this.y2),
			Math.max(this.x1, this.x2),
			Math.max(this.y1, this.y2) );
	};
	this.stop = function() {
		hideLayer("zoombox");
		this.sort();
	};
	this.doZoomIn = function() {
		mouse.mode = '';
		if ((this.x2-this.x1 < 2) || (this.y2-this.y1 < 2)) {
			doZoomInPoint();
		} else {
			saveLastExtent();
			var muppx = extent.width / iWidth;
			var muppy = extent.height / iHeight;
			var x0 = extent.minx;
			var y0 = extent.maxy;
			extent.set(x0 + (this.x1 * muppx),
				y0 - (this.y2 * muppy),
				x0 + (this.x2 * muppx),
				y0 - (this.y1 * muppy) );
			limitExtent();
			window.scrollTo(0,0);
			map.request();
		}
	}
	this.doZoomOut = function() {
		mouse.mode = '';
		if ((this.x2-this.x1 < 2) || (this.y2-this.y1 < 2)) {
			doZoomOutPoint();
		} else {	
			saveLastExtent();
			var zWidth = Math.abs(this.x2-this.x1);
			var zHeight = Math.abs(this.y1-this.y2);
			var xRatio = iWidth / zWidth;
			var yRatio = iHeight / zHeight;
			var xAdd = xRatio * extent.width / 2;
			var yAdd = yRatio * extent.height / 2;
			extent.inset(-xAdd,-yAdd);
			limitExtent();
			window.scrollTo(0,0);
			map.request();
		}
	}
}



function isMouseOnMap() {
	return ((mouse.x>=0) && (mouse.x<iWidth) && (mouse.y>=0) && (mouse.y<iHeight));
}

function isMouseOnOV() {
	return ((ovmap.visible) && (mouse.x<i2Width+2) && (mouse.y<i2Height+2));
}

// handle mouse move event on map document
function mapMouseMove(e) {
	window.status="";
	getImageXY(e);

	// UPDATE COORDINATES (but only when NOT zooming/panning)
  if (mouse.mode=='') {
	  var muppx = extent.width / iWidth;
	  mapX = muppx * mouse.x + extent.minx;
	  var muppy = extent.height / iHeight;
	  mapY = muppy * (iHeight-mouse.y) + extent.miny;
	
	  var uX = Math.round(mapX * 100 + (5/10)) / 100
	  var uY= Math.round(mapY * 100 + (5/10)) / 100

	  var aLL = sp2ll( mapX, mapY );
	  parent.CoordFrame.update(uX+"'", uY+"'", aLL[0], aLL[1]);
  }

	// UPDATE INTERACTIVES?
	switch(mouse.mode) {
		case 'zooming' : zoomer.drag(mouse.x, mouse.y); break;
		case 'panning' : panner.drag(mouse.x, mouse.y); break;
	}
	return false;
}


// handle mouse down event on map document
function mapMouseDown(e) {
  if (!e) var e = window.event;
  var theButton = (e.which) ? e.which : e.button;
	if (theButton != LEFT_BUTTON) return;
	getImageXY(e);
	if (isMouseOnMap()) {
		if (isMouseOnOV()) {
			ovMouseDown(mouse.x,mouse.y);
		} else {
			switch(activeTool) {
				case 'zoomin':
					mouse.mode = 'zooming';
					zoomer.start(mouse.x,mouse.y);
					break;
				case 'zoomout':
					mouse.mode = 'zooming';
					zoomer.start(mouse.x,mouse.y);
					break;
				case 'pan':
					mouse.mode = 'panning';
					panner.start(mouse.x,mouse.y);
					break;
				case 'identify':
					requestIdentify(e);
					break;
			}
		}
	}
	return false;
}


function mapMouseUp(e) { 
	if ((activeTool=='zoomin') && (mouse.mode=='zooming')) {
		zoomer.stop();
		zoomer.doZoomIn();
	}
	if ((activeTool=='zoomout') && (mouse.mode=='zooming')) {
		zoomer.stop();
		zoomer.doZoomOut();
	}
	if ((activeTool=='pan') && (mouse.mode=='panning')) {
		panner.stop();
		panner.doPan();
	}
	return false;
}


function ovMouseDown(x,y) {
	x -= 2;
	y -= 2;
	var ovmuppx = ovextent.width / i2Width;
	var ovmuppy = ovextent.height / i2Height;
	var ovmapx = x * ovmuppx + ovextent.minx;
	var ovmapy = (i2Height-y) * ovmuppy + ovextent.miny;
	var xadd = extent.width / 2;
	var yadd = extent.height / 2;
	saveLastExtent();
	extent.set(ovmapx-xadd, ovmapy-yadd, ovmapx+xadd, ovmapy+yadd);
  limitExtent();
	map.request();
}
