var map=null;			
var tema_actual=null;
var markersArray=[];
var capas_wms_activas=new Array();
var cluster=null;

window.onresize=function(){ dimensionar(); }

function dimensionar()
{
	document.getElementById('centro').style.width=parseInt(document.body.clientWidth)-370;
	var alto=parseInt(document.body.clientHeight)-parseInt(document.getElementById('cabecera').clientHeight)-parseInt(document.getElementById('pie').clientHeight);
	var ancho=parseInt(document.body.clientWidth)-parseInt(document.getElementById('herramientas').clientWidth)-parseInt(document.getElementById('lateral_izquierdo').clientWidth)-parseInt(document.getElementById('lateral_derecho').clientWidth)-parseInt(document.getElementById('banners').clientWidth);
	document.getElementById('cuerpo').style.height=alto;
	if(navigator.appName.indexOf("Microsoft")==-1)
	{
		document.getElementById('centro').setAttribute('style', 'position: relative; float: left; overflow: hidden; width: '+ancho+'px; height: '+parseInt(document.body.clientHeight)+'px');
		document.getElementById('cuerpo').setAttribute('style', 'width:'+ancho+'px; height: '+alto+'px;');
		//document.getElementById('contenedor_noticia').setAttribute('style', document.getElementById('contenedor_noticia').getAttribute("style")+'width:'+(parseInt(document.getElementById('cuerpo_contenido').clientWidth)-20)+'px; height: '+(parseInt(document.getElementById('cuerpo_contenido').clientHeight)-40)+'px;');
		document.getElementById('d_noticia').setAttribute('style', document.getElementById('d_noticia').getAttribute("style")+'width:'+(parseInt(document.getElementById('cuerpo_contenido').clientWidth)-40)+'px; height: '+(parseInt(document.getElementById('cuerpo_contenido').clientHeight)-60)+'px;');
		document.getElementById('d_noticia_contenido').setAttribute('style', document.getElementById('d_noticia_contenido').getAttribute("style")+'width:'+(parseInt(document.getElementById('cuerpo_contenido').clientWidth)-60)+'px; height: '+(parseInt(document.getElementById('cuerpo_contenido').clientHeight)-80)+'px;');
	}
	else
	{
		document.getElementById('cuerpo_contenido').style.height=parseInt(document.getElementById('cuerpo').clientHeight);				
		document.getElementById('cuerpo_contenido').style.width=parseInt(document.getElementById('cuerpo').clientWidth);
		document.getElementById('d_noticia').style.width=parseInt(document.getElementById('cuerpo_contenido').clientWidth-20);
		document.getElementById('d_noticia').style.height=parseInt(document.getElementById('cuerpo_contenido').clientHeight-20); 
		document.getElementById('d_noticia_contenido').style.width=parseInt(document.getElementById('cuerpo_contenido').clientWidth)-40;
		document.getElementById('d_noticia_contenido').style.height=parseInt(document.getElementById('cuerpo_contenido').clientHeight)-40; 				
	}
}

function iniciar_menu()
{
	if(map!=null) map.closeInfoWindow();
	for (var filas=0;filas<document.getElementById('tb_menu_1').rows.length;filas++) 
	{
		for (var columnas=0;columnas<document.getElementById('tb_menu_1').rows[filas].cells.length;columnas++) 
		{
			document.getElementById('tb_menu_1').rows[filas].cells[columnas].className='td_menu_temas';
		}
	}
}			

function pedir_fichero(id) 
{
	document.getElementById('contenedor_noticia').style.display='block';
	var fichero_url = document.location+"administrador/noticias/noticia.php?id="+id;
	
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=procesar_fichero;
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			//xml_http=new ActiveXObject('Microsoft.xml_http');
			xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			if (xml_http) 
			{
				xml_http.onreadystatechange=procesar_fichero;
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}
}

function procesar_fichero() 
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{
			document.getElementById('d_noticia_contenido').innerHTML=xml_http.responseText;
		}
	}
}	

function imprimir_fecha()
{
	var fecha=new Date();
	var diames=fecha.getDate(); 
	var diasemana=fecha.getDay();
	var mes=fecha.getMonth() +1 ;
	var ano=fecha.getFullYear();

	var textosemana = new Array (7); 
	  textosemana[0]="Domingo";
	  textosemana[1]="Lunes";
	  textosemana[2]="Martes";
	  textosemana[3]="Miércoles";
	  textosemana[4]="Jueves";
	  textosemana[5]="Viernes";
	  textosemana[6]="Sábado";

	var textomes = new Array (12);
	  textomes[1]="Enero";
	  textomes[2]="Febrero";
	  textomes[3]="Marzo";
	  textomes[4]="Abril";
	  textomes[5]="Mayo";
	  textomes[6]="Junio";
	  textomes[7]="Julio";
	  textomes[8]="Agosto";
	  textomes[9]="Septiembre";
	  textomes[10]="Octubre";
	  textomes[11]="Noviembre";
	  textomes[12]="Diciembre";

	document.write(textosemana[diasemana] + ", " + diames + " de " + textomes[mes] + " de " + ano);
}

function comprobar_fechas()
{
	var fecha_inicio=document.getElementById('fecha_ini').value.replace('/','-').replace('/','-');
	var fecha_final=document.getElementById('fecha_fin').value.replace('/','-').replace('/','-');				
	pedir_noticias();
}
	
function filtrar_tema(id_tema)
{
	tema_actual=id_tema;
	pedir_noticias();
}

// cuando se carga la página y cuando cambian las fechas 
function pedir_noticias() 
{
	map.clearOverlays();
	document.getElementById('txt_cargando').style.display='block';
	
	var cadena_busqueda=document.getElementById('buscar_noticias_txt').value;
	if(document.getElementById('busqueda_titulo').checked)
		var ambito=1;
	else var ambito=2;
	
	if(navigator.appName.indexOf("Microsoft")!=-1) cadena_busqueda=url_encode(cadena_busqueda);
	var fichero_url = document.location+"clases/noticias_xml.php?fecha_ini='"+document.getElementById('fecha_ini').value+"'&fecha_fin='"+document.getElementById('fecha_fin').value+"'&tema="+tema_actual+"&cadena="+cadena_busqueda+"&ambito="+ambito;
					
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=procesar_noticias;
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			//xml_http=new ActiveXObject('Microsoft.xml_http');
			if (xml_http) 
			{
				xml_http.onreadystatechange=procesar_noticias;
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}
}

