
var book_now = {

	visible: false,
	visible2: false,

	outerBox: null,
	box: null,
	box2: null,
	tab: null,

	slide_padding: 5,
	slide_height: 0,

	/**
	 * Initialise things
	 */
	init: function() {

		// the elements we're dealing with
		this.outerBox = $("booking-sliders");
		this.box = $("book-now");
		this.box2 = $("check-availability");

		if (this.box && this.box2) {

			this.tab = this.box.getElementsByClassName("tab")[0];

			// how far it should slide
			this.slide_height = this.box.getHeight() - this.tab.getHeight() - this.slide_padding;

			// hide the box initially
			this.disappear();

			// set it to work onclick
			this.box.getElementsByTagName("h2")[0].onclick = function() {
				book_now.toggle();
			};
			this.box2.getElementsByTagName("h2")[0].onclick = function() {
				book_now.toggle2();
			};

		}

	},

	/**
	 * Just hide the box, don't use an effect
	 */
	disappear: function() {

		this.box.setStyle({
			top: (this.slide_height * -1) + "px"
		});
		this.box2.setStyle({
			top: (this.slide_height * -1) + "px"
		});

		this.visible = false;
		this.visible2 = false;

	},

	/**
	 * Slide up if down and down if up
	 */
	toggle: function() {

		if (this.visible) {
			this.hide();
		} else {
			this.show();
		}

	},

	/**
	 * Slide up if down and down if up
	 */
	toggle2: function() {

		if (this.visible2) {
			this.hide2();
		} else {
			this.show2();
		}

	},

	/**
	 * Slide it up
	 */
	hide: function() {

		if (!(this.visible && this.visible2)) {

			new Effect.Morph(
				this.outerBox, {
					style: { height: "40px" },
					duration: 1
				});

		}

		new Effect.MoveBy(
			this.box,
			(this.slide_height * -1),
			0
			);

		$$("#book-now h2")[0].update('<img src="files/images/arrow-right.gif" alt="" />Book now');

		this.visible = false;

	},

	/**
	 * Slide it up
	 */
	hide2: function() {

		if (!(this.visible && this.visible2)) {

			new Effect.Morph(
				this.outerBox, {
					style: { height: "40px" },
					duration: 1
				});

		}

		new Effect.MoveBy(
			this.box2,
			(this.slide_height * -1),
			0
			);

		$$("#check-availability h2")[0].update('<img src="files/images/arrow-right-red.gif" alt="" />Check availability');

		this.visible2 = false;

	},

	/**
	 * Slide it down
	 */
	show: function() {

		if (!(this.visible || this.visible2)) {

			new Effect.Morph(
				this.outerBox, {
					style: { height: ((this.box.offsetHeight + 2) + "px") },
					duration: 1
				});

		}

		new Effect.MoveBy(
			this.box,
			(this.slide_height),
			0
			);

		$$("#book-now h2")[0].update('<img src="files/images/arrow-up.gif" alt="" />Close');

		this.visible = true;

	},

	/**
	 * Slide it down
	 */
	show2: function() {

		if (!(this.visible || this.visible2)) {

			new Effect.Morph(
				this.outerBox, {
					style: { height: ((this.box.offsetHeight + 2) + "px") },
					duration: 1
				});

		}

		new Effect.MoveBy(
			this.box2,
			(this.slide_height),
			0
			);

		$$("#check-availability h2")[0].update('<img src="files/images/arrow-up-red.gif" alt="" />Close');

		this.visible2 = true;

	}

};


var book_now_arrival = {

	arrival_weekday : null,
	departure_date : null,
	arrival_day : null,
	arrival_month : null,

	init : function() {

		this.arrival_weekday = $("arrival_weekday");
		this.departure_date = $("departure_date");
		this.arrival_day = $("arrival_day");
		this.arrival_month = $("arrival_month");
		this.nights = $("nights");

		this.arrival_day.onchange = function() {
			book_now_arrival.update();
		}
		this.arrival_month.onchange = function() {
			book_now_arrival.update();
		}
		this.nights.onchange = function() {
			book_now_arrival.update();
		}

		this.update();

	},

	update : function() {

		var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
		var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

		var day = this.arrival_day.options[this.arrival_day.selectedIndex].value;

		var month_year = this.arrival_month.options[this.arrival_month.selectedIndex].value;
		var month = month_year.substr(5, 2);
		var year = month_year.substr(0, 4);

		var nights = parseInt(this.nights.value);

		var arrival_date = new Date(year, month - 1, day, 1, 0, 0);

		var departure_date = new Date();
		departure_date.setTime(arrival_date.getTime() + (nights * 86400000));

		this.arrival_weekday.innerHTML = days[arrival_date.getDay()];
		this.departure_date.innerHTML = "Departing: " + days[departure_date.getDay()] + ", " + departure_date.getDate() + " " + months[departure_date.getMonth()] + " " + departure_date.getFullYear();

	}


}

book_now.init();
book_now_arrival.init();