(function(){
	$.fn.infiniteCarousel = function(){
		function repeat(str, n){
			return new Array( n + 1 ).join(str);
		}
	
		return this.each(function(){
			// magic!
			var $wrapper = $('> div', this).css('overflow', 'hidden'),
			$slider = $wrapper.find('> div').width(30000),
			$items = $slider.find('> div'),
			$single = $items.filter(':first')
			
			singleWidth = $single.outerWidth(),
			visible = Math.ceil($wrapper.innerWidth() / singleWidth),
			currentPage = 1,
			pages = Math.ceil($items.length / visible);
			/* TASKS */
			// 1. pad the pages with empty element if required
			if ($items.length % visible != 0) {
				// pad
				$slider.append(repeat('<div class="empty" />', visible - ($items.length % visible)));
				$items = $slider.find('> div');
			}

			// 2. create the carousel padding on left and right (cloned)
			$items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
			$items = $slider.find('> div');
	
			// 3. reset scroll
			$wrapper.scrollLeft(singleWidth * visible);

			// 4. paging function
			function gotoPage(page){
				var dir = page < currentPage ? -1 : 1,
				n = Math.abs(currentPage - page),
				left = singleWidth * dir * visible * n;
		
				$wrapper.filter(':not(:animated)').animate({
					scrollLeft : '+=' + left
				}, 3000, function(){
					//if page == last page - then reset position
					if(page > pages){
						$wrapper.scrollLeft(singleWidth * visible);
						page = 1;
					}else if(page == 0){
						page = pages;
						$wrapper.scrollLeft(singleWidth * visible * pages);
					}
					currentPage = page;
				});
			}
	
			// 5. insert the back and forward link
			//$wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
	
			// 6. bind the back and forward links
			$('a.car_back').click(function () {
				gotoPage(currentPage - 1);
				clearTimeout(window.car_timer);
				window.car_timer = setInterval(function(){
					if (window.autoscrolling){
						$('.infiniteCarousel').trigger('next');
					}
				}, 1500);
				return false;
			});
	
			$('a.car_forward').click(function () {
				gotoPage(currentPage + 1);
				clearTimeout(window.car_timer);
				window.car_timer = setInterval(function(){
					if (window.autoscrolling){
						$('.infiniteCarousel').trigger('next');
					}
				}, 1500);
				return false;
			});
	
			$(this).bind('goto', function (event, page) {
				gotoPage(page);
			});
	
			// THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
			$(this).bind('next', function () {
				gotoPage(currentPage + 1);
			});
		});
	};
})(jQuery);

$(document).ready(function(){


	// THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
	window.autoscrolling = true;

	$('.infiniteCarousel').infiniteCarousel().mouseover(function (){
		window.autoscrolling = false;
	}).mouseout(function (){
		window.autoscrolling = true;
	});
	window.car_timer = setInterval(function(){
		if (window.autoscrolling){
			$('.infiniteCarousel').trigger('next');
		}
	}, 5000);
	//-- set reflection and image hover
	
	$(".carousel-item img").reflect({/* Put custom options here */})
	.mouseover(function(){
		//moving the div left a bit is completely optional
		//but should have the effect of growing the image from the middle.
		//$(this).parents("div").addClass("hoverd");
		$(this).parents("div").css({"zIndex":"8"})
		$(this).stop().animate({
			"top": "0",
			"width": "100",
			"height":"141"
		}, 200);
		if (!$.browser.msie){
			$(this).parents("div:first").find("canvas").css({"zIndex":"8"}).stop().animate({
				"width": "100",
				"height":"50",
				"top":"146px"
			}, 200);
		}else{
			$(this).next().css({"zIndex":"8"}).stop().animate({
			"width": "100"
			}, 200);
		}
		
		
	}).mouseout(function(){
		var elm = $(this);
		$(this).parents("div").css({"zIndex":"2"})
		if (!$.browser.msie){
			$(this).parents("div:first").find("canvas").stop().animate({
				"width": "85",
				"height":"40",
				"top":"10px"
			}, 100);
		}else{
			$(this).next().stop().animate({
				"width": "85"
			}, 100);
		}
		$(this).stop().animate({
			"top": "10",
			"height":"120",
			"width":"85"
		}, 200,function(){
				$(elm).parents("div").removeClass("hoverd");
			});
	});
	$(".carousel-item").css({"zIndex":"2"})
$(".carousel-item").find("canvas").css({"zIndex":"2"})
});
