var hpogmap_map = null;
var hpogmap_kml = "";
var hpogmap_lat = 48.8542748;
var hpogmap_lng = 2.3491323;
var hpogmap_zoom = 7;
var hpogmap_control = 0x01;
var hpogmap_mode = '';
var hpogmap_icon = '';
var hpogmap_iconw = 32;
var hpogmap_iconh = 32;
var hpogmap_spotx = 18;
var hpogmap_spoty = 4;

var hpogmap_markers = null;
var hpogmap_gicon = null;
var hpogmap_key = 0;

function hpogmap_load(key) {
	var mapId = document.getElementById("map");
	
	if (!mapId) {
		setTimeout("hpogmap_load("+key+")",200);
		return;
	}
	
	if (GBrowserIsCompatible()) {
		hpogmap_map = new GMap2(mapId);
		
		if (hpogmap_map) {
			hpogmap_key = key;
			hpogmap_markers = null;
			
			if (hpogmap_control & 0x01)
				hpogmap_map.addControl(new GSmallMapControl());
			if (hpogmap_control & 0x02)
				hpogmap_map.addControl(new GMapTypeControl());
			if (hpogmap_control & 0x04)
				hpogmap_map.addControl(new GOverviewMapControl());
			
			var point = new GLatLng(hpogmap_lat, hpogmap_lng);
			hpogmap_map.setCenter(point, hpogmap_zoom);
			
			if (hpogmap_mode == 'kmlfile') {
				var kml = new GGeoXml(hpogmap_kml);
				if (kml)
					hpogmap_map.addOverlay(kml);
			}
			else if (hpogmap_mode == 'pidfile') {
				if (hpogmap_icon != '') {
					hpogmap_gicon = new GIcon(G_DEFAULT_ICON, hpogmap_icon);
					if (hpogmap_gicon) {
						hpogmap_gicon.iconSize = new GSize(hpogmap_iconw, hpogmap_iconh);
						hpogmap_gicon.infoWindowAnchor = new GPoint(hpogmap_spotx, hpogmap_spoty);
					}
				}
				
				GDownloadUrl(hpogmap_kml, hpogmap_parseKml);
			}
		}
	}
	
}

function hpogmap_parseKml(data) {
	var reg=new RegExp("[ ,;]+", "g");
	var domXml = GXml.parse(data);
	var	t_markers = new Array();
	
	if (domXml) {
		var markers = domXml.documentElement.getElementsByTagName('Placemark');
		
		for (var i = 0;i < markers.length;i++) {
			var marker = markers[i];
			var key = marker.getAttribute('id');
			var name = marker.getElementsByTagName('name')[0].firstChild.nodeValue;
			var description = '';
			if (marker.getElementsByTagName('description').length)
				description = marker.getElementsByTagName('description')[0].firstChild.nodeValue;
			var points = marker.getElementsByTagName('Point');
			var coordinates = points[0].getElementsByTagName('coordinates');
			var t_coords = coordinates[0].firstChild.nodeValue.split(reg);
			
			var html = '<strong>'+name+'</strong><br/>'+description;
			
			// Création des markers
			var point = new GLatLng(parseFloat(t_coords[1]), parseFloat(t_coords[0]));
			var gmarker = new GMarker(point, hpogmap_gicon);
			
			if (gmarker) {
				gmarker.bindInfoWindowHtml(html);
				hpogmap_map.addOverlay(gmarker);
				
				// Création de la collection de markers !
				t_markers[i] = new Array();
				t_markers[i].push(key);
				t_markers[i].push(gmarker);
			}
		}
	}
	
	// On active l'info bulle de la clé
	hpogmap_popKey(hpogmap_key);
}

function hpogmap_unload() {
	GUnload();
}

function hpogmap_createInfoMarker(point, info) {
	var marker = new GMarker(point, hpogmap_gicon);
	GEvent.addListener( marker , "click" , function(){marker.openInfoWindowHtml(info);} );
	
	return marker;
}

function hpogmap_getMarker(key) {
	if (hpogmap_markers) {
		var nbMarkers = hpogmap_markers.length;
		var gmarker = null;
		
		for (var i = 0; i < nbMarkers; i++) {
			if (hpogmap_markers[i][0] == key)
				return hpogmap_markers[i][1];
		}
		
		hpogmap_viewAll();
	}
	
	return null;
}

function hpogmap_popKey(key) {
	var gm = hpogmap_getMarker(key);
	
	if (gm) {
		hpogmap_key = key;
		hpogmap_map.setCenter(gm.getLatLng(), hpogmap_zoom);
		GEvent.trigger(gm, "click");
	}
}

function hpogmap_viewAll() {
	var point = new GLatLng(47, 2.5);
	hpogmap_map.setCenter(point, 5);
}
