// JavaScript file for general settings

// we have to make sure that no function gets executed while
// the page is refreshing
ready = false;

var theServletPath = "";

// current extent
var minx = 0;
var maxx = 0;
var maxy = 0;
var miny = 0;

// maximum bounding box
var fullMinx = 0;
var fullMaxx = 0;
var fullMaxy = 0;
var fullMiny = 0;

// new extent by mouse drag 
var newMinx = 0;
var newMiny = 0;
var newMaxx = 0;
var newMaxy = 0;

/*var boxMinx = 0;
var boxMiny = 0;
var boxMaxx = 0;
var boxMaxy = 0;
*/
var minScale = 0;
var maxScale = 1;
var mapClickAsRecenter = true;
var mapBoxAsZoom = true;
var hasLocator = false;

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 setReady(bool) {
	ready = bool;
}

function refreshMap() {

  if (ready) {
/*	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 +
	   "?layers=" + layers +
      "&minx=" + newMinx + "&miny=" + newMiny  +
      "&maxx=" + newMaxx + "&maxy=" + newMaxy +
      "&bminx=" + fullMinx + "&bminy=" + fullMiny  +
      "&bmaxx=" + fullMaxx + "&bmaxy=" + fullMaxy +
      "&state=" + state +
      "&ispath=" + ispath;
   
	document.location = str;
*/

	minx = newMinx;
	miny = newMiny;
	maxx = newMaxx;
	maxy = newMaxy;
	var str = 'http://' + ispath + '?WMTVER=1.0.0&REQUEST=map&SRS=&BBOX=' + minx + ',' + miny + ',' + maxx + ',' + maxy + '&WIDTH=' + iWidth + '&HEIGHT=' + iHeight + '&LAYERS=' + layers + '&STYLES=&FORMAT=JPEG&BGCOLOR=&TRANSPARENT=&EXCEPTIONS=';
	//alert(str);
	image.src = str;
	
  }
}

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;
*/	
	zoomToFull();
  }
}

function setState(newState) {
  if (ready) {
	state = newState;
	
	if (state == "box")
		mapBoxAsZoom = false;
	else
		mapBoxAsZoom = true;

    if (isIE && document.all.theTop) {
	    if (state == "pan")
  		    document.all.theTop.style.cursor = "move";
		else
  		    document.all.theTop.style.cursor = "crosshair";
	}

	if (state == "zoomIn")
		swap('toolZoomIn', 'on'); 
	else
		swap('toolZoomIn', 'off'); 
	if (state == "zoomOut")
		swap('toolZoomOut', 'on'); 
	else
		swap('toolZoomOut', 'off'); 
	if (state == "pan")
		swap('toolPan', 'on'); 
	else
		swap('toolPan', 'off'); 
  }
}

/*function customMapBox(newMinx, newMiny, newMaxx, newMaxy) {
   if (ready) {
      boxMinx = newMinx;
      boxMiny = newMiny;
      boxMaxx = newMaxx;
      boxMaxy = newMaxy;
   }
}*/

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 boxWidth = 1;
	clipLayer("zoomBoxTop",zminx,zmaxy,zmaxx,zmaxy+boxWidth);
	clipLayer("zoomBoxLeft",zminx,zmaxy,zminx+boxWidth,zminy);
	clipLayer("zoomBoxRight",zmaxx-boxWidth,zmaxy,zmaxx,zminy);
	clipLayer("zoomBoxBottom",zminx,zminy-boxWidth,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);
}


