// JavaScript Document
//<![CDATA[
var map, cluster, icons={}, markersArray=[];

var iconOptions = {
	width:20,
	height:20,
	primaryColor:"#FF0000",
	shape:"circle"
};

function initialize() {
	if (google.maps.BrowserIsCompatible()) {
		map = new google.maps.Map2(document.getElementById('map'));
		var customUI = map.getDefaultUI();
		map.setUI(customUI);
		//map.setCenter(new google.maps.LatLng(40.23,-5.23), 6);
		map.setCenter(new GLatLng(0, 0), 0, G_NORMAL_MAP);


//    map.addControl(new GSmallMapControl());
//    map.enableDoubleClickZoom();
 //   map.enableScrollWheelZoom();
 //   map.addControl(new PanoMapTypeControl());


/*      var layers = [];      
          layers[0] = new GLayer("org.wikipedia.es");
          layers[0].Visible = false;
          layers[0].Added = false;
          
          layers[1] = new GLayer("org.wikipedia.en");
          layers[1].Visible = false;
          layers[1].Added = false;
          
          layers[2] = new GLayer("com.panoramio.all");
          layers[2].Visible = false;
          layers[2].Added = false;

          layers[3] = new GLayer("com.panoramio.popular");
          layers[3].Visible = false;
          layers[3].Added = false;
      
      // === Create the layerControl, but don't addControl() it ===
      // = Pass it an array of names for the checkboxes =
      var layerControl = new LayerControl(["Wiki ES", "Wiki EN", "Fotos", "Populares"]);*/

      var layers = [];      
          layers[0] = new GLayer("org.wikipedia.es");
          layers[0].Visible = false;
          layers[0].Added = false;
          
          layers[1] = new GLayer("com.panoramio.all");
          layers[1].Visible = false;
          layers[1].Added = false;
      
      // === Create the layerControl, but don't addControl() it ===
      // = Pass it an array of names for the checkboxes =
      var layerControl = new LayerControl(["Wiki ES", "Fotos"]);

      // === Create the MoreControl(), and do addControl() it ===
      map.addControl(new MoreControl());



		var marker, category;

		for (var i=0; i<json.length; i++) {
			category = json[i].tipo;
			
			var html  = '<div class="contDatosPunto clearfix"><a href="' + json[i].url_local + '"><span class="nombrePunto">' + json[i].nombre + '<\/span></a><br \/><img src="fotos\/' + json[i].imagen1 + '" alt="' + json[i].nombre + '" class="imgPunto" \/><br \/>' + json[i].direccion +  '<br \/>' + json[i].localidad + ', ' + json[i].provincia + '<\/div>';

			marker = newMarker(new google.maps.LatLng(json[i].lat, json[i].lon), json[i].id, category, html, json[i].nombre);
			markersArray.push(marker);
		}		
		
		for(i=markersArray.length-1; i>=0; i--){
			map.addOverlay(markersArray[i]);
		}
		
		cluster = new ClusterMarker(map, { markers:markersArray } );
		cluster.fitMapToMarkers();

		map.savePosition();

	}
}

function defaultIcon(tipoLocal) {
	icon = new google.maps.Icon();
	icon.image = "img/"+tipoLocal+".png";
	icon.shadow = "img/house_shadow.png";
	icon.iconSize = new google.maps.Size(30, 30);
	//icon.shadowSize = new google.maps.Size(52, 35);
	icon.iconAnchor = new google.maps.Point(30 >> 1, 30 >> 1);
	icon.infoWindowAnchor=new GPoint(15, 5);

	return icon;
}

function newMarker(markerLocation, markerId, markerTipo, html, markerNombre) {
	var marker = new google.maps.Marker(markerLocation, {title:markerNombre, icon:defaultIcon(markerTipo)});
	marker.category = markerTipo;
	google.maps.Event.addListener(marker, 'click', function() {
		///marker.openInfoWindowHtml('<p>Marker['+markerId+'] clicked.<\/p>');
		marker.openInfoWindowHtml(html);
	});
	return marker;
}

function toggleClustering() {

	cluster.clusteringEnabled = !cluster.clusteringEnabled;
	cluster.refresh(true); // true required to force a full update of the markers - otherwise the update would occur next time that the map is zoomed or the active markers change
}

function toggleMarkers(checkbox, category) {
	if(checkbox.checked) {
		for(var i=markersArray.length-1; i>=0; i--) {
			if(markersArray[i].category===category) {
				markersArray[i].show();
			}
		}
	} else {

		for(var i=markersArray.length-1; i>=0; i--) {
			if(markersArray[i].category===category) {
				markersArray[i].hide();
			}
		}
	}
	cluster.refresh();
}

//]]>