(function ($) {
	$(function() {
	
		var carousel_mc = '#carousel_home',
			defaults = {
				 dispItems	: 2,
				 pagination	: true,
				 loop		: true
			},
			getScrollItems = function (mc) {
				var items = defaults.dispItems;
				
				if ($(mc + '.one').length) {
					items = 1;
				} else if ($(mc + '.two').length) {
					items = 2;
				} else if ($(mc + '.three').length) {
					items = 3;
				}
				
				return items;
			},
		
		$carousel_mc = $(carousel_mc);
		
		// Prevents duplicate instantiation
		if ($.fn.carousel && $carousel_mc.length && !$carousel_mc.data('hasCarousel') && !$('.carousel-wrap', carousel_mc).length) {	
			$carousel_mc
				.data('hasCarousel', true)
				.carousel($.extend({}, defaults, {dispItems: getScrollItems(carousel_mc)}));
		}
		
		// check if cached fancybox plugin is loaded and add carousel-check to onComplete-Stack
		if ($.cachedAjaxLightbox) {
			$.cachedAjaxLightbox.addOnComplete(function ($content) {
				var $lightboxCarousel = $content.find(carousel_mc);
				
				// Prevents duplicate instantiation
				if ($.fn.carousel && $lightboxCarousel.length && !$lightboxCarousel.data('hasCarousel') && !$('.carousel-wrap', carousel_mc).length) {
					$(carousel_mc, $content)
						.data('hasCarousel', true)
						.carousel($.extend({}, defaults, {dispItems: getScrollItems(carousel_mc)}));
				}
			});
		}
	});

}(jQuery));
