var MapFunc = new Array();
var arrayCircuits = new Array();

function StkMapFunc(Obj)
{
	if(typeof(Obj) == "function")
	{
	    MapFunc[MapFunc.length] = Obj; 
	}
}

function initMapAction()
{
	for(i=0; i<MapFunc.length; i++) 
	{
		//alert(FuncOL[i]);
		MapFunc[i]();
	} 
}

function init_MapFicheCircuit()
{
	var zoom = 14;
	
	if ( document.getElementById('geox') )
		var geox = document.getElementById('geox').innerHTML ;

	if ( document.getElementById('geoy') )
		var geoy = document.getElementById("geoy").innerHTML ;

	if( document.getElementById("zoom").innerHTML != 0 )
		var  zoom = parseInt(document.getElementById("zoom").innerHTML);

	map = new GMap2(document.getElementById("map_canvas"));
	if(geox != 0 && geoy != 0)
	{
		var center = new GLatLng(geox, geoy);
		map.setCenter(center, zoom);
	}
	
	var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));

	map.addControl(new GLargeMapControl);
	map.addControl(new GMapTypeControl(), bottomRight);
	map.enableDoubleClickZoom();
	map.setMapType(G_SATELLITE_MAP);


    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 26);
    baseIcon.shadowSize = new GSize(37, 26);
    baseIcon.iconAnchor = new GPoint(9, 26);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);
    baseIcon.image = "/images/marquer_default.png";

/*	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
*/

	// Icon Draggable
//	var marker = new GMarker(center, {draggable: true});
	var marker = new GMarker(center, { draggable: false, icon:baseIcon });

	GEvent.addListener(marker, "dragstart", function() {
	  map.closeInfoWindow();
	});
	
	//GEvent.addListener(marker, "dragend", function() {
	//  marker.openInfoWindowHtml("Just bouncing along..." + marker.getPoint().x);
	//});

	map.addOverlay(marker);
//window.setTimeout(setupWeatherMarkers, 0);
}

function init_MapLstCircuit()
{
    // Create a base icon for all of our markers that specifies the
    // shadow, icon dimensions, etc.
    var baseIcon = new GIcon(G_DEFAULT_ICON);
    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    baseIcon.iconSize = new GSize(20, 26);
    baseIcon.shadowSize = new GSize(37, 26);
    baseIcon.iconAnchor = new GPoint(9, 26);
    baseIcon.infoWindowAnchor = new GPoint(9, 2);


	var MyOverlay = function(marker, html) {
		this.marker = marker;
		this.html = html;
	}

	MyOverlay.prototype = new GOverlay();
	MyOverlay.prototype.initialize = function(map) {
		var div = document.createElement("div");
		div.className = 'map-info-box';
		div.innerHTML = this.html;
		//alert( this.html );
		
		// offsets based on popup div dimensions
		offsetX = 240;
		offsetY = 208;
		
		div.style.top = (map.fromLatLngToDivPixel(this.marker.getPoint()).y - offsetY) + 'px';
		div.style.left = (map.fromLatLngToDivPixel(this.marker.getPoint()).x - offsetX) + 'px';
		
		div.onclick = closeOverlay;
		
		this._map = map;
		this._div = div;
		
		map.getPane(G_MAP_FLOAT_PANE).appendChild(div);  
	}
	
	MyOverlay.prototype.remove = function(){
		this._div.parentNode.removeChild(this._div);
	}
	
	MyOverlay.prototype.redraw = function() {
		//haven't had need for this yet
	}
	
	function closeOverlay() {
		if (currentMarker) {
			map.removeOverlay(currentMarker.overlay);
			currentMarker.show();
		}
	}	
	
    // Creates a marker whose info window displays the letter corresponding
    // to the given index.
    function createMarker(point, idCircuit) {

		var PersoIcon = new GIcon(baseIcon);
		PersoIcon.image = "/images/marquer_default.png";
		markerOptions = { icon:PersoIcon };
		var marker = new GMarker(point , markerOptions);

		// The new marker "mouseover" listener
		//GEvent.addListener(marker,"mouseover", function() {
		//marker.openInfoWindowHtml(name+"<br><br>cliquez sur le spot pour obtenir les infos");
		//}); 

		GEvent.addListener(marker, "click", function() {
			/*var selection = {
							image: '/home_01.gif',
							link: '/ok',
							name:'nom',
							location:'ici' 
							};
			html = InfoTemplate.evaluate(selection);*/
			html = showInfoBox( idCircuit );
			//alert(html);

			if (typeof MyOverlay !== 'undefined') {
				
				if (typeof currentMarker !== 'undefined') {
					closeOverlay();
				}
				if (!this.overlay) {
					// just recording this for use in the closeOverlay function
					this.overlay = new MyOverlay(this, html);
					
				}
				currentMarker = this;
				map.panTo(new GLatLng(this.getPoint().lat(), this.getPoint().lng()));
				map.addOverlay(this.overlay);
				this.hide();
			} else {
				marker.openInfoWindowHtml(html);
			}
		});

      return marker;
    }

	GDownloadUrl("/lstcircuits.xml", function(data) {
		var xml = GXml.parse(data);
		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")), 9);
			var marker = createMarker(point, parseInt(markers[i].getAttribute("id")) );
			map.addOverlay(marker);
		}
	});
}

