
// Este fichero crea el widget
// Crea el iframe y hace la llamada correspondiente al widget adecuado

// Comprueba si se debe cargar automáticamente o no
// Se debe cargar automáticamente cuando viene de una página externa
// NO se debe cargar automáticamente cuando viene del wizard de creación

if (typeof evitaCargaAutomaticaAvaibook == "undefined" || evitaCargaAutomaticaAvaibook == null || !evitaCargaAutomaticaAvaibook)
	creaIframe(null);

///////////////////////////////////////////////////////////////////////////////////////////////////
//											creaIframe
///////////////////////////////////////////////////////////////////////////////////////////////////

function creaIframe(donde){
	// Función que crea el iframe donde pintar el widget

	// Automatizo la ruta base dependiendo del dominio en el que estemos (para no tener que cambiar la ruta si estamos en local)
	if ((document.domain == "localhost") ||
		(document.domain == "avai-serv") ||
		(document.domain == "localhost:80") ||
		(document.domain == "192.168.1.10") ||
		(document.domain == "192.168.1.33") ||
		(document.domain == "192.168.1.20") ||
		(document.domain == "192.168.1.21") ||
		(document.domain == "192.168.1.22") ||
		(document.domain == "") ||
		(document.domain == "undefined") ||
		(document.domain == null))
		var rutaBase = 'http://localhost/avaibook/';
	else if (document.domain == "www.avaibook.com")// Estamos en la zona privada (hago esto para que no se pierda el colorcillo ese de la barra de direcciones dentro del wizard)
		var rutaBase = 'https://www.avaibook.com/';
	else
		var rutaBase = 'http://www.avaibook.com/';

	// Creo el iframe donde se cargará el widget y asigno atributos
	var el = document.createElement("iframe");

	el.setAttribute('id', 'ifrm');
	el.setAttribute('scrolling','no');
	el.setAttribute('style', 'border: 0;');
	el.setAttribute('style', 'margin-bottom: 6px;');
	el.setAttribute("frameBorder","0");

	///////////////////////////////////////////////////////////////////////////////////////////////

	// Dependiendo de las variables, creamos los parámetros a pasar a los archivos php que ejecutan los widgets

	var extras = '';

	if (typeof avaibook_id_widget != "undefined" && avaibook_id_widget != null && avaibook_id_widget > 0)
		extras += "id="+avaibook_id_widget+"&";
	else
		avaibook_id_widget = '';	// Esto es para evitar el error en la creación del div que contiene al widget

	if (typeof avaibook_ancho != "undefined" && avaibook_ancho != null && avaibook_ancho > 0)
		extras += "ancho="+avaibook_ancho+"&";

	if (typeof avaibook_alto != "undefined"  && avaibook_alto != null  && avaibook_alto > 0)
		extras += "alto="+avaibook_alto+"&";

	if (typeof avaibook_color_fondo_widget != "undefined" && avaibook_color_fondo_widget != null)
		extras += "fondo="+avaibook_color_fondo_widget+"&";

	if (typeof avaibook_color_texto_widget != "undefined" && avaibook_color_texto_widget != null)
		extras += "texto="+avaibook_color_texto_widget+"&";

	if (typeof avaibook_color_texto_patrocinado != "undefined" && avaibook_color_texto_patrocinado != null)
		extras += "patrocinio_color="+avaibook_color_texto_patrocinado+"&";

	if (typeof avaibook_icono_widget != "undefined" && avaibook_icono_widget != null)
		extras += "icono="+avaibook_icono_widget+"&";

	if (typeof avaibook_borde_widget != "undefined" && avaibook_borde_widget != null)
		extras += "borde="+avaibook_borde_widget+"&";
	else
		avaibook_borde_widget = 0;

	if (typeof avaibook_borde_color_widget != "undefined" && avaibook_borde_color_widget != null)
		extras += "borde_color="+avaibook_borde_color_widget+"&";

	if (typeof avaibook_texto_widget != "undefined" && avaibook_texto_widget != null)
		extras += "textoCual="+avaibook_texto_widget+"&";

	if (typeof avaibook_leyenda != "undefined" && avaibook_leyenda != null)
		extras += "leyenda="+avaibook_leyenda+"&";

	if (typeof avaibook_propietario != "undefined" && avaibook_propietario != null && parseInt(avaibook_propietario) > -1)
		extras += "propietario="+avaibook_propietario+"&";

	if (typeof avaibook_alojamiento != "undefined" && avaibook_alojamiento != null && parseInt(avaibook_alojamiento) > -1)
		extras += "alojamiento="+avaibook_alojamiento+"&";

	if (typeof avaibook_unidad_alojativa != "undefined" && avaibook_unidad_alojativa != null && parseInt(avaibook_unidad_alojativa) > -1)
		extras += 'unidad_alojativa='+avaibook_unidad_alojativa+"&";

	if (typeof avaibook_listado != "undefined" && avaibook_listado != null && avaibook_listado)
		extras += 'listado='+avaibook_listado+"&";

	if (typeof avaibook_idioma != "undefined" && avaibook_idioma != null)
		extras += 'lang='+avaibook_idioma+"&";
	else
		extras += 'lang=ES&';

	///////////////////////////////////////////////////////////////////////////////////////////////

	// Continuo poniendo atributos al iframe e indico el archivo a ejecutar dependiendo del tipo de widget

	var margen = 4;	// Indicar aquí el margen que se pone en el body de cada widget (hay que poner el doble por eso de que hay 2 lados)

	// Inicializo los parámetros de anchura y altura de un widget para hacer más sencillo su manejo
	if ((typeof avaibook_ancho == "undefined") || (avaibook_ancho == null) || (parseInt(avaibook_ancho) <= 0))
		var anchoWidget = 0;
	else
		var anchoWidget = parseInt(avaibook_ancho) + parseInt(margen);

	if ((typeof avaibook_alto == "undefined") || (avaibook_alto == null) || (parseInt(avaibook_alto) <= 0))
		var altoWidget = 0;
	else
		var altoWidget = parseInt(avaibook_alto) + parseInt(margen);

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                                   WIDGET BOTÓN RESERVA                                    //
	///////////////////////////////////////////////////////////////////////////////////////////////

	if (avaibook_modo == "botonReserva"){

		if (((typeof avaibook_alojamiento != "undefined") && (avaibook_alojamiento != null) && (parseInt(avaibook_alojamiento) > 0)) || (avaibook_listado)){

			if ((typeof avaibook_boton != "undefined") && (avaibook_boton != null) && (avaibook_boton == 'grande')){
				// Tipo de botón reserva "GRANDE"

				// Configuro los datos específicos del botón
				anchoWidget = 150 + margen;
				altoWidget = 75 + margen;
				extras += 'boton_grande_avaibook=true';

			}else if ((typeof avaibook_boton != "undefined") && (avaibook_boton != null) && (avaibook_boton == 'pequenyo')){
				// Tipo de botón reserva "PEQUEÑO"

				// Configuro los datos específicos del botón
				anchoWidget = 150 + margen;
				altoWidget = 32 + margen;
				extras += 'boton_peq_avaibook=true';

			}else{

				// Configuro los datos específicos del botón
				if (anchoWidget == 0)
					anchoWidget = 130 + margen;

				if (altoWidget == 0)
					altoWidget = 35 + margen;

			}

			// Configuro el ancho y alto del iframe
			el.setAttribute('width', anchoWidget + 'px');
			el.setAttribute('height', altoWidget + 'px');

			// Añado el fichero a ejecutar dentro del iframe
			el.setAttribute('src', rutaBase+'widgets/botonReserva.php?'+extras);

		}else
			alert("Error botonReserva: avaibook_alojamiento undefined");

	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                                WIDGET TARIFAS ALOJAMIENTO                                 //
	///////////////////////////////////////////////////////////////////////////////////////////////

	if (avaibook_modo == "infoTarifas"){

		if ((typeof avaibook_alojamiento != "undefined") && (avaibook_alojamiento != null) && (parseInt(avaibook_alojamiento) > 0)){

			// Configuro los datos específicos de las tarifas
			if (anchoWidget == 0)
				anchoWidget = 400 + margen;

			if (altoWidget == 0)
				altoWidget = 300 + margen;

			// Activo el scroll si es necesario ante la imposiblidad de saber de antemano la altura del contenido del widget
			el.setAttribute('scrolling','auto');

			// Configuro el ancho y alto del iframe
			el.setAttribute('width', anchoWidget + 'px');
			el.setAttribute('height', altoWidget + 'px');

			// Añado el fichero a ejecutar dentro del iframe
			el.setAttribute('src', rutaBase+'widgets/tarifasAlojamiento.php?'+extras);

		}else
			alert("Error tarifas: avaibook_alojamiento undefined");

	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                                     WIDGET CALENDARIO                                     //
	///////////////////////////////////////////////////////////////////////////////////////////////

	if (avaibook_modo == "calendario"){

		if ((typeof avaibook_alojamiento != "undefined") && (avaibook_alojamiento != null) && (parseInt(avaibook_alojamiento) > 0)){
////////////////////////////////////////////////////////////////////

// Compatibilidad con widgets antiguos
if ((typeof avaibook_id_widget != "undefined") && (avaibook_id_widget != null) && (parseInt(avaibook_id_widget) > 0) && (parseInt(avaibook_id_widget) < 777)){

	if (typeof avaibook_ancho != "undefined" && avaibook_ancho!=null && parseInt(avaibook_ancho) > 0){
		el.setAttribute('width', (parseInt(avaibook_ancho)+40)+'px');
	}else{
		el.setAttribute('width', '120px');
	}

	if (typeof avaibook_leyenda != "undefined" && avaibook_leyenda!=null){
		extraAlturaLeyenda = 40;
	}else{
		extraAlturaLeyenda = 0;
	}

	// Para matriz no cambio el ancho.
	if (typeof avaibook_tipo == "undefined" || avaibook_tipo==null || avaibook_tipo!='MATRIZ'){

		// Puedo configurar el elto de un botón
		if (typeof avaibook_alto != "undefined" && avaibook_alto!=null && parseInt(avaibook_alto) > 0){
			if (typeof avaibook_lectura != "undefined" && avaibook_lectura){
				el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+40)+'px');
			}else{
				if (typeof avaibook_boton != "undefined" && avaibook_boton=='grande'){
					//el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+150)+'px');
					el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+200)+'px');
				}else if (typeof avaibook_boton != "undefined" && avaibook_boton=='pequenyo'){
					//el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+130)+'px');
					el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+180)+'px');
				}else{

					//el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+130)+'px');
					el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+180)+'px');
				}

			}
		}else{

			el.setAttribute('height', '80px');
		}
	}else{

		// Puedo configurar el elto de un botón
		if (typeof avaibook_alto != "undefined" && avaibook_alto!=null && parseInt(avaibook_alto) > 0){
		}else{ avaibook_alto = 150; }

		if (typeof avaibook_lectura != "undefined" && avaibook_lectura){
			el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda))+'px');
		}else{
			if (typeof avaibook_boton != "undefined" && avaibook_boton=='grande'){
				//el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+60)+'px');
				el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+110)+'px');
			}else if (typeof avaibook_boton != "undefined" && avaibook_boton=='pequenyo'){
				//el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+60)+'px');
				el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+110)+'px');
			}else if (typeof avaibook_boton != "undefined" && avaibook_boton=='css'){
				//el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+40)+'px');
				el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+90)+'px');
			}else{
				//el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda))+'px');
				el.setAttribute('height', (parseInt(avaibook_alto)+parseInt(extraAlturaLeyenda)+50)+'px');
			}
		}

	}

}else{

	// Configuro el ancho y alto del iframe
	el.setAttribute('width', anchoWidget + 'px');
	el.setAttribute('height', altoWidget + 'px');

}

