// JavaScript Document

/*
* version = "2.18"
* date = "25/04/2006"
* author = "Elvis Del Tedesco"
*/

	function PDMap(elementname, iconfilename, markerfilename) {
		//Inizializza la mappa
		if (GBrowserIsCompatible()) {
		    map = new GMap2(document.getElementById(elementname));
			iconmap = new Hashtable();
			
			pdmap_load(iconfilename, markerfilename);
		} else {
			var message = '&lt;p&gt; Il browser non &egrave; supportato per la visualizzazione della mappa. &lt;/&gt;';
   		    document.getElementById(elementname).innerHTML = message;
		}
		
		//Dichiara la funzione di unload
		this.unload = pdmap_unload;

	}
	
	/**
	* Scarica GMaps
	*
	**/
	function pdmap_unload() {
		GUnload();
	}
	
	/**
	* Si occupa di caucare la mappa ed i dati dal file xml
	*
	**/
    function pdmap_load(iconsfile, markerfile) { 
    	map.addControl(new GSmallMapControl());
	    map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(46.25869538678214, 12.52716064453125), 9, G_HYBRID_MAP);
	  
		// Carica le icone
		GDownloadUrl(iconsfile, function(data, responsecode) {
		if (responsecode != 200) {
			alert ("Impossibile caricare il file delle icone. Verranno usate le icone di default");
		} else {
			pdmap_loadicon (data);
		}
		});
	  	  
  	  	// Carica i dati dei marker
	  	GDownloadUrl(markerfile, function(data, responsecode) {
			if (responsecode != 200) {
				alert ("Impossibile caricare il file dei dati. La mappa sarà senza marker");
			} else {
				pdmap_loadmarker (data);
			}
		});
    }

	/**
	* Carica le icone
	*
	**/
	function pdmap_loadicon(XMLdata) {
		// Costruisce la hash table per le icone
		var xml = GXml.parse(XMLdata);
				
		var iconvect = xml.documentElement.getElementsByTagName("icon");
		for (var i = 0; i < iconvect.length; i++) {
			var name = iconvect[i].getAttribute("name");
			var image = iconvect[i].getAttribute("image");
			var sizeX = parseInt(iconvect[i].getAttribute("sizeX"));
			var sizeY = parseInt(iconvect[i].getAttribute("sizeY"));
			var shadow = iconvect[i].getAttribute("shadow");
			var sizeXShadow = parseInt(iconvect[i].getAttribute("sizeXShadow"));
			var sizeYShadow = parseInt(iconvect[i].getAttribute("sizeYShadow"));
			var anchorX = parseInt(iconvect[i].getAttribute("anchorX"));
			var anchorY = parseInt(iconvect[i].getAttribute("anchorY"));
			
			var icon = new GIcon();
			icon.image = image
 		  	icon.shadow = shadow
	  		icon.iconSize = new GSize(sizeX, sizeY);
			icon.shadowSize = new GSize(sizeXShadow, sizeYShadow);
			icon.iconAnchor = new GPoint(anchorX, anchorY);
			iconmap.put(name, icon);
		}
	}
		
	/**
	* Crea il marker e ci associa l'evento
	* E' stato necessario effettuare una funzione separata per creare il marker in quanto
	* creandolo nella funzione principale non rendeva funzionante l'evento
	*
	**/
	function pdmap_createmarker(point, icon, nomea, uniformrl) {
		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "click", function() {
			map.panTo(point);
			map.openInfoWindowHtml(point, 
				'<b><a href="' + uniformrl + ' " target="_blank">' + nomea + "</a></b><br />" 
				+ "Lat: " + parseFloat(point.lat()) + "N<br />" 
				+ "Lgn: " + parseFloat(point.lng()) + "E<br />" 
			);
		});
		return marker;
	}
			
	/**
	* Carica i marker	
	*
	**/
	function pdmap_loadmarker(XMLdata) {
		var xml = GXml.parse(XMLdata);
						
		var markers = xml.documentElement.getElementsByTagName("marker");
		
		for (var i = 0; i < markers.length; i++) {
	        var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),
                                   parseFloat(markers[i].getAttribute("lng")));
			var icon = iconmap.get(markers[i].getAttribute("type"));
			var markername = markers[i].getAttribute("name");
			var url = markers[i].getAttribute("url");
									
			map.addOverlay(pdmap_createmarker(point, icon, markername, url));
		}
	}
	

/**
    Created by: Michael Synovic
    on: 01/12/2003
   
    This is a Javascript implementation of the Java Hashtable object.
   
    Contructor(s):
     Hashtable()
              Creates a new, empty hashtable
   
    Method(s):
     void clear()
              Clears this hashtable so that it contains no keys.
     boolean containsKey(String key)
              Tests if the specified object is a key in this hashtable.
     boolean containsValue(Object value)
              Returns true if this Hashtable maps one or more keys to this value.
     Object get(String key)
              Returns the value to which the specified key is mapped in this hashtable.
     boolean isEmpty()
              Tests if this hashtable maps no keys to values.
     Array keys()
              Returns an array of the keys in this hashtable.
     void put(String key, Object value)
              Maps the specified key to the specified value in this hashtable. A NullPointerException is thrown is the key or value is null.
     Object remove(String key)
              Removes the key (and its corresponding value) from this hashtable. Returns the value of the key that was removed
     int size()
              Returns the number of keys in this hashtable.
     String toString()
              Returns a string representation of this Hashtable object in the form of a set of entries, enclosed in braces and separated by the ASCII characters ", " (comma and space).
     Array values()
              Returns a array view of the values contained in this Hashtable.
           
*/
function Hashtable(){
    this.clear = hashtable_clear;
    this.containsKey = hashtable_containsKey;
    this.containsValue = hashtable_containsValue;
    this.get = hashtable_get;
    this.isEmpty = hashtable_isEmpty;
    this.keys = hashtable_keys;
    this.put = hashtable_put;
    this.remove = hashtable_remove;
    this.size = hashtable_size;
    this.toString = hashtable_toString;
    this.values = hashtable_values;
    this.hashtable = new Array();
}

/*=======Private methods for internal use only========*/

function hashtable_clear(){
    this.hashtable = new Array();
}

function hashtable_containsKey(key){
    var exists = false;
    for (var i in this.hashtable) {
        if (i == key && this.hashtable[i] != null) {
            exists = true;
            break;
        }
    }
    return exists;
}

function hashtable_containsValue(value){
    var contains = false;
    if (value != null) {
        for (var i in this.hashtable) {
            if (this.hashtable[i] == value) {
                contains = true;
                break;
            }
        }
    }
    return contains;
}

function hashtable_get(key){
    return this.hashtable[key];
}

function hashtable_isEmpty(){
    return (parseInt(this.size()) == 0) ? true : false;
}

function hashtable_keys(){
    var keys = new Array();
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null)
            keys.push(i);
    }
    return keys;
}

function hashtable_put(key, value){
    if (key == null || value == null) {
        throw "NullPointerException {" + key + "},{" + value + "}";
    }else{
        this.hashtable[key] = value;
    }
}

function hashtable_remove(key){
    var rtn = this.hashtable[key];
    this.hashtable[key] = null;
    return rtn;
}

function hashtable_size(){
    var size = 0;
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null)
            size ++;
    }
    return size;
}

function hashtable_toString(){
    var result = "";
    for (var i in this.hashtable)
    {     
        if (this.hashtable[i] != null)
            result += "{" + i + "},{" + this.hashtable[i] + "}\n";  
    }
    return result;
}

function hashtable_values(){
    var values = new Array();
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null)
            values.push(this.hashtable[i]);
    }
    return values;
}
