//jsCarousel Plugin

$.fn.jsCarousel = function(options){
	if(typeof options == 'undefined'){
		options = new Object();
	}

	//setup
	var DEFAULT_PREV_SELECTOR    = '.ant';
	var DEFAULT_NEXT_SELECTOR    = '.prox';
	var DEFAULT_PANE_SELECTOR    = '.carouselItems:first ul:first';
	var DEFAULT_ITEMS_SELECTOR   = 'li';
	var DEFAULT_DISABLED_CLASS   = 'disabled';
	var VERIFICATION_CLASS       = 'carousel-bound';

	var prevSelector   = options.prev          || DEFAULT_PREV_SELECTOR;
	var nextSelector   = options.next          || DEFAULT_NEXT_SELECTOR;
	var paneSelector   = options.pane          || DEFAULT_PANE_SELECTOR;
	var itemsSelector  = options.itemsSelector || DEFAULT_ITEMS_SELECTOR;
	var disabledClass  = options.disabledClass || DEFAULT_DISABLED_CLASS;
	var beforeStart    = options.before        || null;
	var callBack	   = options.callBack      || null;

	$(this).each(function(ind){
		//init
		if ($(this).hasClass(VERIFICATION_CLASS)) {return true}
		if (beforeStart != null) {beforeStart.apply(this, [])}

		//setup
		var $container   = $(this);
		var $prev        = $container.find(prevSelector);
		var $next        = $container.find(nextSelector);
		var $pane        = $container.find(paneSelector);
		var $items       = $pane.find(itemsSelector);

		//get width and size information
		var totalWidth = 0;
		$items.each(function(){
			totalWidth += $(this).outerWidth(true);
		})
		var avgItemWidth = parseInt(totalWidth / $items.size(),10);
		var carouselWidth = $container.width() - $next.outerWidth(true) - $next.outerWidth(true);
		var carouselLength = Math.ceil(carouselWidth / avgItemWidth);
		var carouselVisibleItems = Math.ceil(totalWidth / carouselWidth);
		var itemWidth = Math.ceil(totalWidth / carouselLength)


		//store data into carousel
		$pane.data('handler' , new jsScroll($pane));
		$pane.data("isAnimating", false);
		$pane.data("itemWidth", itemWidth);
		$pane.data("carouselOffset", 0);
		$pane.data("carouselLength", carouselLength);
		$pane.data("carouselVisibleItems", carouselVisibleItems);
		var handler = $pane.data('handler');
		
		//bind events
		var $controls = $prev.add($next);
		$controls.click(function(e){
				e.preventDefault();
				if(!$pane.data("isAnimating")) {
					if($(this).is("." + prevSelector) && !$(this).is("." + disabledClass)){
						handler.move('left')
					} else if (!$(this).is("." + disabledClass)) {
						handler.move('right');
					}
				}

				// disabled nav ant
				if($pane.data("carouselOffset") == 0) {
					$prev.addClass(disabledClass);
				} else {
					$prev.removeClass(disabledClass);
				}
				// disabled nav prox
				if($pane.data("carouselOffset") == carouselVisibleItems) {
					$next.addClass(disabledClass);
				} else {
					$next.removeClass(disabledClass);
				}
				return false;
		})
		$(this).addClass(VERIFICATION_CLASS);
	})
}





//moviment class = proximo e anterior
jsScroll = function($element){
	this.element = $element;
}

jsScroll.prototype.move = function(direction){
	var $carousel = this.element;
	var length = $carousel.data("carouselLength");
	var offset = $carousel.data("carouselOffset");
	var visible_items = $carousel.data("carouselVisibleItems");
	var itemWidth = $carousel.data("itemWidth");

	if (direction == "right") {
		if (length - offset <= visible_items) { // end of carousel
			return false;
		} else {
			$carousel.data("isAnimating", true);
			$carousel.data("carouselOffset", offset + 1);
			$carousel.css("position", "absolute").animate({
				left: ((offset + 1) * itemWidth) * -1
			}, "normal", null, function () {
				$carousel.data("isAnimating", false);
			});
			return true;
		}
	} else {
		if (offset == 0) { // end of carousel
			return false;
		} else {
			$carousel.data("isAnimating", true);
			$carousel.data("carouselOffset", offset - 1);
			$carousel.css("position", "absolute").animate({
				left: ((offset - 1) * itemWidth) * -1
			}, "normal", null, function () {
				$carousel.data("isAnimating", false);
			});
			return true;
		}
	}
}

