// JavaScript Document
	function _ObtenerEtiqueta () {
		var Label = this.Label;
		var LabelAux = Label.split(this.Mapa.Idioma.Separador);
		if (LabelAux.length > this.Mapa.Idioma.PosIdiomaActual)
			return LabelAux[this.Mapa.Idioma.PosIdiomaActual];
		else
			return LabelAux[0];
	}
	function _Ordenar () {
		var n = 0;
		for (n = 0; n<this.Hijos.length;n++) {
			if (this.Hijos[n].Tipo != "Capa")
				this.Hijos[n].Ordenar();
		}
		this.Hijos.sort(_fnOrdenar);
	}
	function _fnOrdenar (objA,objB) {
		return objB.Prioridad - objA.Prioridad;
	}
	function _EstablecerPrioridad (Prioridad) {
		if (this.Padre)
			if (this.Padre.Prioridad < Prioridad)
				this.Padre.EstablecerPrioridad(Prioridad);
		this.Prioridad = Prioridad;
	}
	function _ObtenerCLSMapa (Mapa,Vista, Idioma) {
		this.MGMap = Mapa;
		this.Escala = Mapa.getScale();
		var colMGGrupos = Mapa.getMapLayerGroups();
		for (n=0;n<colMGGrupos.count;n++) {
			var objMGGrupo = colMGGrupos.item(n);	
			var GrupoName = objMGGrupo.getName().substring(objMGGrupo.getName().lastIndexOf('#') + 1);
			var Prefijo = objMGGrupo.getName().substring(0,objMGGrupo.getName().lastIndexOf('#'));
			//var GREstruct = objMGGrupo.getName().split(this.SeparaGrupos);
			var GREstruct = GrupoName.split(this.SeparaGrupos);
			var strRuta = "";
			var m = 0;
			var objGrupoPadre = null;
			while (m < GREstruct.length - 1) {				
				
				if (strRuta == "")
				
					strRuta = GREstruct[m];
				else
					strRuta += this.SeparaGrupos + GREstruct[m];				
				
				if (!this.Indice["GR_" + strRuta]) {
					var objGrupoPadre = new clsGrupo(this, strRuta,"tmp");
				} else {
					var objGrupoPadre = this.Indice["GR_" + strRuta];
				}
				m++;
			}
			if (!this.Indice["GR_" + GrupoName]) {
				var objGrupo = new clsGrupo(this, GrupoName, Prefijo);
			} else {
				var objGrupo = this.Indice["GR_" + GrupoName];
				objGrupo.Prefijo = Prefijo;
			}
			objGrupo.Obtener(objMGGrupo);
			if (objGrupoPadre == null)
				this.AgregarGrupo(objGrupo);
			else
				objGrupoPadre.AgregarGrupo(objGrupo);
		}
		var colMGCapas = Mapa.getMapLayersEx();
		for (n=0;n<colMGCapas.count;n++) {
			var objMGCapa = colMGCapas.item(n);
			var CapaName = objMGCapa.getName().substring(objMGCapa.getName().lastIndexOf('#') + 1);
			var Prefijo = objMGCapa.getName().substring(0,objMGCapa.getName().lastIndexOf('#'));
			var objCapa = new clsCapa(this, CapaName,Prefijo);
			var objMGGrupo = objMGCapa.MapLayerGroup;
			if (objMGGrupo) {
				var GrupoName = objMGGrupo.getName().substring(objMGGrupo.getName().lastIndexOf('#') + 1);
				var objGrupo = this.Indice["GR_" + GrupoName];
				if (objGrupo) {
					objGrupo.AgregarCapa(objCapa);
				} else {
					alert("Error al obtener capas");
				}
			} else {
				this.AgregarCapa(objCapa);
			}
			objCapa.Obtener(objMGCapa);
		}

		if (Vista != null || Idioma != null)			
			this.AplicarVista(Vista, Idioma);		
		this.ActualizarDisponibilidad(Mapa.getScale());
		this.Ordenar();
	}
	
	function _AplicarVista (Vista, Idioma)
	{
		for (n = 0; n < this.Hijos.length; n++)
			_AplicarVistaANodo(this.Hijos[n], Vista, Idioma)
	}

	function _AplicarVistaANodo (obj, Vista, Idioma)
	{	
		var n=0;
		if (obj.Tipo != "Capa")
		{
			for(n=0;n<obj.Hijos.length;n++){				
				_AplicarVistaANodo(obj.Hijos[n], Vista, Idioma);
				
			}
			
		}
		var optPadre = _LeerOpcionesAvanzadas(obj.Prefijo,Vista,Idioma);		
		
		switch (optPadre){
			case 0:
				if (obj.Visibilidad != false)
					obj.CambiarVisibilidad();				
				obj.Mostrar = false;				
				break;
			case 1:
				if (obj.Visibilidad !=false)
					obj.CambiarVisibilidad();				
				//obj.Mostrar = true;				
				break;			
			case 2:
				if (obj.Visibilidad !=true)
					obj.CambiarVisibilidad();				
				//obj.Mostrar = true;				
				break;
		}
		
		
	}
	
	function _LeerOpcionesAvanzadas (Prefijo,Vista,Idioma){
		var blnVista = false;
		var blnIdioma = false;
		var blnPasaPorI = false;
		var blnPasaPorV = false;
		var n=0;		
		
		if (Prefijo == ""){
			return 3;
		}else{
			var Param = Prefijo.split('#');
			if (!(Param.length == 0 && Param[0] == '')){ 
				//Significa que tiene idioma y vista
				
				var i=0;
				for(i=0;i<Param.length;i++){
					var Tipo = Param[i].substring(0,2);
					switch(Tipo)
					{
						case "v-":		
							blnPasaPorV = true;
							if (Vista != null)
							{
								var Aux = Param[i].substring(2,Param[i].length);
								var TiposVista = Aux.split('.');							
								blnVista = false;
								for (n=0;n<TiposVista.length;n++){
									if (TiposVista[n] == Vista){
										blnVista = true;
									}
								}
								if (n == 0) blnVista = true;							
							}
							break;
						case "i-":
							blnPasaPorI = true;
							if (Idioma != null)
							{
								var Aux = Param[i].substring(2,Param[i].length);
								var TiposIdioma = Aux.split('.');
								blnIdioma = false;
								for(n=0;n<TiposIdioma.length;n++){
									if (TiposIdioma[n] == Idioma){
										blnIdioma = true;
									}
								}								
								if (n == 0) blnIdioma = true;
							}
							break;
						default: return -1;
					}
				}
			}else{ 
				return 3;
			}		
			
			if (blnPasaPorV == false){
				blnVista = true;
			}
			if (blnPasaPorI == false){
				blnIdioma = true;
			
			}
			if (blnIdioma == false){
				return 0;
			}else if(blnVista == true && blnIdioma == true){
				return 2;
			}else{
				return 1;			
			}
		}
	}
	function _ObtenerCLSGrupo (MGGrupo) {
		this.Mostrar = MGGrupo.getShowInLegend();
		this.Visibilidad = MGGrupo.Visibility;
		this.Label = MGGrupo.getLegendLabel();
	}
	function _ObtenerCLSCapa (MGCapa) {
		this.Visibilidad = MGCapa.Visibility;
		this.Mostrar = MGCapa.ShowInLegend;
		this.EstablecerPrioridad(MGCapa.Priority);
		this.Label = MGCapa.getLegendLabel();
		this.TipoDeCapa = MGCapa.getLayerType();
		var colStyle = MGCapa.getMapLayerStyles();
		var n=0;
		for (n=0;n<colStyle.count;n++) {
			this.AgregarRango(colStyle.item(n).MinDisplayRange,colStyle.item(n).MaxDisplayRange);
		}
	}
	function _AgregarCapa (objCapa) {
		objCapa.Padre = this;
		this.Hijos.push(objCapa);
	}
	function _AgregarGrupo (objGrupo) {
		objGrupo.Padre = this;
		this.Hijos.push(objGrupo);
	}
	function _AgregarRango (EscalaMin, EscalaMax) {
		var objRango = new clsRango();
		objRango.EscalaMin = EscalaMin;
		objRango.EscalaMax = EscalaMax;
		this.Rangos.push(objRango);
	}
	function _ActualizarDisponibilidadCapa (Escala) {
		var n = 0;
		this.Disponibilidad = false;
		while (!this.Disponibilidad && n < this.Rangos.length) {
			this.Disponibilidad = this.Disponibilidad || (Escala > this.Rangos[n].EscalaMin && Escala <= this.Rangos[n].EscalaMax);
			n++;
		}
	}
	function _ActualizarDisponibilidad (Escala) {
		var key = "";
		this.Disponibilidad = false;
		var n = 0;
		for (n=0; n < this.Hijos.length; n++) {
			this.Hijos[n].ActualizarDisponibilidad(Escala);
			this.Disponibilidad = this.Disponibilidad || this.Hijos[n].Disponibilidad;
		}
	}
	function _VisibilidadHeredada() {
		if (this.Visibilidad)
			if (this.Padre)
				if (this.Padre.Tipo == "Grupo")
					return this.Visibilidad && this.Padre.VisibilidadHeredada();
		return this.Visibilidad;
	}
	function _CambiarVisibilidad() {
		this.Mapa.MGMap.setAutorefresh(false);
		if (EstablecerVisibilidad(this, !this.Visibilidad)) {
			this.Visibilidad = !this.Visibilidad;
			this.Mapa.MGMap.refresh();
		}
		this.Mapa.MGMap.setAutorefresh(true);
	}
	function EstablecerVisibilidad (obj, blnVisibilidad) {
		var MGMap = obj.Mapa.MGMap;
		if (MGMap) {
			if (!MGMap.isBusy()) {
				//setPointer();
				if (obj.Tipo == "Capa") {
					if (obj.Prefijo == "")
						var MapLayer = MGMap.getMapLayer(obj.Nombre);
					else
						var MapLayer = MGMap.getMapLayer(obj.Prefijo + "#" + obj.Nombre);
					if (MapLayer) {							
						MapLayer.setVisibility (blnVisibilidad);
					}				
				} else {
					if (obj.Prefijo == "")
						var MapLayerGroup = MGMap.getMapLayerGroup(obj.Nombre);
					else
						var MapLayerGroup = MGMap.getMapLayerGroup(obj.Prefijo + "#" + obj.Nombre);
					if (MapLayerGroup) {						
						MapLayerGroup.setVisibility (blnVisibilidad);
						ModificarVisibilidadHijos(obj,blnVisibilidad);
					}					
				}
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	function ModificarVisibilidadHijos (obj, blnVisibilidad) {
		var key = "";
		var objTmp = null;
		var objMG = null;
		var n = 0 ;
		for (n=0;n<obj.Hijos.length;n++) {
			objTmp = obj.Hijos[n];
			if (objTmp.Tipo == "Capa") {
				if (objTmp.Prefijo == "")
					objMG = obj.Mapa.MGMap.getMapLayer(objTmp.Nombre);
				else
					objMG = obj.Mapa.MGMap.getMapLayer(objTmp.Prefijo + "#" + objTmp.Nombre);				
				objMG.setVisibility(blnVisibilidad && objTmp.Visibilidad);
			} else {
				if (objTmp.Prefijo == "")
					objMG = obj.Mapa.MGMap.getMapLayerGroup(objTmp.Nombre);
				else
					objMG = obj.Mapa.MGMap.getMapLayerGroup(objTmp.Prefijo + "#" + objTmp.Nombre);
				objMG.setVisibility(blnVisibilidad && objTmp.Visibilidad);
				ModificarVisibilidadHijos(objTmp,blnVisibilidad);
			}
		}
	}
	//Clases
	function clsGrupo (Mapa, Nombre, Prefijo) {
		this.Padre = null;
		this.Mapa = Mapa;
		this.Tipo = "Grupo";
		this.Nombre = Nombre;
		this.Label = "";
		this.key = "GR_" + Nombre;
		this.Prefijo = Prefijo;
		this.DameEtiqueta = _ObtenerEtiqueta;
		this.Visibilidad = false;
		this.VisibilidadHeredada = _VisibilidadHeredada;
		this.Mostrar = false;
		this.Disponibilidad = false;
		this.Hijos = new Array();
		this.AgregarCapa = _AgregarCapa;
		this.AgregarGrupo = _AgregarGrupo;
		this.Obtener = _ObtenerCLSGrupo;
		this.Prioridad = 0;
		this.EstablecerPrioridad = _EstablecerPrioridad;
		this.Ordenar = _Ordenar;
		if (Nombre != "")
			this.Mapa.Indice[this.key] = this;
		this.ActualizarDisponibilidad = _ActualizarDisponibilidad;
		this.CambiarVisibilidad = _CambiarVisibilidad;
	}
	function clsCapa (Mapa, Nombre, Prefijo) {
		this.Padre = null;
		this.Mapa = Mapa;
		this.Tipo = "Capa";
		this.Nombre = Nombre;
		this.Prefijo = Prefijo;
		this.key = "CP_" + Nombre;
		this.Label = "";
		this.DameEtiqueta = _ObtenerEtiqueta;
		this.Visibilidad = false;
		this.VisibilidadHeredada = _VisibilidadHeredada;
		this.Mostrar = false;
		this.Disponibilidad = false;
		this.Prioridad = 0;
		this.Rangos = new Array();
		this.AgregarRango = _AgregarRango;
		this.Obtener = _ObtenerCLSCapa;
		this.EstablecerPrioridad = _EstablecerPrioridad;
		if (Nombre != "")
			this.Mapa.Indice[this.key] = this;
		this.ActualizarDisponibilidad = _ActualizarDisponibilidadCapa;
		this.CambiarVisibilidad = _CambiarVisibilidad;
	}
	function clsRango () {
		this.EscalaMin = 0;
		this.EscalaMax = 0;
	}
	function clsIdioma(Separador, PosIdiomaActual) {
		this.Separador = Separador;
		this.PosIdiomaActual = PosIdiomaActual;
	}
	function clsMapa (Nombre, URL, SeparadorLabel, PosLabel, SeparaGrupos) {
		this.Idioma = new clsIdioma(SeparadorLabel, PosLabel);
		this.Indice = new Array();
		this.Mapa = this;
		this.MGMap = null;
		this.SeparaGrupos = SeparaGrupos;
		this.Label = Nombre;
		this.URL = URL;
		this.Tipo = "Mapa";
		this.Hijos = new Array();
		this.Disponibilidad = true;
		this.Prioridad = 0;
		this.EstablecerPrioridad = _EstablecerPrioridad;
		this.DameEtiqueta = _ObtenerEtiqueta;
		this.AgregarCapa = _AgregarCapa;
		this.AgregarGrupo = _AgregarGrupo;
		this.Obtener = _ObtenerCLSMapa;
		this.Ordenar = _Ordenar;
		this.ActualizarDisponibilidad = _ActualizarDisponibilidad;
		this.AplicarVista = _AplicarVista;
	}
