/*****************************************************************************/
/*                                2010 Shio2e                                */
/*    JAVASCRIPT BUSCADOR :: ANUNCIOS :: TIPOS :: PORTAL :: MULTIPAGINA      */
/*****************************************************************************/

buscador.anuncios.tipos.portalMultipagina = {
	// VARIABLES CONFIGURACION
	id_div_pagina				: 'pmp_pagina',
	prefijo_opc_menu 			: 'opc_',
	prefijo_div_seccion 		: 'pdt_',
	numOpcMenuPatrocinadores	: 7,
	comunicaciones_nombre		: 'Comunicacion',

	// VARIABLES INTERNAS
	parent 						: buscador.anuncios.tipos,
	elementos					: null,

	patrocinadores_generales	: {},
	anuncios_generales			: {},

	// FUNCIONES
	mostrar : function (anuncio, datos){
		// CONFIGURAR PORTAL
		this.actual_anuncio 			= anuncio.id_anuncio;
		this.patrocinadores_generales 	= datos.extras.patrocinadores;
		this.anuncios_generales 		= datos.extras.anuncios;

		// GUARDAR ELEMENTOS
		this.guardarElementos(datos.extras.elementos);

		// MOSTRAR BOTONES
		$('b_portal_multipagina').show();
		if (this.actual_anuncio == 156) {
			$('b_pmp_recomendar').show();
			$('b_pmp_inscripcion').show();
			$('b_pmp_novedades').hide();
			$('b_pmp_trabajo').hide();
		} else {
			$('b_pmp_recomendar').hide();
			$('b_pmp_inscripcion').hide();
			$('b_pmp_novedades').show();
			$('b_pmp_trabajo').show();
		}

		// DIRECCION COMERCIO
		var info_comercio = '';
		info_comercio += '<h2>'+datos.nombre+'</h2>';
		info_comercio += datos.direccion+'<br/>'+datos.codigo_postal+' '+datos.poblacion+'<br/>'+datos.provincia;
		$(this.parent.parent.divInfoComercio).update(info_comercio);

		// CARTELERIA
		$('cartel_pmp_img').alt = anuncio.nombre;
		$('cartel_pmp_img').src = 'http://img.shio2e.com/n/buscador-anuncio-'+anuncio.id_anuncio+'.jpg';

		// TITULO CONTENIDO
		$('pmp_titulo_generico').update(datos.extras.titulo_contenido != '' ? datos.extras.titulo_contenido : 'instalaciones');

		// PATROCINADORES
		this.mostrarPatrocinadores(null, this.patrocinadores_generales);

		// ANUNCIOS
		this.mostrarAnuncios(null, this.anuncios_generales);

		// IMAGEN DE FONDO DEL MENU
		if (anuncio.id_anuncio == 209) {
			$('lista_opciones_multipagina').setStyle({background: 'background:center bottom no-repeat url(\'http://img.shio2e.com/n/buscador-anuncio-209-menu.png\')'});
		} else {
			$('lista_opciones_multipagina').setStyle({background: 'none'});
		}

		// TITULOS SELECTORES
		$('pmp_selector_1_nombre').update('');
		$('pmp_selector_2_nombre').update('');
		$('pmp_selector_3_nombre').update('');
		$('pmp_selector_4_nombre').update('');
		switch(anuncio.id_anuncio){
			case '156':
				$('pmp_selector_1_nombre').update('Poblaci&oacute;n:<br/>');
				$('pmp_selector_2_nombre').update('Liga:<br/>');
				$('pmp_selector_3_nombre').update('Divisi&oacute;n:<br/>');
				break;
			case '209':
				$('pmp_selector_1_nombre').update('&Aacute;reas de actuaci&oacute;n:<br/>');
				$('pmp_selector_2_nombre').update('Poblaci&oacute;n:<br/>');
				$('pmp_selector_3_nombre').update('Centro:<br/>');
				break;
		}

		// MOSTRAR ELEMENTOS DE LA PORTADA DEL PORTAL
		this.mostrarElemento(0, 0);
	},

	mostrarPatrocinadores : function (patrocinadores_pagina, patrocinadores_generales){
		patrocinadores_pagina = patrocinadores_pagina == null ? new Array() : patrocinadores_pagina;

		// JUNTAR LOS 2 ARRAY DE PATROCINADORES
		var patrocinadores = patrocinadores_pagina;
		patrocinadores_generales.each(function(patrocinador) {
			patrocinadores.push(patrocinador);
		}.bind(this));

		// VARIABLES
		var prefijo = 'li.pd.';
		var nuevo_listado = '';
		var style;

		// PATROCINADORES - ELIMINAR TODAS LAS OPCIONES DEL LISTADO ACTUAL
		var i = 0;
		while($(prefijo+i))
			$(prefijo+(i++)).remove();

		// PATROCINADORES - CREAR LOS NUEVOS ELEMENTOS
		var creados = 0;
		patrocinadores.each(function(obj) {
			style = creados < this.numOpcMenuPatrocinadores ? '' : 'display: none;';

			nuevo_listado += '<li id="li.pd.'+creados+'" style="'+style+'" onmouseover="javascript:buscador.fichaTecnica.mostrarInfoListado('+obj.id_comercio+', \''+addslashes(obj.nombre)+'\', ['+obj.categorias.join(',')+'], \'pd.\', 0, 5, 133);" onmouseout="javascript:buscador.fichaTecnica.ocultarInfo();">';
			nuevo_listado += '<a id="pd.'+obj.id_comercio+'" href="javascript:void(0);" onclick="javascript:buscador.abrir(\'ficha\', {id_comercio: '+obj.id_comercio+'});">'+obj.nombre_abreviado+'</a>';
			nuevo_listado += '</li>';

			creados++;
		}.bind(this));

		$('sponsors_pmp').update(nuevo_listado);
		menuListadoVertical.reset('pd');
	},

	mostrarAnuncios : function (anuncios_pagina, anuncios_generales){
		anuncios_pagina = anuncios_pagina == null ? new Array() : anuncios_pagina;

		// JUNTAR LOS 2 ARRAY DE PATROCINADORES
		var anuncios = anuncios_pagina;
		anuncios_generales.each(function(anuncio) {
			anuncios.push(anuncio);
		}.bind(this));

		// VARIABLES
		var prefijo = 'li.anu.';
		var nuevo_listado = '';
		var style;

		// ANUNCIOS - ELIMINAR TODAS LAS OPCIONES DEL LISTADO ACTUAL
		var i = 0;
		while($(prefijo+i))
			$(prefijo+(i++)).remove();

		// ANUNCIOS - CREAR LOS NUEVOS ELEMENTOS QUE PASEN EL FILTRO
		var creados = 0;
		anuncios.each(function(obj) {
			style = creados < buscador.numOpcMenuAnu ? '' : 'display: none;';

			nuevo_listado += '<li id="li.anu.'+creados+'" class="peso_'+obj.peso+'" style="'+style+'">';
			nuevo_listado += '<a href="javascript:void(0);" onclick="javascript:buscador.anuncios.clic('+obj.id_anuncio+');buscador.abrir(\'carteleria\', {id_anuncio: '+obj.id_anuncio+', tipo: '+obj.tipo+', id_comercio: '+obj.id_comercio+', nombre: \''+addslashes(obj.nombre)+'\'});"><img id="anuncio.'+obj.id_anuncio+'" src="http://img.shio2e.com/n/buscador-anuncio_banner-'+obj.id_anuncio+'.png"></a>';
			nuevo_listado += '</li>';

			//creados++;
			creados = creados + parseInt(obj.peso);
		}.bind(this));

		$('pmp_minibanners').update(nuevo_listado);
		menuListadoVertical.reset('anu');
	},

	guardarElementos : function (elementos){
		this.elementos = elementos;
	},

	mostrarElemento : function (nivel, id_elemento){
	////// TRATAMIENTO DE LOS DATOS DE ENTRADA
		if (typeof id_elemento == 'undefined') return;

		id_elemento = id_elemento + ' ';
		if (id_elemento.startsWith('-')) {
			id_elemento = parseInt(id_elemento.substring(1));
			nivel = (id_elemento == 0) ? 0 : (nivel - 1);
		} else {
			id_elemento = parseInt(id_elemento);
		}

	////// DEFINICION DE VARIABLES
		var comunicaciones_encontradas=false, opciones;
		var comunicaciones_mostrar = (nivel == -1);

	////// OBTENER DATOS DEL id_elemento
		// elemento: {id, n, ic, ec}
		var elemento_seleccionado = null;
		this.elementos.each(function(elemento) {
			if (parseInt(elemento.id) == id_elemento)
				elemento_seleccionado = elemento;
		}.bind(this));
		if (elemento_seleccionado == null && id_elemento > 0) {
			alert('ERROR1: Elemento no encontrado');
			return;
		}

		// ELEMENTO RAIZ
		if (id_elemento == 0)
			elemento_seleccionado = {
				id: 0,
				n: '',
				ic: -1,
				ec: 1
			};

	////// HAY QUE MOSTRAR UNA PAGINA
		if (elemento_seleccionado != null && (elemento_seleccionado.ec == 0 || comunicaciones_mostrar)) {
			// CAMBIAR FONDO DEL CUADRO
			$('cartel_pmp_img').src = 'http://img.shio2e.com/n/buscador-anuncio-'+this.actual_anuncio+'-fondo.jpg';

			// ACTUALIZAR DIV PAGINA
			$(this.id_div_pagina).show();
			$(this.id_div_pagina).update('Cargando...');

			// CARGAR PAGINA Y MOSTRARLA
			new Ajax.Request('/exec/ajax/pmp_pagina.php', {
				parameters: {
					id_anuncio: encodeURIComponent(this.actual_anuncio),
					id_elemento: encodeURIComponent((comunicaciones_mostrar ? '-' : '')+id_elemento)
				},
				onSuccess: function(respuesta) {
					// DESGLOSAR RETORNO
					var partes 	= respuesta.responseText.split('|||');
					var pagina 	= partes[0].replace('||-', '||');
					var json 	= partes[1].evalJSON();

					// ACTUALIZAR PAGINA
					$(this.id_div_pagina).update(pagina);

					// ACTUALIZAR PATROCINADORES
					this.mostrarPatrocinadores(json.patrocinadores, this.patrocinadores_generales);

					// ACTUALIZAR ANUNCIOS
					this.mostrarAnuncios(json.anuncios, this.anuncios_generales);
				}.bind(this)
			});

			return;
		}

	////// TRATAMIENTO VISUAL DE LAS OPCIONES DEL MENU
		// OCULTAR TODOS LOS SELECTORES CON NIVEL SUPERIOR AL ACTUAL
		var nivel_actual = nivel + 1;
		var i = (nivel_actual == 1 ? 2 : nivel_actual);
		while($('pmp_selector_'+i)) {
			$('pmp_selector_'+i).hide();
			i++;
		}

		// RESETEAR SELECTOR DEL NIVEL ACTUAL
		if ($('pmp_selector_'+nivel_actual))
			$('pmp_selector_'+nivel_actual+'_select').clear();

	////// CONFIGURAR SELECTOR DEL NIVEL ACTUAL
		if (elemento_seleccionado != null && elemento_seleccionado.ec == 1) {
			opciones = '<option value="-'+id_elemento+'">-</option>';
			this.elementos.each(function(elemento) {
				if (elemento.n == this.comunicaciones_nombre) {
					comunicaciones_encontradas = true;
				} else if (parseInt(elemento.ic) == id_elemento && (nivel_actual != 1 || parseInt(elemento.ec) == 1))
					opciones += '<option value="'+elemento.id+'">'+elemento.n+'</option>';
			}.bind(this));

			if ($('pmp_selector_'+nivel_actual)) {
				if (opciones == '') return;
				$('pmp_selector_'+nivel_actual).show();
				$('pmp_selector_'+nivel_actual+'_select').show();
				$('pmp_selector_'+nivel_actual+'_select').update(opciones);
				if ($('pmp_selector_'+nivel_actual+'_ul')) $('pmp_selector_'+nivel_actual+'_ul').hide();
			} else {
				alert('ERROR2: selector no encontrado');
			}
		}

	////// MOSTRAR OPCIONES DE LA CARPETA ELEGIDA SI ES DE MENU
		if (elemento_seleccionado != null && elemento_seleccionado.ec == 2) {
			opciones = '';
			this.elementos.each(function(elemento) {
				if (elemento.n == this.comunicaciones_nombre) {
					comunicaciones_encontradas = true;
				} else if (parseInt(elemento.ic) == id_elemento && parseInt(elemento.ec) == 0)
					opciones += '<li><a href="javascript:void(0);" onclick="javascript:buscador.anuncios.tipos.portalMultipagina.mostrarElemento('+nivel_actual+', '+elemento.id+');">'+elemento.n+'</a></li>';
			}.bind(this));

			if ($('pmp_selector_'+nivel_actual)) {
				if (opciones == '') return;
				$('pmp_selector_'+nivel_actual).show();
				$('pmp_selector_'+nivel_actual+'_select').hide();
				if ($('pmp_selector_'+nivel_actual+'_ul')) {
					$('pmp_selector_'+nivel_actual+'_ul').show();
					$('pmp_selector_'+nivel_actual+'_ul').update(opciones);
				}
			} else {
				alert('ERROR3: selector no encontrado');
			}
		}

	////// MOSTRAR OPCIONES DEL MENU INICIAL
		opciones = '';
		this.elementos.each(function(elemento) {
			if (elemento.n == this.comunicaciones_nombre) {
				comunicaciones_encontradas = true;
			} else if (parseInt(elemento.ic) == 0 && parseInt(elemento.ec) == 0)
				opciones += '<li><a href="javascript:void(0);" onclick="javascript:buscador.anuncios.tipos.portalMultipagina.mostrarElemento(0, '+elemento.id+');">'+elemento.n+'</a></li>';
		}.bind(this));

		if (comunicaciones_encontradas)
			opciones += '<li><a href="javascript:void(0);" onclick="javascript:buscador.anuncios.tipos.portalMultipagina.mostrarElemento(-1, '+id_elemento+');">Noticias</a></li>';

		if (opciones != '') {
			$('pmp_paginas').show();
			$('pmp_paginas').update(opciones);
		}
	}
}
