/**
 * Contiene funciones para manejar el posicionamiento de los distintos componentes de la pagina.
 */
(function() {
	PorArriba.LayoutControl = function() {
		
		this.swfMovie = null;				// Referencia al objeto que contiene la pelicula flex.
		this.loadingMessage = null;			// Referencia al div del mensaje de loading.
		this.bottomBar = null				// Referencia al div de la barra inferior para publicidades.
		this.bgDegrade = null;				// Referencia al div de fondo con degrade.
		this.publicidad = null;				// Referencia al div de publicidad.
		
		/*
		 * Es un contador que indica cuando se esta mostrando un mensaje de loading, si es mayor a cero se muestar el mensaje.
		 * Cada vez que se solicita mostrar el mensaje de loading, esta variable se incrementa en 1 y cuando se solicita
		 * ocultar el mensaje de loading esta variable se decrementa en 1, cuando llega a cero se oculta el mensaje de loading.
		 */
		this.showLoadingMessage = 0;

	 	/**
	 	 * Regresa el objeto SWF del menu
	 	 */
	 	this.getSWF = function(){
	 		if (this.swfMovie == null) this.swfMovie = thisMovie("PorArribaFlex");
	 		return this.swfMovie;
	 	}
	 	
		/**
		 * Regresa el item BottomBar
		 */
		this.getBottomBar = function () {
			if (this.bottomBar == null) this.bottomBar = document.getElementById('bottomBar'); 
			return this.bottomBar;
		}

		/**
		 * Regresa el div del degrade
		 */
		this.getBGDegrade = function() {
			if (this.bgDegrade == null) this.bgDegrade = document.getElementById('bgDegrade'); 
			return this.bgDegrade;
		}

		/**
		 * Regresa el div de loading message
		 */
		this.getLoadingMessage = function() {
			if (this.loadingMessage == null) this.loadingMessage = document.getElementById('loading'); 
			return this.loadingMessage;
		}

		/**
		 * Regresa el div de publicidad
		 */
		this.getDivPublicidad = function() {
			if (this.publicidad == null) this.publicidad = document.getElementById('publicidad'); 
			return this.publicidad;
		}

		/**
		 * Posiciona y redimensiona los componente en la pantalla
		 */
		this.putAndResizeComponents = function () {
			this.putBottomBar();
			this.resizeDivBackground();
			this.resizeMapArea();
			this.putMapArea();
		}
	
		/**
		 * Posiciona la barra inferior de la pagina
		 */
		this.putBottomBar = function () {
			var bBar = this.getBottomBar();
			var swf = this.getSWF();
			bBar.style.top = swf.clientHeight - parseInt(bBar.style.height);
			bBar.style.left = swf.clientWidth;
		}
		
		/**
		 * Redimensiona el div de fondo
		 */
		this.resizeDivBackground = function () {
			var bg = this.getBGDegrade();
			var swf = this.getSWF();
			bg.style.height = swf.clientHeight;
			bg.style.width = document.body.clientWidth;
			
			//Esto lo hago por la porqueria del IE6 que no resfresca automaticamente el 
			//tamaÃ±o del img, entonces lo fuerzo al setarle el width.
			var img = document.getElementById('imgDegrade');
			img.style.width = "100%";
		}
		
		/**
		 * Posiciona el area del mapa
		 */
		this.putMapArea = function (){
			if (mapManager != null) {
				var mapArea = mapManager.mapIFrame;
				var swf = this.getSWF();
				mapArea.style.top = 5;
				mapArea.style.left = swf.clientWidth;
			}
		}
	
		/**
		 * Redimensiona el mapa
		 */
		this.resizeMapArea = function () {
			if (mapManager != null) mapManager.resizeMap();
		}
		
		/**
		 * Setea el ancho del swf
		 */
		this.setSWFWidth = function(width){
			var swf = this.getSWF();
			swf.width = width;
		}
		
		/**
		 * Muestra todos los componentes de la pantalla
		 */
		this.showAllComponents = function() {
			var bBar = this.getBottomBar();
			var bgDegrade = this.getBGDegrade();
			if (mapManager != null) mapManager.showMap();
			bBar.style.visibility = "visible";
			bgDegrade.style.visibility = "visible";
		}


		/**
		 * Muestra el mensaje de loading.
		 * Por cada mensaje de loading que se muestre se incrementa la propiedad showLoadingMessage, que
		 * actua a modo de bandera; cuando es cero se quita el mensaje.
		 */
		this.showLoading = function() {
			if (!this.showLoadingMessage) {
				var messageStyle = this.getLoadingMessage().style;
				var mapStyle = mapManager.mapIFrame.style;
				
				var topMap = strToInt(mapStyle.top);
				
				// Verifica si el mapa esta posicionado, de no ser asi lo posiciona
				if (topMap == 0) {
					this.putMapArea();
					topMap = strToInt(mapStyle.top);
				}
	
				messageStyle.top = topMap + 5;
				messageStyle.left = strToInt(mapStyle.width) - strToInt(messageStyle.width) - 5 + strToInt(mapStyle.left);
				
				var labelLoading = document.getElementById("labelLoading");
				var txt;

				try {
					var swf = this.getSWF();
					
					/*
					 * TODO: el llamado a "localizatorGetText", falla cuando muestro calles.
					 */
					
					txt = swf.getSWF().localizatorGetText('cargando');
					if (isUndefined(txt)) txt = 'Cargando...';
				} catch (e) {
					txt = 'Cargando...';
				}
				labelLoading.innerHTML = "<b>"+ txt +"</b>";
				
				messageStyle.visibility = "visible";
			}
			this.showLoadingMessage++;
		}
		
		/**
		 * Oculta el mensaje de loading.
		 * Por cada mensaje que se oculta se decrementa la propiedad showLoadingMessage, cuando estaÃ§
		 * llega a cero se quita el mensaje.
		 */
		this.hideLoading = function (forceHide) {
			if (isUndefined(forceHide))	forceHide = false;
	
			if (this.showLoadingMessage) {
				if (forceHide) this.showLoadingMessage = 0;
				else this.showLoadingMessage--;
				
				if (!this.showLoadingMessage) this.getLoadingMessage().style.visibility = "hidden";
			}
		}
		
		/**
		 * Muestra el div de publicidad
		 */
		this.showDivPublicidad = function () {
			var styleDivPub = this.getDivPublicidad().style;
			var swf = this.getSWF();
			styleDivPub.top = 80;
			styleDivPub.left = swf.clientWidth + 5;
			styleDivPub.visibility = "visible";

			var linkPub = document.getElementById("linkPublicidad");
			try {
				var swf = this.getSWF();
				linkPub.innerHTML = "<b>"+ swf.localizatorGetText('contacto'); +"</b>";
			} catch (e) {
				
			}
			
		}
		
		/**
		 * Oculta el div de publicidad
		 */
		this.hideDivPublicidad = function () {
			var styleDivPub = this.getDivPublicidad().style;
			styleDivPub.visibility = "hidden";
		}
		
		/**
		 * Muestra las calles
		 */
		this.mostrarCalles = function() {
			this.getSWF().mostrarCalles();
		}
		
		/**
		 * Oculta las calles
		 */
		this.ocultarCalles = function() {
			this.getSWF().ocultarCalles();
		}
		
		/**
		 * Cambia los textos por un cambio de lenguaje
		 */
		this.changeLanguageForLocale = function () {
			/*
			 * Cambia el lenguaje del chek mostrar calles 
			 */
			var td = document.getElementById("tdCheckCallesControl")
			try {
				var swf = this.getSWF()
				td.innerHTML = swf.localizatorGetText('mostrarCalles');
			} catch (e) {
				td.innerHTML = "Mostrar Calles";
			}	
			
		}
	}
})();

fileLoaded("LayoutControl.js");
