var MultiBuscador = function (newNombre){
	this.inicia ( newNombre );
	this.url_images = "/es/img/reservas";
	// El archivo de traducción de este gadget esta en la carpeta lang/es.js
	
	
	this.initialize = function ( ){
		
		this.get_languaje ();
		this.get_vip ();
		/*
		 * 
		 * Inicia los calendarios
		 * 
		 */
		
		this.fecha_entrada = Calendar.setup({
                    inputField     :    "entrada_fecha_1",     // id of the input field
                   ifFormat        :    "%d-%m-%Y",      // format of the input field
                    button         :    "entrada_img_1",  // trigger for the calendar (button ID)
                    align          :    "Tl",           // alignment (defaults to "Bl")
                    singleClick    :    true
                });
        this.fecha_salida = Calendar.setup({
                    inputField     :    "salida_fecha_1",     // id of the input field
                   ifFormat        :    "%d-%m-%Y",      // format of the input field
                    button         :    "salida_img_1",  // trigger for the calendar (button ID)
                    align          :    "Tl",           // alignment (defaults to "Bl")
                    singleClick    :    true
                });
                
		if ( this.session_vip ){
	     	$('entrada_img_1').src = "/images/cal_ico_transp.gif";
	     	$('salida_img_1').src = "/images/cal_ico_transp.gif";  
         }else{
	     	$('entrada_img_1').src = "/images/cal_ico_blanco.gif";
	     	$('salida_img_1').src = "/images/cal_ico_blanco.gif";
			}
		
		/*
		 * Inicia buscar hoteles por palabras clave
		 */
		this.keyword_form = $('buscador_por_palabras_clave');
		this.keyword_input_form = $("search_buscador_por_palabras_clave").value;
		$("search_buscador_por_palabras_clave").observe ('focus', this.limpiar_titulo.bindAsEventListener(this)  );
		$("boton_buscador_por_palabras_clave").observe ('click', this.search_by_keyword.bindAsEventListener(this) );
		/*
		 * Inicia buscar hoteles por tipo de viaje y destino
		 */
		this.type_place_form = $('buscador_destinos_estilos_viaje');
		$("send_buscador_destinos_estilos_viaje").observe ('click', this.search_by_type_and_city.bindAsEventListener(this) );
		/*
		 * Reservar hoteles
		 */
		 
		this.select_room_form = $("rooms_form_buscar");
		this.select_adults_form = $("adultos_form_buscar");
		this.select_babies_form = $("babies_form_buscar");
		this.select_ninnos_form = $("ninnos_form_buscar");
		
		$("destinos_form_buscar").observe ( 'change', this.load_hotels_by_destination.bindAsEventListener(this) );
		$("idhotel_form_buscar").observe ( 'change', this.set_ninnos_babbies_views.bindAsEventListener(this) );
		
		this.select_room_form.observe('change', this.set_human_max.bindAsEventListener( this ) );
		this.select_adults_form.observe('change', this.set_max_childrens.bindAsEventListener( this ) );
		this.select_ninnos_form.observe('change', this.update_selected_people.bindAsEventListener( this ) );
		this.select_babies_form.observe('change', this.update_selected_people.bindAsEventListener( this ) );
		
	}
	/**
	 * Reservar hoteles
	 * @param {Object} event
	 */
	this.find_hotels = function ( event ){
		
		//this.submit_gadget ();
		var formulario = $(this.form_name);
		formulario.submit();

	}
	
	this.load_hotels_by_destination = function (){
		
		var index = $('form_buscar').destinos.selectedIndex;
		var temp = $('form_buscar').destinos[index].value;
		
		this.request = new Ajax.Request('/es/xml_reservas_hoteles.php',
					  {
					    method:'post',
						parameters: "provincia="+temp,
						onSuccess: this.on_validate.bindAsEventListener( this )
					    //onFailure: this.on_invalidate.bindAsEventListener( this )
						}
					  )
		
		
	}
	
	this.validate = function(Response){
		this.hoteles = new Array();
		try {
			eval( Response.responseText );
		} 
		catch (e) {
			alert ( e );
		}
		this.fill_hotel_select();
	}
	
	this.fill_hotel_select = function (){
		$('idhotel_form_buscar').update ('');
		this.hoteles.each (
			function ( room ){
				$('idhotel_form_buscar').insert ('<option value="'+room.id+'">'+room.nombre+'</option>');		
			}
		);
		$("idhotel_form_buscar").selectedIndex = 0;
		this.set_ninnos_babbies_views();
	}
	

	
	
	/**
	 * Buscar Hoteles por tipo de viaje y destino
	 * @param {Object} event
	 */
	this.search_by_type_and_city = function (){
		/*
		if ( $("input_tipo_viaje").value == "" ) {
			alert ("Por favor escoja un tipo de viaje");
			return false;
		}
		if ( $("destinos_buscador_destinos_estilos_viaje").selectedIndex == 0 ){
			alert ("Por favor escoja un destino");
			return false;
		}*/
		var data  =  this.type_place_form.serialize(true);
		var h = (this.languaje=='es')?('hoteles'):('hotels');
		if (data.disciplina1 != '' && data.disciplina2 != ''){
			var url = '/' + h + '/' + data.disciplina1 + '/f:' + data.disciplina2;
		}
		if (data.disciplina1 != '' && data.disciplina2 == ''){
			var url = '/' + h + '/' + data.disciplina1;
		}
		if (data.disciplina1 == '' && data.disciplina2 != ''){
			var url = '/' + h + '/' + data.disciplina2;
		}
		window.location= url;

	}
	
	
	
	
	
	/**
	 * Buscar Hoteles por Palabras Clave
	 * @param {Object} event
	 */
	this.limpiar_titulo = function ( event ){
		
		if ( this.keyword_input_form == $("search_buscador_por_palabras_clave").value){
			$("search_buscador_por_palabras_clave").value = "";
		}
		
		
	}
	this.search_by_keyword = function (){
		var input = $("search_buscador_por_palabras_clave").value;
		if ( !(input == "" || input == " buscar por palabras" || input == " " || input == "  ") ){
			var data =   this.keyword_form.serialize(true);
			var h = (this.languaje=='es')?('hoteles'):('hotels');
			data.busc = data.busc.replace(/ /g,"+");
			window.location = "/" + h + "/search/" + data.busc ;
		}
		
	}
	


}
MultiBuscador.prototype = new PersonRoom();
var m = new MultiBuscador ();
m.initialize ();
