/**
 * Zoom on the specified area by the coords and the projection.
 */
function goToArea(iLat, iLong) {

	itZoomToBox(new Array(parseFloat(iLong) - 0.1, parseFloat(iLat) + 0.1), new Array(parseFloat(iLong) + 0.1, parseFloat(iLat) - 0.1), "EPSG:4326");
	
	dijit.byId('GoToResultDialogBox').hide();
}

// this function will be called by our JSON callback
// the parameter jData will contain an array with geonames objects
function getLocation(jData) {
  if (jData == null) {
    // There was a problem parsing search results
    return;
  }

  var html = '<table cellspacing=5>';
  var geonames = jData.geonames;
  for (i=0;i< geonames.length;i++) {
     var name = geonames[i];
     // we create a simple html list with the geonames objects
     // the link will call the center() javascript method with lat/lng as parameter
     var adminArea = "";
     if (name.adminName1 != "") {
     	adminArea = ' (' + name.adminName1 + ')';
     }
     html = html + '<tr><td align="right"><img src="style/default/images/geonames/icone_' + name.fcl.toLowerCase() + '.gif" title="' + gMessages["js.message.geonames" + name.fcl] + '"></td><td align="left"><a class="infoterreLink" href="javascript:goToArea(' + name.lat +',' + name.lng + ');">' + name.name + adminArea + '</a></td></tr>';
  }
  html += '</table>';
  document.getElementById('gotoResultDiv').innerHTML = html;
  dijit.byId('GoToResultDialogBox').show();
}

// calls the geonames JSON webservice with the search term
function search() {
  var adresse = 'http://ws5.geonames.org/searchJSON?q=' +  encodeURIComponent(document.getElementById('gotoText').value)  + '&maxRows=20&callback=getLocation&country=PF,YT,NC,GP,MQ,GF,RE,FR';

 	dojo.xhrGet(
	{
		url : "./proxy?url=" + escape(adresse),
		handleAs: "json",
		load: function(responseObject, ioArgs) {
			eval(responseObject);
		}
	}
	);
 
}

/**
 * Zoom on the specified area.
 */
function zoomOnArea(val) {
	if (val!="default") {
		// Get the context from name
		var context = getContextFromName(val);
	
		// Zoom on the bbox	
		if (context) {
			// Zoom
			itZoomToBox(context.bbox_init[0], context.bbox_init[1], "EPSG:4326");
		}
	}
}

/**
 * Get the bbox of the specified region
 */
function zoomOnRegion(val) {
	if (val!="default") {
		// Build the request
		var request = 
			{
				method : "getRegion", 
				code: val
			};
		
		// Send it to the server side
		dojo.xhrGet({
	        url: "gazetteer",
			handleAs: "xml",
			content: request,
			load: function(response, ioArgs){zoomOnItemHandler(response);}
		});
	}
}

/**
 * Get the bbox of the specified departement
 */
function zoomOnDepartement(val) {
	if(val!="default"){
		// Build the request
		var request = 
			{
				method : "getDepartement", 
				code: val
			};
		
		// Send it to the server side
		dojo.xhrGet({
	        url: "gazetteer",
			handleAs: "xml",
			content: request,
			load: function(response, ioArgs){zoomOnItemHandler(response);}
		});
	}
}

/**
 * Zoom on the specified item
 */
function zoomOnItemHandler(xml) {
	// Get the item node
	var itemNode = xml.selectSingleNode("/Gazetteer/Item");

	// Get the coordinates
	var maxX = itemNode.selectSingleNode("MaxX").firstChild.nodeValue;
	var minX = itemNode.selectSingleNode("MinX").firstChild.nodeValue;
	var maxY = itemNode.selectSingleNode("MaxY").firstChild.nodeValue;
	var minY = itemNode.selectSingleNode("MinY").firstChild.nodeValue;
	if(minX.indexOf("$")==-1){
		var ul = new Array(parseFloat(minX), parseFloat(maxY));
		var lr = new Array(parseFloat(maxX), parseFloat(minY));
		// Zoom
		itZoomToBox(ul,lr, "EPSG:4326");
	}
}


/**
 * Zoom on the specified coordinates.
 */
function zoomOnCoordinates() {
	// Get the coordinates
	var epsg = "EPSG:27582";
	var latLongRadio = document.getElementById("goToLatLongRadio");
	if (latLongRadio.checked) {
		epsg = "EPSG:4326";
	}
	var x = document.getElementById("goToCoordinatesX").value;
	var y = document.getElementById("goToCoordinatesY").value;
	var distance = document.getElementById("goToDistance").value;
	
	// Control the input parameters
	if (x && y && distance) {
		var scale = parseFloat(distance) * 10000000 / (document.getElementById('mainMapPane').offsetWidth * mbScaleFactor);
		itZoomToScale({lon:parseFloat(x),lat:parseFloat(y)}, scale, epsg);
	}
}
