$(document).ready(function() {
	
	// Initialise PrettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_square'});
	
	// Set the datepicker defaults
	if($.datepicker)
	{
		$.datepicker.setDefaults({
			dateFormat:		"dd/mm/yy"
		});
	}

	// Add onSelect function to automatically populate the departure date field
	$('.with_datepicker').datepicker({
		minDate		: 0,
		onSelect	: function(date,inst)
		{
			if(inst.id == 'dp_arrival')
			{
				// Create a new date based on the arrival date and add 1 day to it.					
				var new_date = new Date(inst.currentYear,inst.currentMonth,inst.currentDay);
				new_date.setDate(new_date.getDate()+1);
				
				// If the new generated date is earlier than the current departure date
				// then set the departure to be arrival + 1.
				if($('#dp_departure').datepicker('getDate') < new_date)
				{
					$('#dp_departure').datepicker('setDate',new_date);
				}
			}
		}
	});
	
	$('.open_map_view').click(function(){

		$(this).usefulDialog({
			'content': function(callback)
			{
				$.get("/book-now/ajax/load_map_view.asp?nocache",function(result) { callback(result); });
			},
			'title': "Roomzzz Map View",
			'width': 780,
			'height': 480,
			'open': function()
			{
				// When the window first opens, we need to trigger this resize to make
				// sure Google maps recognises the DIV size correctly. Annoying...
				google.maps.event.trigger(xmap.map, 'resize');
				xmap.map.setCenter(latlngbounds.getCenter());
				xmap.map.fitBounds(latlngbounds);

				window.usefulDialogReference = $(this);
			}
		});

		return false;
		
	});		
					   
						   
	//////////////////////////////////////////////////////////////////////////////////////
	/// LOAD MORE REVIEWS FOR REVIEWS BLOCK //////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	
	$('.review_block').find('.hidden_review').wrapAll('<div id="hidden_reviews_holder"></div>')
	
	$('#load_more_reviews').click(function(){
		
		$(this).remove();
		$('#hidden_reviews_holder').children().css('display','block').parent().slideDown('slow').fadeIn('slow');
		return false;
		
	});

	//////////////////////////////////////////////////////////////////////////////////////
	/// SLIDER ANIMATIONS ////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////				
	
	headingHeight = 0;
	singleLineHeight = 20;
	
	$('.slider_inner').each(function(){ // Find tallest box
		
		var itemHeight = $(this).find(':header:first').height();
		if(itemHeight > headingHeight) headingHeight = itemHeight;
		
	});
	
	$('.slider_inner').each(function(){

		
		var itemHeadingHeight = $(this).find(':header:first').height();
		var currentOffset = $(this).css('bottom').replace('px','');
		
		// Only run the individual normalisation code for normal sliders
		if(! $(this).parent().hasClass('square_slider') ) {
			
			
			if(itemHeadingHeight > singleLineHeight) {
				$(this).children('h3').addClass('double_height_slide');
				//offsetAdjustment = (itemHeadingHeight / 2) // LR - No longer adjusting offset, just adding the class
				offsetAdjustment = 0;
			}else{
				offsetAdjustment = 0;	
			}
			

		}else{ // Square Sliders
		
			if( headingHeight > itemHeadingHeight ) { // Single line H3's
				$(this).find('h3').addClass('extra_padding');
			}
			
			if( headingHeight > singleLineHeight ) {
				$(this).addClass('double_height_slide');
				offsetAdjustment = (headingHeight / 2);				
			}else{
				offsetAdjustment = 0;	
			}
			
		}
		
		$(this).data('lowerOffset', Number(currentOffset) + Number(offsetAdjustment));
		$(this).css('bottom',$(this).data('lowerOffset'))
		
	});
	
	$('.slider_wrap').hover(
	
		function() { // Hover on
		
			$(this).find('.slider_inner').animate( { bottom:'0' }, { queue:false, duration:400 })
			
		},
		
		function() { // Hover off
		
			originalOffset = $(this).find('.slider_inner').data('lowerOffset');
			$(this).find('.slider_inner').animate({ bottom:originalOffset }, { queue:false, duration:400 })	
			
		}
	
	);
	
	//////////////////////////////////////////////////////////////////////////////////////
	/// WHITE NAVIGATION FADES ///////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////		
	
	$('.fader_links li:not(.subnav li)').hover(
	
		function() { //Hover on function
			
			$(this).animate( { backgroundColor:'#FFFFFF' }, 100, function() {
			
				$(this).css('backgroundColor','#FFFFFF'); //Force it to be white on callback
			
			});

		},

		function() { // Hover off function
		
			// If anchor or its parent do not have the 'selected' selected
			
			if( (!$(this).parent().hasClass('selected')) && (!$(this).hasClass('selected')) ) {	
				
				// Revert background colour to default
				
				$(this).animate({ backgroundColor:'#F0F1EE' }, { queue:false, duration:200, complete:function(){
																											  
					$($(this)).removeAttr("style"); // Clean up the 'style' attribute on this link
				
				}
				});
					
			}
			
		}
	
	);
	
	//////////////////////////////////////////////////////////////////////////////////////
	/// IMAGE THUMBNAIL FADES ////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////
	
	$('.img_thumbnails a').live('click',function() {
		
		//Only continue if NOT selected
		if (!$(this).hasClass('selected'))
		{
			
			if ($('.temp_image').length > 0)
			{
				var currentMasterPath = $('.temp_image').attr('src');
			}
			else
			{
				var currentMasterPath = $('.master_image').attr('src');				
			}
			
			var selectedThumb = $(this).find('img');
			
			// String replace 'thumb' with 'main' to get the path of new image
			var currentPath = $(this).children('img').attr('src');
			var imagePath = currentPath; // dg 090523 - use same image - not thumb or main
			
			// Load new image into wrapper master with special class (remove any previous ones first.)
			$('.temp_image').remove();
			
			var currentTemp = $('.img_master_wrapper').append('<img src="' + imagePath + '" class="temp_image">');

			// Switch over the image thumbnails
			selectedThumb.attr('src',currentMasterPath);			

			// Fade in the new main image
			$('img.temp_image').fadeIn('medium', function(){
			
				// Switch the attributes (temp into master)
				$('.master_image').attr('src',$(this).attr('src'));

			});

		}
		
		return false;

	});

});	