var DialogController = new Class({
	
	Extends: WindowController,
	
	active: false,
	
	done: false,
	
	changed: true,
	
	numberOfRooms :0,
	
	withSofa : false,
	
	stepNumber 	: 2,
	stepTitle	: "Opciones",
	
	ajaxProperties: {
		apartmentId	: "APARTMENT_ID"
	},
	
	initialize: function(container)
	{
		this.parent(container);
		this.selector 			= new PeopleSelector("peopleContainer", "peopleInput");
		this.questionPanel 		= new QuestionPanel("questionPanel");
		this.conclusionPanel	= new ConclusionPanel("questionPanel");
		this.totalPeople 	= {adults:4, children:0};
		this.selector.createRoomMatrix([{adults:2, children:0}, {adults:2, children:0}]);
		
		/*this.questionPanel.container.setStyle("visibility", "hidden");
		this.conclusionPanel.container.setStyle("visibility", "hidden");*/
		
		
		this.bus = new SearchBus();
		
		this.createDoneButton();
		
		this.originalPrevious	= this.container.getPrevious();
		this.container 			= this.container.dispose();
		
		this.registerEvents();
		//this.hideChildren();
		
		this.calendarDone();
		
	},
	
	recoverData: function()
	{
		this.loading();
		new Request.JSON({url:"public/dialogcontroller/getroomsdistribution", onComplete: function(response){
			this.loaded();
			var falseResponse 	= ($defined(response.ANSWER) && $defined(response.ANSWER.result) && response.ANSWER.result == false);
			
			if(falseResponse)
			{
				this.maximize();
				return false;
			}
			
			this.selector.createRoomMatrix($H(response.ANSWER.result));
			//this.doneButton.inject(this.container);
			
			this.getConclusions();
			this.getQuestions();
			if(response.ANSWER.isDefault)
			{
				this.maximize();
				return false;
			}
			
			this.minimize();
			this.peopleDone(true);
			
		}.bind(this)}).send();
	},
	
	getTotalAdultsAndChildren: function(people)
	{
		var totalPeople 		= {};
		totalPeople.adults 		= 0;
		totalPeople.children 	= 0;
		this.numberOfRooms = 0;
		$H(people).each(function(thisPeople){
			this.numberOfRooms++;
			totalPeople.adults 		= totalPeople.adults + thisPeople.adults;
			totalPeople.children 	= totalPeople.children + thisPeople.children;
			
		}.bind(this));
		
		this.totalPeople = totalPeople;
		
		return totalPeople;
	},	
	
	registerEvents: function()
	{
		this.bus.registerCallback(this.setPeople.bind(this)			,"numPeopleChange");
		this.bus.registerCallback(this.answerQuestion.bind(this)	,"onQuestionAnswered");
		this.bus.registerCallback(this.answerConclusion.bind(this)	,"onConclusionAnswered");
		
		//this.bus.registerCallback(this.calendarDone.bind(this)		, "firstControllerDone");
		//this.bus.registerCallback(this.calendarDone.bind(this)		, "firstControllerFirstSelection");
		this.bus.registerCallback(this.resetApartmentSelected.bind(this)		, "firstControllerChanged");
		this.bus.registerCallback(this.setUpNewSearch.bind(this)	, "setUpNewSearch");
	},
	
	setUpNewSearch: function()
	{
		this.totalPeople 	= {adults:1, children:0};
		this.selector.createRoomMatrix([this.totalPeople]);
		this.active = false;
		this.container.dispose();
	},
	
	calendarDone: function()
	{
		if(this.active)
		{
			return  false;
		}
		
		this.recoverData();
		this.active = false; 
		this.container.inject(this.originalPrevious, "after");
	},
	
	createDoneButton: function()
	{
		this.doneButton = new Element("a", {href:"#", html: "<span>Continuar >></span>", "class":"done"})/*.inject(this.container);*/
		this.doneButton.addEvent("click", function(event){event.stop()});
		this.doneButton.addEvent("click", this.peopleDone.bind(this));
	},
	
	peopleDone: function(firstDataRecovered)
	{
		
		if($defined(firstDataRecovered) && firstDataRecovered === true)
		{
			this.minimize();
			this.changed 	= false;
			this.done 		= true;
			this.bus.trigger("secondControllerDone");
			return false;
		}
		
		if(!this.changed)
		{
			this.minimize();
			return false;
		}
		
		var data = {};
		data["NUMBER_OF_PEOPLE"] = this.selector.getValue();
		new Request.JSON({url:"public/searchcontroller/setnumberofpeople", onComplete: function(response){
			this.resetApartmentSelected();
		}.bind(this), data:{"QUERY":JSON.encode(data)}}).send();
		
	},
	
	getPeople: function()
	{
		this.numberOfRooms = 0;
		this.loading();
		new Request.JSON({url:"public/dialogcontroller/getroomsdistribution", onComplete: function(response){
			this.loaded();
			var falseResponse 	= ($defined(response.ANSWER.result) && response.ANSWER.result == false);
			var peopleIsZero	= ($defined(response.ANSWER.NUMBER_OF_PEOPLE) && response.ANSWER.NUMBER_OF_PEOPLE == 0);
			
			if(falseResponse || peopleIsZero)
			{
				this.maximize();
				return false;
			}
			
			
			this.selector.createRoomMatrix($H(response.ANSWER.result));
		}.bind(this)}).send();
	},
	
	setPeople: function()
	{
		this.loading();
		var data = {};
		data["NUMBER_OF_PEOPLE"] = this.selector.getValue();
		//this.doneButton.inject(this.container);
		new Request.JSON({url:"public/dialogcontroller/setnumberofpeople", onComplete: function(response){
			this.getPeople();
			this.getQuestions();
			this.getConclusions();
			this.changed = true;
		}.bind(this), data:{"QUERY":JSON.encode(data)}}).send();
		
	},
	
	resetApartmentSelected: function()
	{
		new Request.JSON({url:"public/apartmentselectioncontroller/unsetselectedapartment", onComplete:function(response){
			
			this.minimize();
			this.changed 	= false;
			this.done 		= true;
			this.bus.trigger("secondControllerDone");
			this.bus.trigger("secondControllerChanged");
			try{
			pageTracker._trackPageview("/userSelectedPeople");
			}
			catch(exception){}
			
		}.bind(this)}).send();
	},	
	
	getQuestions: function()
	{
		this.questionPanel.loading();
		new Request.JSON({url:"public/dialogcontroller/getquestions", onComplete:this.printQuestions.bind(this)}).send();
	},
	
	getConclusions: function()
	{
		this.conclusionPanel.loading();
		new Request.JSON({url:"public/dialogcontroller/getconclusions", onComplete:this.printConclusions.bind(this)}).send();
	},
	
	printQuestions: function(response)
	{
		
		if (response.ANSWER.result.inquiries.length > 0) {
			this.questionPanel.container.setStyle("visibility", "visible");
		}
		else {
			//this.questionPanel.container.setStyle("visibility", "hidden");
		}
		
		this.questionPanel.loaded();
		this.questionPanel.removeQuestions();
		this.loading();
		response.ANSWER.result.inquiries.each(function(question){
			this.loaded();
			
			if(question.type == "OPTIONS")
			{
				this.questionPanel.addQuestion(new OptionalQuestion(question.data.INQUIRED_CONCEPT, question.title, question.data.OPTIONS));
			}

			if(question.type == "NUMERIC")
			{
				var options = this.getMinMaxOptions(question.data.MIN_VALUE, question.data.MAX_VALUE);
				this.questionPanel.addQuestion(new OptionalQuestion(question.data.INQUIRED_CONCEPT, question.title, options));
			}
			
		}.bind(this))
	},
	
	getMinMaxOptions: function(min, max)
	{
		var options = {};
		for(counter = min; counter<=max;counter++)
		{
			options[counter] = counter;
		}
		
		return options;
	},
	
	printConclusions: function(response)
	{
		
		if (response.ANSWER.result.conclusions.length > 0) {
			this.conclusionPanel.container.setStyle("visibility", "visible");
		}
		else{
			//this.conclusionPanel.container.setStyle("visibility", "hidden");
		}
		
		this.conclusionPanel.loaded();
		this.loading();
		this.conclusionPanel.removeConclusions();
		response.ANSWER.result.conclusions.each(function(conclusion){
				this.loaded();
				this.conclusionPanel.addConclusion(new Conclusion(conclusion.data.CONCLUSION_CONCEPT, conclusion.title, conclusion.data.OPTIONS));
		}.bind(this))
	},
	
	checkSofa: function(id, answer)
	{
		if(id == "COUCH_ALLOWED" && answer == "yes")
		{
			this.withSofa = true;
		}
		if(id == "COUCH_ALLOWED" && answer == "flush")
		{
			this.withSofa = false;
		}
		var text = this.generateHeaderText(this.totalPeople);
		this.titleHeader.set("html", text);
		if($defined(this.info.infoElement))
		{
			this.info.infoElement.destroy();
		}
		this.info.infoElement = new Element("h1", {"class":"stepTitle", html:"<span class='number'>2</span> <span class='title'>"+text+"</span>"}).inject(this.info);
	},
	
	answerQuestion: function(params)
	{
		this.loading();
		var data = {};
		data["QUESTION_VALUE"] 	= params.answer;
		data["QUESTION_KEY"] 	= params.question.getId()
		this.questionPanel.loading();
		this.changed = true;
		new Request.JSON({url:"public/dialogcontroller/answerquestion",data:{"QUERY":JSON.encode(data)}, onComplete:this.questionAnswered.bind(this)}).send();
	},
	
	questionAnswered: function(response)
	{
		if(response.ANSWER.result == true)
		{
			this.getQuestions();
			this.getConclusions();
			this.getPeople();
		}
	},
	
	conclusionAnswered: function(response)
	{
		if(response.ANSWER.result == true)
		{
			this.getQuestions();
			this.getConclusions();
			this.getPeople();
		}
	},
	
	answerConclusion: function(params)
	{
		this.loading();
		var data = {};
		data["QUESTION_VALUE"] 	= params.answer;
		data["QUESTION_KEY"] 	= params.conclusion.getId();
		this.conclusionPanel.loading();
		this.changed = true;
		new Request.JSON({url:"public/dialogcontroller/answerquestion",data:{"QUERY":JSON.encode(data)}, onComplete:this.conclusionAnswered.bind(this)}).send();
	}
	
})
