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 = 20;
var hpogmap_iconh = 32;
var hpogmap_spotx = 18;
var hpogmap_spoty = 4;

var hpogmap_markersArray = null;
var hpogmap_markers = null;
var hpogmap_gicon = null;
var hpogmap_key = 0;

function goSearchStreet(){
	
	if(document.getElementById('txtSearchStreet')){
		
		if(document.getElementById('txtSearchStreet').value == '')
			alert('Veuillez renseigner le nom de la rue');
		else document.getElementById('formSearchStreet').submit();
		
	}
	
}

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);
					}
				}
				
				hpogmap_viewAll();
				
				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 icon = marker.getElementsByTagName('Icon')[0];
			if ( icon )
			{
				var iconUrl = icon.getElementsByTagName('href')[0].firstChild.nodeValue;
				var iconWidth = icon.getElementsByTagName('width')[0].firstChild.nodeValue;
				var iconHeight = icon.getElementsByTagName('height')[0].firstChild.nodeValue;
				var iconX = icon.getElementsByTagName('x')[0].firstChild.nodeValue;
				var iconY = icon.getElementsByTagName('y')[0].firstChild.nodeValue;
			}
			else
			{
				var iconUrl = hpogmap_icon;
				var iconWidth = hpogmap_iconw;
				var iconHeight = hpogmap_iconh;
				var iconX = hpogmap_spotx;
				var iconY = hpogmap_spoty;
			}
			
			var html = description;
			
			// Création des markers
			var hpogmap_gicon = new GIcon(G_DEFAULT_ICON, iconUrl);
			hpogmap_gicon.iconSize = new GSize(Math.ceil(iconWidth), Math.ceil(iconHeight));
			hpogmap_gicon.shadowSize = new GSize(Math.ceil(iconX), Math.ceil(iconY));
			
			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);
				t_markers[i].push(html);
			}
		}
	}
	
	hpogmap_markersArray = t_markers;
	// 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(hpogmap_lat, hpogmap_lng);
	hpogmap_map.setCenter(point, hpogmap_zoom);
}

/**
 * Fonctions spécifiques à la liste de sélection de POI
 */
function hpogmap_changePoint(longitude, latitude, zoom, infos)
{
	var point = new GLatLng(latitude, longitude);
	hpogmap_map.setCenter(point, zoom);
	
	markerInfos = hpogmap_getMarkerByInfos(longitude, latitude);
	if ( markerInfos )
	{
		marker  = markerInfos[1];
		html	= markerInfos[2];
		marker.openInfoWindowHtml(html);
	}
}

function hpogmap_getMarkerByInfos(longitude, latitude)
{
	if (hpogmap_markersArray) {
		var nbMarkers = hpogmap_markersArray.length;
		
		for (var i = 0; i < nbMarkers; i++) {
			latLng = hpogmap_markersArray[i][1].getLatLng();
			
			if (latLng.x == longitude && latLng.y == latitude)
				return hpogmap_markersArray[i];
		}
	}
	
	return null;
}



/************************************
*** fonction afficher/masquer le menu
************************************/
var hpogmap_menuOpened = 1;

function hpogmap_menuHide(id)
{
	document.getElementById('hpogmap_menu' + id).style.display = 'none';
}

function hpogmap_menuShow(id)
{
	if ( id == hpogmap_menuOpened ){
		
		return false;
	}
		
		
	if ( hpogmap_menuOpened )
		hpogmap_menuHide(hpogmap_menuOpened);
		
	if ( document.getElementById('hpogmap_menu' + id) )
	{
		document.getElementById('hpogmap_menu' + id).style.display = 'block';
		hpogmap_menuOpened = id;
	}
}