$(window).load(function(){
	
	// Set the page height on shorter pages so that the background image fills the browser window

	var docHeight = $(document).height();

	// console.log(docHeight);

	$('#news #wrap, #news #content-wrap, #news #interior-content').css('height', docHeight);
	$('#aftercare #wrap, #aftercare #content-wrap, #aftercare #interior-content').css('height', docHeight);
	$('#downloads #wrap, #downloads #content-wrap, #downloads #interior-content').css('height', docHeight);
	$('#visit #wrap, #visit #content-wrap, #visit #interior-content').css('height', docHeight);
	$('#events #wrap, #events #content-wrap, #events #interior-content').css('height', docHeight);

});

$(document).ready(function(){

	// Home page slideshow (Shop Gallery)
	$('#header-slides').cycle({
		fx: 'fade',
		speed: 2000, 
		timeout: 4000
	});
	
	// Set the dimensions of the thumbnails on the gallery page
	if($('#tattoo-thumbnails').size()) SetMargins($('#tattoo-thumbnails img'), 60, 60);

	// Set the dimensions of the thumbnails on the home page
	if($("#home .gallery-preview-img").size()) SetMargins($("#home .gallery-preview-img img"), 115, 115);

	// Set the dimensions of the thumbnails for any page that uses the vertical thumbnail list
	if($("#tattoo-thumbnails").size()) SetMargins($("#tattoo-thumbnails img"), 60, 60);

	// Gallery images - fancybox
	$("a[rel=b13_gallery_photos]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			// title = ''; // don't want to show this - remove this entire line to show photo title on modal
			return '<span id="fancybox-title-over"><em>Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; </em>' + title : '') + '</span>';
		}
	});
	
});

function SetMargins(obj, width, height) {

	$ptr = obj ? $(obj) : $('.photos .listing img');
	width = width ? width : 140;
	height = height ? height: 140;

	// Place all the images centered within its box
	$ptr.each(function() {
	    var w = $(this).width();
	    var h = $(this).height();
	    if(w > h) {
	        w = parseInt((w / h) * width);
	        $(this)
	            .height(height+'px')
	            .width(w+'px')
	            .css('margin-left', '-'+parseInt((w - width) / 2)+'px');
	    } else if(h > w) {
	        h = parseInt((h / w) * height);
	        $(this)
	            .width(width+'px')
	            .height(h+'px')
	            .css('margin-top', '-'+parseInt((h - height) / 2)+'px');
	    } else {
	        $(this)
	            .width(width+'px')
	            .height(height+'px');
	    }
	});

}
