var ovmap = {
	
	init: function(id) {
		var elem = document.getElementById(id);
		this.width = elem.offsetWidth - 3;
		this.height = elem.offsetHeight - 6;
		this.div = document.createElement("div");
		this.div.style.left = "3px";
		this.div.style.top = "3px";
		this.div.style.width = this.width + "px";
		this.div.style.height = this.height + "px";
		this.div.style.position = "relative";
		this.div.style.overflow = "hidden";
		this.div.onmousedown = this.onMouseDown;
		elem.appendChild(this.div);
		this.image = document.createElement("img");
		this.image.onload = function() {
			var s = readCookie("Map-Extents");
			if (s)
			{
				var ea = s.split(',');
				ovmap.extents = { left: ea[0] * 1, top: ea[1] * 1, right: ea[2] * 1, bottom: ea[3] * 1};
				ovmap.updateZoomRect();
			}
		}
		this.image.width = this.width;
		this.image.height = this.height;
		this.div.appendChild(this.image);
		var url = mapServerURL + "?User=&VName=" + initViewName + "_OV";
		url += "&Cmd=ZoomAll&ISize=" + this.width + ";" + this.height;
		this.image.src = url;
		this.zoomArea = document.createElement("div");
		this.zoomArea.className = "zoomarea";
		this.zoomArea.style.position = "absolute";
		this.zoomArea.style.visible = "hidden";
		this.div.appendChild(this.zoomArea);
	},
	
	updateZoomRect: function() {
		if (!this.extents || !map.extents) {
			this.zoomArea.style.visible = "hidden";
			return;
		}
		var l = this.getX(map.extents.left);
		var t = this.getY(map.extents.top);
		var r = this.getX(map.extents.right);
		var b = this.getY(map.extents.bottom);
		setZoomRect(this.zoomArea, l, t, r - l, b - t);
		this.zoomArea.style.visible = "";
	},

	getX: function(wx) {
		return Math.round((wx - this.extents.left) * this.width / (this.extents.right - this.extents.left));
	},

	getY: function(wy) {
		return Math.round((wy - this.extents.top) * this.height / (this.extents.bottom - this.extents.top));
	},
	
	getWX: function(x) {
		return this.extents.left + x * (this.extents.right - this.extents.left) / this.width;
	},

	getWY: function(y) {
		return this.extents.top + y * (this.extents.bottom - this.extents.top) / this.height;
	},
	
	zoom: function(zoomRect) {
		var ml = map.getX(this.getWX(zoomRect.left));
		var mt = map.getY(this.getWY(zoomRect.top));
		var mw = map.getX(this.getWX(zoomRect.left + zoomRect.width)) - ml;   		
		var mh = map.getY(this.getWY(zoomRect.top + zoomRect.height)) - mt;
		map.zoom(ml, mt, mw, mh);
	},	
	
	onMouseDown: function(e)
	{
		if (!ovmap.image || !ovmap.image.complete)
		  return false;
		if (!e) var e = window.event;
		ovmap.focusPos = getCursorPos(e);
		ovmap.focusPos.x -= getAbsLeft(ovmap.div);
		ovmap.focusPos.y -= getAbsTop(ovmap.div);
		document.onmouseup = ovmap.onMouseUp;
		if (getMouseButton(e) == "LEFT") {
			document.onmousemove = ovmap.onMouseMove;
		}
		setZoomRect(ovmap.zoomArea, ovmap.focusPos.x, ovmap.focusPos.y, 0, 0);
		return false;	
	},
	
	onMouseMove: function(e)
	{
		if (!e) var e = window.event;
		var pos = getCursorPos(e);
		pos.x = Math.min(Math.max(0, pos.x - getAbsLeft(ovmap.div)), ovmap.width - 1);
		pos.y = Math.min(Math.max(0, pos.y - getAbsTop(ovmap.div)), ovmap.height - 1);
		var l = pos.x < ovmap.focusPos.x ? pos.x : ovmap.focusPos.x;
		var t = pos.y < ovmap.focusPos.y ? pos.y : ovmap.focusPos.y;
		var w = Math.abs(pos.x - ovmap.focusPos.x);
		var h = Math.abs(pos.y - ovmap.focusPos.y);
		setZoomRect(ovmap.zoomArea, l, t, w, h);
		return false;	
	},
	
	onMouseUp: function(e)
	{
		if (!e) var e = window.event;
		var button = getMouseButton(e);
		document.onmousemove = null;
		document.onmouseup = null;
		var zoomRect = getZoomRect(ovmap.zoomArea);
		if (zoomRect.width > 1 && zoomRect.height > 1) {
			ovmap.zoom(zoomRect)
		} 
		else {
			var ocx = ovmap.getX((map.extents.left + map.extents.right) / 2);
			var ocy = ovmap.getY((map.extents.top + map.extents.bottom) / 2);
			var dx = ovmap.focusPos.x - ocx;
			var dy = ovmap.focusPos.y - ocy;
			var l = ovmap.getX(map.extents.left);
			var t = ovmap.getY(map.extents.top);
			var r = ovmap.getX(map.extents.right);
			var b = ovmap.getY(map.extents.bottom);
			setZoomRect(ovmap.zoomArea, l + dx, t + dy, r - l, b - t);
			var x = map.getX(ovmap.getWX(ovmap.focusPos.x));
			var y = map.getY(ovmap.getWY(ovmap.focusPos.y));
			map.center(x, y);
		} 
		return false;	
	}
}
