var centerLatitude=41.387917;
var centerLongitude=2.169919;
var startZoom=12;

var map;

function init()
{
		if(GBrowserIsCompatible())
		{
			map=new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
			retrieveMarkers();
		}                                     
}

function retrieveMarkers(){
	var request=GXmlHttp.create();
	
	//tell the request where to retrieve date from
	request.open('GET','xml/map_data.php?date=09-02-2010&date2=28-02-2010&id_lugar=&tipo=6',true);
	
	//tell the request what to do when the states changes
	request.onreadystatechange=function()
	{
		if(request.readyState==4)
		{
			var xmlDoc=request.responseXML;
			
			var markers=xmlDoc.documentElement.getElementsByTagName("marker");
			
			for(var i=0; i<markers.length; i++)
			{
				var lat=markers[i].getAttribute("lat");
				
				var lng=markers[i].getAttribute("lng");
				
				var latlng=new GLatLng(parseFloat(lat),parseFloat(lng));
				
				var name=markers[i].getAttribute("name");
				
				var id_location=markers[i].getAttribute("id_location");
				
				var address=markers[i].getAttribute("address");
				
				var html='<h3><a href="index.php?date=&date2=&id_lugar='+id_location+'">'+name+'</a></h3>';
                html+="----------";
                html+='<p class="windowinfo">'+address+'</p>';
				html+="----------<br />";                
				//alert(markers[i].childNodes.length);
				for(j=0;j<markers[i].childNodes.length;j++)
				{
					//if(j>0)
					//	hmtl+="<br>-----------------------<br>";
					//alert(markers[i].childNodes[j].getAttribute("name"));
					html+='<p class="windowinfo"><b style="color:#00abe2;"><a href="concierto.php?id_lugar='+id_location+'&id_concierto='+markers[i].childNodes[j].getAttribute("id_concierto")+'">'+markers[i].childNodes[j].getAttribute("name")+'</a></b></p>';
					html+='<p class="windowinfo">Fecha: '+markers[i].childNodes[j].getAttribute("fecha")+'</p>';
					if(markers[i].childNodes[j].getAttribute("hora")!='')
                    	html+='<p class="windowinfo">Hora: '+markers[i].childNodes[j].getAttribute("hora")+'</p>';
                   
                   	html+='<p class="windowinfo">Precio: '+markers[i].childNodes[j].getAttribute("precio")+'</p>';
                    if(markers[i].childNodes[j].getAttribute("festival"))
                    	html+='<p class="festival windowinfo">'+markers[i].childNodes[j].getAttribute("festival")+'</p>';
                    	
                    html+="---------------------<br />";
				}
				
				var marker=createMarker(latlng,html);
				map.addOverlay(marker);
								
			}//for
		}//if
	}//function
	request.send(null);
}


function createMarker(latlng,html)
{
	var marker=new GMarker(latlng);
	GEvent.addListener(marker,'mouseover',function (){
		var markerHTML=html;
		marker.openInfoWindowHtml(markerHTML,{autoScroll:true,maxHeight:300,maxWidth:200});
	})
	return marker;
}
window.onload=init;
window.onunload=GUnload;