
(function($) {
	jQuery.fn.geeCarousel = function(options) {
		/*--------------------------------
		/ Defualt Settings
		--------------------------------*/
		settings = jQuery.extend({
					wrapperID: '.geeCarousel',
					prevBtnID: '.geeCarouselPrev',
					nextBtnID: '.geeCarouselNext',
					animateDuration: 50
		}, options);
		
		/*--------------------------------
		/ Private Properties
		--------------------------------*/
		var wrapper;
		var container;
		var elements;
		var elementCount;
		var elementWidth;
		var wrapperWidth;
		var containerWidth;
		var currentPos;
		
		/*--------------------------------
		/ Functions
		--------------------------------*/
		init = function() {
			//alert(this.attr('class'));
			wrapper			= $(options.wrapperID);
			container		= $(options.wrapperID + ' ul');
			elements		= $(options.wrapperID + ' ul li');

			elementCount	= parseInt(elements.size());
			elementWidth	= parseInt(elements.outerWidth(true));
			wrapperWidth	= parseInt(wrapper.width());
			containerWidth	= elementCount * elementWidth;
			currentPos		= 0;

			if($.browser.msie && $.browser.version=="6.0") 
				containerWidth += 2;

			
			container.css('width',containerWidth);

            // hide right button if enough to show all buttons
            if (wrapperWidth >= (elementWidth * elementCount))
                $(options.nextBtnID).hide();
                
			if (options.startPos != null) {
				if($.browser.msie && $.browser.version=="6.0") 
				    MoveNext(options.startPos-16);
				else
				    MoveNext(options.startPos);

			}
		}
		
		/*--------------------------------
		/ Control Buttons
		--------------------------------*/
		function MoveNext(position) {
			container.animate({left: position-2}, options.animateDuration);
			currentPos = position;
            
            var WhichItem = ((currentPos / elementWidth) * -1);
            
			if (WhichItem > 0)
			    $(options.prevBtnID).show();
			else
			    $(options.prevBtnID).hide();
			    
			if (WhichItem < elementCount - 8)
			    $(options.nextBtnID).show();
			else
			    $(options.nextBtnID).hide();
		}

		$(options.prevBtnID).click(function() {
			var nextPos = currentPos + elementWidth;
			if (nextPos % elementWidth != 0)
				nextPos = ((nextPos/elementWidth) * elementWidth);
//				nextPos = ((nextPos/elementWidth) * elementWidth) - elementWidth;

			if (nextPos <= 0) {
				MoveNext(nextPos);
			}
			
		});

		$(options.nextBtnID).click(function() {
			var nextPos = currentPos - elementWidth;
			if (nextPos % elementWidth != 0)
				nextPos = ((nextPos/elementWidth) * elementWidth);
//				nextPos = ((nextPos/elementWidth) * elementWidth) + elementWidth;
						
			if ((((containerWidth + nextPos) - wrapperWidth) + elementWidth) >= 0 ) { 
				MoveNext(nextPos);
			}
		});	
		
		// Initialize
		init();
	}
})(jQuery);