function showInfoBox( idCircuit )
{
	var url = "/ajax/GetContentCircuits.php";
	var params = '?gere=DrawPopMapInfos&id=' + idCircuit;
 	retour = SendRequest(url + params);
	return retour;
}

function showOverlay(markerKey)
{
	alert('ok');
/*
	if (activeMarker)
	{
		closeOverlay();
	}
	
	patt = /[a-z]+_\d+/;
 
	if (markerKey != null && typeof(markerKey) != 'object')
	{
		activeMarker = Map.getMarker(markerKey);
	}
	else
	{
		activeMarker = this;
	}
	
	if (!activeMarker) return;
 
	activeMarker.hide();
 
	if (link = $($(activeMarker.place.key + '-link')))
	{
		link.addClassName('active');
	}
	
	if (activeMarker.place.type == 'Hotel')
	{
		activeMarker.overlay = new InfoBox(activeMarker, 517, 212, 'map-info-box', hotelInfoTemplate, 240, -4);
	}
	else if (activeMarker.place.type == 'Destination')
	{
		activeMarker.overlay = new InfoBox(activeMarker, 517, 212, 'map-info-box destination', destInfoTemplate, 240, -4);
	}
	else
	{
		activeMarker.overlay = new InfoBox(activeMarker, 375, 187, 'map-info-box small', infoTemplate, 173, -6);
	}
	
	Map.setActiveKey(activeMarker.key);
	selectTab(typeToTab(activeMarker.place.type));
	Map.getMap().addOverlay(activeMarker.overlay);
	Map.getMap().panTo(new GLatLng((activeMarker.getPoint().lat() + (Map.getLatSpan() / 5) ), activeMarker.getPoint().lng()));
*/
}

function closeOverlay()
{	
	if (activeMarker)
	{
	 	Map.getMap().removeOverlay(activeMarker.overlay);
		activeMarker.show();
	}
	Map.setActiveKey(false);
	activeMarker = false;
	return false;
}

/*##############################################################*/

function init_MapWeather()
{
  var IMAGES = [ "sun", "rain", "snow", "storm" ];
  var ICONS = [];
	
function getWeatherIcon() {
  var i = Math.floor(IMAGES.length*Math.random());
  if (!ICONS[i]) {
    var icon = new GIcon();
    icon.image = "http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/examples/images/"
        + IMAGES[i] + ".png";
    icon.iconAnchor = new GPoint(16, 16);
    icon.infoWindowAnchor = new GPoint(16, 0);
    icon.iconSize = new GSize(32, 32);
    icon.shadow = "http://gmaps-utility-library.googlecode.com/svn/trunk/markermanager/release/examples/images/"
        + IMAGES[i] + "-shadow.png";
    icon.shadowSize = new GSize(59, 32);
    ICONS[i] = icon;
  }
  return ICONS[i];
}

  /*function getRandomPoint() {
    var lat = 48.25 + (Math.random() - 0.5)*14.5;
    var lng = 11.00 + (Math.random() - 0.5)*36.0;
    return new GLatLng(Math.round(lat*10)/10, Math.round(lng*10)/10);
  }*/

  function getWeatherMarkers(n) {
    var batch = [];
    for (var i = 0; i < n; ++i) {
      batch.push(new GMarker(getRandomPoint(), { icon: getWeatherIcon() }));
    }
    return batch;
  }

  function getWeatherMarkers2( center ) {
    var batch = [];
      batch.push(new GMarker(center, { icon: getWeatherIcon() }));
    return batch;
  }

  function setupWeatherMarkers() {
    mgr = new GMarkerManager(map);
	var center = new GLatLng(45, 2);
    mgr.addMarkers(getWeatherMarkers2( center ), 3);
    //mgr.addMarkers(getWeatherMarkers(200), 6);
    //mgr.addMarkers(getWeatherMarkers(1000), 8);
    mgr.refresh();
  }	
	window.setTimeout(setupWeatherMarkers, 0);
}