function procesar_noticias() 
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{
			var xmlDoc=xml_http.responseXML.documentElement;
			noticias = xmlDoc.getElementsByTagName('noticia');
			map.clearOverlays();						
			markersArray=[];
			for(indice=0;indice<noticias.length;indice++)
			{
				var latitud=noticias[indice].getElementsByTagName('latitud');
				mi_latitud=latitud[0].firstChild.nodeValue;
				
				var longitud=noticias[indice].getElementsByTagName('longitud');
				mi_longitud=longitud[0].firstChild.nodeValue;
				
				var id=noticias[indice].getElementsByTagName('id');
				mi_id=id[0].firstChild.nodeValue;
				
				var titulo=noticias[indice].getElementsByTagName('titulo');
				mi_titulo=titulo[0].firstChild.nodeValue;
				
				punto = new GLatLng(mi_latitud,mi_longitud);							
				var marca_google = createMarker(punto , mi_id, mi_titulo);
				markersArray.push(marca_google);						
			}
			document.getElementById('txt_cargando').style.display='none';
			cluster.removeMarkers();
			cluster.addMarkers(markersArray);
			cluster.refresh();
			actualizar_wms();
		}
	}
}

var mi_control_zoom;
var mi_control_zoom_textual;
var mi_control_capas;
var geocoder=new GClientGeocoder();
geocoder.setBaseCountryCode('ES');

function buscar_localizacion()
{
	var resultado=geocoder.getLatLng(document.getElementById('buscar_txt').value,function(resultado)
	{					
		if(resultado)
		{
			hacer_zoom(resultado.lat(), resultado.lng(),15);
		}
		else alert("Direccion no encontrada")
	});
}

function hacer_zoom(latitud, longitud,nivel_zoom)
{
	if(nivel_zoom==-1)
	{
		nivel_zoom=map.getZoom();
		nivel_zoom+=3;
	}
	else
	{
		if(nivel_zoom==-2)
		{
			nivel_zoom=map.getZoom();
			nivel_zoom-=3;
		}
	}
	map.setCenter(new GLatLng(latitud , longitud),nivel_zoom);
}			



function iniciar()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById('cuerpo_contenido'));
		GEvent.addListener(map, 'move', function() 
		{
			limpiar_wms();
		});
		GEvent.addListener(map, 'moveend', function()  
		{			
			actualizar_wms();
		});
		
		mi_control_zoom=new GLargeMapControl3D();
		map.addControl(mi_control_zoom, new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10)));			
		mi_control_escala=new GScaleControl();
		map.addControl(mi_control_escala, new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(70,5)));		
		map.addControl(new GOverviewMapControl());
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		map.setMapType(G_NORMAL_MAP);
		map.setCenter(new GLatLng(39.7178489 , -3.3022522),6);
		
		mapTypeControl = new MMapTypeControl({background:'transparent url(./imagenes/fondo_opcion_capa.png)',foreground:'#000000',direction:'H'});
		map.addControl(mapTypeControl, new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(450,0)));
		
		function myClusterClick(args) 
		{
			cluster.defaultClickAction=function()
			{
				map.setCenter(args.clusterMarker.getLatLng(), map.getBoundsZoomLevel(args.clusterMarker.clusterGroupBounds))
				delete cluster.defaultClickAction;
			}
			var html='<div style="margin-top: 10px; overflow: auto; width:300px; height: 250px;"><b>'+args.clusteredMarkers.length+' Noticias</b>:<br/>';
			for (i=0; i<args.clusteredMarkers.length; i++) 
			{
				//alert(args.clusteredMarkers[i].);
				html+="<p style='margin: 0px; margin-top: 4px;'><a href='javascript:cluster.triggerClick("+args.clusteredMarkers[i].index+")' style='font-size:11px; color: #2B4B8C; text-decoration: none;' onmouseover=\"this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">"+args.clusteredMarkers[i].getTitle()+"</a></p>";
			}
			html+="<p style='margin: 0px; margin-top: 10px; font-size: 11px;'><a href='javascript:void(0)' onclick='cluster.defaultClickAction(); map.closeInfoWindow();' style='text-decoration: none; color: blue;' onmouseover=\"this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">Zoom</a> a estas noticias</p></div>";
			//	args.clusterMarker.openInfoWindowHtml(html);
			map.openInfoWindowHtml(args.clusterMarker.getLatLng(), html);
		}

		cluster=new ClusterMarker(map, {clusterMarkerTitle:'Click para ver información de estas %count noticias' , clusterMarkerClick:myClusterClick });
		cluster.intersectPadding=0;
		
		pedir_noticias();	
	}
}


/////////////////////////////////////////////////////
// CREAR MARCA
/////////////////////////////////////////////////////
function createMarker(point, id, titulo)
{
	var marker = new GMarker(point,{icon:iconoMarca, title:titulo}); 
	
	GEvent.addListener(marker, 'click', function()
	{
		var fichero_url = document.location+"clases/noticia_extendida_xml.php?id="+id;
		if (window.XMLHttpRequest) 
		{
			xml_http=new XMLHttpRequest();
			xml_http.onreadystatechange=function(){ procesar_noticia_extendida(id,marker); };
			xml_http.open("GET", fichero_url, true);
			xml_http.send(null);
		} 
		else 
			if (window.ActiveXObject) 
			{
				//xml_http=new ActiveXObject('Microsoft.xml_http');
				xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
				if (xml_http) 
				{
					xml_http.onreadystatechange=function(){ procesar_noticia_extendida(id,marker); };
					xml_http.open('GET', fichero_url, false);
					xml_http.send();
				}
			}					
	});
	GEvent.addListener(marker, "infowindowopen", function() { map.panTo(point); });

	return marker;
}

