/**
 * @author Rygor Kharytanovich based on Stéphane Roucheray carousel 
 * @extends jquery
 */

jQuery.fn.extend({
	carousel: function (config) {
		
		if (this) {
			
			var config = config || {},
				me = jQuery(this),
				viewport = jQuery(this).find('.carousel-viewport'),
				previous = jQuery(this).find('.carousel-prev'),
				next = jQuery(this).find('.carousel-next'),
				element = viewport.find('.carousel-items'),
				items = element.children(),
				increment = items.width(),
				numElmts = items.length,
				sizeFirstElmnt = increment,
				shownInViewport = Math.round(viewport.width() / sizeFirstElmnt),
				firstElementOnViewPort = 1,
				isAnimating = false;
			
			config.onSelect = config.onSelect || null;
			config.onScroll = config.onScroll || null;
			
			var scrollPrev = function () {
				if (!isAnimating) {
					if (firstElementOnViewPort == 1) {
						jQuery(element).css('left', "-" + numElmts * sizeFirstElmnt + "px");
						firstElementOnViewPort = numElmts;
					}
					else {
						firstElementOnViewPort--;
					}
					jQuery(element).animate({
						left: "+=" + increment,
						y: 0,
						queue: true
					}, "swing", function(){isAnimating = false;});
					isAnimating = true;
					if (config.onScroll) config.onScroll(firstElementOnViewPort);
				}
			}
			
			var scrollNext = function () {
				if (!isAnimating) {
					if (firstElementOnViewPort > numElmts) {
						firstElementOnViewPort = 2;
						jQuery(element).css('left', "0px");
					}
					else {
						firstElementOnViewPort++;
					}
					jQuery(element).animate({
						left: "-=" + increment,
						y: 0,
						queue: true
					}, "swing", function(){isAnimating = false;});
					isAnimating = true;
					if (config.onScroll) config.onScroll(firstElementOnViewPort);
				}
			}
			
			var scrollTo = function (idx) {
				if (!isAnimating) {
					jQuery(element).animate({
						left: "-=" + increment * (idx - firstElementOnViewPort),
						y: 0,
						queue: true
					}, "swing", function(){isAnimating = false;});
					isAnimating = true;
					firstElementOnViewPort = idx;
					if (config.onScroll) config.onScroll(firstElementOnViewPort);
				}
			}
			
			var setActive = function (idx) {
				me
					.find('.carousel-item-active')
						.removeClass('carousel-item-active')
					.end()
					.find('.carousel-item:eq(' + (idx - 1)  + '), .carousel-item:eq(' + (idx + numElmts - 1)  + ')')
						.addClass('carousel-item-active');
			}
			
			for (i = 0; i < shownInViewport; i++) {
				jQuery(element).css('width',(numElmts+shownInViewport)*increment + "px");
				if (shownInViewport <= numElmts) {
					jQuery(element).append(jQuery(items[i]).clone());
				}
			}
			
			jQuery(this).find('.carousel-item').each(function (idx, el) {
				var num = idx % numElmts + 1;
				$(el).click(function () {
					setActive(num);
					if (config.onSelect) {
						config.onSelect(num);
					}
				})
			});
			
			jQuery(previous).click(function(event){
				event.preventDefault();
				scrollPrev()
			});
			
			jQuery(next).click(function(event){
				event.preventDefault();
				scrollNext();
			});
			
			return {
				scrollNext: function () {
					scrollNext();
				},
				scrollPrev: function () {
					scrollPrev();
				},
				scrollTo: function (idx) {
					scrollTo(idx);
				},
				setActive: function (idx) {
					setActive(idx);
				}
			}
			
		}
		
	}
}) 
