$(document).ready(function() {


/* ==========================================================================
   Slideshow
   ========================================================================== */

initSlideshow();


function initSlideshow() {
	$('.slideshow .display').cycle({
		fx:     'fade',
		speed:   1000,
		timeout: 5000,
		next:		'.nextSlide',
		next:		'.nextClick',
		prev:		'.previousSlide',
		pager:	'.slideshowNav',
		pagerAnchorBuilder: function(idx, slide) {
			return '<a href="#"></a>';
		}
	});
}
	
/* ==========================================================================
   News navigation
   ========================================================================== */

function newsNav(carousel) {
	jQuery('.newsNav a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
		return false;
	});
	jQuery('.nextArticle').bind('click', function() {
		carousel.next();
		return false;
	});
	jQuery('.previousArticle').bind('click', function() {
		carousel.prev();
		return false;
	});
};
$('.news ul').jcarousel({
	wrap: 'simple',
	scroll: 1,
	visible: 4,
	initCallback: newsNav,
	buttonNextHTML: null,
	buttonPrevHTML: null,
	start: 1,
	itemFirstInCallback: {
		onAfterAnimation: function(c, o, i, s) {
		size = c.options.size; i = (((i - 1) % size) + size) % size;
			jQuery('.newsNav a').removeClass('active');
			jQuery('.newsNav a:eq('+i+')').addClass('active');
		}
	}
});

/* ==========================================================================
   Product navigation 
   ========================================================================== */

function productNav(carousel) {
	jQuery('.productNav a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('rel')));
		return false;
	});
	jQuery('.nextProduct').bind('click', function() {
		carousel.next();
		return false;
	});
	jQuery('.previousProduct').bind('click', function() {
		carousel.prev();
		return false;
	});
};
$('.products ul').jcarousel({
	wrap: 'circular',
	scroll: 1,
	initCallback: productNav,
	buttonNextHTML: null,
	buttonPrevHTML: null,
	itemVisibleInCallback: {
		onAfterAnimation: function(c, o, i, s) {
		size = c.options.size; i = (((i - 1) % size) + size) % size;
			jQuery('.productNav a').removeClass('active');
			jQuery('.productNav a:eq('+i+')').addClass('active');
		}
	}
});

/* ==========================================================================
   Preview animations
   ========================================================================== */

$('.preview').hover(function() {
		$(this).stop().animate({ opacity : '0.8' }, 200);
}, function() {
		$(this).stop().animate({ opacity : '1' }, 200);
});

/* ==========================================================================
   Limit text output on News
   ========================================================================== */

$('.news article').each(function() {
	var image = $(this).find('img').height();
	var header = $(this).find('h2').height();
	var date = $(this).find('h6').height();
	var height = image + header + date;
	var max = 516; //546
	var limit = max - height;
	if (limit >= 0) {
		var paragraph = $(this).find('div');
		$(paragraph).css({ height: limit });
		$(paragraph).dotdotdot({
			after: "span"
		});
	}
});

/* ==========================================================================
   Limit text output on Featured
   ========================================================================== */

$('.featured article').each(function() {
	$(this).find('.text').dotdotdot({
		after: "span"
	});
});

/* ==========================================================================
   Product zoom functions
   ========================================================================== */

$('.cloud-zoom').click(function() {
	$('.magnify').stop().animate({ opacity: '0' }, 100);
	$('.cloud-zoom').CloudZoom({ showTitle: false });
	return false;
});

$('.display').click(function() {
	$('.magnify').stop().animate({ opacity: '1' }, 100);
	$('.cloud-zoom').each(function() {
		if ($(this).length) { $(this).data('zoom').destroy(); }
	});
});

/* ==========================================================================
   Apply InfiniteScroll on News
   ========================================================================== */
				
$('#news').infinitescroll({
	navSelector  	: ".infiniteScroll a:last",
	nextSelector 	: ".infiniteScroll a",
	itemSelector 	: "#news article",
	debug		 	: true,
	dataType	 	: 'html',
	// appendCallback	: false, // USE FOR PREPENDING
	// pathParse     	: function( pathStr, nextPage ){ return pathStr.replace('2', nextPage ); }
	}, function(newElements){
		$(this).fadeIn();
		//USE FOR PREPENDING
		//$(newElements).css('background-color','#ffef00');
		//$(this).prepend(newElements);
		//END OF PREPENDING
		window.console && console.log('context: ',this);
		window.console && console.log('returned: ', newElements);
	});

});