function procesar_noticia_extendida(mi_id, marker, mi_latitud, mi_longitud) 
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{
			var xmlDoc=xml_http.responseXML.documentElement;
			var noticia=xmlDoc.getElementsByTagName('noticia');
			var titulo=xmlDoc.getElementsByTagName('titulo');
			mi_titulo=titulo[0].firstChild.nodeValue;
			
			var fecha=xmlDoc.getElementsByTagName('fecha');
			mi_fecha=fecha[0].firstChild.nodeValue;
			
			var imagen_fuente=xmlDoc.getElementsByTagName('imagen_fuente');
			if(imagen_fuente.length>0) mi_imagen_fuente="<img src='./imagenes/fuentes/"+imagen_fuente[0].firstChild.nodeValue+"'>";
			else mi_imagen_fuente="";
			
			var nombre_fuente=xmlDoc.getElementsByTagName('nombre_fuente');
			if(nombre_fuente.length>0) mi_nombre_fuente="<p style='margin: 0px; text-align: right; color: #666666; font-size: 10px;'>Fuente: "+nombre_fuente[0].firstChild.nodeValue+"</p>";
			else mi_nombre_fuente="";

			var enlace=xmlDoc.getElementsByTagName('enlace');
			if(enlace.length>0 && enlace[0].firstChild && enlace[0].firstChild.nodeValue!="") mi_enlace="<p style='margin: 0px; text-align: right;'><a href='"+enlace[0].firstChild.nodeValue.replace(/mi_ampersan/g, "&")+"' target='_blank' style='text-decoration: none; font-size: 10px; color: blue;'>Abrir noticia original</a></p>";
			else mi_enlace="";
			
			var capas=xmlDoc.getElementsByTagName('capa');
			if(capas.length>0)
			{
				mis_capas="<p style='font-size: 11px; margin-bottom: 0px;'>M&aacute;s informaci&oacute;n:</p>";							
				for(indice_capas=0;indice_capas<capas.length;indice_capas++)
				{
					var id_capa=capas[indice_capas].getElementsByTagName('capa_id');
					mi_id_capa=id_capa[0].firstChild.nodeValue;
					var titulo_capa=capas[indice_capas].getElementsByTagName('titulo_capa');
					mi_titulo_capa=titulo_capa[0].firstChild.nodeValue;
					var nombre_capa=capas[indice_capas].getElementsByTagName('nombre_capa');
					mi_nombre_capa=nombre_capa[0].firstChild.nodeValue;
					var nombre_servidor=capas[indice_capas].getElementsByTagName('nombre_servidor');
					mi_nombre_servidor=nombre_servidor[0].firstChild.nodeValue;
					var url_servidor_capa=capas[indice_capas].getElementsByTagName('url_servidor_capa');
					mi_url_servidor_capa=url_servidor_capa[0].firstChild.nodeValue;
					var servicio_servidor_capa=capas[indice_capas].getElementsByTagName('servicio_servidor_capa');
					mi_servicio_servidor_capa=servicio_servidor_capa[0].firstChild.nodeValue;
					var version_servidor_capa=capas[indice_capas].getElementsByTagName('version_servidor_capa');
					version_servidor_capa=version_servidor_capa[0].firstChild.nodeValue;
					var consultable_capa=capas[indice_capas].getElementsByTagName('consultable_capa');
					consultable_capa_capa=consultable_capa[0].firstChild.nodeValue;
					var codigo_capa=capas[indice_capas].getElementsByTagName('codigo_capa');
					codigo_capa=codigo_capa[0].firstChild.nodeValue;
					var mi_servidor_formato="";
					var srs_capa=capas[indice_capas].getElementsByTagName('srs_capa');
					mi_srs=srs_capa[0].firstChild.nodeValue;
					if(mi_srs.indexOf("4326")>0) mi_srs="EPSG:4326";
					else if(mi_srs.indexOf("4230")>0) mi_srs="EPSG:4230";
					else mi_srs="";
					if(mi_srs!="")
						mis_capas+="<a href='javascript:;' onclick=\"anyadir_capa('"+mi_titulo_capa+"','"+codigo_capa+"','"+mi_url_servidor_capa+"','"+version_servidor_capa+"','"+mi_nombre_capa+"','"+mi_servidor_formato+"','"+mi_srs+"','"+mi_nombre_servidor+"');\" style='margin: 0px; color: blue; text-decoration: none; font-size: 10px;'>"+mi_titulo_capa+"</a><br/>";
					//cambio_capa(mi_servidor_direccion,mi_servidor_version,mi_nombre,mi_servidor_formato,mi_srs)
				}
			}
			else mis_capas="";
			
			//var zoom_menos="<p style=''><a href='javascript:;' onclick=\"hacer_zoom("+mi_latitud+","+mi_longitud+",-2);\"><img src='./imagenes/zoom_menos.png' onmouseover=\"this.src='./imagenes/zoom_menos_over.png';\" onmouseout=\"this.src='./imagenes/zoom_menos.png';\"></a>";
			//var zoom_mas="<a href='javascript:;' onclick=\"hacer_zoom("+mi_latitud+","+mi_longitud+",-1);\"><img src='./imagenes/zoom_mas.png' onmouseover=\"this.src='./imagenes/zoom_mas_over.png';\" onmouseout=\"this.src='./imagenes/zoom_mas.png';\"></a></p>";							

			var html=mi_imagen_fuente+"<div style='width: 500px; background: #ffffff;'><p style='margin: 0px; font-size: 9px; color: #666666;'>"+mi_fecha+"</p>"+"<p style='margin: 0px; border: 1px solid #eeeeee; padding: 10px;'>"+mi_titulo+"<a href='javascript:;'onclick=\"pedir_fichero('"+mi_id+"');\" style='margin-left: 10px; font-size: 10px; color: blue; text-decoration: none;'>Leer m&aacute;s</a></p>"+mis_capas+mi_enlace+mi_nombre_fuente+"</div>";

			marker.openInfoWindowHtml(html);			
		}
	}
}


/////////////////////////////////////////////////////
// PERSONALIZACION DE LAS MARCAS
/////////////////////////////////////////////////////
var iconoMarca = new GIcon(G_DEFAULT_ICON);
iconoMarca.image = './imagenes/marcas/marca.png';
var tamanoIcono = new GSize(20,20);
iconoMarca.iconSize = tamanoIcono;
iconoMarca.shadow = './imagenes/marcas/sombra.png';
var tamanoSombra = new GSize(30,20);
iconoMarca.shadowSize = tamanoSombra;
iconoMarca.iconAnchor = new GPoint(10, 10);

function iniciar_pagina()
{
	dimensionar();	
	iniciar();
	if(document.getElementById('fecha_ini')) document.getElementById("fecha_ini").readOnly=true;
	if(document.getElementById('fecha_fin')) document.getElementById("fecha_fin").readOnly=true;
	if(navigator.appVersion.indexOf('Safari')!=-1 && navigator.appVersion.indexOf('Chrome')==-1)
	{
		if(document.getElementById('fecha_ini')) document.getElementById("fecha_ini").style.fontSize="9px";
		if(document.getElementById('fecha_fin')) document.getElementById("fecha_fin").style.fontSize="9px";
	}
}

//////////////////////////////////////
//                  CLASE CAPA_WMS               //
//////////////////////////////////////
capa_wms = function()
{
	this.titulo="";
	this.nombre_servidor="";
	this.url_base="";
	this.version_wms="";
	this.capas="";
	this.estilo="";
	this.formato="";
	this.color_fondo="";
	this.transparencia="";
	this.srs="";
	this.puntero_GGO=null;
	this.estado=false;
	this.codigo="";
}

