(function($) {
	
	$.fn.mapaGalicia = function() {
		
		var map = document.getElementById("mapgalicia");
		
		if (GBrowserIsCompatible()) {
	
			var gmap = new GMap2(map);
			gmap.addControl(new GSmallMapControl());
			gmap.addControl(new GMapTypeControl()) ;
			gmap.addControl( new GOverviewMapControl(new GSize(100,100)) );
			//gmap.setCenter (new GLatLng(39.4,-3.017),5);
			gmap.setCenter (new GLatLng(42.875547,-8.543734), 7)
			
			//creo los polígonos de España
			var coordenadas_espana = country.ES.coord;
			for (i=0; i<coordenadas_espana.length; i++){
				gmap.addOverlay(new GPolygon(coordenadas_espana[i],"#dd6600", 2, 0.7, "#99FF00", 0.5));
			}
			
			function makeIcon (image) {
				var icon = new GIcon();
				icon.image = "/img/ong.png";
				icon.shadow = "/img/ong-shadow.png";
				icon.iconSize = new GSize(16, 16);
				icon.shadowSize = new GSize(24, 16);
				icon.iconAnchor = new GPoint(8, 16);
				icon.infoShadowAnchor = new GPoint(0, 0);
				icon.infoWindowAnchor = new GPoint(8, 1);	
				return icon;
			}
			
			function formatTabOne (input) {				
				var html 	 = "<div class=\"bubble\">";
				html 		+= "<h1><img src='" + input.logo + "' style='float:left; margin-right:8px;' />" + input.ong + "</h1>";			
				html 		+= "<p>" + input.direccion +  "<br />";
				html 		+= input.localidad + ", " + input.provincia +  "<br />";
				html 		+= input.codigopostal + "</p>";
				html 		+= "<p><a href='" + input.url + "'>" + input.textourl + "</a></p>";
				html		+= "</div>";					
				return html;			
			}
			
			function formatTabTwo (input) {
				var html 	 = "<div class=\"bubble\">";
				html		+= ""	
				if(input.acciongalicia != null) {
					html 	+= "<h2>" + input.titulogalicia + "</h2>";
					html 	+= "<p>" + input.acciongalicia + "</p>";
				}
				if(input.accionmundo != null) {
					html 	+= "<h2>" + input.titulomundo + "</h2>";
					html 	+= "<p>" + input.accionmundo + "</p>";
				}
				html 		+= "</div>";					
				return html;			
			}
						
			function createMarker(input) {
			
				var marker = new GMarker(input.point, makeIcon(input.markerImage) );						
				var tabs_array	= [ new GInfoWindowTab("A ONGD", formatTabOne(input) ),
									new GInfoWindowTab("Información", formatTabTwo(input) ) ];
							
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowTabsHtml(tabs_array);
				});
				
				return marker;
			}
	
			function parseJson (doc) {
							
				var jsonData = eval("(" + doc + ")")
						
				for (var i = 0; i < jsonData.markers.length; i++) {
					var marker = createMarker(jsonData.markers[i]);
					gmap.addOverlay(marker);
				}			
			}     	
			
			GDownloadUrl("/ongs-galicia.json.php", function(data, responseCode) { 
				parseJson(data);
			});
		
		} else {
			alert("Sorry, your browser cannot handle the true power of Google Maps");
		}
	};

})(jQuery);