/**
 * Namespace Nirven
 */
var Nirven = (function() {
	return {
		/**
		 * Namespace Turismo 
		 */
		Turismo : { 
			/**
			 * 
			 */
			Hoteles : {
				/**
				 * 
				 */
				Buscador : {
					/**
					 * Configuración por defecto del buscador
					 */
					config : {
						date : { numberOfMonths : 2, minDate : 1},
						autocomplete : { url : null, options : { width : 260, selectFirst : false } },
						formSelector : null,
						dateSelector : null, 
						zonaSelector : null, 
						habitacionesSelector : null, 
						childsSelector : null, 
						habitacionesOnChange : null, 
						childsOnChange : null
					}, 
					/**
					 * Inicialización del buscador 
					 */
					init : function(settings) {
						if (settings) {
							jQuery.extend(this.config, settings);
						}
						if (this.config.formSelector != null) {
							jQuery(this.config.formSelector).validate();
						}
						if (this.config.dateSelector != null) {
							jQuery(this.config.dateSelector).datepicker(this.config.date);
						}
						if (this.config.zonaSelector != null) {
							jQuery(this.config.zonaSelector).autocomplete(this.config.autocomplete.url, this.config.autocomplete.options);
						}
						if (this.config.habitacionesSelector != null && this.config.habitacionesOnChange) {
							jQuery(this.config.habitacionesSelector).change(this.config.habitacionesOnChange);
						}
						if (this.config.childsSelector != null && this.config.childsOnChange) {
							jQuery(this.config.childsSelector).live('change', this.config.childsOnChange);
						}
					}
				}
			}
		}
	};
})();

/**
 * Extensión de jquery con la funcionalidad del botón comprar.
 * Se asume formulario buy_hotel_form sino se debe sobre cargar en settings. 
 * También se asume que este botón sirve para seleccionar la sucursal y una 
 * vez realiza esta elección se pasa a la elección de la habitación.    
 */
(function($) {
	jQuery.fn.boton_comprar = function(settings) {
		var config = { destino : { form : '#buy_hotel_form', sucursal_id : '#sucursal_id', accion : '#accion' },  
				origen : { contenedor : '.listado', sucursal : 'input[name^=id_sucursal]' } };
		if (settings) {
			jQuery.extend(config, settings);
		}
		this.each(function() {
			// Deberia asociar este elemento la funcion click. 
			$(this).click(function() {
				if (jQuery(config.destino.form) && jQuery(config.destino.sucursal_id)) {
					var sucursal_id = $(this).parentsUntil(config.origen.contenedor).find(config.origen.sucursal).val();
					jQuery(config.destino.sucursal_id).val(sucursal_id);
					jQuery(config.destino.accion).val('comprar');
					jQuery(config.destino.form).submit();
				}
			});
		});
		return this;
	}
})(jQuery);

/**
 * Extensión de jquery con la funcionalidad del botón consultar.
 * Se asume formulario buy_hotel_form sino se debe sobre cargar en settings. 
 * También se asume que este botón sirve para seleccionar la sucursal y una 
 * vez realiza esta elección se pasa a la elección de la habitación.    
 */
(function($) {
	$.fn.boton_consultar = function(settings) {
		var config = { destino : { form : '#consulta_hotel_form', sucursal_id : '#cst_sucursal_id', accion : '#cst_accion' },  
				origen : { contenedor : '.listado', sucursal : 'input[name^=id_sucursal]' } };
		if (settings) {
			jQuery.extend(config, settings);
		}
		this.each(function() {
			// Deberia asociar este elemento la funcion click. 
			jQuery(this).click(function() {
				if (jQuery(config.destino.form) && jQuery(config.destino.sucursal_id)) {
					var sucursal_id = $(this).parentsUntil(config.origen.contenedor).find(config.origen.sucursal).val();
					jQuery(config.destino.sucursal_id).val(sucursal_id);
					jQuery(config.destino.accion).val('consultar');
					jQuery(config.destino.form).submit();
				}
			});
		});
		return this;
	}
})(jQuery);