capa_wms.prototype.set_titulo = function(titulo){ this.titulo = titulo; }
capa_wms.prototype.set_nombre_servidor = function(nombre_servidor){ this.nombre_servidor = nombre_servidor; }
capa_wms.prototype.set_url_base = function(url_base){ this.url_base = url_base; }
capa_wms.prototype.set_version_wms = function(version_wms){ this.version_wms = version_wms; }
capa_wms.prototype.set_capas = function(capas){ this.capas = capas; }
capa_wms.prototype.set_estilo = function(estilo){ this.estilo = estilo; }
capa_wms.prototype.set_formato = function(formato){ this.formato = formato; }
capa_wms.prototype.set_color_fondo = function(color_fondo){ this.color_fondo = color_fondo; }
capa_wms.prototype.set_transparencia = function(transparencia){ this.transparencia = transparencia; }
capa_wms.prototype.set_srs = function(srs){ this.srs = srs; }
capa_wms.prototype.set_puntero_GGO = function(puntero_GGO)
{
	if(this.puntero_GGO!=null) 
	{
		map.removeOverlay(this.puntero_GGO); 		
	}
	this.puntero_GGO = puntero_GGO; 
}
capa_wms.prototype.set_estado = function(estado){ this.estado = estado; }
capa_wms.prototype.set_codigo = function(codigo){ this.codigo = codigo; }

capa_wms.prototype.visualizar = function()
{
	map.addOverlay(this.puntero_GGO); 
	actualizar_listado_capas();
}
capa_wms.prototype.ocultar = function()
{
	map.removeOverlay(this.puntero_GGO);
	this.puntero_GGO.hide();
}

capa_wms.prototype.get_titulo = function(){ return this.titulo; }
capa_wms.prototype.get_nombre_servidor = function(){ return this.nombre_servidor; }
capa_wms.prototype.get_url_base = function(){ return this.url_base; }
capa_wms.prototype.get_version_wms = function(){ return this.version_wms; }
capa_wms.prototype.get_capas = function(){ return this.capas; }
capa_wms.prototype.get_estilo = function(){ return this.estilo; }
capa_wms.prototype.get_formato = function(){ return this.formato; };
capa_wms.prototype.get_color_fondo = function(){ return this.color_fondo; }
capa_wms.prototype.get_transparencia = function(){ return this.transparencia; }
capa_wms.prototype.get_srs = function()
{
	mi_srs="";
	if(this.srs.indexOf("4326")>0) mi_srs="EPSG:4326";
	else
	{
		if(this.srs.indexOf("4230")>0) mi_srs="EPSG:4230";
		else mi_srs="";
	}
	return mi_srs;
};
capa_wms.prototype.get_puntero_GGO = function(){ return this.puntero_GGO; };
capa_wms.prototype.get_estado = function(){ return this.estado; };
capa_wms.prototype.get_codigo = function(){ return this.codigo; };


///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

function actualizar_listado_capas()
{
	var capas_disponibles_contenido_txt="<table cellpadding='0' cellspacing='0' style='width: 195px; font-size: 9px;'>";
	var esta_capa=null;
	for(var indice=capas_wms_activas.length-1;indice>=0;indice--)
	{
		esta_capa=capas_wms_activas[indice];
		if(indice==0) 
		{
			if(capas_wms_activas.length==1) capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'></td><td style='border-bottom: 1px solid #99B3CC;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"pedir_info_capa('"+esta_capa.get_nombre_servidor()+"','"+esta_capa.get_capas()+"');\" style='text-decoration: none; color: #444444;' title='Ver datos del mapa'>"+esta_capa.get_titulo()+"</a></td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar esta capa'>x</a></td>";
			else capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'><img src='./imagenes/subir_capa.png' onclick=\"subir_capa("+indice+");\" style='cursor: pointer;'></td><td style='border-bottom: 1px solid #99B3CC;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"pedir_info_capa('"+esta_capa.get_nombre_servidor()+"','"+esta_capa.get_capas()+"');\" style='text-decoration: none; color: #444444;' title='Ver datos del mapa'>"+esta_capa.get_titulo()+"</a></td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar este mapa'>x</a></td>";
			/*
			if(capas_wms_activas.length==1) capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'></td><td style='border-bottom: 1px solid #99B3CC;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'>"+esta_capa.get_titulo()+"</td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar esta capa'>x</a></td>";
			else capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'><img src='./imagenes/subir_capa.png' onclick=\"subir_capa("+indice+");\" style='cursor: pointer;'></td><td style='border-bottom: 1px solid #99B3CC;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'>"+esta_capa.get_titulo()+"</td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar este mapa'>x</a></td>";
			*/
		}
		else
		{
			if(indice!=capas_wms_activas.length-1) capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'><img src='./imagenes/subir_capa.png' onclick=\"subir_capa("+indice+");\" style='cursor: pointer;'></td><td style='border-bottom: 1px solid #99B3CC;'><img src='./imagenes/bajar_capa.png' onclick=\"bajar_capa("+indice+");\" style='cursor: pointer;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"pedir_info_capa('"+esta_capa.get_nombre_servidor()+"','"+esta_capa.get_capas()+"');\" style='text-decoration: none; color: #444444;' title='Ver datos del mapa'>"+esta_capa.get_titulo()+"</a></td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar este mapa'>x</a></td>";
			else capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'></td><td style='border-bottom: 1px solid #99B3CC;'><img src='./imagenes/bajar_capa.png' onclick=\"bajar_capa("+indice+");\" style='cursor: pointer;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"pedir_info_capa('"+esta_capa.get_nombre_servidor()+"','"+esta_capa.get_capas()+"');\" style='text-decoration: none; color: #444444;' title='Ver datos del mapa'>"+esta_capa.get_titulo()+"</a></td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar este mapa'>x</a></td>";
			/*
			if(indice!=capas_wms_activas.length-1) capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'><img src='./imagenes/subir_capa.png' onclick=\"subir_capa("+indice+");\" style='cursor: pointer;'></td><td style='border-bottom: 1px solid #99B3CC;'><img src='./imagenes/bajar_capa.png' onclick=\"bajar_capa("+indice+");\" style='cursor: pointer;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'>"+esta_capa.get_titulo()+"</td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar este mapa'>x</a></td>";
			else capas_disponibles_contenido_txt+="<tr><td style='border-bottom: 1px solid #99B3CC; border-left: 1px solid #99B3CC;'></td><td style='border-bottom: 1px solid #99B3CC;'><img src='./imagenes/bajar_capa.png' onclick=\"bajar_capa("+indice+");\" style='cursor: pointer;'></td><td style='padding-left: 5px; border-bottom: 1px solid #99B3CC;'>"+esta_capa.get_titulo()+"</td><td style='width: 12px; border-bottom: 1px solid #99B3CC; border-right: 1px solid #99B3CC;'><a href='javascript:;' onclick=\"eliminar_capa("+esta_capa.get_codigo()+");\" style='color: red; font-weight: bold; text-decoration: none; font-size: 9px;' title='Eliminar este mapa'>x</a></td>";
			*/
		}
	}
	document.getElementById('capas_disponibles_contenido').innerHTML=capas_disponibles_contenido_txt+"</table>";
}

function subir_capa(indice)
{
	var aux=capas_wms_activas[indice];
	capas_wms_activas[indice]=capas_wms_activas[indice+1];
	capas_wms_activas[indice+1]=aux;	
	actualizar_listado_capas();	
	actualizar_wms();
}

