/*****************************************************************************/
/*                                2010 Shio2e                                */
/*                   JAVASCRIPT BUSCADOR :: FICHA TECNICA                    */
/*****************************************************************************/

buscador.fichaTecnica = {
	// VARIABLES CONFIGURACION
	prefijo 		: 'com.',
	id_ventana_info : 'listado_ventana',
	class_general 	: '',
	class_buscador 	: 'popup_buscador',

	// VARIABLES INTERNAS

	// FUNCIONES
	mostrarInfo : function (elemento, nombre, categorias, com_top, com_left, width){
		com_top = typeof com_top == 'undefined' ? 0 : com_top;
		com_left = typeof com_left == 'undefined' ? 0 : com_left;
		width = typeof width == 'undefined' ? 137 : width;

		var posicion = elemento.viewportOffset();
		var left = posicion.left + com_left;
		var bottom = document.viewport.getHeight() - posicion.top - com_top;

		$(this.id_ventana_info).className = this.class_general;
		$(this.id_ventana_info).show();
		$(this.id_ventana_info).update('<h2>'+nombre+'</h2>'+buscador.categorias.nombreXIdCategorias(categorias));
		$(this.id_ventana_info).setStyle({ bottom: bottom+'px', left: left+'px', width: width+'px' });
	},

	mostrarDescripcionBuscador : function (elemento, nombre, descripcion, com_top, com_left){
		com_top = typeof com_top == 'undefined' ? 0 : com_top;
		com_left = typeof com_left == 'undefined' ? 0 : com_left;

		var posicion = elemento.viewportOffset();
		var left = posicion.left + com_left;
		var bottom = document.viewport.getHeight() - posicion.top - com_top;

		$(this.id_ventana_info).className = this.class_buscador;
		$(this.id_ventana_info).show();
		$(this.id_ventana_info).update('<h2>'+nombre+'</h2>'+descripcion);
		$(this.id_ventana_info).setStyle({ bottom: bottom+'px', left: left+'px' });
	},

	mostrarInfoBuscador : function (elemento, nombre, descripcion){
		this.mostrarDescripcionBuscador(elemento, nombre, descripcion);
	},

	mostrarInfoGoogle : function (elemento, id_comercio, nombre, categorias){
		var com = mapa.offsetRespectoContenedor(elemento);
		this.mostrarInfo($(mapa.contenedor), nombre, categorias, com.top, com.left);
	},

	mostrarInfoListado : function (id_comercio, nombre, categorias, prefijo, com_top, com_left, width){
		prefijo = typeof prefijo == 'undefined' ? this.prefijo : prefijo;
		com_top = typeof com_top == 'undefined' ? 0 : com_top;
		com_left = typeof com_left == 'undefined' ? -1 : com_left;
		width = typeof width == 'undefined' ? 137 : width;
		this.mostrarInfo($(prefijo+id_comercio).ancestors()[0], nombre, categorias, com_top, com_left, width);
	},

	ocultarInfo : function (){
		$(this.id_ventana_info).hide();
	},

	mostrarFicha : function (id_comercio) {
		if (typeof id_comercio != "undefined" && id_comercio > 0)
			new Ajax.Request('/exec/ajax/ficha_comercio.php', {
				parameters: { id_comercio: id_comercio },
				onSuccess: function(respuesta) {
					var datos = respuesta.responseText.evalJSON();

					$('tit_ficha').update(datos.nombre);
					$('logo_ficha').update('<img src="'+datos.logo_url+'" alt="'+datos.nombre+'" style="width: '+datos.logo_width+'px; height: '+datos.logo_height+'px;" />');
					$('logo_contacto').update('<img src="'+datos.logo_url+'" alt="'+datos.nombre+'" style="width: '+datos.logo_width+'px; height: '+datos.logo_height+'px;" />');
					$('t_ficha_direc').update(datos.direccion);
					$('t_ficha_cp').update(datos.codigo_postal);
					$('t_ficha_loc').update(datos.poblacion);
					$('t_ficha_prov').update(datos.provincia);
					if (datos.telefono == '') {
						$('t_ficha_tlf').hide();
					} else {
						$('t_ficha_tlf').show();
						$('t_ficha_tlf').update(datos.telefono);
					}
					if (datos.email == '') {
						$('t_ficha_mail').hide();
					} else {
						$('t_ficha_mail').show();
					}
					if (datos.web == '') {
						$('t_ficha_web').hide();
					} else {
						$('t_ficha_web').show();
						$('t_ficha_web').update('<a href="'+(datos.web.substring(0,7) != 'http://' ? 'http://' : '')+datos.web+'" target="_blank">'+datos.web+'</a>');
					}
					$('desc_ficha').update(buscador.categorias.nombreXIdCategorias(datos.categorias));

					if (datos.escaparate_online == '') {
						$('website_ficha').hide();
						$('t_ficha_nom_website').hide();
					} else {
						$('website_ficha').show();
						$('t_ficha_nom_website').show();
						$('website_ficha').href = (datos.escaparate_online.substring(0,7) != 'http://' ? 'http://' : '')+datos.escaparate_online;
						$('t_ficha_nom_website').update('de '+datos.nombre);
					}

					buscador.fichaTecnica.ocultarFormularioContacto();

					if (datos.tiene_anuncios == '1') {
						$('tiene_anuncios').update('<a href="javascript:void(0);" onclick="javascript:buscador.abrir(\'ficha_ofertas\', {id_comercio: '+id_comercio+'});"><img src="/img/buscador/ofertas.png" style="margin-top:-5px;" alt="Ofertas" /></a>');
					} else {
						$('tiene_anuncios').update('');
					}

					$('modulo_ficha').show();

					var mapa_ficha = new mapaClass();
					mapa_ficha.nuevo('map_ficha', datos.latitud, datos.longitud);
					mapa_ficha.marcadorGoogle(datos.latitud, datos.longitud);
					mapa_ficha.zoom(16);
				}
			});
	},

	mostrarFormularioContacto : function () {
		$('form_contacto').show();
		$('error_ficha_nombre').hide();
		$('error_ficha_email').hide();
		$('error_ficha_consulta').hide();
		$('msg_ficha_formulario').update('');
	},

	ocultarFormularioContacto : function () {
		$('form_contacto').hide();
	},

	procesarFormularioContacto : function () {
		var id_div_msg 	= 'msg_ficha_formulario';
		var id_error 	= 'error_ficha_';

		$(id_div_msg).show();
		$(id_div_msg).update('Enviando...');

		// CONSULTA AJAX
		new Ajax.Request('/exec/ajax/ficha_comercio_contactar.php', {
			parameters: {
				nombre: encodeURIComponent($('formulario_ficha_contacto').nombre.value),
				email: encodeURIComponent($('formulario_ficha_contacto').email.value),
				consulta: encodeURIComponent($('formulario_ficha_contacto').consulta.value)
			},
			onSuccess: function(respuesta) {
				$(id_error+'nombre').hide();
				$(id_error+'email').hide();
				$(id_error+'consulta').hide();

				if (respuesta.responseText != 'ok') {
					$(id_div_msg).update('Revisa que los datos sean correctos.');
					if (respuesta.responseText.startsWith('errores-')) {
						respuesta.responseText.split('-').each(function(nombre, posicion) {
						  if ($(id_error+nombre)) $(id_error+nombre).show();
						});
					}
				} else {
					$(id_div_msg).update('Datos recibidos con éxito.');
					$('formulario_ficha_contacto').nombre.value 	= '';
					$('formulario_ficha_contacto').email.value 		= '';
					$('formulario_ficha_contacto').consulta.value 	= '';
				}
			}
		});
	}
};
