// Site specific JavaScript
jQuery(function($){ 
	if ($('body.page-home')[0] || $('body.page-revisited')[0] || $("a.fancybox, [rel='lightbox[prodgroup]']")[0] ) {
		$.refractiv.core.load([
			$.refractiv.libraries.cycle,
			$.refractiv.libraries.ui.accordion,
			$.refractiv.libraries.fancybox2
		]);
		
		$("#slideshow")
		.find("#slides")
		.cycle({
			fx: 'fade',
			pause: 1,
			prev: '#prev',
			next: '#next',
			pager: '#pager'
		})
		.end()
		.hover(function() {
			$("#controls, #pager").fadeIn();
		},
		function() {
			$("#controls, #pager").fadeOut();
		});

		// Gallery Settings
		var gallery = {
			width: 920,
			padding: 5,
			margin: 5,
			total: $('#gallery').children().length
		}

		// Calculate the thumbnail sizes
		gallery.size = Math.floor((gallery.width - ((gallery.padding*2 + gallery.margin) * gallery.total)) / gallery.total);

		// Start the gallery
		$("#gallery").cycle({
			pager: "#thumbnails",
			// Build custom pager markup to display thumbnails
			pagerAnchorBuilder: function(idx, slide) {
				return '<a href="#"><img src="' + $(slide).find("img").attr("data-original") + '?height=' + gallery.size + '&amp;cropw=' + gallery.size + '&amp;scf=1" /></a>'; 
			} 
		});

		if ($('.accordion')[0]) {
			$.refractiv.core.load([$.refractiv.libraries.ui.themes.aristo]);
			$('.accordion').accordion({
				autoHeight: false
			});
		}

		// Run fancyboxes
		$("a.fancybox, [rel='lightbox[prodgroup]']").fancybox();
	}
});
