<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title        = "Mapa de Distritos Mineros Colombianos"
             description  = "Mapa de Distritos Mineros Colombianos (Versión 0.1 - demostración)"
			 author       = "Jorge Iván Meza Martínez"
			 author_email = "jimezam@gmail.com"
			 height       = "200">
<Require feature="sharedmap" />
<Require feature="dynamic-height" />
</ModulePrefs>
<Content type="html"><![CDATA[

<style type="text/css">
<!--

.failed
{
	font-size: 10px;
	color: red;
}

.approved
{
	font-size: 10px;
	color: green;
}

-->
</style>

Informacion

<p><a href="#" onClick="startup(); return false;">Load Data</a></p>

<p>Estado: <span id="status">Ready</span>.</p>

<p id="content"></p>

<script>

//////////////////////////////////////////////////////////////////////////////////////

/* Sección de variables globales */

// Mapplet reference.

var map         = new GMap2();

// Geocoder, used to translate city/department/country information to lattitude/longitude coordenates.

var geocoder    = new GClientGeocoder();

// Time to Google cache the data.

var timeToCache = 60 * 60 * 12; 	// 12 horas.

// Stores the XML tree of the received remote data.

var xmlData     = null;

//////////////////////////////////////////////////////////////////////////////////////

/* Executed at startup, it fires all the processes */

function startup()
{
	// Clear the content message

	setContentMsg("");

	// Clear the older markers
	
	cleanupMarkers();

	// Where to get the remote XML source
	
	var dm_source = "http://demo.jorgeivanmeza.com/GMaps/demo_dm/distritos.xml";
	
	// Get the remote XML data
	
	_IG_FetchXmlContent(dm_source, function(response) 
	{
		// Validate failure or incorrect data received
	
		if(response == null || 
		   typeof(response) != 'object' || 
		   response.firstChild == null)
		{
			setStatusMsg("Failed reading the remote XML data source.");
			return;
		}
		
		// Update status message
		
		setStatusMsg("Remote XML data source succesfuly loaded.");
		
		// Store the <distrito> tags on the global variable
		
		xmlData = response.getElementsByTagName("distrito");
		
		// Process the received data
		
		processData();
		
	}, { refreshInterval: timeToCache });
	
	/////////////////////////////////////////////////////////////////////////////////
	
	// Center the map on an specific point
	
	map.setCenter(new GLatLng(4.647302, -74.096268));  // Bogotá.
	map.setZoom(6);

	/////////////////////////////////////////////////////////////////////////////////

	// Update the height of the frame
	
	_IG_AdjustIFrameHeight();
	
	/////////////////////////////////////////////////////////////////////////////////
}

//////////////////////////////////////////////////////////////////////////////////////

/* Process the remote information previously received */

function processData()
{
	// Check that the data is OK

	if (!xmlData || xmlData == false || xmlData == undefined)
	{
		setStatusMsg("XML data source is not defined, I cannot process it.");
		return;
	}

	// Update the status message
	
	setStatusMsg("Processing data ... " + xmlData.length + " records.");
	
	// Walk thru each <distrito> and process it adding its marker
	
	for(var i=0; i<xmlData.length; i++)
	// for(var i=0; i<20; i++)
	{
		addMarker(i);
	}
	
	// Update the status message when all it's done
	
	setStatusMsg("All done!");
}

//////////////////////////////////////////////////////////////////////////////////////

/* Process an individual marker */

function addMarker(index)
{
	// Get the specific information about the index-th marker

	var distrito = xmlData[index];

	var codigo = distrito.getAttribute("codigo");
	var nombre = distrito.getAttribute("nombre");
	var muncod = distrito.getAttribute("muncod");
	var munnom = distrito.getAttribute("munnom");
	var depcod = distrito.getAttribute("depcod");
	var depnom = distrito.getAttribute("depnom");		

	// Prepare the string information of the marker to be geocoded
	
	var ubicacion = munnom + ", " + depnom + ", Colombia";

	// Ask for the geocoded information
	
	geocoder.getLatLngAsync(ubicacion, function(latlng) 
	{
		// If the geocoding process fails, update the content message
	
		if(!latlng)
			addContentMsg("<div class='failed'>FAIL GeoCoding <b>" + ubicacion + "</b>.</div>");
		else
		{
			// If the geocoding process success, update the content message
		
			addContentMsg("<div class='approved'>OK GeoCoding <b>" + ubicacion + "</b> @ <b>" + latlng + "</b>.</div>");

			// Create the marker information
			
			xmlData[index].marker = createMarker(latlng, distrito);
			
			// Add it to the map
			
			map.addOverlay(xmlData[index].marker);
		}
	});
}
		
//////////////////////////////////////////////////////////////////////////////////////

/* Create a marker from specific data */

function createMarker(location, distrito)
{
	// Get the specific <distrito> information

	var codigo = distrito.getAttribute("codigo");
	var nombre = distrito.getAttribute("nombre");
	var muncod = distrito.getAttribute("muncod");
	var munnom = distrito.getAttribute("munnom");
	var depcod = distrito.getAttribute("depcod");
	var depnom = distrito.getAttribute("depnom");		

	// Create the GMarker object with last data
	
	var marker = new GMarker(location, { title: munnom + " (" + depnom + ") - " + nombre });
	
	// Add onClick listener to respond and show an InfoWindow
	
	GEvent.addListener(marker, "click", function() 
	{
		var html = "<div style='background-color: #FDF2B8;'><span style='font-weight: bolder;'>" + nombre + "</span><br /><span>" + munnom + 
		           " (" + depnom + ")" + "</span></div><div style='background-color: #DAF1F8;'></div>";
		
		marker.openInfoWindowHtml(html);
	});
	
	return marker;
}

//////////////////////////////////////////////////////////////////////////////////////

/* Clean up all previously data */

function cleanupMarkers()
{
	// Check that there is previously data 

	if(xmlData == null || xmlData.length == 0)
		return;
	
	// Walk thru each older marker
	
	for (var i=0; i<xmlData.length; i++)
	{
		// Remove onClick listeners
	
		var marker = xmlData[i];
		
		GEvent.clearListeners(marker, "click");
	}
	
	// Clear the remote data storage object
	
	xmlData = new Array();
}

//////////////////////////////////////////////////////////////////////////////////////

/* Set a message on the status message facility */

function setStatusMsg(message)
{
	_gel('status').innerHTML = message;
}

/* Set a message on the content message facility */

function setContentMsg(message)
{
	_gel('content').innerHTML = message;
}

/* Append a message on the content message facility */

function addContentMsg(message)
{
	_gel('content').innerHTML += message;
}

//////////////////////////////////////////////////////////////////////////////////////

</script>

]]></Content>
</Module>
