/* Custom scripts */
/*	
	Project name: Crescent Moon
	Project URI: http://www.crescent-moon.co.uk
	Author name: Ben Wellby
	Date: 3.07.11
	Version: 1
*/

$(document).ready(function() {	
	
	// slideshow (http://jquery.malsup.com/cycle/)
	$("#slideshow").cycle({
		fx: 'fade',
		timeout: 5000,
		speed: 500,
		pause: 1,
		prev: '#prev',
		next: '#next',
		pager: '#nav',
		cleartype: false // disable cleartype corrections
	});
	
	// slideshow - disable animation on interaction
	$("#slideshow-nav, #prev, #next").not('.stopped').find('a, span').click(function(){
	$('#slideshow').cycle('pause');
	$("#slideshow-nav, #prev, #next").addClass('stopped');
	return false;
	});
	
	// homepage black and white images
	$("ul.product-home li").hover(function() { //On hover...

		var thumbOver = $(this).find("img").attr("src"); //Get image url and assign it to 'thumbOver'

		//Set a background image(thumbOver) on the <a> tag - Set position to bottom
		$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});

		//Animate the image to 0 opacity (fade it out)
		$(this).find("span").stop().fadeTo('normal', 0 , function() {
			$(this).hide() //Hide the image after fade
		});
	} , function() { //on hover out...
		//Fade the image to full opacity 
		$(this).find("span").stop().fadeTo('normal', 1).show();
	});
	
	// fancybox.net
	$(".gallery-detail a").not("a.back-to-top").fancybox({
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'overlayColor'		: '#000',
		'overlayOpacity'	: 0.7,
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	
	// smooth page scroll
	$('a[href*=#]').click(function() {
	
	if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});
	
	// smooth back to top
	$('a.back-to-top').click(function(){
		$('html, body').animate({scrollTop: '0px'}, 500);
		event.preventDefault();
	});
	
	// gallery nth-child spacing (every 4th image)
	$("ul.gallery-detail li:nth-child(4n+5)").css("margin-right","0px");
	
	// google analytics
	var _gaq = _gaq || [];
		_gaq.push(['_setAccount', 'UA-25671481-1']);
		_gaq.push(['_trackPageview']);
	
	(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
});
