/**
 * Add Click event to the calendar pane to load the calendar
 */
var calHeight;

var quick_book_arrive_day;
var quick_book_arrive_month_year;
var quick_book_nights;

var ajax_arrival = false;
var ajax_nights = false;
var ajax_depart = false;

function getPrice(arrival,departure,nights) {
	availability_request = new Request.JSON({
		url: ajax_quickprice,
		onRequest:function(){
			$$('.quick_book_price').each(function(e,i){e.set('html','loading price...');});
		},
		onSuccess: function(response){
			// update price box
			$$('.quick_book_price').each(function(e,i){e.set('html','<span class="label">Book now for only:</span><span class="display_price">'+response.price_display+'</span>');});
		},
		onFailure: function(){
			$$('.quick_book_price').each(function(e,i){e.set('html','There was a problem loading pricing data for this property, please contact us using the number at the top of the page');});
			alert('There was a problem loading pricing data for this property, please contact us using the number at the top of the page.');
		}
	}).send('arrival='+arrival+'&departure='+departure+'&nights='+nights+'&prop_ref='+propref+'&currency=GBP');
}

function checkAvailability() {
// convert nights and arrival day to integers
	quick_book_nights = Number.toInt(quick_book_nights);
	quick_book_arrive_day = Number.toInt(quick_book_arrive_day);
	quick_book_depart_day = quick_book_arrive_day + quick_book_nights;
	
	// make ajax request to get availability
	ajax_arrival = quick_book_arrive_month_year + '-' + quick_book_arrive_day;
	ajax_nights = quick_book_nights;
	ajax_depart = quick_book_arrive_month_year + '-' + quick_book_depart_day;
	
	// format dates for display
	arrival_date = new Date.parse(ajax_arrival);
	arrival_date_dsp = arrival_date.format('%A %d%o %B %Y');
	depart_date = new Date.parse(ajax_depart);
	depart_date_dsp = depart_date.format('%A %d%o %B %Y');
	
	availability = true;
	
	availability_request = new Request.JSON({
		url: ajax_quickbook+'&dateFrom='+ajax_arrival+'&dateTo='+ajax_depart,
		onRequest:function(){
			$$('.quick_availability').each(function(e,i){e.setStyle('display','none');});
			$$('.quick_book_details').each(function(e,i){e.removeClass('error').set('html','loading availability...');});
		},
		onSuccess: function(response){
			if(response && $type(response) == 'array'){
				response.each(function(r){
					availability = false;
				});
			}
			// update box with if available or not
			if (availability) {
				$$('.quick_book_details').each(function(e,i){e.set('html','<div><span class="label">Arrive on:</span> '+arrival_date_dsp+'</div><div><span class="label">Depart:</span> '+depart_date_dsp+' ('+quick_book_nights+' nights)</div><a href="#" id="quick_reset" onclick="reset_quick();return false;">Change dates</a>');});
				updateQuickBookBtn('Book now');
				getPrice(ajax_arrival,depart_date,quick_book_nights);
			} else {
				$$('.quick_availability').each(function(e,i){e.setStyle('display','block');});
				$$('.quick_book_details').each(function(e,i){e.set('html','This property is not available on '+arrival_date_dsp+' for '+quick_book_nights+' nights, please try another date.').addClass('error');
				updateQuickBookBtn('Check availability');});
			}
		},
		onFailure: function(){
			reset_quick();
			alert('There was a problem loading this property\'s availability. Please contact us for live availability.');
		}
	}).send();
}

function updateQuickBookBtn(btn_text) {
	$$('.quick_book_button').each(function(el,index) {
		el.set('value',btn_text);
	});
}

function reset_quick() {
	$$('.check_nights').each(function(e,i){e.set('value','');});
	$$('.check_arrival_day').each(function(e,i){e.set('value','');});
	$$('.check_arrival_month_year').each(function(e,i){e.set('value','');});
	$$('.quick_availability').each(function(e,i){e.setStyle('display','block');});
	$$('.quick_book_details').each(function(e,i){e.set('html','').removeClass('error');});
	$$('.quick_book_price').each(function(e,i){e.set('html','');});
	updateQuickBookBtn('Check availability')
}

