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("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 == "clb_xinxeta_directe"){
			idPeticion = 3;
			makeRequest('http://www.tv3.cat/elclub/xml_dinamic/clb_xinxeta_directe.xml');
		} else if (categoria == "clb_xinxeta_mobil"){
			idPeticion = 3;
			makeRequest('http://www.tv3.cat/elclub/xml_dinamic/clb_xinxeta_mobil.xml');
		} else if (categoria == "clb_xinxeta_queixa"){
			idPeticion = 3;
			makeRequest('http://www.tv3.cat/elclub/xml_dinamic/clb_xinxeta_queixa.xml');
		} else {
			lanzaPeticion();
		}

      }
    }

var idPeticion = 0;
function lanzaPeticion() {
	idPeticion++;
	if (debug) GLog.write('idPeticion: '+idPeticion);
	if (idPeticion==1) makeRequest('http://www.tv3.cat/elclub/xml_dinamic/clb_xinxeta_directe.xml');	
	if (idPeticion==2) makeRequest('http://www.tv3.cat/elclub/xml_dinamic/clb_xinxeta_mobil.xml');
	if (idPeticion==3) makeRequest('http://www.tv3.cat/elclub/xml_dinamic/clb_xinxeta_queixa.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("clb_xinxeta")[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;
				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("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 <= 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){

		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 == "clb_xinxeta_directe"){
			icona.image = "http://www.tv3.cat/elclub/img/ico_directe.png";
			icona.iconSize = new GSize(32, 31);
		}
		else if (tipus == "clb_xinxeta_mobil"){
			icona.image = "http://www.tv3.cat/elclub/img/ico_avis.png";
			icona.iconSize = new GSize(32, 31);
		}
		else{
			icona.image = "http://www.tv3.cat/elclub/img/ico_queixa.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='';
					
					if (text != 'noinfo'){
						texto = text+'<br>';
					}

					if (url != 'noinfo'){
						titulo = '<b><a title="'+titol+'" href="'+url+'">'+titol+'</a></b><br>';
					}
					
					if ((imatge != 'noinfo') && (url != 'noinfo')){
						imagen = '<a title="'+titol+'" href="'+url+'"><img src=http://www.tv3.cat/multimedia/'+imatge+' alt="'+alt+'" height="90" width="90"/></a>';
					}
					
					if ((imatge != 'noinfo') && (url == 'noinfo')){
						imagen =' <img src=http://www.tv3.cat/multimedia/'+imatge+' alt="'+alt+'" height="90" width="90"/>';
					}
					
					//marker.html  = ['<b>',titol,'</b><br>',lloc,'<br>',text,'<br><br>','<a title="'+titol+'" href="'+url+'"><img src=http://www.tv3.cat/multimedia/',imatge,' alt="'+alt+'" height="90" width="90"/></a>'].join('');
					marker.html  = [titulo,lloc+'<br>',texto,imagen].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);*/
}




