function setFaceboxCloseLocation(href) {
	$(document).unbind('close.facebox');
	$(document).bind('close.facebox', function() {
		window.location.href = href;
	});
}

$(document).ready(function() {
	
	/** AJAX forms -------------------------------------------------------------*/
	
	$('form#new_contact_message').submit(function() {
		updateAntispamField('contact_message[antispam]', [
					'contact_message[name]', 'contact_message[email]', 'contact_message[message]'
		]);
		$.post('/contact_messages', $(this).serialize(), null, 'script');
		return false;
	});
	
	$('form#new_reservation').submit(function() {
		updateAntispamField('reservation[antispam]', [
			'reservation[name]', 'reservation[email]', 'reservation[phone]'
		]);
		$.post('/reservations', $(this).serialize(), null, 'script');
		return false;
	});
	
	$('form#new_guestbook_post').submit(function() {
		updateAntispamField('guestbook_post[antispam]', [
			'guestbook_post[author_name]', 'guestbook_post[author_email]', 'guestbook_post[text]'
		]);
		$.post('/guestbook_posts', $(this).serialize(), null, 'script');
		return false;
	});
	
	$('form#new_newsletter_subscription').submit(function() {
		updateAntispamField('newsletter_subscription[antispam]', [
			'newsletter_subscription[email]'
		]);
		$.post('/newsletter_subscriptions', $(this).serialize(), null, 'script');
		return false;
	});
	
	/** Gallery ----------------------------------------------------------------*/
  
  var onMouseOutOpacity = 0.67;
	
	$('#thumbs ul.thumbs li').css('opacity', onMouseOutOpacity)
		.hover(
			function () {
				$(this).not('.selected').fadeTo('fast', 1.0);
			}, 
			function () {
				$(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
			}
		);
  
  var galleryAdv = $('#pictures').galleriffic('#thumbs', {
		delay:                  5000,
		numThumbs:              12,
		preloadAhead:           10,
		enableTopPager:         true,
		enableBottomPager:      true,
		imageContainerSel:      '#slideshow',
		controlsContainerSel:   '#controls',
		captionContainerSel:    '#caption',
		loadingContainerSel:    '#loading',
		renderSSControls:       true,
		renderNavControls:      true,
		playLinkText:           'Play Slideshow',
		pauseLinkText:          'Pause Slideshow',
		prevLinkText:           '&lsaquo; Previous Photo',
		nextLinkText:           'Next Photo &rsaquo;',
		nextPageLinkText:       'Next &rsaquo;',
		prevPageLinkText:       '&lsaquo; Prev',
		enableHistory:          true,
		autoStart:              false,
		onChange:               function(prevIndex, nextIndex) {
			$('#thumbs ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
		},
		onTransitionOut:        function(callback) {
			$('#caption').fadeTo('fast', 0.0);
			$('#slideshow').fadeTo('fast', 0.0, callback);
		},
		onTransitionIn:         function() {
			$('#slideshow').fadeTo('fast', 1.0);
			$('#caption').fadeTo('fast', 1.0);
		},
		onPageTransitionOut:    function(callback) {
			$('#thumbs ul.thumbs').fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:     function() {
			$('#thumbs ul.thumbs').fadeTo('fast', 1.0);
		}
	});
	
	$('.datepicker').datepicker({
		minDate: new Date()
	});

	/** Hints ---------------------------------------------------------------- **/

	$('input[name="newsletter_subscription[email]"]').hint();
	
  /** Change links if we are in preview mode -------------------------------- */
	
	var address = window.location.href.toString().split("?theme");
  
	if(address.length > 1) {
		$("a").each(function() {
  			$(this).attr('href', $(this).attr('href') + "?theme" + address[1]);
		});

		// deny guestbook pagination links access
		$("#pagination a").click(function(){ return false; });
  	}

	/** View Map -------------------------------------------------------------- */
	
	$('#goto_map').click(function(){
		$.facebox('<iframe src="'+ $(this).attr('href') +'" style="margin-top: 20px; width: 620px; height: 420px; border: none;"></iframe>');
		return false;
	});
});

/** Antispam functions ********************************************************/

function updateAntispamField(antispam_field_name, serializable_fields_names){
	var val = '';
	for(var i=0; i<serializable_fields_names.length; i++){
		val += $('[name=' + serializable_fields_names[i] + ']').val() + '/';
	}
	val = val.substr(0, val.length - 1);
	$('input[name=' + antispam_field_name + ']').val(val);
}