﻿/** geoStatic
*
* This object will create a Static Map, using Google Maps, with several options that will allow
* users to manage a colletion of points saving them to a database.
*
* METHODS
*
* init: Initializes the map 
*
* parseXml: Parse a XML file retrieving the points information
*
* addPoint: Create a new marker with the information provided from the parseXml
*           click: Show point information 
*
* refreshPoints: Load the xml file with the points location and add them to the map overlay
*
* PARAMETERS
*
* cod - receives a cod that identify a single point
*     - if cod=0 show all the available points
*
*/

function geoStatic(cod) 
{
	var map, xml, points, temp_point;
	
	/**
	* Parse the XML file and adds markers to the given latitude and longitude
	*/
	this.parseXml = function( xml )
	{
		// Variables init
		this.xml = xml;
		var points = xml.documentElement.getElementsByTagName("point");
		
		// Cicle that will reads all the points from the XML file
		for (var i = 0; i < points.length; i++)
		{
			// Retrieves the description from the corresponding node
			qual_cod = points[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
			
			// If the parsed cod (qual_cod) is equal to the parameter -> Show the specified point
			// If the parameter is 0 -> Show all the points
			if ( (qual_cod==cod) || (cod=="0") )
			{
				// Retrieves the LAT and LONG from the coordinates node of the XML file
				var latlng = points[i].getElementsByTagName("coordinates");
				
				var newlat=latlng[0].getAttribute("lat");
				var newlong=latlng[0].getAttribute("lng");
				
				latlng = new GLatLng(parseFloat(newlat),parseFloat(newlong));
				
				// Retrieves the description from the corresponding node
				description = points[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				
				//alert (description);
				
				if (description=="")
				{ description="Descrição não disponível"; }
				
				
				// Retrieves the category from the corresponding node
				category = points[i].getElementsByTagName("category")[0].childNodes[0].nodeValue;
				
				// Call the "addPoint" function that will put the marker on the map overlay
				// only if the coordinates are different from 0
				if (newlat!=0 && newlong!=0)
				{
					geo.map.addOverlay(geo.addPoint(latlng,description, category));
				}
			}
		}
	}
	
	/**
	* Creates a new marker with the provided data
	* 
	* return MARKER
	*/
	this.addPoint = function( point, content, category)
	{
		// Variables init
		var icon2use;
		// Define wich icon will be used, based on the point state
		icon2use	=	choose_icon(category);
				
		var marker = new GMarker(point, icon2use); 
		
		// If there's a specific point to be showed, center the map on it
		if (cod!="0")
		{this.map.setCenter(point, 14);}

		// Add "onclick" action to the marker -> Open info window with the point description
		GEvent.addListener(marker, "click", function(latlng) {
			marker.openInfoWindowHtml(content);
		});

		// Put marker on the points array
		this.points.push(marker);
		return marker;
	}

	/**
	* Refresh markers on the map
	*/
	this.refreshPoints = function()
	{
		// Clear any overlay on the map
		this.map.clearOverlays();
		
		// Download XML file and parse it
		GDownloadUrl(geo_url_xml, function(data) {
			geo.parseXml( GXml.parse(data) );
		});
	}

	/**
	* Initialize the map
	*/
	this.init = function()
	{
		this.map = new GMap2(document.getElementById("map_canvas"));
		this.map.setCenter(new GLatLng(initial_lat,initial_lng), initial_zoom);
		this.map.setUIToDefault();
		this.map.disableScrollWheelZoom();
		this.points = new Array();
		
		this.refreshPoints();
	}
	
	this.init();
}
