var debug = false;

// JavaScript Document

/* FUNCIONS GESTIO GOOGLE MAP HOME */

//var pointArray = [];

 /*  function goUrl(id){
	    var pre = "html_dinamic/";
   		var ext = ".htm";
   		document.getElementById("ifr").src = pre+id+ext;      
   }
   */
   	var google_map;
	
    function inicia_gmaps(categoria) {
	//GLog.write('preinicializado...');
      if (GBrowserIsCompatible()) {
        google_map = new GMap2(document.getElementById("goomap2"));
        //map.addControl(new GSmallMapControl()); //zoom petit
        google_map.addControl(new GLargeMapControl()); //zoom gran
        
		google_map.setCenter(new GLatLng(41.38582,2.17323), 7); //Centro del mapa
		
		google_map.setMapType(G_PHYSICAL_MAP);
		
		if (debug) GLog.write('inicializado2...');
        // Add markers to the map		

		lanzaPeticion();
		

      }
    }

var idPeticion = 0;
function lanzaPeticion() {
	idPeticion++;
	if (debug) GLog.write('idPeticion: '+idPeticion);
	if (idPeticion==1) makeRequest('http://www.tv3.cat/elpaisatge/xml_dinamic/pai_paisatge_llistat_parsejat.xml');	
	if (idPeticion==2) makeRequest('http://www.tv3.cat/elpaisatge/xml_dinamic/dty_participacio_llistat_parsejat.xml');
}

var http_request = false;

function makeRequest(url) {

	http_request = false;
		

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();

	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		if (debug) GLog.write('No es posible crear una instancia XMLHTTP');
		return false;
	}
	http_request.onreadystatechange = procesa;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function procesa() {	
	//
	
		if (debug) GLog.write("readyState: "+http_request.readyState);
	if (http_request.readyState == 4) { //4 (completo)
		if (debug) GLog.write("status: "+http_request.status);
		if (http_request.status == 200) { // codigo 200, exito
		
			var xmldoc = http_request.responseXML;
			
			var root = xmldoc.getElementsByTagName("pai_paisatge")[0];
			
			var items_ = root.getElementsByTagName("item");
			
			if (debug) GLog.write(items_.length);
			for (var i = 0 ; i < items_.length ; i++) {
				var item_ = items_[i];
				latitude_ = item_.getAttribute("lat");
				longitude_ = item_.getAttribute("long");
				label_ = item_.getElementsByTagName("titol")[0].firstChild.nodeValue;
				
				//GLog.write(latitude_ + "," + longitude_);
				
				paper_ = item_.getElementsByTagName("tipus")[0].firstChild.nodeValue;
				text_ = item_.getElementsByTagName("text")[0].firstChild.nodeValue;
				url_ = item_.getElementsByTagName("url")[0].firstChild.nodeValue;
				imatge_ = item_.getElementsByTagName("imatge")[0].firstChild.nodeValue;
				lloc_ = item_.getElementsByTagName("lloc")[0].firstChild.nodeValue;
				alt_ = item_.getElementsByTagName("imatge")[0].getAttribute("alt");
				
				dia_ = item_.getElementsByTagName("dia")[0].firstChild.nodeValue;
				mes_ = item_.getElementsByTagName("mes")[0].firstChild.nodeValue;
				any_ = item_.getElementsByTagName("any")[0].firstChild.nodeValue;
				
				if (debug) GLog.write('makeNewPoint');
				makeNewPoint(latitude_,longitude_,paper_,text_,url_,imatge_,label_,lloc_,alt_,dia_,mes_,any_);
			}
			// PER DEFECTE OBRIM EL PRIMER MUNICIPI "AMPOSTA" AL IFRAME
			//goUrl("nit25_ampost");
			
			if (idPeticion < 2) {
				lanzaPeticion();
			}
			
		} else {
			if (debug) GLog.write('Hubo problemas con la petición.');
			//alert('Hubo problemas con la petición.');
		}
	}

}

function makeNewPoint(lat,long,tipus,text,url,imatge,titol,lloc,alt,dia,mes,any){

		var point = new GLatLng(lat,long);
		var baseIcon = new GIcon();
		//baseIcon.shadow = "http://www.tv3.cat/nit25/img/starmapa_sombra.png";
		baseIcon.iconSize = new GSize(26, 26);
		//baseIcon.shadowSize = new GSize(38, 26);
		baseIcon.iconAnchor = new GPoint(0, 0);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		//baseIcon.infoShadowAnchor = new GPoint(18, 25);	
		
		//var marker = new GMarker(point); 
		var icona= new GIcon(baseIcon);
		
		//var icona = new GIcon();
		//GLog.write('tipus'+tipus);
		if (tipus == "paisatge_programa"){
			icona.image = "http://www.tv3.cat/elpaisatge/img/ico_tele.png";
			icona.iconSize = new GSize(21, 23);
		}
		else{
			icona.image = "http://www.tv3.cat/elpaisatge/img/ico_camara.png";
			icona.iconSize = new GSize(21, 16);
		}
		
		// Set up our GMarkerOptions object
		markerOptions = { icon:icona };
		var marker = new GMarker(point, markerOptions);
		GEvent.addListener(marker, "click", function() {

					var imagen='';
					var titulo= titol;
					var texto='';
					var data= dia+'/'+mes+'/'+any;
					
					if (text != 'noinfo'){
						texto = text;
					}

					if (url != 'noinfo'){
						titulo = '<a title="'+titol+'" href="'+url+'">'+titol+'</a>';
					}
					
					if ((imatge != 'noinfo') && (url != 'noinfo')){
						imagen = '<a title="'+titol+'" href="'+url+'"><img src=http://www.tv3.cat/multimedia/'+imatge+' alt="'+alt+'" /></a>';
					}
					
					if ((imatge != 'noinfo') && (url == 'noinfo')){
						imagen =' <img src=http://www.tv3.cat/multimedia/'+imatge+' alt="'+alt+'" />';
					}
					
					
					if (tipus == "paisatge_programa"){
						//marker.html  = ['<div id="bocata">'+imagen,'<div class="info"><span class="data">Data: ' + data +'</span>','<span class="lloc">'+titulo+'</span>','<span class="enviat">Enviat per:</span><span class="persona">'+lloc+'</span>','<span class="ciutat">- '+texto+' -</span>','</div></div>'].join('');      A l'espera de que m&d canvii els css
						marker.html  = ['<div id="bocata">'+imagen,'<div class="info"><span class="data">Data: ' + data +'</span>','<span class="lloc">'+titulo+'</span>','<span class="persona">'+lloc+'</span>','</div></div>'].join('');
					}
					else{
						marker.html  = ['<div id="bocata">'+imagen,'<div class="info"><span class="data">Data: ' + data +'</span>','<span class="lloc">'+titulo+'</span>','<span class="persona">'+lloc+'</span>','</div></div>'].join('');
					}
					
					currentMarker = marker;
					marker.openInfoWindowHtml(marker.html);
			});       
			if (debug) GLog.write('addOverlay');
			//alert(marker);
        google_map.addOverlay(marker);
		
		/*GEvent.addListener(marker, "click", function() {
			goUrl(id);
			});       
			GLog.write('addOverlay');
        google_map.addOverlay(marker);*/
}




