var quickBooking = new Class({
	Implements: [Options],

	options: {
		rooms: 'arrdates.iRooms'
	},

	initialize: function(form) {
		if ($(form)) {
			$$('.room_row').setStyle('display', 'none');
			$(form).addEvent('submit', this.formSubmit);
			$(this.options.rooms).addEvent('change', this.changeRooms.bindWithEvent(this));
			this.changeRooms();
		}
	},

	changeRooms: function(evt) {
		var sf = $(this.options.rooms);
		var numberOfRooms = parseInt(sf.options[sf.selectedIndex].value);
		var roomRows = $$('.room_row');
		roomRows.each(function(row){
			row.setStyle('display', 'none');
			for (i = 1; i < numberOfRooms+1; i++) {
				if (row.hasClass('room_nr_'+i)) {
					row.setStyle('display', '');
					break;
				}
			}
		});
	},

	formSubmit: function(evt) {
		evt.preventDefault();
		this.target = 'hws_win';
		window.open(this.action, 'hws_win', 'width=700,height=660,resizable=yes,scrollbars=yes,status=yes');
		this.submit();
	}
});

var quickBookingRooms = new Class({
	Extends: quickBooking,

	options: {
		arrival_day:     'arrival_day',
		arrival_month:   'arrival_month',
		arrival_year:    'arrival_year',
		departure_day:   'departure_day',
		departure_month: 'departure_month',
		departure_year:  'departure_year',
		nights:          'dates.nights',
		arrivalDD:       'dates.arrivalDay',
		arrivalMMYY:     'dates.arrivalMMYY',
		departureDD:     'dates.departureDay',
		departureMMYY:   'dates.departureMMYY',
		dtarrival:       'dates.dtarrival',
		dtdeparture:     'dates.dtdeparture',
		rooms:           'dates.iRooms'
	},

	initialize: function(form) {
		if ($(form)) {
			this.parent(form);
			this.arrivalFields   = [$(this.options.arrival_day), $(this.options.arrival_month), $(this.options.arrival_year)];
			this.departureFields = [$(this.options.departure_day), $(this.options.departure_month), $(this.options.departure_year)];
			this.setDate('arrival');
			this.setDate('departure');
			this.calculateNights(this.arrivalDate, this.departureDate);
			$$(this.arrivalFields).addEvent('change', this.changeArrival.bindWithEvent(this));
			$$(this.departureFields).addEvent('change', this.changeDeparture.bindWithEvent(this));
		}
	},

	setDate: function(field) {
		if ('arrival' == field) {
			this.arrivalDate = new Date($(this.options.arrival_year).value, $(this.options.arrival_month).value - 1, $(this.options.arrival_day).value);
		} else if ('departure' == field) {
			this.departureDate = new Date($(this.options.departure_year).value, $(this.options.departure_month).value - 1, $(this.options.departure_day).value);
		}
	},

	calculateNights: function(firstDate, secondDate) {
		var nights = ((secondDate - firstDate) / (24 * 60 * 60 * 1000));
		if (0 >= nights) {
			return false;
		}
		$(this.options.nights).value = nights;
		return true;
	},

	changeArrival: function(evt) {
		var newArrivalDate = new Date($(this.options.arrival_year).value, $(this.options.arrival_month).value - 1, $(this.options.arrival_day).value);
		if (!this.calculateNights(newArrivalDate, this.departureDate)) {
//			$(this.options.arrival_day).selectedIndex = this.arrivalDate.getDate()-1;
//			$(this.options.arrival_month).selectedIndex = this.arrivalDate.getMonth();
//			for (i = 0; i < $(this.options.arrival_year).options.length; i++) {
//				if ($(this.options.arrival_year).options[i].value == this.arrivalDate.getFullYear()) {
//					$(this.options.arrival_year).selectedIndex = i;
//					break;
//				}
//			}
//			alert('Das Anreisedatum muss vor dem Abreisedatum liegen.')
//			return false;
		}
		this.arrivalDate = newArrivalDate;
		$(this.options.dtarrival).value = this.arrivalDate.getMonth()+1 + '/' + this.arrivalDate.getDate() + '/' + this.arrivalDate.getFullYear();
		$(this.options.arrivalDD).value = this.arrivalDate.getDate();
		$(this.options.arrivalMMYY).value = this.arrivalDate.getMonth()+1 + '/' + this.arrivalDate.getFullYear();
	},

	changeDeparture: function(evt) {
		var newDepartureDate = new Date($(this.options.departure_year).value, $(this.options.departure_month).value - 1, $(this.options.departure_day).value);
		if (!this.calculateNights(this.arrivalDate, newDepartureDate)) {
//			$(this.options.departure_day).selectedIndex = this.departureDate.getDate()-1;
//			$(this.options.departure_month).selectedIndex = this.departureDate.getMonth();
//			for (i = 0; i < $(this.options.departure_year).options.length; i++) {
//				if ($(this.options.departure_year).options[i].value == this.departureDate.getFullYear()) {
//					$(this.options.departure_year).selectedIndex = i;
//					break;
//				}
//			}
//			alert('Das Abreisedatum muss nach dem Anreisedatum liegen.')
//			return false;
		}
		this.departureDate = newDepartureDate;
		$(this.options.dtdeparture).value = this.departureDate.getMonth()+1 + '/' + this.departureDate.getDate() + '/' + this.departureDate.getFullYear();
		$(this.options.departureDD).value = this.departureDate.getDate();
		$(this.options.departureMMYY).value = this.departureDate.getMonth()+1 + '/' + this.departureDate.getFullYear();
	}

});

function initArrOverview() {
	var ao_select = $('arr_overview_select');
	var ao_continue = $('arr_overview_continue');
	if (ao_select && ao_continue) {
		ao_continue.addEvent('click', function(){
			openHWSWindow(ao_select.value);
		});
	}
}

function openHWSWindow(url) {
	window.open(url, 'hws_ao_win', 'width=700,height=660,resizable=yes,scrollbars=yes,status=yes');
}

window.addEvent('domready', function(){
	var qb = new quickBookingRooms('online_booking_form');
	var qba = new quickBooking('online_booking_a_form');
	initArrOverview();
});