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','http://www.conciertosbarcelona.es/xml/map_detalle.php?id_lugar=3&id_concierto=4009',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 address=markers[i].getAttribute("address");
				
                var url=markers[i].getAttribute("url");
                
				var html='<h3><a href="'+url+'" target="_blank">'+name+'</a></h3>';
                html+="----------<br />";
                html+='<p class="windowinforight">'+address+'</p>';
				
                var conciertos=markers[i].getElementsByTagName("concierto");
                //alert(conciertos.length);
				for(j=0;j<conciertos.length;j++)
				{
					if(conciertos[j].getAttribute("festival"))
                    	html+='<p class="festival_detalle">'+conciertos[j].getAttribute("festival")+'</p>';
                        	
					html+='<p class="windowinfo"><b style="color:#00abe2;">'+conciertos[j].getAttribute("name")+'</b></p>';
					html+='<p class="windowinfo">Fecha: '+conciertos[j].getAttribute("fecha")+'</p>';
                    if(conciertos[j].getAttribute("hora")!='')
						html+='<p class="windowinfo">Hora: '+conciertos[j].getAttribute("hora")+'</p>';
                   	if(conciertos[j].getAttribute("precio")!='')
                   		html+='<p class="windowinfo">Precio: '+conciertos[j].getAttribute("precio")+'</p>';

                    	
                    html+="---------------------<br />";
				}
				
				var marker=createMarker(latlng,html);
                
				map.addOverlay(marker);
                marker.openInfoWindowHtml(html,{autoScroll:true,maxHeight:300,maxWidth:200});
			}//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;