
/**
 * Add Click event to the calendar pane to load the calendar
 */
var calHeight;
window.addEvent('domready',function(){

	/**
	 * 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')
		})
	}
	
	//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;
}