function bajar_capa(indice)
{
	var aux=capas_wms_activas[indice-1];
	capas_wms_activas[indice-1]=capas_wms_activas[indice];
	capas_wms_activas[indice]=aux;	
	actualizar_listado_capas();	
	actualizar_wms();
}

function anyadir_capa(titulo_capa,codigo_capa,mi_servidor_direccion,mi_servidor_version,mi_nombre,mi_servidor_formato,mi_srs,mi_nombre_servidor)
{
	var existe=false;
	for(var indice=0;indice<capas_wms_activas.length;indice++)
	{
		if(capas_wms_activas[indice].get_codigo()==codigo_capa) { existe=true; alert("La capa seleccionada ya está en la lista de capas disponibles"); break; }
	}
	if(!existe)
	{	
		var mi_capa=new capa_wms();
		mi_capa.set_titulo(titulo_capa);
		mi_capa.set_nombre_servidor(mi_nombre_servidor);		
		mi_capa.set_url_base(mi_servidor_direccion);
		mi_capa.set_version_wms(mi_servidor_version);
		mi_capa.set_capas(mi_nombre);
		mi_capa.set_estilo("");
		mi_servidor_formato=(mi_servidor_formato!=null && mi_servidor_formato!="")?mi_servidor_formato:"image/png";
		mi_capa.set_formato(mi_servidor_formato);
		mi_capa.set_color_fondo("0xFFFFFF");
		mi_capa.set_transparencia("TRUE");	
		if(mi_srs.indexOf("4326")>0) mi_srs="EPSG:4326";
		else if(mi_srs.indexOf("4230")>0) mi_srs="EPSG:4230";
		else mi_srs="";
		mi_capa.set_srs(mi_srs);
		mi_capa.set_puntero_GGO(pedir_capa_wms(mi_capa));				
		mi_capa.set_estado(true);		
		mi_capa.set_codigo(codigo_capa);	
		capas_wms_activas.push(mi_capa);
		mi_capa.visualizar();
	}
}

function eliminar_capa(codigo_capa)
{
	for(var indice=0;indice<capas_wms_activas.length;indice++)
	{
		if(capas_wms_activas[indice].get_codigo()==codigo_capa) 
		{ 
			capas_wms_activas[indice].ocultar(); 
			capas_wms_activas[indice].set_estado(false); 	
			capas_wms_activas.splice(indice,1);
			actualizar_listado_capas();
			actualizar_wms();
			break; 
		}		
	}
}
	/*
	var fichero_url = document.location+"clases/capa_wms.php?id="+id_capa;
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=function(){ procesar_cambio_capa(); };
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			xml_http=new ActiveXObject('Microsoft.xml_http');
			if (xml_http) 
			{
				xml_http.onreadystatechange=function(){ procesar_cambio_capa(); };
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}		
}
*/

/*
function procesar_cambio_capa()
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{
			var xmlDoc=xml_http.responseXML.documentElement;
			var capa=xmlDoc.getElementsByTagName('capa');
			
			var titulo=xmlDoc.getElementsByTagName('titulo');
			mi_titulo=titulo[0].firstChild.nodeValue;
			
			var servidor_direccion=xmlDoc.getElementsByTagName('servidor_direccion');
			mi_servidor_direccion=servidor_direccion[0].firstChild.nodeValue;
			
			var servidor_version=xmlDoc.getElementsByTagName('servidor_version');
			mi_servidor_version=servidor_version[0].firstChild.nodeValue;
			
			var servidor_formato=xmlDoc.getElementsByTagName('servidor_formato');
			mi_servidor_formato=(servidor_formato[0].firstChild!=null && servidor_formato[0].firstChild.nodeValue!="")?servidor_formato[0].firstChild.nodeValue:"image/png";
				
			var nombre=xmlDoc.getElementsByTagName('nombre');
			mi_nombre=nombre[0].firstChild.nodeValue;
			
			var srs=xmlDoc.getElementsByTagName('srs');
			mi_srs=srs[0].firstChild.nodeValue;
			
			if(mi_srs.indexOf("4326")==-1) mi_srs="EPSG:4326";
				else if(mi_srs.indexOf("4230")==-1) mi_srs="EPSG:4230";
					else mi_srs="";
			
			var mi_capa=new capa_wms();
			mi_capa.set_url_base(mi_servidor_direccion);
			mi_capa.set_version_wms(mi_servidor_version);
			mi_capa.set_capas(mi_nombre);
			mi_capa.set_estilo("");
			mi_capa.set_formato(mi_servidor_formato);
			mi_capa.set_color_fondo("0xFFFFFF");
			mi_capa.set_transparencia("TRUE");
			mi_capa.set_srs(mi_srs);
			mi_capa.set_puntero_GGO(pedir_capa_wms(mi_capa));				
			capas_wms_activas.push(mi_capa);
			mi_capa.visualizar();						
		}
	}				
}
*/
		
function pedir_capa_wms(capa_wms)
{				
	var boundary=map.getBounds();
	var suroeste=boundary.getSouthWest();
	var noreste=boundary.getNorthEast();
	var min_x=suroeste.lng();
	var min_y=suroeste.lat();
	var max_x=noreste.lng();
	var max_y=noreste.lat();
	var bbox=min_x+","+min_y+","+max_x+","+max_y;
	var ancho=map.getSize().width;
	var alto=map.getSize().height;
	if(capa_wms.get_url_base().indexOf("?")==-1) capa_wms.set_url_base(capa_wms.get_url_base()+"?");
	if(capa_wms.get_url_base()[capa_wms.get_url_base().length-1]!="?" && capa_wms.get_url_base()[capa_wms.get_url_base().length-1]!="&") capa_wms.set_url_base(capa_wms.get_url_base()+"&");	
	var peticion_wms=capa_wms.get_url_base()+"service=WMS&VERSION="+capa_wms.get_version_wms()+"&REQUEST=GetMap&"+((capa_wms.get_version_wms()=="1.3.0")?"CRS="+capa_wms.get_srs():"SRS="+capa_wms.get_srs())+"&LAYERS="+capa_wms.get_capas()+"&BBOX="+bbox+"&STYLES="+capa_wms.get_estilo()+"&FORMAT="+capa_wms.get_formato()+"&TRANSPARENT="+capa_wms.get_transparencia()+"&BGCOLOR="+capa_wms.get_color_fondo()+"&WIDTH="+ancho+"&HEIGHT="+alto;
	//alert(peticion_wms);
	return new GGroundOverlay(peticion_wms, new GLatLngBounds(suroeste,noreste));			
}

