var BookingController = new Class({
	
	Extends: WindowController,
	
	options: {
	},
	
	stepNumber 	: 4,
	stepTitle	: "Proceso de reserva",
	
	initialize: function(container, options)
	{
		this.parent(container);
		this.setOptions(options);
		this.bus = new SearchBus();
		this.registerEvents();
	},
	
	setUp: function()
	{
		this.parent();
		this.returnToListButton = new Element("a", {id:"returnToList", href:"#", html:"<span></span>volver al listado de resultados"}).inject(this.container);
		this.returnToListButton.addEvent("click", this.returnToList.bind(this));
		this.loader = new Element("div", {"class":"bookingLoader", "html":"<p>Comprobando disponibilidad</p>"});
	},
	
	returnToList: function(e)
	{
		if(e)e.stop();
		this.cancelRequest();
		this.timer.pause();
		this.timer.initSecs();
		this.hide();
		this.bus.trigger("returnToList");
	},
	
	registerEvents: function()
	{
		this.bus.registerCallback(this.apartmentSelectionDone.bind(this), "apartmentSelectionDone");
		this.bus.registerCallback(this.reset.bind(this), "firstControllerChanged");
		this.bus.registerCallback(this.reset.bind(this), "secondControllerChanged");
		this.bus.registerCallback(this.hide.bind(this), "searchStart");
		this.bus.registerCallback(this.cancelRequest.bind(this), "searchStart");
	},
	
	cancelRequest: function()
	{
		if($defined(this.request))
		{
			this.request.cancel();
		}
	},
	
	reset: function()
	{
		this.container.setStyle("display","none");
	},
	
	apartmentSelectionDone: function(apartment)
	{
		this.show();
		
		
		this.generateInfoBooking(apartment);
		if($defined(this.infoApartment))
		{
			this.infoApartment.dispose();
			this.infoApartment.set("html", "");
		}
		
		this.container.setStyle("display","block");
		if($defined(this.apartment))
		{
			this.apartment.wrapper.destroy();
		}
		
		this.apartment = new Apartment($unlink(apartment.getData()));
		this.apartment.addToFavorite.destroy();
		this.apartment.chooseButton.destroy();
		this.apartment.infoContainer.removeEvents();
		this.loadBookingInfo(apartment);
	},
	
	generateInfoBooking: function(apartment)
	{
		if($defined(this.infoBooking))
		{
			this.infoBooking.destroy();	
		}
		var crsLabel = apartment.data[apartment.dataProperties.crsLabelProperty];
		this.infoBooking = new Element("div", {id:"infoBooking", html:"<h2>Espera un momento...</h2> <p>Tu reserva será gestionada por <span>"+crsLabel+"</span></p>"}).inject(this.returnToListButton, "after");
	},
	
	hide: function()
	{
		this.container.setStyle("display", "none");
	},
	
	show: function()
	{
		this.container.setStyle("display", "block");
	},
	
	loadBookingInfo: function(apartment)
	{
		this.loading();
		var data = apartment.getIdObject();
		
		if(!$defined(this.infoApartment))
		{
			this.infoApartment = new Element("h2", {id:"tuSeleccion"});
		}
		
		this.infoApartment.set("html", "Tu selección");
		this.infoApartment.inject(this.container);
		this.apartment.inject(this.container);
		
		this.request = new Request.JSON({url:"public/multicrsapartmentselectioncontroller/getbookingtext",
				onComplete:this.getBookingTextComplete.bind(this), 
				data:{"QUERY":JSON.encode(data)}}).send();
	},
	
	getBookingTextComplete: function(response)
	{	
		 if(!$defined(response))
         {
                 this.loadBookingInfo(this.apartment);
                 return false;
         } 
		 
		this.loaded();
		this.infoBooking.set("html", this.infoBooking.get("html")+response.ANSWER.result);
		
		$("timerEnds").setStyle("display","none");
		
		if($("resultsReturnLink"))
		{
			var link = $("resultsReturnLink"); 
			link.addEvent("click", function(e){if(e)e.stop()});
			link.addEvent("click", this.returnToList.bind(this));
		}
		
		if($("anchorBooking"))
		{
			var link = $("anchorBooking"); 
			link.addEvent("click", function(e){if(e)e.stop()});
			link.addEvent("click", this.counterFinishes.bind(this, [link]));
			this.timer = new Counter({element:"timer", onInterval:this.counterFinishes.bind(this, [link])});
			
			var link = $("anchorBooking2"); 
			link.addEvent("click", function(e){if(e)e.stop()});
			link.addEvent("click", this.goToBookingForm.bind(this, [link]));
		}
		
	},
	
	counterFinishes: function(link)
	{	
		$("bookingUrl").destroy();
		$("timerEnds").setStyle("display","block");
		this.timer.secs = "";
		this.timer.pause();
		this.goToBookingForm(link);
	},
	
	goToBookingForm: function(anchor)
	{
		
		var url = anchor.get("href");
		var popup = window.open(url);
		
		if(!popup)
		{
			document.location.href = url;
		}
		
	},
	
	getFormDataComplete: function(response)
	{
		this.formValues = response.ANSWER.result;
		var url = "searchBooking.php";//response.ANSWER.result.bookingURL;
		this.iframe.set("src", url);
		this.iframe.inject(this.container);
	},
	
	iframeLoaded: function()
	{
		var iframeDocument = this.iframe.contentWindow.document;
		$H(this.formValues.hiddenValues).each(function(value, key){
			var input = iframeDocument.getElementsByName(key)[0];
			$(input).set("value", value);
		}.bind(this))
		
		if(!this.formValues.TPVAvailable)
		{
			iframeDocument.getElementById("pagoTPV").dispose();
		}
	},
	
	popUpsBlocked: function()
	{
		var popUpsBlocked = true
		
		try {
			var testPopUp = window.open('', '', 'width=1,height=1,left=0,top=0,scrollbars=no');
			if (testPopUp) {
				popUpsBlocked = false
			}
			testPopUp.close();
		}
		catch(e){}
		
		return popUpsBlocked;
	},
	
	loading: function()
	{
		var loader = this.loader;
		loader.set("html", "<p>Comprobando disponibilidad</p>");
		loader.inject(this.container);
		(function()
		{
			loader.set("html","<p>Comprobando precio final</p>");
		}).delay(1000);
		(function()
		{
			loader.set("html","<p>Recuperando informacion de la reserva</p>");
		}).delay(2000);
	},
	
	loaded: function()
	{
		this.loader.dispose();
	}
});