
var http = getHTTPObject();  // see common.js
var url = "/locator/mapurl.jhtml?scale=";  // The server-side script


/* default values */
var scaleFactor = 0.000125;
var msgs = new Object();
msgs.downloading = "downloading map image...";
msgs.zoomInOutFail = "zoom-in/zoom-out map failed";
msgs.waiting = "generating new map...";
msgs.maxZoom = "reached max zoom";
msgs.respNotValid = "handle resp not valid";
var physLocErrs = {
	generic:"generic error", 
	webservice_timeout:"timeout error"
};


var isWorking = false;
	
function handleHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
	  		var xmlDocument = http.responseXML; 
	  		mapStatus = document.getElementById('mapstatus');
	  		if (xmlDocument.getElementsByTagName('url').length > 0) {
	  			var url = xmlDocument.getElementsByTagName('url').item(0).firstChild.data;
	  			document.getElementById('maporama').src = url;
	  			setClass(mapStatus, 'info');
	  			mapStatus.innerHTML = msgs.downloading;
	  		} 
	  		else { // something went wrong
	  			setClass(mapStatus, 'error');
		  		if (xmlDocument.getElementsByTagName('errorcode').length > 0) { 
		  			// an exception was raised in GeoMapServlet
		  			mapStatus.innerHTML = 
		  				physLocErrs[xmlDocument.getElementsByTagName('errorcode').item(0).firstChild.data];
		  		} else {
		  			mapStatus.innerHTML = msgs.zoomInOutFail;
		  		}
	  		}
		    isWorking = false;
	  	} else {
	  		alert(msgs.respNotValid);
	  		divObj = document.getElementById('mapstatus');
	  		divObj.innerHTML = "&nbsp;";
	  		setClass(divObj, 'info');
	  	}
	}
}

function updateMap(zoomInOut, flvItemId) {
	// we use the mapstatus element to display status and error messages
	mapStatus = document.getElementById('mapstatus');
	
	// ### START check some pre-conditions (developers eyes only) ###
	if (!mapStatus) { 
		alert("funct updateMap() => A span or div with id 'mapstatus' is required to display status and error messages.");
		return;
	}
	
	if (zoomInOut != 'in' && zoomInOut != 'out') {
		alert('funct updateMap() => param \'zoomInOut\' only accepts values {\'in\',\'out\'}');
		return;
	}

	if (!flvItemId) {
		alert('funct updateMap() => param \'flvItemId\' is required');
		return;
	}
	// ### END check some pre-conditions ###
	
	if (zoomInOut=='out') {
		scaleFactor = scaleFactor / 2;
	} else {
		if (scaleFactor >= 0.0005) { // are we at max zoom level or not
			mapStatus.innerHTML = msgs.maxZoom;
			return; // max zoom level
		}
		scaleFactor = scaleFactor * 2;
	}

	if (!isWorking && http) {
		http.open("GET", url + escape(scaleFactor) + '&flvItemId=' + escape(flvItemId), true);
		http.onreadystatechange = handleHttpResponse;
		setClass(mapStatus, 'info');
		mapStatus.innerHTML = msgs.waiting;
		isWorking = true;
		http.send(null);
	}
	
}

/* Should be invoked from the onload event of an image. */
function newMapImageArrived() {
	mapStatus = document.getElementById('mapstatus');
	setClass(mapStatus, 'info');
	mapStatus.innerHTML = "&nbsp;";
}