/**
 * jQuery.geeCarousel - Adds functionality for a sliding Carousel
 * Date: 2009/08/15
 *
 * @author Brian Xerri
 * @version 1.2.0
 * @jQuery Version: jquery-1.3.2.js
 *
 ***************************
 * - Added paging functionality
 *
 * Copyright: GeeMultimedia
 **/

(function($) {
	jQuery.fn.geeCarousel = function(options) {
		/*--------------------------------
		/ Defualt Settings
		--------------------------------*/
		settings = jQuery.extend({
					prevBtnID: '.geeCarouselPrev',
					nextBtnID: '.geeCarouselNext',
					pagingID: '.geeCarouselPaging li a',
					enablePaging: false,
					animateDuration: 200,
					enableSlideshow: false,
					slideshowDelay: 5000 }
					, options);
		
		/*--------------------------------
		/ Private Properties
		--------------------------------*/
		var wrapper;
		var container;
		var elements;
		var elementCount;
		var elementWidth;
		var wrapperWidth;
		var containerWidth;
		var currentPos;
		var currentPage;
		var pageSize;
		var slideshowTimeout;
		
		/*--------------------------------
		/ Initialize
		--------------------------------*/
		this.each(function() {
			wrapper			= $(this);
			container		= $('ul', wrapper);
			elements		= $('ul li', wrapper);

			elementCount	= parseInt(elements.size());
			elementWidth	= parseInt(elements.outerWidth(true));
			wrapperWidth	= parseInt(wrapper.width());
			containerWidth	= elementCount * elementWidth;
			currentPos		= 0;
			currentPage		= 1;
			pageSize		= Math.ceil(wrapperWidth / elementWidth);
			
			container.css('width',containerWidth);
			ChangeSelectedPage();

			slideshow();
		});
		
		/*--------------------------------
		/ Control Buttons
		--------------------------------*/
		function MoveNext(position) {
			container.stop().animate({left: position}, options.animateDuration);
			currentPos = position;

			slideshow();
		}

		function ChangeSelectedPage() {
			$(options.pagingID+'.selected').removeClass('selected');
			
			if ( currentPage == 1 )	{
				
				setTimeout(function() {
					container.stop().animate({left: -3920}, 0);
					currentPos = -3920;
				}, options.animateDuration);

				$(options.pagingID+'[href="#1"]').addClass('selected');
			} else { 
				if ($(options.pagingID+'[href="#'+currentPage+'"]').size() > 0) 
					$(options.pagingID+'[href="#'+currentPage+'"]').addClass('selected');
				else {
					setTimeout(function() {
						container.stop().animate({left: 0}, 0);
						currentPos = 0;
					}, options.animateDuration);

					$(options.pagingID+'[href="#1"]').addClass('selected');
				}
			}
		}

		function slideshow() {
			if (options.enableSlideshow == true) {
				clearTimeout(slideshowTimeout);
				slideshowTimeout = setTimeout(function() {
					$(options.nextBtnID).trigger('click');
				}, options.slideshowDelay);
			}
		}

		$(options.prevBtnID).click(function() {
			var nextPos;
			if ( currentPage == 1 )	{
				container.stop().animate({left: -3920}, 0);
				currentPos = -3920;
			}
			
			if ( options.enablePaging == false ) {
				nextPos = currentPos + elementWidth;
				if (nextPos % elementWidth != 0)
					nextPos = ((nextPos/elementWidth) * elementWidth) - elementWidth;
			} else if ( options.enablePaging == true ) {
				nextPos = currentPos + (elementWidth * pageSize);
				if (nextPos % elementWidth != 0)
					nextPos = ((nextPos/(elementWidth * pageSize)) * (elementWidth * pageSize)) - (elementWidth * pageSize);
			}

			if (nextPos <= 0) {
				if ( options.enablePaging == true ) {
					currentPage = (Math.abs(nextPos/elementWidth) / pageSize)+1;
					ChangeSelectedPage();
				}

				MoveNext(nextPos);
			}
		});

		$(options.nextBtnID).click(function() {
			var nextPos;
			if ( currentPage == 1 )	{
				container.stop().animate({left: 0}, 0);
				currentPos = 0;
			}

			if ( options.enablePaging == false ) {
				nextPos = currentPos - elementWidth;
				if (nextPos % elementWidth != 0)
					nextPos = ((nextPos/elementWidth) * elementWidth) + elementWidth;
				
				if ((((containerWidth + nextPos) - wrapperWidth) + elementWidth) > 0 ) { 
					MoveNext(nextPos);
				}
			} else if ( options.enablePaging == true ) {
				nextPos = currentPos - (elementWidth * pageSize);

				if ((((containerWidth + nextPos) - wrapperWidth) + (elementWidth * pageSize)) > 0 ) { 
					currentPage = (Math.abs(nextPos/elementWidth) / pageSize)+1;
					
					MoveNext(nextPos);
					ChangeSelectedPage();
				}
			}
		});

		if (options.enablePaging == true) {
			$(options.pagingID).click(function() {
				if ($(this).hasClass("selected") == false) {
					if ( currentPage == 1 )	{
						container.stop().animate({left: 0}, 0);
						currentPos = 0;
					}
					currentPage = $(this).attr('href').replace('#','');
					var nextPos = (elementWidth * pageSize) * (currentPage-1);
					nextPos = nextPos - (nextPos * 2);

					MoveNext(nextPos);
					ChangeSelectedPage();
				}

				return false;
			});
		}

	}
})(jQuery);