window.addEvent('domready',function(){
	/*
	$$('.quick_book_button').each(function(element,index) {
		element.addEvent('click',function(e) {
			if (element.get('value') != 'Book now') {
				new Event(e).stop();
				quick_book_nights = $('nights').get('value');
				quick_book_arrive_day = $('arrival-day').get('value');
				quick_book_arrive_month_year = $('arrival-month-year').get('value');
				
				// check if we have a number of nights and arrival date
				if (!quick_book_nights || !quick_book_arrive_day || !quick_book_arrive_month_year) {
					alert('Please select an arrival date and how many nights you would like to stay for.');
					return;
				}
				checkAvailability();
			}
		});
	});
	*/

	/**
	 * Add click event to book buttons
	 */	
	$$('.book_button').each(function(el){									
		el.addEvent('click', function(e){					
			//Check if a date range is set, if so, submit book form, else switch to calenadar tab			
			if(hp_inline && hp_inline.arrival && hp_inline.departure){
				$('availability').fireEvent('submit')
			}else{
				$('calendar_pane').fireEvent('click')				
			}								
		})				
	})
	
	/**
	 * Scrolls the users browser to the top of the page when tabs are clicked
	 * if the users window is below the tabs, usef for book button events
	 */
	if ($('hp_tabs')) {
		$('hp_tabs').getElements('dt').each(function(el){
			el.addEvent('click', function(){
				if (window.getScroll().y > 150) 
					window.scrollTo(0, 150);
			})
		})
	}
	
	$$('a.change').each(function(el){				
		el.addEvent('click', function(e){
			new Event(e).stop()
			$('calendar_pane').fireEvent('click', e)			
		})				
	})
	
	/**
	 * Add click event to change dates link in booking box
	 */
	$$('.hp_price_box a.change').each(function(el){				
		el.addEvent('click', function(e){
			new Event(e).stop()
			$('calendar_pane').fireEvent('click', e)			
		})				
	})
	
	/**
	 * Add Calendar tab click event
	 */
	var cal_loaded = 0
	if ($('calendar_pane')) {
		$('calendar_pane').addEvent('click', function(){
			if (!cal_loaded) {
				hp_inline.loadCalendar.delay(10, hp_inline)
				cal_loaded = 1
			}
		})
	}
	
	/**
	 * Add map tab click event
	 */
	var map_loaded = 0
	if($('map_pane')){
		$('map_pane').addEvent('click', function(){
			if(!map_loaded){
				loadMap();
				setTimeout("refreshMap()", 10);		
				map_loaded = 1;
			}		
		})
	}
	
	//Store the function wrapper tween options
	if(!$('hp_calendar_function_wrap')) return;
	$('hp_calendar_function_wrap').store('tween:options', {duration: 200})
	
	//Make sure image in about town isnt too wide
	if($('hp_about_town')){
		var width = $('hp_about_town').getSize().x
		$$('#hp_about_town img').each(function(el){
			if(el.getSize().x > width) el.setStyle('width', width+'px').removeProperty('height')
		})
	}
	
	favourite_remove = false;
	shortlist_refresh = false;
	attachFavLinks();
	
	//Add scroll event to scroll the calendar options down page
	window.addEvent('scroll', doScroll)	
})


/**
 * scroll event to scroll the calendar options down page
 */
function doScroll(){
	
	var scroll = window.getScroll().y
	var calOffset = $('hp_calendar_functions').getPosition().y
	
	if(!calHeight) calHeight = $('hp_calendar_info').getSize().y
	
	var offset = scroll - calOffset
	if(offset < 0) offset = 0	
	if(offset + $('hp_calendar_function_wrap').getSize().y > calHeight) offset = calHeight - $('hp_calendar_function_wrap').getSize().y
	
	$('hp_calendar_function_wrap').get('tween').stop()
	$('hp_calendar_function_wrap').tween('top', offset)
}

	


var mapFix = 1;
function refreshMap(){
	if(mapFix){
		map.checkResize();	
		map.setCenter(point);
		map.checkResize();	
		mapFix = 0;
	}
}	

/** Swf and FLV switcher **/
var lastSwf = 'flash1';
function showFlash(obj, pane){

	//Activate pane
	document.getElementById(lastSwf+'_pane').style.display = 'none';
	document.getElementById(pane+'_pane').style.display = 'block';
	lastSwf = pane;
	
	return false;
}



/** special offer tooltip **
window.addEvent('domready',function(){
	$$('#special_offer').each(function(el) {
		el.addEvent('mouseenter',function(e) {
			inner = el.getFirst();
			inner.setStyle('display','block');
			inner.addEvent('mouseleave',function(ei) {
				inner.setStyle('display','none');
			});
		});
	});
});**/
