(function(){
	
	$.fn.infiniteCarousel = function(){
		function repeat(str, n){
			return new Array( n + 1 ).join(str);
		}
	
		return this.each(function(){
			// magic!
			var $wrapper = $('> .wrapper', this).css('overflow', 'hidden'),
			$slider = $wrapper.find('> div').width(9999),
			$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
				}, 750, 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);
					}

					$('#pk_'+currentPage).find('> img').attr("src", $('#pk_'+currentPage).find('> img').attr("src").replace('_hover.jpg', '.jpg'));
					$('#pk_'+page).find('> img').attr("src", $('#pk_'+page).find('> img').attr("src").replace('.jpg', '_hover.jpg'));

					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');
					}
				}, 5000);
				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');
					}
				}, 5000);
				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);

	$('#pk_1').click(function(){
		$('.infiniteCarousel').trigger('goto', 1);
	});

	$('#pk_2').click(function(){
		$('.infiniteCarousel').trigger('goto', 2);
	});

	$('#pk_3').click(function(){
		$('.infiniteCarousel').trigger('goto', 3);
	});
	
	$('#pk_4').click(function(){
		$('.infiniteCarousel').trigger('goto', 4);
	});
	
	$('#pk_5').click(function(){
		$('.infiniteCarousel').trigger('goto', 5);
	});

	
});
