$(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;

	});
	
	// Custom warning popup box for links with class 'warn_link'
	$('.warn_link').click(function() {
		// Get target of link
		var linkTarget=$(this).attr('href');
		customConfirm(
			function() {
				window.location=linkTarget;
			},
			function() {
				return false;
			},
			'Are you sure you want to do this?'
		);
		return false;
	});
	
	$("ul.fader_links li.dropdown_parent").hover(function() {
		subnav_height = $(this).find("ul.subnav li").length * 33 - 1;
		$(this).find("ul.subnav").show().animate({height:subnav_height + 'px'}, {queue:false, duration:200});
		$(this).find('a:not(.subnav a)').css({
			borderColor		: '#390F1E'
		});
	}, function() {
		$(this).find("ul.subnav").animate({
			height			: 0
		}, {
			queue			: false, 
			duration		: 200,
			complete		: function(){
				$(this).hide();
			}
		});	
		$(this).find('a:not(.subnav a)').css({
			borderColor		: '#F0F1EE',
			borderBottom	: '1px solid #390F1E'
		});
	});		
	
	//////////////////////////////////////////////////////////////////////////////////////
	/// Aparthotel Collapsible List //////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////	

	// Aparthotel List Initialisation
	$('#apart_hotel_list li:first').addClass('noBorders');
	$('#apart_hotel_list li li li:first-child').addClass('noBorders');
	$('.sites_list',$('#apart_hotel_list')).hide();	
	
	// Unravel the selected items child and parent
	var selectedItem = $('#apart_hotel_list .selected');
	selectedItem.parents('ul').show();
	selectedItem.next('ul').show();
	selectedItem.toggleClass('dropped_header');
	
	$('#apart_hotel_list .primary_header').click(function(){
	
		$('.primary_header')
			.not($(this))
			.removeClass('dropped_header')
			.next('ul').slideUp();
			
		$(this)
			.toggleClass('dropped_header')
			.next('ul').slideToggle();	

		return false;
		
	});
	
	// Booking List Initialisation
	$('.booking_header').find('ul').hide();		
	
	$('.booking_header > a').click(function(){
				
		$('.booking_header').not($(this).parent()).find('ul').slideUp();
		$(this).parent().find('ul').slideToggle('slow');
		
		return false;
		
	});	
		

});	// End document.ready

function doPositionFixed() {
	// Checks for IE6
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if(IE6) {
		// Fix height of semi transparent mask (leave yes/no box)
		$("div#popup_mask").css("height", $(window).height() + "px");
		// Add scroll event to make sure popup and mask stay in centre of screen
		$(window).scroll(function() {
			$("div#popup_mask").css("top", $(window).scrollTop() + "px");
			
		});
	} else {
		// Make position fixed for newer browsers
		$('.fixed').css('position','fixed');	
	}		
}


function customConfirm(yes_callback, no_callback, message) {
	var no_callback=no_callback ? no_callback : null;
	// Get target of link
	var target=$(this).attr('href');
	var message=message ? message : 'Are you sure you want to do this?';
	var popup='<div id="popup_mask" class="fixed"></div><div id="popup" class="fixed"><div id="popup_inner"><h3>'+message+'</h3><div class="popup_buttons"><a class="link_button" id="popup_yes">Yes</a> <a class="link_button" id="popup_no">No</a></div></div></div>';
	$('body').append(popup);
	// Call our function to replace headings etc with nicer font		
	
	// Call our function to emulate position fixed for any popups etc
	doPositionFixed();
	// Add href attribute to yes button
	$('#popup_yes').click(function() {
		$('#popup_mask, #popup').remove();
		if(yes_callback) {
			yes_callback();
		}
	});
	// Add href attribute to no button
	$('#popup_no').click(function() {
		$('#popup_mask, #popup').remove();
		if(no_callback) {
			no_callback();
		}
	});
}