

	var CalendarQuote = function () {
	
		var firstDate;
		var secondDate;
		var innerDates = new Array();
		


	}
	CalendarQuote.prototype.selectFirstDate = function (firstDate) {
		
		if (this.firstDate != undefined) 
			this.unHighlightDate (this.firstDate);
		
		this.firstDate = firstDate;
		this.highlightDate (firstDate);
				
		//generate quote if we need to:
		if (this.firstDate != undefined && this.secondDate != undefined) {
			this.switchDatesIfRequired();
			this.getQuote();
		}
	}
	CalendarQuote.prototype.selectSecondDate = function  (secondDate) {

		if (this.secondDate != undefined) 
			this.unHighlightDate (this.secondDate);
		
		this.secondDate = secondDate;
		this.highlightDate (secondDate);
		
		//generate quote if we need to:
		if (this.firstDate != undefined && this.secondDate != undefined) {
			this.switchDatesIfRequired();
			this.getQuote();
		}
	}
	CalendarQuote.prototype.highlightDate = function (date) {

		$('#date_'+date).addClass("highlight");
	}

	CalendarQuote.prototype.unHighlightDate = function (date) {
		
		$('#date_'+date).removeClass("highlight");
	}
	CalendarQuote.prototype.resetIfRequired = function () {
		
		if (this.firstDate != undefined && this.secondDate != undefined) {
			this.resetAll();
		}
	}
	CalendarQuote.prototype.highlightDates = function (dates) {
		if (this.innerDates == undefined) this.innerDates = new Array();
		
		for (date in dates) {
			if (date != this.firstDate && date != this.secondDate) {
				this.innerDates.push(date);
				this.highlightDate(date);
			}
		}
	}
	CalendarQuote.prototype.resetAll = function () {
		
			this.unHighlightDate (this.firstDate);
			this.unHighlightDate (this.secondDate);
			
			this.firstDate = undefined;
			this.secondDate = undefined;
			
			this.resetInnerDates();
			
			this.resetLiveQuoteTooltip();
			
	}
	CalendarQuote.prototype.resetLiveQuoteTooltip = function () {

		$('#dateStatus').html("<span class='help'><img src='/img/icon_info.gif' style='vertical-align:middle;' /> click on two dates to get a live quote</span>");
		
	}
	CalendarQuote.prototype.resetInnerDates = function () {
		
		if (this.innerDates == undefined) return;
		
		for (date in this.innerDates) { 
			this.unHighlightDate (this.innerDates[date]);
		}
		this.innerDates = new Array();
		
	}
	CalendarQuote.prototype.switchDatesIfRequired = function () {
		
		// check if the dates need switching round...
		if (this.firstDate != undefined && this.secondDate != undefined) {

			var firstDateObject = new Date(this.firstDate.substr(0,4), this.firstDate.substr(5,2), this.firstDate.substr(8,2));
			var secondDateObject = new Date(this.secondDate.substr(0,4), this.secondDate.substr(5,2), this.secondDate.substr(8,2));
			
			if (secondDateObject < firstDateObject) {
				var tmp = this.secondDate;
				this.secondDate = this.firstDate;
				this.firstDate = tmp;
			}
		}
	}
	CalendarQuote.prototype.deselectFirstDate = function () {
		this.unHighlightDate (this.firstDate);
		this.firstDate = undefined;
		this.resetInnerDates();
		this.resetLiveQuoteTooltip();
	}
	CalendarQuote.prototype.deselectSecondDate = function () {
		this.unHighlightDate (this.secondDate);
		this.secondDate = undefined;
		this.resetInnerDates();
		this.resetLiveQuoteTooltip();
	}
	CalendarQuote.prototype.getQuote = function () {
		
		var calendarQuote = this;
		$('#dateStatus').html("Getting quote...");
		$.getJSON("/ajax/quote__"+this.firstDate+"_"+this.secondDate+"/",
		        function(data){

					if (!data.available) {
						 calendarQuote.resetAll();
						$('#dateStatus').html("<span class='error'><img src='/img/action_stop.gif' style='vertical-align:middle; margin-top:0px;' /> The dates you selected are not available!</span>");
						
					} else {
						calendarQuote.highlightDates(data.dates);
					//	alert("quote: "+data.quote +" ("+data.nights+" nights)");
						if (data.nights == '1') {
							calendarQuote.resetAll();
							$('#dateStatus').html("<span class='error'><img src='/img/action_stop.gif' style='vertical-align:middle; margin-top:0px;' /> Sorry there is a minimum 2 night stay</span>");
								
							return;
						}
						$('#dateStatus').html("<strong>"+data.quote +"</strong> ("+data.nights+" nights) <a href='#' onclick='calendarQuote.resetAll(); return false;' style='font-size:11px; font-weight:bold;'>clear this search</a> <a href='/book/event/?from="+calendarQuote.firstDate+"&to="+calendarQuote.secondDate+"' class='niceButton' style='vertical-align:middle;display:inline;'>Book Now &raquo;</a>");


					}
					
		        });
		
	}
	CalendarQuote.prototype.dateSelected = function (domObject) {

		// what date has been clicked
		var date = domObject.id.substr(5);
		

		//are they clicking on the first date
		if (date == this.firstDate) {
			this.deselectFirstDate();
			return;
		}

		//are they clicking on the second date
		if (date == this.secondDate) {
			this.deselectSecondDate();
			return;
		}
		
		// are they clicking on an inner date?
		if (this.firstDate != undefined && this.secondDate != undefined && this.innerDates != undefined ) {

			var newDateObject = new Date(date.substr(0,4), date.substr(5,2), date.substr(8,2));

			var firstDateObject = new Date(this.firstDate.substr(0,4), this.firstDate.substr(5,2), this.firstDate.substr(8,2));
			var secondDateObject = new Date(this.secondDate.substr(0,4), this.secondDate.substr(5,2), this.secondDate.substr(8,2));

			if (newDateObject > firstDateObject && newDateObject < secondDateObject) {
				
				var timeDiff = (secondDateObject-firstDateObject) / 2;
		
					
				if (timeDiff < (newDateObject - firstDateObject)) {
					this.resetInnerDates();
					this.selectSecondDate(date);
				} else { 
					this.resetInnerDates();
					this.selectFirstDate(date);
				}
				
				return;
			}

		}
		
		
		//check if they've filled both details and thus need resetting...
		this.resetIfRequired();
			
			
		if (this.firstDate == undefined) 
				this.selectFirstDate(date);
		else 	this.selectSecondDate(date);
		
		

		
	}