// JavaScript file for general settings

// we have to make sure that no function gets executed while
// the page is refreshing
ready = false;

var theServletPath = "";
var placeURL = "";

var minx = -180;
var maxx = 180;
var maxy = 90;
var miny = -90;
var fullMinx = -180;
var fullMaxx = 180;
var fullMaxy = 90;
var fullMiny = -90;
var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;
var boxMinx = -180;
var boxMiny = -90;
var boxMaxx = 180;
var boxMaxy = 90;

var minScale = 0;
var maxScale = 1;
var mapClickAsRecenter = true;
var mapBoxAsZoom = true;

// locator image size
var iWidth = 320;
var iHeight = 242;

if (navigator.appName.indexOf("Netscape")>=0 && (parseInt(navigator.appVersion)<5))
	var vspc = 102;
else if (navigator.appName.indexOf("Netscape")>=0 && (parseInt(navigator.appVersion)>=5))
	var vspc = 96;
else
	var vspc = 101;

// JFS
var hspc = 100;
var vspc = 100;

var hasLocator = false;
// locator image size
var ovWidth = 0;
var ovHeight = 0;
var ovHspc = 0
var ovVspc = 0;

var errorActive = false;
var state = "box"; // zoomIn, zoomOut, pan
var full = false;


function setExtent(minx_,miny_,maxx_,maxy_) {
	minx = minx_;
	miny = miny_;
	maxx = maxx_;
	maxy = maxy_;
}

function setFullExtent(minx_,miny_,maxx_,maxy_) {
	fullMinx = minx_;
	fullMiny = miny_;
	fullMaxx = maxx_;
	fullMaxy = maxy_;
}

function setBoxExtent(minx_,miny_,maxx_,maxy_) {
	boxMinx = minx_;
	boxMiny = miny_;
	boxMaxx = maxx_;
	boxMaxy = maxy_;
	/*
    if (boxMinx != 0 || boxMiny != 0 || boxMaxx != 0 || boxMaxy != 0)
        if (((boxMaxx-boxMinx)*(boxMaxy-boxMiny)*1.1) < ((maxx-minx)*(maxy-miny)))
            drawBox();
	*/
}

function setServletPath(str) {
	theServletPath = str;
}

function setPlaceURL(str) {
	if (str.indexOf("place") > -1) {
		place = value(str,"&place");
		str = removeKey(str,"&place=");
		placeURL = "&place="+escape(place)+str;
	}
}

function setReady(bool) {
	ready = bool;
}

function refreshMap() {

  if (ready) {
	if (newMinx < fullMinx)
		newMinx = fullMinx;
	if (newMiny < fullMiny)
		newMiny = fullMiny;
	if (newMaxx > fullMaxx)
		newMaxx = fullMaxx;
	if (newMaxy > fullMaxy)
		newMaxy = fullMaxy;

	/*selection box adjusted to map extent
	if (newMinx > fullMinx && newMiny > fullMiny &&
		newMaxx < fullMaxx && newMaxy < fullMaxy) {
		// we want to be a bit bigger than the box
		if ((state == "zoomIn") || (state == "zoomOut")) {

			boxMinx = newMinx;
			boxMiny = newMiny;
			boxMaxx = newMaxx;
			boxMaxy = newMaxy;

			addPercent(10);
		} else {
			// box size stays the same and moves as much as the pan
			xmove = minx - newMinx;
			ymove = miny - newMiny;
			boxMinx = boxMinx - xmove;
			boxMiny = boxMiny - ymove;
			boxMaxx = boxMaxx - xmove;
			boxMaxy = boxMaxy - ymove;
		}
	} else if (!full) {
		boxMinx = fullMinx;
		boxMiny = fullMiny;
		boxMaxx = fullMaxx;
		boxMaxy = fullMaxy;
	}
	*/
	str = theServletPath +
		"?minx=" + (Math.round(newMinx*1000000)/1000000) + "&miny=" + (Math.round(newMiny*1000000)/1000000)  +
	    "&maxx=" + (Math.round(newMaxx*1000000)/1000000) + "&maxy=" + (Math.round(newMaxy*1000000)/1000000) +
		"&bminx=" + (Math.round(boxMinx*1000000)/1000000) + "&bminy=" + (Math.round(boxMiny*1000000)/1000000) +
	    "&bmaxx=" + (Math.round(boxMaxx*1000000)/1000000) + "&bmaxy=" + (Math.round(boxMaxy*1000000)/1000000) +
		"&fx1=" + (Math.round(fullMinx*1000000)/1000000) + "&fy1=" + (Math.round(fullMiny*1000000)/1000000) +
	    "&fx2=" + (Math.round(fullMaxx*1000000)/1000000) + "&fy2=" + (Math.round(fullMaxy*1000000)/1000000) +
		"&state=" + state;
	if (showSelectBox) str += "&showsel=true";
	if (placeURL!="") str += "&place=" + escape(placeURL);

	//alert(str);
	document.location = str;
  }
}



function getImageType() {
	if (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) <= 4.5)
		return "JPG";
	else
		return "PNG8";
}

