$(document).ready(function() {
	// Decoding email links
	var reservations_link_email = '<n uers="znvygb:erfreingvbaf@vovmnebpxfubhfr.pbz" ery="absbyybj" pynff="rznvy">Rznvy</n> '; // Link text: 'Email'
	var reservations_link_email_decoded = decode_link(reservations_link_email);
	$('.reservations-email').replaceWith(reservations_link_email_decoded);
	var room39_link_clickhere = '<n uers="znvygb:ebbz39@vovmnebpxfubhfr.pbz" ery="absbyybj" pynff="rznvy">PYVPX URER</n> '; // Link text: 'CLICK HERE'
	var room39_link_clickhere_decoded = decode_link(room39_link_clickhere);
	$('.room39-clickhere').replaceWith(room39_link_clickhere_decoded);
	var events_link = 'ba <n uers="znvygb:riragf@vovmnebpxfubhfr.pbz" ery="absbyybj" pynff="rznvy">riragf@vovmnebpxfubhfr.pbz</n> '; // Link text: 'on <events email>'
	var events_link_decoded = decode_link(events_link);
	$('.events-link').replaceWith(events_link_decoded);
	
	// Vertical alignment
	$('.valign').vAlign();

	// Vertically centre page contents
	centre_wrapper();
	$(window).resize(function() {
		centre_wrapper();
	});
	
	// Initialise Google Maps
	if (jQuery.isFunction(window.google_maps_initialize)) {
		google_maps_initialize();
	}
	
	// Picture frame click events
	$("ul#mini-picture-frames li").click(function() {
		$.fancybox({
			'href': '#large-' + this.id,
			'padding': 0,
			'overlayColor': '#000'
		});
		
		// Set the top edge of the picture frame so it covers the smaller frames underneath the overlay
		$('#fancybox-wrap').css('top', (parseInt($('#wrapper').css('margin-top')) + $('p img#logo').height()) + 'px');
		
		return false;
	});

	// IE6 picture frame hover effect 	
	if (typeof document.body.style.maxHeight === "undefined") { // check for IE6
		$('ul#mini-picture-frames li').hover(
			function() {
				$(this).addClass('mini-pic-jshover');
			},
			function() {
				$(this).removeClass('mini-pic-jshover');
			}
		);
	}
	
	$("ul#mini-picture-frames li a").click(function(event) {
		event.stopPropagation();
	});

	set_random_background();
	
	// Start slideshow
	if (jQuery.isFunction(jQuery.fn.cycle)) {
		$('#slideshow').cycle({
			timeout:  5000, 
			before:   onBefore 
		});
	}

	var totalSlideCount = 15;
	
	function onBefore(curr, next, opts) {
		// On the first pass, addSlide is undefined (plugin hasn't yet created the fn)
		// When we're finished adding slides we'll null it out again
		if (!opts.addSlide) {
			return;
		}

		// onBefore arguments:
		// curr == DOM element for the slide that is currently being displayed
		// next == DOM element for the slide that is about to be displayed
		// opts == slideshow options
			
		var currentImageNum = parseInt(next.src.match(/photo_(\d{1,2})/)[1]);
		
		if (currentImageNum == totalSlideCount) {
			// Final slide in our slide slideshow is about to be displayed so there are no more to fetch
			opts.addSlide = null;
			return;
		} 
		
		// Add our next slide
		opts.addSlide('<img src="/house/images/photo_' + (currentImageNum + 1) + '.jpg" width="674" height="354" />');
	};
	
	$('#links #information a').click(function() {
		var link_id = $(this).parent().attr('id');
		$('#slideshow').addClass('hidden');
		$('#picture-frame .info-panel').addClass('hidden');
		$('#picture-frame .' + link_id).removeClass('hidden');
		set_random_background(); // Get a new background image
	});
	
	$('#links #photos a').click(function() {
		var link_id = $(this).parent().attr('id');
		$('#slideshow').removeClass('hidden');
		$('#picture-frame .info-panel').addClass('hidden');
		set_random_background(); // Get a new background image
	});
	
	$('#links #contact a').click(function() {
		set_random_background(); // Get a new background image
	});
});

Math.RandomInteger = function(n, m) {
	if (!m) { m = 1; } // default range starts at 1
	var max = n > m ? n : m; // doesn't matter which value is min or max
	var min = (n === max) ? m : n; // min is value that is not max
	var d = max - min + 1; // distribution range
	return Math.floor(Math.random() * d + min);
};

function centre_wrapper() {
	var win_height = $(window).height();
	var new_wrapper_top_margin = (win_height - $('#wrapper').height()) / 2;
	if (new_wrapper_top_margin > 0) {
		$('#wrapper').css('margin-top',new_wrapper_top_margin);
	}
}

function set_random_background() {
	var random_background = new Image(); //preload image
	random_background.src = '/house/images/background_' + Math.RandomInteger(1, 5) + '.jpg'; //preload image
	$('body').css('background-image','url(' + random_background.src + ')');
}

function decode_link(link) {
	decoded_link = link.replace(/[a-zA-Z]/g, function(c) { return String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26); });
	return decoded_link;
}

// Vertical centering
(function ($) {
	$.fn.vAlign = function(container) {
		return this.each(function(i) {
			if (container == null) {
				container = 'div';
			}
			var padding_px = 0;
			$(this).html("<" + container + ">" + $(this).html() + "</" + container + ">");
			var el = $(this).children(container + ":first");
			var elh = $(el).height(); //new element height
			var ph = $(this).height(); //parent height
			if (elh > ph) { // if new element height is larger apply this to parent
				$(this).height(elh + padding_px);
				ph = elh + padding_px;
			}
			var nh = (ph - elh) / 2; // new margin to apply
			$(el).css('margin-top', nh);
		});
	};
})(jQuery);