function actualizar_wms()
{
	for(var indice=0;indice<capas_wms_activas.length;indice++)
	{
		if(capas_wms_activas[indice].get_estado()) 
		{
			capas_wms_activas[indice].set_puntero_GGO(pedir_capa_wms(capas_wms_activas[indice]));
			capas_wms_activas[indice].visualizar();
		}
	}
}

function limpiar_wms()
{
	for(var indice=0;indice<capas_wms_activas.length;indice++) capas_wms_activas[indice].ocultar();
}




































var regiones=new Array();
var codigo_servidor_actual="";
var nombre_servidor_actual="";
var direccion_servidor_actual="";
var servicios_servidor_actual="";
var version_servidor_actual="";


function abrir_ventana_wms()
{
	document.getElementById('anyadir_wms').style.display="block";					
	var fichero_url = "../../clases/buscar_regiones.php";	
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=procesar_regiones;
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			//xml_http=new ActiveXObject('Microsoft.xml_http');
			if (xml_http) 
			{
				xml_http.onreadystatechange=procesar_regiones;
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}
}

function procesar_regiones() 
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{
			var cadena="<p style='width: 100%; text-align: right; margin:0px; padding-right: 5px; font-weight: bold; font-size: 14px;'><a href='javascript:;' onclick=\"document.getElementById('anyadir_wms').style.display='none';\" style='text-decoration: none; color: blue;'>X</a></p>";
			cadena+="<table cellpadding='0' cellspacing='0' style='height: 330px; table-layout: fixed;'><tr><td style='width: 270px;'>";
				cadena+="Servidores disponibles<br/>";
				cadena+="<div id='d_servidores' style='height: 300px; width: 255px; overflow: auto; margin-left: 10px;'>";
				cadena+="<table style='width: 255px; height: 300px; font-size: 10px; border: 1px solid #000000;'>";
				
			var xmlDoc=xml_http.responseXML.documentElement;
			ambitos = xmlDoc.getElementsByTagName('ambito');
			var indice=0;
			contenidoHTML="<tr><td style='text-align: left;'><a href='javascript:;' style='font-size: 11px; text-decoration: none; color: blue;' onclick=\"pedir_servidores_por_region("+indice+"); document.getElementById('div_"+indice+"').style.display=(document.getElementById('div_"+indice+"').style.display=='block')?'none':'block';\" onmouseover=\"this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\"><img src='imagenes/ambito.png'> ESPAÑA</a><br/>";
			contenidoHTML+="<div id='div_"+indice+"' style='font-size: 11px; margin-left: 10px; display: none;'>Cargando ...</div>";
			cadena+=contenidoHTML;							
			regiones.push(" ");
			
			cadena+="<br/>Por Comunidades:<br/>"
			
			for(indice=1;indice<ambitos.length;indice++)
			{
				var region=ambitos[indice].attributes.getNamedItem("region").value;
				contenidoHTML="";
				contenidoHTML+="<a href='javascript:;' style='font-size: 11px; text-decoration: none; color: blue;' onclick=\"pedir_servidores_por_region("+indice+"); document.getElementById('div_"+indice+"').style.display=(document.getElementById('div_"+indice+"').style.display=='block')?'none':'block';\" onmouseover=\"this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\"><img src='imagenes/ambito.png'> "+region+"</a><br/>";
				contenidoHTML+="<div id='div_"+indice+"' style='font-size: 11px; margin-left: 10px; display: none;'>Cargando ...</div>";
				cadena+=contenidoHTML;							
				regiones.push(region);
			}
			
			cadena+="</td></tr></table></div>";
			cadena+="</td><td style='width: 240px;'>";
				cadena+="Capas WMS disponibles:<br/>";
				cadena+="<div id='d_capas_wms' style='width: 235px; height: 300px;'>";
				cadena+="<select id='p_capas_wms' name='p_capas_wms[]' multiple style='width: 235px; height: 300px; overflow: auto; font-size: 10px; border: 1px solid #000000;'>";
				cadena+="</select></div></td>";
			cadena+="<td style='width: 60px; vertical-align: middle; text-align: center;'><div style='width: 50px; height: 16px; border: 1px solid #000000; text-align: center; font-size: 9px; cursor: pointer;' onmouseover=\"this.style.background='#ffffff';\" onmouseout=\"this.style.background='transparent';\" onclick=\"anadirme_capas();\">Añadir</div>";
			cadena+="<br/>";
			cadena+="<div style='width: 50px; height: 16px; border: 1px solid #000000; text-align: center; font-size: 9px; cursor: pointer;' onmouseover=\"this.style.background='#ffffff';\" onmouseout=\"this.style.background='transparent';\" onclick=\"eliminarme_capas();\">Eliminar</div></td>";
			cadena+="</td><td style='width: 240px;'>";
				cadena+="Capas WMS seleccionadas:<br/>";
				cadena+="<div id='d_capas_elegidas'>";
				cadena+="<select id='p_capas_asignadas' name='p_capas_asignadas[]' multiple style='width: 240px; height: 300px; overflow: auto; font-size: 10px; border: 1px solid #000000;'>";
				cadena+="</select></div>";
			cadena+="</td></tr></table><p style='text-align: right;'><a href='javascript:;' onclick='anyadir_capas_seleccionadas();' style='text-decoration: none; color: blue;' onmouseover=\"this.style.textDecoration='underline';\" onmouseout=\"this.style.textDecoration='none';\">Añadir capas seleccionadas al mapa</a></p>";										
			document.getElementById('anyadir_wms_contenido').innerHTML=cadena;
		}
	}
}

		
function pedir_servidores_por_region(indice_region) 
{					
	var fichero_url = "../../clases/buscar_servidores_por_region.php?region="+regiones[indice_region]+"&aux=0";
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=function aux(){ procesar_servidores_por_region(indice_region); };
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			//xml_http=new ActiveXObject('Microsoft.xml_http');
			if (xml_http) 
			{
				xml_http.onreadystatechange=function aux(){ procesar_servidores_por_region(indice_region); };
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}
}

function procesar_servidores_por_region(indice_region) 
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{				
			var xmlDoc=xml_http.responseXML.documentElement;
			
			var servidores = xmlDoc.getElementsByTagName('server');
			var contenido="";
			for(indice_servidor=0;indice_servidor<servidores.length;indice_servidor++)
			{
				var nombre_servidor=servidores[indice_servidor].getElementsByTagName('nombre');
				mi_nombre=nombre_servidor[0].firstChild.nodeValue;					
				var direccion_servidor=servidores[indice_servidor].getElementsByTagName('direccion');
				mi_direccion=direccion_servidor[0].firstChild.nodeValue;
				var servicios_servidor=servidores[indice_servidor].getElementsByTagName('servicios');
				mi_servicios=servicios_servidor[0].firstChild.nodeValue;
				try
				{
				var version_servidor=servidores[indice_servidor].getElementsByTagName('version');
				mi_version=version_servidor[0].firstChild.nodeValue;
				}catch(e){ var mi_version="1.1.0";}					
				var codigo_servidor=servidores[indice_servidor].getElementsByTagName('codigo');
				mi_codigo=codigo_servidor[0].firstChild.nodeValue;
				
				contenido+="<tr><td onclick=\"buscar_capas_por_servidor('"+mi_codigo+"','"+mi_nombre+"','"+mi_direccion+"','"+mi_servicios+"','"+mi_version+"');\" style='font-size: 10px; text-align: left; background: #ffffff; cursor: pointer;' onmouseover=\"this.style.background='#dddddd';\" onmouseout=\"this.style.background='#ffffff';\">"+mi_nombre+"</td></tr>";
			}	
			document.getElementById("div_"+indice_region).innerHTML="<table cellpadding='0' cellspacing='0'>"+contenido+"</table>";
		} 
	}
}

