

var HouseAvailability = new Class({

	Implements: [Options],
	initialize: function(){
	
	},
	
	nomecasa: '',
	/**
	 * sets the height from the top where the choose a date
	 * popup should appear, and must be set before calling chooseADate
	 */
	chooseADateTop: 0,
	chooseADateLeft: 0,
	contactPageURL: '',
	
	/**
	 * Since we have more than one XClose, we put the addevents stuff in a function
	 * that we can call more than once.
	 */
	doXCloseMouseEvents: function(closeDivId){
		//shows that X close has been selected
		$(closeDivId).addEvent('mouseover', function(){
			this.setStyle('backgroundColor', '#dddddd');
		});
		//shows that X close has been de-selected
		$(closeDivId).addEvent('mouseout', function(){
			this.setStyle('backgroundColor', '')
		});
	},
	
	
	
	/**
	 * Opens the check availability window
	 */
	chooseADate: function(evt){

		//close any open results
		houseAvailability.closeADiv('availabilityResultsOuter');

		$('chooseADateDiv').setStyle('position', 'absolute');
		$('chooseADateDiv').setStyle('top', this.chooseADateTop + 'px');
		$('chooseADateDiv').setStyle('left', this.chooseADateLeft + 'px');
		
		var datesSlider = new Fx.Slide($('datesDiv'),{
		id: 'datesSlider',
		duration: 1
	}).hide().slideOut('horizontal').chain(function(){
		//we had to delay showing for Chrome and Safari
		setTimeout(function(){$('chooseADateDiv').setStyle('visibility','visible')},50);
		datesSlider.setOptions({duration: 500}).show().slideIn('horizontal');
	}).chain(function(){
		
		//get DatesDiv distanceFromBottom using the method in utils.js, which
		//measures the distance from the bottom of datesDiv to the bottom of the window
		var distanceFromBottom = getDistanceFromBottom($('datesDiv'));
		
		//if it's a negative number then the bottom is below the visible window
		if(distanceFromBottom < 0){
			scrollToBottomOfObject(distanceFromBottom);
		}			
	});

		
	},//end chooseADate()

	getChooseADateDivTop: function(){
		var chooseADateDivTop = $('chooseADateDiv').getPosition().y;
		//we've got the mainTable problem, as long as mainTable is still there
		if($defined($('mainTable'))){
			chooseADateDivTop = chooseADateDivTop - $('mainTable').getPosition().y;
		}
		return chooseADateDivTop;
	},
	
	getVitObjectTop: function(objectId){
		var objectTop = $(objectId).getPosition().y;
		//we've got the mainTable problem, as long as mainTable is still there
		if($defined($('mainTable'))){
			objectTop = objectTop - $('mainTable').getPosition().y;
		}
		return objectTop;
	},
	
	getVitObjectLeft: function(objectId){
		var objectLeft = $(objectId).getPosition().x;
		//we've got the mainTable problem, as long as mainTable is still there
		if($defined($('mainTable'))){
			objectLeft = objectLeft - $('mainTable').getPosition().x;
		}
		return objectLeft;
	},
	
	closeADiv: function(divId){
		
		if(!$defined(divId)){
			return;
		}
		//if it isn't visible
		if($(divId).getStyle('visibility') == 'hidden'){
			return;
		}
		
		var datesCloser = new Fx.Morph($(divId),{
			duration: 500
		});
		datesCloser.start({
			opacity: 0
		}).chain(function(){
			$(divId).setStyle('opacity','1');				
			$(divId).setStyle('visibility','hidden');	
		});
		
		if($defined($('quickDatesDiv')) && $('quickDatesDiv').getStyle('visibility') === 'visible'){
			$('closeButton').fireEvent('click');
		}
		
	},
	
	//checks the fields on the Choose a date div
	fieldsOk: function(){
		
		if($('data_arrivoSelect').get('type') == 'text'){
			//since they can't enter the field and fiddle with it, all we have to do is
			//check that the date is 10 characters long
		    if($('data_arrivoSelect').value.length == 10){
		    	return true;
		    }
		}else{
			if($('data_arrivoSelect').selectedIndex > 0){
				return true;
			}
		}
		//on the House description page
		alert($('selectADateTranslation').value);
	},
	
	closeResults: function(){
		houseAvailability.closeADiv('availabilityResultsOuter');
	},
	
	/**
	 * Using ajax, calls the .jsp that shows availability
	 */
	check: function(){
		
		if(!houseAvailability.fieldsOk()){
			return;
		}
		
		var myRequest = new Request({
			url: '/includedocs/houseInclude/DoAvailability.jsp;jsessionid=' + $('jsessionid').value,
			method: 'get',
			onSuccess: function(responseText, responseXML){
				$('availabilityResultsOuter').setStyle('top', houseAvailability.availabilityTop + 'px');
				$('availabilityResultsOuter').setStyle('left', houseAvailability.availabilityLeft + 'px');
				$('availabilityResultsOuter').set('html',responseText);
				$('availabilityResultsOuter').setStyle('visibility', 'visible');
				
				houseAvailability.doXCloseMouseEvents('xCloseResults');
				$('xCloseResults').addEvent('click', houseAvailability.closeResults);
				
				//close the choose a date window
				houseAvailability.closeADiv('chooseADateDiv');
				//find any errorMessage and alert it
				var htmlElement = new Element('div',{html: responseText});
				var errorInput = htmlElement.getElement('input[id=errorMessage]');
				if($defined(errorInput)){
					var errorMessage = errorInput.get('value');
					if($defined(errorMessage) && errorMessage.length > 0){
						alert(errorMessage);
					}
				}
				
				var myDrag = new Drag.Move($('availabilityResultsOuter'));
			},
			onFailure: function(responseText, responseXML){
				/*
				widget.toggleCursor("off");
				alert('Something went wrong. Connection is down?')
				*/
			}
		});
		
		var parms = 'nomecasa=' + $('availabilityNomecasa').value;
		parms += '&' + 'data_arrivo=' + $('data_arrivoSelect').value;
		
		if($defined($('noOfNights'))){
			parms += '&' + 'noOfNights=' + $('noOfNights').value;
		}//end if
		if($defined($('noOfMonths'))){
			parms += '&' + 'noOfMonths=' + $('noOfMonths').value;
		}//end if
		parms += '&' + 'noOfPeople=' + $('noOfPeopleSelect').value;
		parms += '&' + 'contactPageURL=' + encodeURIComponent(houseAvailability.contactPageURL);
		if($defined($('aunparm'))){
			parms += '&' + 'aun=' + encodeURIComponent($('aunparm').value);			
		}//end if
		if($defined(houseAvailability.popupBackgroundColor)){
			parms += '&' + 'popupBackgroundColor=' + encodeURIComponent(houseAvailability.popupBackgroundColor);	
		}
		if($defined(houseAvailability.popupLinksColor)){
			parms += '&' + 'popupLinksColor=' + houseAvailability.popupLinksColor;	
		}
		
		myRequest.send(parms);
		

	}//end check()
	
});



