/**
 * jQuery.geeCarousel - Adds functionality for a sliding Carousel
 * Date: 2009/08/15
 *
 * @author Brian Xerri
 * @version 1.3.0
 * @jQuery Version: jquery-1.3.2.js
 *
 ***************************
 * - Added paging functionality
 * - Added easing parameter
 *
 * 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,
					easingMethod: 'swing',}
					, 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 = -containerWidth;
			currentPage = 0;
			pageSize = Math.floor(wrapperWidth / elementWidth);
			

			container.prepend(elements.clone()).append(elements.clone()).css({
					width: (containerWidth *3) + 'px',
					left: '-' + containerWidth + 'px'
				});
			slideshow();
		});
		
		/*--------------------------------
		/ Control Buttons
		--------------------------------*/
		function MoveNext(position) {
			container.stop();

			var pos = container.position();
			if(pos.left < (-1.5 *containerWidth)) {
				container.css({
						'left': pos.left + containerWidth
					});
				position += containerWidth;
			}
			else if(pos.left > -0.5 * containerWidth) {
				container.css({
						'left': pos.left - containerWidth
					});
				position -= containerWidth;
			}

			container.stop().animate({left: position}, options.animateDuration, options.easingMethod);
			currentPos = position;

			slideshow();
		}

		function ChangeSelectedPage() {
			$(options.pagingID+'.selected').removeClass('selected');
			
			if ( currentPage == 0 )	{
				
				setTimeout(function() {
					container.stop().animate({left: -1456}, 0);
					currentPos = -1456;
				}, 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 ( options.enablePaging == true ) {
				nextPos = currentPos + (elementWidth * pageSize);
			}
			else {
				nextPos = currentPos + elementWidth;
			}

			MoveNext(nextPos);
		});

		$(options.nextBtnID).click(function() {
			var nextPos;			
			if ( options.enablePaging == true ) {
				nextPos = currentPos - (elementWidth * pageSize);
			}
			else {
				nextPos = currentPos - elementWidth;
			}

			MoveNext(nextPos);
		});

		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);