////////////////////////////////////////////////////////////////////

			if ((typeof avaibook_lectura != "undefined") && (avaibook_lectura)){
				// Calendario Lectura

				switch(avaibook_tipo){
					case 'MES':
						el.setAttribute('src', rutaBase+'widgets/calendarioLectura.php?'+extras+'modo=MES');
						break;
					case 'TRESMES':
						el.setAttribute('src', rutaBase+'widgets/calendarioLectura.php?'+extras+'modo=TRESMES');
						break;
					case 'MATRIZ':
						// Activo el scroll si es necesario ante la imposiblidad de saber de antemano la altura del widget
						//el.setAttribute('scrolling','auto');
						el.setAttribute('src', rutaBase+'widgets/calendarioMatriz.php?'+extras+'modo=lectura');
						break;
					default:
						el.setAttribute('src', rutaBase+'widgets/calendarioLectura.php?'+extras+'modo=ANUAL');
				}

			}else if ((typeof avaibook_lectura != "undefined") && (!avaibook_lectura)){
				// Calendario Reserva

				var aux = "";

				if ((typeof avaibook_boton != "undefined") && (avaibook_boton != null)){

					if (avaibook_boton == 'grande')
						aux = "boton_grande_avaibook=1&";

					if (avaibook_boton == 'pequenyo')
						aux = "boton_peq_avaibook=1&";

				}

				switch(avaibook_tipo){
					case 'MES':
						el.setAttribute('src', rutaBase+'widgets/calendarioReserva.php?'+extras+aux+'modo=MES');
						break;
					case 'TRESMES':
						el.setAttribute('src', rutaBase+'widgets/calendarioReserva.php?'+extras+aux+'modo=TRESMES');
						break;
					case 'MATRIZ':
						// Activo el scroll si es necesario ante la imposiblidad de saber de antemano la altura del widget
						//el.setAttribute('scrolling','auto');
						el.setAttribute('src', rutaBase+'widgets/calendarioMatriz.php?'+extras+aux+'modo=reserva');
						break;
					default:
						el.setAttribute('src', rutaBase+'widgets/calendarioReserva.php?'+extras+aux+'modo=ANUAL');
				}

			}else
				alert("Error calendario: avaibook_lectura undefined");

		}else
			alert("Error calendario: avaibook_alojamiento undefined");

	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                                 WIDGET FORMULARIO RESERVA                                 //
	///////////////////////////////////////////////////////////////////////////////////////////////

	// Son dos condiciones para mantener la compatibilidad con widgets ya creados
	if ((avaibook_modo == "formularioReserva") || (avaibook_modo == "rangoReservaManual")){

		if (((typeof avaibook_alojamiento != "undefined") && (avaibook_alojamiento != null) && (parseInt(avaibook_alojamiento) > 0)) || (avaibook_listado)){

			if ((typeof avaibook_tipo != "undefined") && (avaibook_tipo != null) && ((avaibook_tipo == 'vertical') || (avaibook_tipo == 'columna'))){

				// Necesario para el cálculo del alto
				if (parseInt(avaibook_borde_widget) < 1)
					avaibook_borde_widget = 1;

				// Configuro los datos específicos del formulario
				anchoWidget = 235 + margen;
				altoWidget = 103 + parseInt(avaibook_borde_widget*3) + margen;
				extras += 'vertical=1';

			}else{

				// Necesario para el cálculo del alto
				if (parseInt(avaibook_borde_widget) < 1)
					avaibook_borde_widget = 1;

				// Configuro los datos específicos del formulario
				anchoWidget = 600 + margen;
				altoWidget = 50 + parseInt(avaibook_borde_widget*3) + margen;

			}

			// Configuro el ancho y alto del iframe
			el.setAttribute('width', anchoWidget + 'px');
			el.setAttribute('height', altoWidget + 'px');

			// Añado el fichero a ejecutar dentro del iframe
			el.setAttribute('src', rutaBase+'widgets/formularioReserva.php?'+extras);

		}else
			alert("Error formularioReserva: avaibook_alojamiento undefined");

	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                        WIDGET FORMULARIO RESERVA CON INFO ADICIONAL                       //
	///////////////////////////////////////////////////////////////////////////////////////////////

	// Son dos condiciones para mantener la compatibilidad con widgets ya creados
	if ((avaibook_modo == "formularioReservaConInfo") || (avaibook_modo == "rangoReserva")){

		if (((typeof avaibook_alojamiento != "undefined") && (avaibook_alojamiento != null) && (parseInt(avaibook_alojamiento) > 0)) || (avaibook_listado)){

			if ((typeof avaibook_tipo != "undefined") && (avaibook_tipo != null) && ((avaibook_tipo == 'vertical') || (avaibook_tipo == 'columna'))){

				// Necesario para el cálculo del alto
				if (parseInt(avaibook_borde_widget) < 1)
					avaibook_borde_widget = 1;

				// Configuro los datos específicos del formulario
				anchoWidget = 235 + margen;
				altoWidget = 202 + parseInt(avaibook_borde_widget*3) + margen;
				extras += 'con_info=1&vertical=1';

			}else{

				// Necesario para el cálculo del alto
				if (parseInt(avaibook_borde_widget) < 1)
					avaibook_borde_widget = 1;

				// Configuro los datos específicos del formulario
				anchoWidget = 450 + margen;
				altoWidget = 188 + parseInt(avaibook_borde_widget*3) + margen;
				extras += 'con_info=1';

			}

			// Configuro el ancho y alto del iframe
			el.setAttribute('width', anchoWidget + 'px');
			el.setAttribute('height', altoWidget + 'px');

			// Añado el fichero a ejecutar dentro del iframe
			el.setAttribute('src', rutaBase+'widgets/formularioReserva.php?'+extras);

		}else
			alert("Error formularioReservaConInfo: avaibook_alojamiento undefined");

	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                                       WIDGET SELLO                                        //
	///////////////////////////////////////////////////////////////////////////////////////////////

	if (avaibook_modo == "sello"){

		// Añado el tipo de sello a los parámetros del widget
		if ((typeof avaibook_tipo_sello != "undefined") && (avaibook_tipo_sello != null) && (avaibook_tipo_sello == "A"))
			extras += 'tipo_sello=A';
		else
			extras += 'tipo_sello=P';

		// Configuro los datos específicos del sello
		if (anchoWidget == 0)
			anchoWidget = 150 + margen;

		if (altoWidget == 0)
			altoWidget = 150 + margen;

		// Configuro el ancho y alto del iframe
		el.setAttribute('width', anchoWidget + 'px');
		el.setAttribute('height', altoWidget + 'px');

		// Añado el fichero a ejecutar dentro del iframe
		el.setAttribute('src', rutaBase+'widgets/selloAvaibook.php?'+extras);

	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                                       WIDGET MAPA                                         //
	///////////////////////////////////////////////////////////////////////////////////////////////

	if (avaibook_modo == "mapa"){

		// Configuro los datos específicos de las tarifas
		if (anchoWidget == 0)
			anchoWidget = 415 + margen;

		if (altoWidget == 0){
			if (avaibook_tipo == 'S')
				altoWidget = 370 + margen;
			else
				altoWidget = 700 + margen;
		}

		// Configuro el ancho y alto del iframe
		el.setAttribute('width', anchoWidget + 'px');
		el.setAttribute('height', altoWidget + 'px');

		// Añado el fichero a ejecutar dentro del iframe
		if (typeof avaibook_tipo != "undefined" && avaibook_tipo != null && avaibook_tipo == 'R')
			el.setAttribute('src', rutaBase+'widgets/mapa.php?'+extras+'modo=R');
		else
			el.setAttribute('src', rutaBase+'widgets/mapa.php?'+extras+'modo=S');

	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	//                                       WIDGET METEO                                        //
	///////////////////////////////////////////////////////////////////////////////////////////////

	if (avaibook_modo == "meteo"){

		// Configuro los datos específicos de la predicción
		if (avaibook_tipo == 'A')
			anchoWidget = 210 + margen;	// Normal
		else
			anchoWidget = 220 + margen;	// Compacto

		if (avaibook_tipo == 'A')
			altoWidget = 210 + margen;	// Normal
		else
			altoWidget = 175 + margen;	// Compacto

		// Configuro el ancho y alto del iframe
		el.setAttribute('width', anchoWidget + 'px');
		el.setAttribute('height', altoWidget + 'px');

		if (typeof avaibook_tipo != "undefined" && avaibook_tipo != null && avaibook_tipo == 'B')
			el.setAttribute('src', rutaBase+'widgets/tiempo.php?'+extras+'&modo=B');
		else
			el.setAttribute('src', rutaBase+'widgets/tiempo.php?'+extras+'&modo=A');

	}

	///////////////////////////////////////////////////////////////////////////////////////////////

	// Se cumple cuando se llama al widget desde una página externa. Se crea un div donde poner el widget
	if (donde == null){
		donde = avaibook_id_widget + " contentWdg " + parseInt(Math.random()*100000);
		document.write("<div id='" + donde + "'></div>");
	}

	// Se añade el iframe creado al div (ya sea pasado como parámetro o creado en el paso anterior)
	document.getElementById(donde).appendChild(el);

}// creaIframe

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

// Ponemos todos los campos a null para que no interfieran en otros widgets que pudiera haber en la página

avaibook_id_widget = null;			// Id del widget
avaibook_propietario = null;		// Id del propietario si corresponde
avaibook_alojamiento = null;		// Id del alojamiento si corresponde
avaibook_unidad_alojativa = null;	// Id de la unidad alojativa si corresponde

avaibook_modo = null;				// Define el tipo de widget

avaibook_idioma = null;				// Define el idioma del widget
avaibook_listado = null;			// Indica si el botón o formulario tiene que ir al reserva_a o al reserva_z

avaibook_lectura = null;			// Indica si el calendario tiene botón de reserva o no (para el resto de widgets no tiene importancia)
avaibook_leyenda = null;			// Indica si el calendario tiene leyenda o no (para el resto de widgets no tiene importancia)
avaibook_tipo = null;				// Tipo de calendario 'MES' 'TRESMES' 'MATRIZ'
avaibook_boton = null;				// Tipo de botón de reserva si aparece: 'grande' 'pequenyo' 'css'
avaibook_tipo_sello = null;			// Tipo de sello

avaibook_color_fondo_widget = null;			// Color del fondo
avaibook_color_texto_widget = null;			// Color del texto
avaibook_color_texto_patrocinado = null;	// Color del texto patrocinado

avaibook_icono_widget = null;		// Icono a mostrar en el botón
avaibook_texto_widget = null;		// Texto a mostrar en el botón

avaibook_borde_widget = null;		// Ancho del borde
avaibook_borde_color_widget = null;	// Color del borde

avaibook_ancho = null;				// Ancho del widget
avaibook_alto = null;				// Alto del widget

