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;
	var idPeticion = 0;
	var tipusxin = 0;
	
    function inicia_gmaps(categoria) {
	//GLog.write('preinicializado...');
      if (GBrowserIsCompatible()) {
        google_map = new GMap2(document.getElementById("gmap"));
        //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
		
		if (debug) GLog.write('inicializado2...');
        // Add markers to the map		

		if (categoria == "est_xinxeta_heli"){
			tipusxin = 1;
			idPeticion = 4;
			makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_heli.xml');
		} else if (categoria == "est_xinxeta_textos"){
			tipusxin = 0;
			idPeticion = 4;
			makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_textos.xml');
		} else if (categoria == "est_xinxeta_fotos"){
			tipusxin = 0;
			idPeticion = 4;
			makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_fotos.xml');
		} else if (categoria == "est_xinxeta_videos"){
			tipusxin = 0;
			idPeticion = 4;
			makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_videos.xml');
		}  else {
			idPeticion = 0;
			lanzaPeticion();
		}

      }
    }


function lanzaPeticion() {
	idPeticion++;
	if (debug) GLog.write('idPeticion: '+idPeticion);
	if (idPeticion==1) {
				tipusxin = 1;
				makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_heli.xml');	
	}
	if (idPeticion==2) {
				tipusxin = 0;
				makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_textos.xml');
	}
	if (idPeticion==3) {
		tipusxin = 0;
				makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_fotos.xml');
	}
	if (idPeticion==4) {
			tipusxin = 0;
				makeRequest('http://www.tv3.cat/espaiterra/xml_dinamic/est_xinxeta_videos.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("est_xinxeta")[0];		
			
			var items_ = root.getElementsByTagName("item");
			
			if (debug) GLog.write(items_.length);
			if (debug) GLog.write("tipusxin: "+tipusxin);
			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;
				paper_ = item_.getElementsByTagName("tipus")[0].firstChild.nodeValue;
				text_ = item_.getElementsByTagName("text")[0].firstChild.nodeValue;
				url_ = item_.getElementsByTagName("url")[0].firstChild.nodeValue;
				lloc_ = "";
				if (tipusxin == 0) {
					lloc_ = item_.getElementsByTagName("lloc")[0].firstChild.nodeValue;
				}else{ lloc_ = "noinfo" }
				imatge_ = item_.getElementsByTagName("imatge")[0].firstChild.nodeValue;
				alt_ = item_.getElementsByTagName("imatge")[0].getAttribute("dia");
				
				if (debug) GLog.write('makeNewPoint');
				makeNewPoint(latitude_,longitude_,paper_,text_,url_,imatge_,label_,lloc_,alt_);
			}
			// PER DEFECTE OBRIM EL PRIMER MUNICIPI "AMPOSTA" AL IFRAME
			//goUrl("nit25_ampost");
			
			if (idPeticion <= 3) {
				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){

		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, 32);
		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();
		if (debug) GLog.write('tipus'+tipus);
		if (tipus == "est_xinxeta_heli"){
			icona.image = "http://www.tv3.cat/espaiterra/img/map-icon-helicopter.png";
			icona.iconSize = new GSize(32, 31);
		}
		else if (tipus == "est_xinxeta_textos"){
			icona.image = "http://www.tv3.cat/espaiterra/img/map-icon-textos.png";
			icona.iconSize = new GSize(32, 31);
		}
		else if (tipus == "est_xinxeta_fotos"){
			icona.image = "http://www.tv3.cat/espaiterra/img/map-icon-fotos.png";
			icona.iconSize = new GSize(32, 31);
		}
		else{
			icona.image = "http://www.tv3.cat/espaiterra/img/map-icon-video.png";
			icona.iconSize = new GSize(32, 31);
		}
		// Set up our GMarkerOptions object
		markerOptions = { icon:icona };
		var marker = new GMarker(point, markerOptions);
		
		GEvent.addListener(marker, "click", function() {
 
				 var imagen='';
				 var titulo='<b>'+titol+'</b><br>';
				 var texto='';
				 var lugar='';
				 
				 if (text != 'noinfo'){
				  texto = text+'<br>';
				 }

				 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+'" height="66" width="90"/></a>';
				 }
				 
				 if ((imatge != 'noinfo') && (url == 'noinfo')){
				  imagen =' <img src=http://www.tv3.cat/multimedia/'+imatge+' alt="'+alt+'" height="66" width="90"/>';
				 }
				 
				 if(lloc != 'noinfo'){
					lugar = lloc+'<br>'
				 }
				 
				 marker.html  = ['<div id="bocata" style="width: 350px;">'+imagen,'<span class="titol_gmaps_punts"><strong>'+titulo+'</strong><br>',lugar,'</span><span class="descripcio_gmaps">'+texto+'</span><br/></span></div>'].join('');
				 currentMarker = marker;
				marker.openInfoWindowHtml(marker.html);
			});       
			if (debug) GLog.write('addOverlay');
        google_map.addOverlay(marker);
		
		/*GEvent.addListener(marker, "click", function() {
			goUrl(id);
			});       
			GLog.write('addOverlay');
        google_map.addOverlay(marker);*/
}




