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

jQuery.fn.extend({
	maincarousel: function (options) {
		
		if (this) {
			
			var me = jQuery(this),
				options = options || {},
				viewport = jQuery(this).find('.carousel-viewport'),
				previous = jQuery(this).find('.carousel-prev'),
				next = jQuery(this).find('.carousel-next'),
				element = viewport.find('.carousel-items');
				pages = me.find('.pages a'),
				titleEl = jQuery(this).find('.info .title'), 
				linkMoreEl = jQuery(this).find('.info .more a')[0], 
				items = element.children(),
				increment = items.width(),
				numElmts = items.length,
				sizeFirstElmnt = increment,
				shownInViewport = Math.round(viewport.width() / sizeFirstElmnt),
				firstElementOnViewPort = 1,
				isAnimating = false,
				rotateInterval = null;
			
			options.autorotate = (options.autorotate || true);
			options.rotateDelay = (options.rotateDelay || 8);
			
			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;
					updateInfo()
					if (options.autorotate) flushAutorotate()
				}
			}
			
			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;
					updateInfo();
					if (options.autorotate) flushAutorotate()
				}
			}
			
			var scrollTo = function (pageNum) {
				if (!isAnimating) {
					jQuery(element).animate({
						left: "-=" + increment * (pageNum - firstElementOnViewPort),
						y: 0,
						queue: true
					}, "swing", function(){isAnimating = false;});
					isAnimating = true;
					firstElementOnViewPort = pageNum;
					updateInfo();
					if (options.autorotate) flushAutorotate()
				}
			}
			
			var startAutorotate = function () {
				rotateInterval = setInterval(function () {
					scrollNext();
				}, options.rotateDelay * 1e3);
			}
			
			var stopAutorotate = function () {
				clearInterval(rotateInterval);
			}
			
			var flushAutorotate = function () {
				stopAutorotate();
				startAutorotate();
			}
			
			var updateInfo = function () {
				
				var idx = ((firstElementOnViewPort - 1) % numElmts == 0) ? 0 : firstElementOnViewPort - 1;
				
				pages.removeClass('active')
				$(pages.get(idx)).addClass('active');
				
			}
			
			pages.each(function (idx, el) {
				$(el).click(function () {
					scrollTo(idx + 1);
				});
			});
			
			for (i = 0; i < shownInViewport; i++) {
				jQuery(element).css('width',(numElmts+shownInViewport)*increment + "px");
				jQuery(element).append(jQuery(items[i]).clone());
			}
			
			jQuery(previous).click(function(event){
				event.preventDefault();
				scrollPrev()
			});
			
			jQuery(next).click(function(event){
				event.preventDefault();
				scrollNext();
			});
			
			if (options.autorotate) {
				startAutorotate();
			}
			
			return {
				scrollNext: function () {
					scrollNext();
				},
				scrollPrev: function () {
					scrollPrev();
				}
			}
			
		}
		
	}
}) 

