var chcoms;
var regionadms;
var clusterer;
var om=null;
var polys = [];
var regions = [];
var redIcon=new GIcon(G_DEFAULT_ICON);
redIcon.image='./img/red.png';
redIcon.shadow='./img/shadow.png';
redIcon.iconSize=new GSize(20,20);
redIcon.shadowSize=new GSize(30,20);
redIcon.iconAnchor=new GPoint(10,20);
redIcon.infoWindowAnchor=new GPoint(5,10);
//redIcon.infoShadowAnchor=new GPoint(55,50);

function Setup()
{
	try
	{
			
		if(!GBrowserIsCompatible())
			{
				mapDiv.innerHTML='Sorry, your browser is not compatible with Google Maps.';
				return;
			}
	
		var mapDiv=document.getElementById('map');
		om=null;
		om=new OverlayMessage(mapDiv);
		clusterer=new Clusterer(map);
		om.Set('Merci de patienter...');
		var currentLocation =  document.location.href;
		currentLocation += 'data_fccq.xml';
		HttpGet(currentLocation,RequestChecker);

	}
	catch(e)
	{
		GLog.write('Setup:\n'+Props(e));
	}
}

function RequestChecker(request)
{
	try
	{	
	var xmlDoc=request.responseXML.documentElement;
	var chcomElements=xmlDoc.getElementsByTagName('ch');
	chcoms=[];
	for(var s=0;s<chcomElements.length;++s)
	{
		var chcom=new Object();
		
		coords = chcomElements[s].getElementsByTagName("coord");

		var type = chcomElements[s].getElementsByTagName("t");
		var name = chcomElements[s].getElementsByTagName("n");
		var adresse = chcomElements[s].getElementsByTagName("adr");
		var site = chcomElements[s].getElementsByTagName("web");

		chcom.name=name[0].firstChild.nodeValue
		chcom.type=type[0].firstChild.nodeValue
		chcom.adresse=adresse[0].firstChild.nodeValue
		chcom.site=site[0].firstChild.nodeValue
		//alert(chcom.name);
		
		
			chcom.lat=parseFloat(coords[0].getAttribute('lat'));
			chcom.lng=parseFloat(coords[0].getAttribute('lng'));
			chcom.location=new GLatLng(chcom.lat,chcom.lng);
			chcom.liste=coords[0].getAttribute('lst');
			
			
		chcoms.push(chcom);
	}
	chcoms.sort(CompareNames);

	var panelHtml='<font size="-1">';
	for(var s=0;s<chcoms.length;++s)
		{
			var chcom=chcoms[s];
			var marker=new GMarker(chcom.location,{icon:redIcon,title:chcom.name});
			chcom.marker=marker;
			GEvent.addListener(marker,'click',MakeCaller(PopUp,s));
			clusterer.AddMarker(marker,chcom.name);
			var link='<a style="text-decoration:none; color:black;" href="javascript:PopUp( '+s+' )">';
			var html='<span style="font-family:Arial Narrow;">'+link+'<img border="0" width="15px" src="./img/red.png" style="vertical-align:middle;"></a> '+link+chcom.name+'</a></span><br/>';
			panelHtml+=html;
		}
	panelHtml+='</font>';
	document.getElementById('panel').innerHTML=panelHtml;
	om.Clear();
	}

	catch(e)
	{
		GLog.write('RequestChecker:\n'+Props(e));
	}
}

function CompareNames(a,b)
{
if(a.name<b.name)
return-1;else if(a.name>b.name)
return 1;else
return 0;
}

function PopUp(s)
{
	try
	{
		var chcom=chcoms[s];
		map.setCenter(new GLatLng(chcom.lat, chcom.lng), 9);
		
		var html_site = "";
		
		
		if (chcom.site != ""){
		html_site='<tr><td style="font-family:Arial Narrow; font-size:12px;"><a href="'+chcom.site+'" target="_blank" style="text-decoration:none">Site Internet</a></td></tr>';
		}
		
		var html='<table width="250"><tr><td style="font-family:Arial Narrow; font-size:13px;"><b>'+chcom.type+'&nbsp;'+chcom.name+'</b></td></tr><tr><td style="font-family:Arial Narrow; font-size:12px;">'+chcom.adresse+'</td></tr>'+html_site+'</table>';
		
		chcom.marker.openInfoWindowHtml(html,{maxWidth:300});
		
					if (polys.length != 0){
						for (var z=0; z < polys.length; z++) {
						map.removeOverlay(polys[z]);
					}
						}
					
						var tabPoly = chcom.liste.split(" ");
							for(i=0; i < tabPoly.length;i++) {
							
							var tabZ=[];
							
								var tabCoord = tabPoly[i].split(";");
								
									for(j=0; j < tabCoord.length;j++) {
									var tab;
									tabZ[j] = 0;
									tab= tabCoord[j].split(",");
									tabZ[j] = new GLatLng(parseFloat(tab[1]), parseFloat(tab[0]));
									
								}
								poly = new GPolygon(tabZ, "red", 2, 1, "white", .3);
								polys.push(poly);
								map.addOverlay(poly);
							}
	}

	catch(e)
	{
		GLog.write('PopUp:\n'+Props(e));
	}
}
