(function(jQuery) {
	jQuery.fn.fadeInSequence = function(fadeInTime, timeBetween)
	{
		//Default Values
		timeBetween = typeof(timeBetween) == 'undefined' ? 100 : timeBetween;
		fadeInTime = typeof(fadeInTime) == 'undefined' ? 300 : fadeInTime;
		//The amount of remaining time until the animation is complete.
		//Initially set to the value of the entire animation duration.
		var remainingTime = jQuery(this).size() * (fadeInTime+timeBetween);
		
		var i=0; //Counter
		
		return jQuery(this).each(function()
		{
			//Wait until previous element has finished fading and timeBetween has elapsed
			jQuery(this).delay(i++*(fadeInTime+timeBetween));
			//Decrement remainingTime
			remainingTime -= (fadeInTime+timeBetween);
			if(jQuery(this).css('display') == 'none')
			{
				jQuery(this).fadeIn(fadeInTime);
			}
			else //If hidden by other means such as opacity: 0
			{
				jQuery(this).animate({'opacity' : 1}, fadeInTime);
			}
			//Delay until the animation is over to fill up the queue.
			jQuery(this).delay(remainingTime+timeBetween);
		});
	};
})(jQuery);


/** Product Navigation Plugin
(function($) {
	$.fn.productNavigation = function(){
		
		return this.each(function(el){
		
			var el = this;				
			var targetDiv = el.hash;				
			if(targetDiv == '#product-details'){
				
				// Needs seperate controls for the images
				$(el).click(function(){
					$('#scroller-inner').scrollTo(targetDiv,600);
					$('#scroll-images-wrapper ul li').hide();	
					$('#scroll-images-wrapper ul li').fadeInSequence();
						
					$(el).parent().parent().children().removeClass('current');
					$(el).parent().addClass('current');											
					return false;
				});
									
			}else{
			
				$(el).click(function(){
					$(targetDiv).css('opacity', '0');		
					$(targetDiv).animate({'opacity' : 1}, 800);				
					$('#scroller-inner').scrollTo(targetDiv, 600);
					$(el).parent().parent().children().removeClass('current');
					$(el).parent().addClass('current');
					return false;
				});

			}
			
			
		});
	}
	
})(jQuery); */


$(document).ready(function() {

	// Gallery
	// -----------------------------

	$('#scroll-images-wrapper').serialScroll({
		items:'ul',
		prev:'#img_prev a',
		next:'#img_next a',
		offset:0, //when scrolling to photo, stop 230 before reaching it (from the left)
		start:0, //as we are centering it, start at the 2nd
		duration:1200,
		force:true,
		stop:true,
		lock:false,
		cycle:false, //don't pull back once you reach the end
		easing:'easeOutQuart', //use this easing equation for a funny effect
		jump: false //click on the images to scroll to them
	});
	
	$('#show-background a').toggle(function(){
		$('#content').animate({ 'opacity':0 });
		$('#scroller').animate({ 'opacity':0 });
		$('#navigation').animate({'opacity' : 0.3});
		$(this).text('Show the content');
		
		$(window).click(function(){
			$('#navigation').animate({'opacity' : 1});
			$('#content').animate({ 'opacity':1 });
			$('#scroller').animate({ 'opacity':1 });
			$(this).unbind('click');
		});
			 	
	}, function(){
		$('#content').animate({ 'opacity':1 });
		$('#navigation').animate({'opacity' : 1});
		$('#scroller').animate({ 'opacity':1 });
		$(this).text('Hide the content')
	});


	/*$('#content h2 a').productNavigation();
	$('#scroller-nav li a').productNavigation();*/
	
	// Utilities
	// -----------------------------	

	$('#scroll-images-wrapper:not(.no-lightbox) ul li a').fancybox();

	/*$('li .caption').hide();

	$('.with-caption').hover(function(){ 
			$(this).find('.caption').fadeIn('fast')
		}, function(){ 
			$(this).find('.caption').fadeOut('fast') 
	});*/

	Cufon.replace('#navigation a', { fontFamily: 'SchoolBook' });
	Cufon.replace('h2', { fontFamily: 'SchoolBook' });
		
	// Product Search
	// -----------------------------
	var $wall = $('#content-list');
	$wall.masonry({
		itemSelector: 'li:not(.off)',
		singleMode: true,
		columnWidth: 220,
		resizeable:false,
		animate: true,
		animationOptions: {
			duration: 1000,
			queue: false
		}
	}, function(){
	});
	
	$('#filter li a').click(function(){
		// Navigation
		$('#product-filter li').removeClass('on');
		$(this).parent().addClass('on');
	
		// Reset	
		$wall.find('li').removeClass('off');
		$wall.masonry();
	
		// Get the class to be highlighted
		var productType = $(this).attr('id');
			
		// If 'View All' clicked
		if(productType == 'all') {		
			$wall.find('li')
				.removeClass('off')
				.css({'opacity':1});
		} else {
			// hide visible boxes
			$wall.find('li:not(.type-'+productType+')').addClass('off').animate({'opacity':0.3});
			// show hidden boxes
			$wall.find('li.type-'+productType).removeClass('off').css({'opacity':1});
		}
		
		// Run the masonry func
		$wall.masonry();
						
		return false;
	});
	
	
		
	
	// Product page content
	// -----------------------------
	$('#scroller-nav li a').click(function(){
		var whereTo = $(this).attr('href');
		
		// Navigation
		$('#scroller-nav li').removeClass('on');
		$(this).parent().addClass('on');
		
			// Scroll
			$('#scroller').scrollTo(whereTo, 800, function(){
				
			});

		return false;

	});
	
	// Cycle for the main image
	
	$('.main-image').cycle();	
	
});

