var load_gmaps = function() {
    load();
}

var unload_gmaps = function() {
    GUnload();
}

function get_query(variable) {
    var query = window.location.search.substring(1);
    if (variable == '*') return query;
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
	var pair = vars[i].split("=");
	if (pair[0] == variable) {
	    return pair[1];
	}
    }
}

function createMarker(point, label, icon) {
  if (icon) {
    var tinyIcon = new GIcon();
    tinyIcon.image = icon;
    tinyIcon.iconSize = new GSize(32, 32);
    tinyIcon.iconAnchor = new GPoint(16, 16);
    tinyIcon.infoWindowAnchor = new GPoint(16, 16);
    var Marker = new GMarker(point, {icon:tinyIcon});
  } else {
    var Marker = new GMarker(point);
  }
  GEvent.addListener(Marker, "click", function() {
    Marker.openInfoWindowHtml(label);
  });
  return Marker;
}

function load() {
    resize();
    if (GBrowserIsCompatible()) {
	//alert("toto");
	if (GMAP_latitude == '' || GMAP_longitude == '') alert("il faut indiquer une lattitute ET une longitude");
	var MAP = new GMap2(document.getElementById("map"),{mapTypes:[G_PHYSICAL_MAP,G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP]});
	var Point = new GLatLng(GMAP_latitude, GMAP_longitude);
	MAP.addControl(new GLargeMapControl());
	if (GMAP_allow_sat) {
	    MAP.addControl(new GMapTypeControl());
	}
	if (typeof GMAP_zoom == "undefined") {
	  GMAP_zoom = 8;
        }
        MAP.hideControls();
        GEvent.addListener(MAP, "mouseover", function(){
          MAP.showControls();
        });
        GEvent.addListener(MAP, "mouseout", function(){
          MAP.hideControls(); 
        });
	MAP.setCenter(Point, GMAP_zoom);
	MAP.enableScrollWheelZoom();
	new GKeyboardHandler(MAP,window);
	if (typeof GMAP_icon != "undefined") {
	  var tinyIcon = new GIcon();
	  tinyIcon.image = GMAP_icon;
	  tinyIcon.iconSize = new GSize(32, 32);
	  tinyIcon.iconAnchor = new GPoint(16, 16);
	  tinyIcon.infoWindowAnchor = new GPoint(16, 16);
	  var mark_icon = new GMarker(Point, {icon:tinyIcon});
          MAP.addOverlay(mark_icon);
        }
	if (typeof GMAP_points != "undefined") {
	    for (i=0;i<GMAP_points.length;i++) {
		if (GMAP_points[i].length > 4) {
		    alert('le point '+i+' dont le nom est '+GMAP_points[i][0]+' à trop de parametres. Maximum 4: nom, latitude, longitude, image');
		} else {
		    var point_mark = new GLatLng(GMAP_points[i][1], GMAP_points[i][2]);
		    if (typeof GMAP_points[i][3] != "undefined") my_icon = GMAP_points[i][3];
		    else if (typeof GMAP_points_icon != "undefined") my_icon = GMAP_points_icon;
		    else my_icon = ''; 
		    MAP.addOverlay(createMarker(point_mark, GMAP_points[i][0], my_icon));
		}
	    }
	}
	if (typeof GMAP_lines != "undefined") {
	    for (i=0;i<GMAP_lines.length;i++) {
		if (GMAP_lines[i].length > 5) {
		    alert('la ligne '+i+' dont le nom est '+GMAP_lines[i][0]+' à trop de parametres. Maximum 5: nom, liste de points, couleur ligne, largeur ligne, image');
		} else {
		    var Pline = new Array();
		    for (j=0;j<GMAP_lines[i][1].length;j+=2) {
			Pline[j] = new GLatLng(GMAP_lines[i][1][j],GMAP_lines[i][1][j+1]);
		    }
		    if (typeof GMAP_lines[i][3] == "undefined") Lwidth = 3;
		    else Lwidth = GMAP_lines[i][3];
		    var polyline = new GPolyline(Pline, GMAP_lines[i][2], Lwidth);
		    MAP.addOverlay(polyline);
		    var point_mark = Pline[Pline.length-1];
		    if (typeof GMAP_lines[i][4] != "undefined") var my_icon = GMAP_lines[i][4];
		    else if (typeof GMAP_lines_icon != "undefined") var my_icon = GMAP_lines_icon;
		    else var my_icon = ''; 
		    MAP.addOverlay(createMarker(point_mark, GMAP_lines[i][0], my_icon));
		}
	    }
	}
	if (typeof GMAP_traces != "undefined") {
	    for (i=0;i<GMAP_traces.length;i++) {
		if (GMAP_traces[i].length > 5) {
		    alert('la trace '+i+' dont le nom est '+GMAP_trace[i][0]+' à trop de parametres. Maximum 5 nom, fichier de trace, couleur ligne, largeur ligne, image');
		} else {
		    if (typeof GMAP_traces[i][4] != "undefined") var trace_icon = GMAP_traces[i][4];
		    else if (typeof GMAP_traces_icon != "undefined") var trace_icon = GMAP_traces_icon;
		    else var trace_icon = ''; 
		    var Url = GMAP_traces[i][1]+'&trace_name='+encodeURI(GMAP_traces[i][0]);
		    if (typeof GMAP_traces[i][2] != "undefined") Url = Url+'&trace_color='+GMAP_traces[i][2].replace(/^[^a-zA-Z0-9]+/g,'');		
		    if (typeof GMAP_traces[i][3] != "undefined") Url = Url+'&trace_width='+GMAP_traces[i][3];
		    if (trace_icon != '') Url = Url+'&trace_icon='+encodeURI(trace_icon);
		    //alert(Url);
		    if (document.getElementById("resbox")) document.getElementById("resbox").innerHTML = Url;
		    var geoXml = new GGeoXml(Url);
		    MAP.addOverlay(geoXml);
		}
	    }
	}
	if (typeof GMAP_kml != "undefined") {
	    if (typeof get_query('mode') != "undefined") {
		GMAP_extra_url = get_query('*');
		if (typeof get_query('north') != "undefined") {
		    mySpace = new GLatLngBounds(new GLatLng(get_query('south'),get_query('west')), new GLatLng(get_query('north'),get_query('east')));
		    var newZoom = MAP.getBoundsZoomLevel(mySpace); 
		    var newCenter = mySpace.getCenter();
		    MAP.setCenter(newCenter,newZoom);
		}
	    } else {
		GMAP_extra_url = 'page='+get_query('page')+'&mode=global_view';
	    }
	    var Url = GMAP_kml+'?'+GMAP_extra_url;
	    if (document.getElementById("resbox")) document.getElementById("resbox").innerHTML = Url;
	    var geoXml = new GGeoXml(Url);
	    MAP.addOverlay(geoXml);
	    //REGION = geoXml.getElementsByTagName("Region");
	    //alert(REGION.length);
	    //MAP.setCenter(REGION.getCenter(), MAP.getBoundsZoomLevel(REGION)); 
	}

	if (document.getElementById("resbox")) {
	    GEvent.addListener(MAP, 'click', function(overlay, point) {
		document.getElementById("resbox").innerHTML = 'latitude :<em>'+point.y.toFixed(5)+'</em> longitude : <em>'+point.x.toFixed(5)+'</em>';
	    });
	}
	if (typeof GMAP_html != "undefined") {
	  if (typeof GMAP_icon != "undefined") {
	    GEvent.addListener(mark_icon, "click", function() {
	      mark_icon.openInfoWindowHtml(GMAP_html);
	    });
            mark_icon.openInfoWindowHtml(GMAP_html);
          } else {
	    MAP.openInfoWindowHtml(MAP.getCenter(),GMAP_html);
	    //MAP.openInfoWindowHtml(new GLatLng(43.5944152,1.450975),GMAP_html);
          }
        }
    } else {
	alert('navigateur incompatible avec Google Maps');
    }
}

function resize() {
    resize_all();
    if (IsIeBug) {
	var Size=3;
    } else {
	var Size=0;
    }
    document.getElementById("map").style.height=findPageHeight()-Size+'px';
}

if (window.addEventListener) {
    window.addEventListener("load", load_gmaps, false);
    window.addEventListener("unload", unload_gmaps, false);
} else if (document.body.onload) {
    document.body.onload = load_gmaps;
    document.body.onunload = unload_gmaps;
}

