var map


$(function(){
// on load		
	if(postalCode != '' && gmapmsg == ''){
		// cache l'image de la carte
		$("carte_message").hide();
		// affiche la carte
		// ***** Use SVG to draw and fill polygons
		_mSvgEnabled = true;
		_mSvgForced = true;
		
		map = new GMap2(document.getElementById("map"));
		maptype = new GMapType(G_NORMAL_MAP.getTileLayers(), G_NORMAL_MAP.getProjection(), 'Zone_elagage', {'maxResolution': 14, 'minResolution': 6})
		map.setMapType(maptype); 
		map.addControl(new GLargeMapControl());
		//map.addControl(new GMapTypeControl());
		
		//map.setCenter(new GLatLng(45.54505503, -73.60984188), 6);
		
		postalLatLng = new GClientGeocoder()
		postalLatLng.getLatLng(postalCode,function(point){	
			newmaptype = new GMapType(G_NORMAL_MAP.getTileLayers(), G_NORMAL_MAP.getProjection(), 'Zone_elagage', {'maxResolution': 13, 'minResolution': 13})
			map.setCenter(point, 13, newmaptype);		
			geoPostalCode = new GMarker(point);
			map.addOverlay(geoPostalCode);
			//geoPostalCode.bindInfoWindow(postalCode);			
		}); 
		//alert(postalLatLng);

		//map.setCenter(new GLatLng(45.54505503, -73.60984188), 6);

		getPoints();

		
		
		
	}else{
		// affiche l'image de la carte
		$("carte_message").show();			
	}
	
	
	// remet le code postal
	$('#txt_codepostal').attr('value', postalCode);			
	// Ajouter le "Masquage" du champs Code Postal
	$('#txt_codepostal').mask("a9a 9a9");
	$("document").unload(function(){GUnload()});
});
	
function changerAffichageAnnee(caseacocher, idAnnee){
	if(polygons[idAnnee]){
		if(caseacocher.checked){
			polygons[idAnnee].show()
		}else{
			polygons[idAnnee].hide()
		}
	}
}
	
function draw_polygones(){
	if(postalCode != '' && gmapmsg == ''){
		
		
		
		/* cacher en attandants le nouveaux kml, a effacer apres
		
		if(aAnnee2009.length > 0){
			polygons[0] = new GPolygon(aAnnee2009,'#d48b51', 2, 1, '#d48b51', 0.5);
			map.addOverlay(polygons[0]);
			polygonDisplayed[0] = true;
			//polyBounds = polygons[1].getBounds()
			//polyCenter = polyBounds.getCenter()
			//map.setCenter(new GLatLng(polyCenter.lat(),polyCenter.lng()), map.getBoundsZoomLevel(polyBounds));
		}	
		
		*/
		
		
		
		if(aAnnee2010.length > 0){
			polygons[1] = new GPolygon(aAnnee2010,'#658ea4', 2, 1, '#658ea4', 0.5);
			map.addOverlay(polygons[1]);
			polygonDisplayed[1] = true;
			//polyBounds = polygons[2].getBounds()
			//polyCenter = polyBounds.getCenter()
			//map.setCenter(new GLatLng(polyCenter.lat(),polyCenter.lng()), map.getBoundsZoomLevel(polyBounds));
		}
		if(aAnnee2011.length > 0){
			polygons[2] = new GPolygon(aAnnee2011,'#990000', 2, 1, '#990000', 0.5);
			map.addOverlay(polygons[2]);
			polygonDisplayed[2] = true;
			//polyBounds = polygons[2].getBounds()
			//polyCenter = polyBounds.getCenter()
			//map.setCenter(new GLatLng(polyCenter.lat(),polyCenter.lng()), map.getBoundsZoomLevel(polyBounds));
		}
		// deactive les case des annees sans zone
		for(i=0; i<3; i++){
			if(!polygons[i]){
				$('#polygon'+i).attr('checked',0);
				$('#polygon'+i).attr('disabled',1);
			}
		}
	}
}
$(window).load(function () {
  draw_polygones()
});

function estCodePostal(codepostal) {
	if (codepostal.search) {
		if (codepostal.search(/^[A-Za-z]\d[A-Za-z]\s*\d[A-Za-z]\d$/) != -1){
			return codepostal;
		}else{
			gmapmsg = 'non-valide'
			return '';
		}
	}
}