/**
 * @author Slide Carousel
 * @extends jquery
 */

jQuery.fn.extend({
	carousel: function () {
		if (this) {
			var me = jQuery(this),
				viewport = me.find('.carousel-viewport'),
				previous = me.find('.carousel-prev'),
				next = me.find('.carousel-next'),
				element = viewport.find('.carousel-items');
				pages = me.find('.pages a'),
				items = element.children(),
				increment = items.outerWidth("true"),
				numElmts = items.length,
				sizeFirstElmnt = increment,
				shownInViewport = Math.round(viewport.width() / sizeFirstElmnt),
				firstElementOnViewPort = 1,
				rotatePlugin = 'easeInOutElastic',
				isAnimating = false,
				data = [],
				autorotate = null,
				autorotateDelay = 3000;
			
			var initAutorotate = function () {
				autorotate = setInterval(function () {
					scrollNext();
				}, autorotateDelay)
			}
			
			var stopAutorotate = function () {
				clearInterval(autorotate);
			}
		
			var scrollPrev = function () {
				if (!isAnimating) {
					if (firstElementOnViewPort == 1) {
						jQuery(element).css('left', "-" + numElmts * sizeFirstElmnt + "px");
						firstElementOnViewPort = numElmts;
					}
					else {
						firstElementOnViewPort--;
					}
					
					animateElements("+=" + increment);
				}
			}
			
			var scrollNext = function () {
				if (!isAnimating) {
					if (firstElementOnViewPort > numElmts) {
						firstElementOnViewPort = 2;
						jQuery(element).css('left', "0px");
					}
					else {
						firstElementOnViewPort++;
					}
					
					animateElements("-=" + increment);
				}
			}
			
			var scrollTo = function (pageNum) {
				if (!isAnimating) {
					pages.removeClass('active');
					jQuery(pages[pageNum-1]).addClass('active');
					
					positioner = pageNum - firstElementOnViewPort;
					firstElementOnViewPort = pageNum;
					animateElements("-=" + increment * (positioner));
				}
			}
			
			var animateElements = function (data) {
				jQuery(element).animate({
					left: data,
					y: 0,
					queue: true
				}, rotatePlugin, function(){isAnimating = false;});
				
				isAnimating = true;
				updateInfo();
			}
				
			var updateInfo = function () {
				var idx = ((firstElementOnViewPort - 1) % numElmts == 0) ? 0 : firstElementOnViewPort - 1;
				pages.removeClass('active')
				$(pages.get(idx)).addClass('active');
			}	
				
			var init = function () {
				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();
				});
				
				pages.each(function (idx, el) {
					$(el).click(function () {
						scrollTo(idx + 1);
					});
				});
				
				me.mouseover(function(){stopAutorotate();}).mouseout(function(){initAutorotate();});
	
				initAutorotate();
				
				scrollTo(1);
			}
			
			init();
			
			return {
				scrollNext: function () {
					scrollNext();
				},
				scrollPrev: function () {
					scrollPrev();
				}
			}
		}
	}
}) 
