$(document).ready(function() {
	

	$('.slow_cycle').cycle(
	{
		speed: 1000, 
		timeout: 8500
	});
	
	
	$('.medium_cycle').cycle(
	{
		speed: 1000, 
		timeout: 6500
	});
		
		
	$('.fast_cycle').cycle(
	{
		speed: 1000, 
		timeout: 3500
	});
	
	
	
	$('#read_more').click(function(){
		
		$('#footer_box_text').slideToggle("slow", function(){
	        $.scrollTo('#footer_box_text', "slow");
	    });
		
		return false;
	});
	
	// external links
	$('a.external').click(function()
	{
		$(this).attr('target', '_blank');
	});
	


	// go through each form assigning a handler to save buttons
	// making sure that all labels 
	var errorAlert = '<div id="errorAlert" class="message error"><p style="margin:0;"><strong>Error</strong><br/>Please fill in all required fields (marked with a <img src="/img/forms/required.gif" style="background:#fff;padding:3px;" />) before submitting the form.</p></div>';

	$('form').submit(function()
	{
		var ret = true;
		var f = $(this).parent();
		var errorText = '<p><strong>Please fix the following errors:</strong><br/>';

		$('label[class="required"]', $(this)).each(function()
		{
			var lf = $(this).attr('for');
			var e = $('*[name="' + lf + '"]', f);
			var v = e.val();
			if ( v == '' )
			{
				e.addClass('errorInput').effect("pulsate", { times: 3 }, 150);
				$('#errorAlert').remove();
				$('p.message').after(errorAlert);
				$('#errorAlert').show();
				errorText += '<span style="width:30%;float:left;">' + lf.replace(/_/g, ' ').replace('cust', '').replace('del', 'Delivery') + '</span>';

				ret = false;
			}
			else
			{
				$('#errorAlert').hide();
				e.removeClass('errorInput');
				// ret = true;
			}
		});

		if ( ret === false )
		{
			$('#errorAlert').remove();
		
			errorText + '</p>';
			$(this).before(errorAlert);
			window.location.href += '#';
		}
		
		return ret;
		
	});
	
	
	$("a.fancybox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200,
		'titleShow'		:	false
	});
	
	
	
	
	//Only run this code on the cottage page !
	if ( $('#body_cottages').size() == 1 ) {
			
		$('body').css('position','relative');
		$('body').append('<div id="booking_overlay" style="display: none; z-index: 1500;" ></div>');
		$('#booking_overlay').click(function(){$('#booking_panel').fadeOut('slow'); $('#booking_overlay').hide(); });
		
		$('.booking_link').click(function(){	
			$('#booking_overlay').css("zIndex", 1500);
			$('#booking_panel').css("zIndex", 1600);
			$('#booking_panel').css("top", "200px");
	    	$('#booking_panel').css("left", ( $(window).width() - $('#booking_panel').width() ) / 2 + "px");
				
			$('#booking_panel').html('<p style="text-align: center; margin-top: 30px; height: 100px;">Loading - Please wait...</p>');	
			$('#booking_panel').load('/p/'+$(this).attr('href'), function(){
				$('.booking_panel_close').click(function(){$('#booking_panel').fadeOut('slow'); $('#booking_overlay').hide(); });
				$('.booking_panel_submit').click(function(){
				
					var arrival_date = $('#booking_form').find(':checked').attr('rel');
					if ( arrival_date !== undefined && arrival_date != '' ) {
						$('#arrival_date').val(arrival_date);
					}
						
					$('#booking_form').submit(); 
				});
			});
			
							
			$('#booking_overlay').show();
			
			$('#booking_panel').fadeIn('slow');
			return;	
		});
		
		//Check if we have a date from the availability page!
		if(window.location.hash) { 			
			var date = window.location.hash.substr(1, window.location.hash.length-1);
			var href = $('#row_' + date).find('a:first').attr('href');
			href += '&full';
			$('#row_' + date).find('a:first').attr('href', href);
			$('#row_' + date).find('a:first').click();
		}
		
	}
	
	
	$(function() {
	    $("[name=paymethod]").click(function(){
	            $('.toHide').hide();
	            $("#pay-"+$(this).val()).show('slow');
	    });
	 });


});