function addPercent(value) {

	// add %
	value = 1 + value/100;

	var width = Math.abs(newMaxx-newMinx);
	var height = Math.abs(newMaxy-newMiny);
	var cx = newMinx + width/2;
	var cy = newMiny + height/2;

	newMinx = cx - width*value/2;
	newMiny = cy - height*value/2;
	newMaxx = cx + width*value/2;
	newMaxy = cy + height*value/2;
}

function refreshMapExtent(minx_,miny_,maxx_,maxy_) {

  if (ready) {
	newMinx = minx_;
	newMiny = miny_;
	newMaxx = maxx_;
	newMaxy = maxy_;

	refreshMap();
  }
}

function zoomToFull() {
  if (ready) {
	full = true;

	newMinx = fullMinx;
	newMiny = fullMiny;
	newMaxx = fullMaxx;
	newMaxy = fullMaxy;

	refreshMap();
  }
}

function deleteBox() {
  if (ready) {
	hideZoomBox();

	boxMinx = -180;
	boxMiny = -90;
	boxMaxx = 180;
	boxMaxy = 90;
	showSelectBox=false;
	placeURL = "";

	//zoomToFull();
	newMinx = minx;
	newMiny = miny;
	newMaxx = maxx;
	newMaxy = maxy;
	refreshMap();
  }
}

function setState(newState) {
  if (ready) {
  	var oldState = state;
	state = newState;

	if (state == "box")
		mapBoxAsZoom = false;
	else
		mapBoxAsZoom = true;

	if (isIE) {
	    if (state == "pan") {
  		    document.all.theMap.style.cursor = "move";
		} else if (state == "box") {
			document.all.theMap.style.cursor = "crosshair";
		} else {
			document.all.theMap.style.cursor = "crosshair";
		}
	}

	if (state == "box")
		MM_swapImage('toolselectarea','','images/toolselectarea_press.gif',1);
	else
		MM_swapImage('toolselectarea','','images/toolselectarea_off.gif',1);

	if (state == "zoomIn")
		MM_swapImage('toolzoomin','','images/toolzoomin_press.gif',1);
	else
		MM_swapImage('toolzoomin','','images/toolzoomin_off.gif',1);

	if (state == "zoomOut") {
		state = oldState;
		zoomButton(2);
	}

	if (state == "pan")
		MM_swapImage('toolpan','','images/toolpan_press.gif',1);
	else
		MM_swapImage('toolpan','','images/toolpan_off.gif',1);
  }
}

function customMapBox(newMinx, newMiny, newMaxx, newMaxy) {

  if (ready) {
	boxMinx = newMinx;
	boxMiny = newMiny;
	boxMaxx = newMaxx;
	boxMaxy = newMaxy;
	str = theServletPath + "?minx=" + minx + "&miny=" + miny + "&maxx=" + maxx + "&maxy=" + maxy +
		"&bminx=" + (Math.round(boxMinx*1000000)/1000000) + "&bminy=" + (Math.round(boxMiny*1000000)/1000000) +
	    "&bmaxx=" + (Math.round(boxMaxx*1000000)/1000000) + "&bmaxy=" + (Math.round(boxMaxy*1000000)/1000000) +
		"&fx1=" + (Math.round(fullMinx*1000000)/1000000) + "&fy1=" + (Math.round(fullMiny*1000000)/1000000) +
	    "&fx2=" + (Math.round(fullMaxx*1000000)/1000000) + "&fy2=" + (Math.round(fullMaxy*1000000)/1000000) +
		"&state=" + state + placeURL;

	if (showSelectBox) str += "&showsel=true";

	//alert(str);
	document.location = str;
  }

}

function drawBox() {

	var width = Math.abs(maxx - minx);
	var height = Math.abs(maxy - miny);
	var pixelX = iWidth / width;
	var pixelY = iHeight / height;

	var zminx = pixelX * (boxMinx-minx);
	var zminy = iHeight - (pixelY * (boxMiny-miny));
	var zmaxx = pixelX * (boxMaxx-minx);
	var zmaxy = iHeight - (pixelY * (boxMaxy-miny));

	var ovBoxSize = 1;
	clipLayer("zoomBoxTop",zminx,zmaxy,zmaxx,zmaxy+ovBoxSize);
	clipLayer("zoomBoxLeft",zminx,zmaxy,zminx+ovBoxSize,zminy);
	clipLayer("zoomBoxRight",zmaxx-ovBoxSize,zmaxy,zmaxx,zminy);
	clipLayer("zoomBoxBottom",zminx,zminy-ovBoxSize,zmaxx,zminy);
	showZoomBox();
}

function value(str,search) {
	var s = str.indexOf(search,0);
	if (s == -1) return -1;
	var e = str.indexOf("&",s+1);
	if (e == -1) e = str.length;
	return str.substring(s+search.length+1,e);
}

function removeKey(string,key) {
	if (string.indexOf(key) != -1) {
		start =  string.indexOf(key);
		end =  string.indexOf("&",start+2);
		if (end == -1)
			string = string.substring(0,start);
		else
			string = string.substring(0,start)+string.substring(end,string.length);
	}
	return string;
}
