var map='';
var custommap;

var baseIcon = new GIcon();
baseIcon.image = "/medias/marker.gif";
//baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(8,9);
baseIcon.shadowSize = new GSize(8,9);
baseIcon.iconAnchor = new GPoint(8,9);
baseIcon.infoWindowAnchor = new GPoint(9,2);


function load(){
	if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
		//////////////////////////////////////////////
		var copyright = new GCopyright(1,
					  new GLatLngBounds(new GLatLng(10000,10000), 
										new GLatLng(10000,10000)),
					  1,
					  " 2008 apg");
					  
		var copyrightCollection = new GCopyrightCollection('Apg');
		copyrightCollection.addCopyright(copyright);
		
		function CustomGetTileUrl(a,b){
			var f = "/medias/map/10"+a.x+"_10"+a.y+"_"+b+".gif";
			//document.write(f+"<br />");
			//alert(f);
			return f;
		}
			
		var tilelayers = [new GTileLayer(copyrightCollection , 1, 2)];
		tilelayers[0].getTileUrl = CustomGetTileUrl;
		
		custommap = new GMapType(tilelayers, new GMercatorProjection(12), "Makeawish");
		
		map.addMapType(custommap);

		map.setCenter(new GLatLng(49.9, 2.3509871), 1, custommap);
		
		var lieux = getLieux();
		//alert(lieux);
		map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT,new GSize(0,40)));
		
	}
}

var result = 'titi';
function getLieux(){
	var url = "/front/fr/map/async_getLieux/0";
	var jSonRequest = new Json.Remote(url, {
		method: 'get',
		evalScripts: false,
		onComplete:function(response){
			var result = Json.evaluate( this.response.text );
			for(i=0;i<result.length;i++){
				var bulle = '<table width="200" border="0" cellpadding="0" cellspacing="0" class="bulle">'
							  +'<tr valign="top">'
							    +'<td width="51"><img src="/medias/flag/'+result[i]["pays"]+'.png" width="48" height="48" /></td>'
							    +'<td width="2">&nbsp;</td>'
							    +'<td width="133">'+result[i]['name']+'<br />'+result[i]['sstitre']+'<br />'+result[i]['ville']+'</td>'
							  +'</tr>'
							  +'<tr valign="top">'
							    +'<td>&nbsp;</td>'
							    +'<td>&nbsp;</td>'
							    +'<td>&nbsp;</td>'
							  +'</tr>'
							  +'<tr valign="top">'
							    +'<td colspan="3"><a target="_blank" href="'+result[i]["site_web"]+'">'+result[i]["site_web"]+'</a></td>'
							  +'</tr>'
							+'</table>';
				
				addMarker(result[i]['latitude'],result[i]['longitude'],bulle)			
			}
		}
	}).send();			
}

function addMarker(latitude, longitude, bulle){
	var point = new GLatLng(latitude,longitude);
	var marker = new GMarker(point, baseIcon);
	var infoTabs = [
		new GInfoWindowTab("description", bulle)
	];
	GEvent.addListener(marker, "click", function() {
		map.closeInfoWindow();
		map.openInfoWindowTabsHtml(point,infoTabs); 	
	});		
	
	map.addOverlay(marker);
	return;
}

window.addEvent('domready', function() {
    load();
});