function buscar_capas_por_servidor(mi_codigo_servidor,mi_nombre_servidor, mi_direccion_servidor, mi_servicios_servidor, mi_version_servidor)
{
	document.getElementById("d_capas_wms").innerHTML="Buscando capas ...";
	
	codigo_servidor_actual=mi_codigo_servidor;
	nombre_servidor_actual=mi_nombre_servidor;
	direccion_servidor_actual=mi_direccion_servidor;
	servicios_servidor_actual=mi_servicios_servidor;
	version_servidor_actual=mi_version_servidor;

	var fichero_url = "../../clases/buscar_capas_wms.php?codigo_servidor="+codigo_servidor_actual;
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=function aux(){ procesar_capas(); };
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			//xml_http=new ActiveXObject('Microsoft.xml_http');
			if (xml_http) 
			{
				xml_http.onreadystatechange=function aux(){ procesar_capas(); };
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}
}

function procesar_capas()
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{
			var xmlDoc=xml_http.responseXML.documentElement;						
			var capas = xmlDoc.getElementsByTagName('capa');
			var contenido="";
			for(indice_capa=0;indice_capa<capas.length;indice_capa++)
			{
				var titulo=capas[indice_capa].getElementsByTagName('titulo');
				mi_titulo=titulo[0].firstChild.nodeValue;
				var nombre=capas[indice_capa].getElementsByTagName('nombre');
				mi_nombre=nombre[0].firstChild.nodeValue;		
				var servidor=capas[indice_capa].getElementsByTagName('servidor');
				mi_servidor=servidor[0].firstChild.nodeValue;
				var consultable=capas[indice_capa].getElementsByTagName('consultable');
				mi_consultable=consultable[0].firstChild.nodeValue;
				var codigo_capa=capas[indice_capa].getElementsByTagName('codigo_capa');
				mi_codigo_capa=codigo_capa[0].firstChild.nodeValue;
				var srs=xmlDoc.getElementsByTagName('srs');
				mi_srs=srs[0].firstChild.nodeValue;
				
				contenido+="<option value='"+mi_titulo+";"+mi_nombre+";"+direccion_servidor_actual+";"+servicios_servidor_actual+";"+version_servidor_actual+";"+mi_consultable+";"+mi_srs+";"+mi_codigo_capa+";"+mi_servidor+"' style='background: #ffffff;' onmouseover=\"this.style.background='#dddddd';\" onmouseout=\"this.style.background='#ffffff';\">"+mi_titulo+"</option>";
			}
			if(contenido=="") contenido="Sin capas";		
			document.getElementById('d_capas_wms').innerHTML="<select id='p_capas_wms' name='p_capas_wms[]' multiple style='width: 230px; height: 300px; overflow: auto; font-size: 10px; border: 1px solid #000000;'>"+contenido+"</select>";
		} 
	}
}

		
function anadirme_capas()
{
	var capas_disponible=document.getElementById('p_capas_wms');
	var capas_asignadas=document.getElementById('p_capas_asignadas');
	var num_opcion=capas_asignadas.options.length;
	for(var indice=0;indice<capas_disponible.length;indice++)
	{
		if(capas_disponible.options[indice].selected) 
		{				
			capas_asignadas.options[num_opcion] = new Option(capas_disponible.options[indice].text, capas_disponible.options[indice].value, false, false);
			num_opcion++;	
		}
	}
}

function eliminarme_capas()
{
	var capas_asignadas=document.getElementById('p_capas_asignadas');
	for(var indice=capas_asignadas.length-1;indice>=0;indice--)
	{
		if(capas_asignadas.options[indice].selected) 
		{				
			opcion_a_borrar = capas_asignadas.options[indice];
			opcion_a_borrar.parentNode.removeChild(opcion_a_borrar);
		}
	}
}

function anyadir_capas_seleccionadas()
{
	document.getElementById('anyadir_wms').style.display='none';
	for(var indice=0;indice<document.getElementById("p_capas_asignadas").length;indice++)
	{
		var array_parametros=document.getElementById("p_capas_asignadas").options[indice].value.split(";");		
		anyadir_capa(array_parametros[0],array_parametros[7],array_parametros[2],array_parametros[4],array_parametros[1],"",array_parametros[6],array_parametros[8]);
	}
}





function buscar_noticias()
{
	document.getElementById('txt_cargando').style.display='block';
	var cadena_busqueda=document.getElementById('buscar_noticias_txt').value;
	if(document.getElementById('busqueda_titulo').checked)
		var ambito=1;
	else var ambito=2;

	
	var fichero_url = document.location+"clases/buscar_noticias.php?cadena="+escape(cadena_busqueda)+"&ambito="+ambito;
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=procesar_busqueda;
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			//xml_http=new ActiveXObject('Microsoft.xml_http');
			xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			if (xml_http) 
			{
				xml_http.onreadystatechange=procesar_busqueda;
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}
}

function procesar_busqueda()
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{
			var cadena="";
			var xmlDoc=xml_http.responseXML.documentElement;
			var noticias_busqueda=xmlDoc.getElementsByTagName('noticia');
			
			for(indice=0;indice<noticias_busqueda.length;indice++)
			{
				var titulo=noticias_busqueda[indice].getElementsByTagName('titulo');
				mi_titulo=titulo[0].firstChild.nodeValue;	
				cadena+="<p style='margin: 0px; margin-bottom: 7px;'>"+mi_titulo+"</p>";
			}	
			document.getElementById('resultados_noticias').innerHTML=cadena;
		}
	}			
}

var info_capa_servidor="";
function pedir_info_capa(servidor, capa)
{
	info_capa_servidor=servidor;
	var fichero_url = document.location+"clases/info_capa_xml.php?servidor="+servidor+"&capa="+capa;
					
	if (window.XMLHttpRequest) 
	{
		xml_http=new XMLHttpRequest();
		xml_http.onreadystatechange=procesar_info_capa;
		xml_http.open("GET", fichero_url, true);
		xml_http.send(null);
	} 
	else 
		if (window.ActiveXObject) 
		{
			//xml_http=new ActiveXObject('Microsoft.xml_http');
			xml_http = new ActiveXObject('MSXML2.XMLHTTP.3.0');
			if (xml_http) 
			{
				xml_http.onreadystatechange=procesar_info_capa;
				xml_http.open('GET', fichero_url, false);
				xml_http.send();
			}
		}
}

function url_encode(string) 
{
	string=string.replace(/\r\n/g,"\n");
	var utftext=""; 
	for (var n = 0; n < string.length; n++) 
	{
		var c = string.charCodeAt(n); 
		if (c < 128) 
		{
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) 
		{
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else 
		{
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	return utftext;
}
		
function procesar_info_capa()
{
	if (xml_http.readyState==4) 
	{
		if (xml_http.status==200) 
		{			
			var cadena="";
			var mi_titulo="";
			var mi_srs="";
			var mi_scalemin="";
			var mi_scalemax="";
			var mi_queryable="";
			var mi_minx="";
			var mi_miny="";
			var mi_maxx="";
			var mi_maxy="";
			var mi_leyenda="";
			var mi_direccion="";
			var mi_metadato="";
			
			var xmlDoc=xml_http.responseXML.documentElement;
			var datos_capa=xmlDoc.getElementsByTagName('layer');			
			
			var titulo=datos_capa[0].getElementsByTagName('titulo');
			if(titulo.length>0 && titulo[0].firstChild!=null) mi_titulo=titulo[0].firstChild.nodeValue;
				
			var srs=datos_capa[0].getElementsByTagName('srs');
			if(srs.length>0 && srs[0].firstChild!=null) mi_srs=srs[0].firstChild.nodeValue;
			
			var scalemin=datos_capa[0].getElementsByTagName('scalemin');
			if(scalemin.length>0 && scalemin[0].firstChild!=null) mi_scalemin=scalemin[0].firstChild.nodeValue;
			
			var scalemax=datos_capa[0].getElementsByTagName('scalemax');
			if(scalemax.length>0 && scalemax[0].firstChild!=null) mi_scalemax=scalemax[0].firstChild.nodeValue;
			
			var queryable=datos_capa[0].getElementsByTagName('queryable');
			if(queryable.length>0 && queryable[0].firstChild!=null) mi_queryable=queryable[0].firstChild.nodeValue;
			
			var minx=datos_capa[0].getElementsByTagName('minx');
			if(minx.length>0 && minx[0].firstChild!=null) mi_minx=minx[0].firstChild.nodeValue;
			
			var miny=datos_capa[0].getElementsByTagName('miny');
			if(miny.length>0 && miny[0].firstChild!=null) mi_miny=miny[0].firstChild.nodeValue;
			
			var maxx=datos_capa[0].getElementsByTagName('maxx');
			if(maxx.length>0 && maxx[0].firstChild!=null) mi_maxx=maxx[0].firstChild.nodeValue;
			
			var maxy=datos_capa[0].getElementsByTagName('maxy');
			if(maxy.length>0 && maxy[0].firstChild!=null) mi_maxy=maxy[0].firstChild.nodeValue;
			
			var leyenda=datos_capa[0].getElementsByTagName('leyenda');
			if(leyenda.length>0 && leyenda[0].firstChild!=null) mi_leyenda=leyenda[0].firstChild.nodeValue;

			var direccion=datos_capa[0].getElementsByTagName('direccion');
			if(direccion.length>0 && direccion[0].firstChild!=null) mi_direccion=direccion[0].firstChild.nodeValue;

			var metadato=datos_capa[0].getElementsByTagName('metadato');
			if(metadato.length>0 && metadato[0].firstChild!=null) mi_metadato=metadato[0].firstChild.nodeValue;
			
			
			cadena+="<p style='width: 100%; text-align: right; margin-top: 2px;'><a href='javascript:;' onclick=\"document.getElementById('info_capa').style.display='none';\" style='text-decoration: none; color: blue; font-size: 14px; font-weight: bold; margin-right: 5px;'>X</a></p>";
			cadena+="<table cellpadding='0' cellspacing='4' style='text-align: left; margin: 20px; font-size: 11px;'>";
			cadena+="<tr><td colspan='2' style='text-align: center; padding-bottom: 15px; font-size: 14px; font-weight: bold;'>Informaci&oacute;n de la capa WMS</td></tr>";
			cadena+="<tr><td style='width: 150px; text-align: right;'>T&iacute;tulo:</td><td style='font-weight: bold;'>"+mi_titulo+"</td></tr>";
			cadena+="<tr><td style='width: 150px; text-align: right;'>Servidor:</td><td style='font-weight: bold;'>"+info_capa_servidor+"</td></tr>";			
			cadena+="<tr><td style='text-align: right; vertical-align: top;'>SRS:</td><td style='font-weight: bold;'>"+mi_srs+"</td></tr>";
			cadena+="<tr><td style='text-align: right;'>Escala m&iacute;nima:</td><td style='font-weight: bold;'>"+mi_scalemin+"</td></tr>";
			cadena+="<tr><td style='text-align: right;'>Escala m&aacute;xima:</td><td style='font-weight: bold;'>"+mi_scalemax+"</td></tr>";
			cadena+="<tr><td style='text-align: right;'>Devuelve informaci&oacute;n:</td><td style='font-weight: bold;'>"+(mi_queryable==0?'NO':'SI')+"</td></tr>";
			cadena+="<tr><td style='text-align: right;'>M&iacute;nima coordenada x:</td><td style='font-weight: bold;'>"+mi_minx+"</td></tr>";
			cadena+="<tr><td style='text-align: right;'>M&iacute;nima coordenada y:</td><td style='font-weight: bold;'>"+mi_miny+"</td></tr>";
			cadena+="<tr><td style='text-align: right;'>M&aacute;xima coordenada x:</td><td style='font-weight: bold;'>"+mi_maxx+"</td></tr>";
			cadena+="<tr><td style='text-align: right;'>M&aacute;xima coordenada y:</td><td style='font-weight: bold;'>"+mi_maxy+"</td></tr>";
			cadena+="<tr><td style='text-align: right; vertical-align: top;'>URL de la leyenda:</td><td style='font-weight: bold;'>"+mi_leyenda+"</td></tr>";
			cadena+="<tr><td style='text-align: right; vertical-align: top;'>URL del servidor:</td><td style='font-weight: bold;'>"+mi_direccion+"</td></tr>";
			cadena+="<tr><td style='text-align: right; vertical-align: top;'>Metadato:</td><td style='font-weight: bold;'>"+mi_metadato+"</td></tr>";
			cadena+="</table>";
						
			document.getElementById('info_capa').style.display='block';
			document.getElementById('info_capa_contenido').innerHTML=cadena;
		}
	}	
}
