$.fn.Carousel = function(options) {
		
		var t  = this;
		var $t = $(t);
		
		var defaultOptions = {
			nextButton: '',
			prevButton: '',
			display: 5,
			index: 0,
			transition: false,
			css: {}
		}
		
		var Carousel = $.extend(true, {}, defaultOptions, options);
		
		function int(string) {
			return string ? parseInt(string.replace('px', '')) : false;
		}
		
		function getWidth(numSlides) {
		
			if(numSlides > 0) {
				var width = 0;
				
				for(var i = 0; i < numSlides; i++) {
					width += Carousel.items.eq(i).width();	
				}
				
				return -parseInt(width);
			}
			else {
				var width = 0;
				
				for(var i = 0; i > numSlides; i--) {
					width += Carousel.items.eq(i).width();	
				}
				
				return parseInt(width);
			}
		}
		
		Carousel.goto = function(slide, callback) {
					
			if(!slide) slide = 0;
			
			if(typeof slide == "function") {
				callback = slide;
				slide = 0;
			}
			
			var difference = Carousel.index - slide;
			
			if(Carousel.items.length > Carousel.display) {
			
			//if(Carousel.index <= Carousel.items.length && !Carousel.transition) {				
				Carousel.transition = true;
				
				//var left = int(Carousel.list.css('left'));
				
				//alert(Math.abs(slide));
				
				var numSlides = Math.abs(slide);
				
				if(numSlides > Carousel.items.length - Carousel.display)
					numSlides = Carousel.items.length - Carousel.display;
				
				left = getWidth(numSlides);
				
				//if(slide > Carousel.index && 0 > (Carousel.index - Carousel.display)) {		
					Carousel.list.animate({
						left: left
					}, function() {
						Carousel.index = slide;
						Carousel.transition = false;
						Carousel.left = left;
						
						if(typeof callback == "function")
							callback();
					});
					
				//}				
			}
			
		}
		
		Carousel.next = function(callback) {			
			Carousel.goto(Carousel.index + 1, callback);
		}
		
		Carousel.prev = function(slide, callback) {			
			Carousel.goto(Carousel.index - 1, callback);
		}
		
		Carousel.init = function() {
			t.each(function() {
				var $t = $(this);
				
				Carousel.list = $t;
				Carousel.items = $t.find('li');
				
				var height = Carousel.items.getMaxHeight();				
				
				$t.wrap('<div class="ui-Carousel" />');				
				$t.parent().css('overflow-x', 'hidden');
				
				/*
				$t.css($.extend(true, Carousel.css, {
					position: 'absolute'
				}));
				*/
				
				$t.parent().css($.extend(true, Carousel.css, {
					position: 'relative',
					height: height
				}));
				
				Carousel.list.css('position', 'relative');
				
				var start = 0;
				
				Carousel.items.each(function(i) {
					var item 	= $(this);
					//var margin 	= int(item.css('margin-left')) + int(item.css('margin-right'));
									
					item.css({
						position: 'absolute',
						left: start,
						top: 0
					});		
					
					start += item.outerWidth();
					
				});
				$(Carousel.nextButton).click(function() {
					Carousel.next();
					
					return false;
				});
				
				$(Carousel.prevButton).click(function() {
					Carousel.prev();
					
					return false;
				});
			});
		}
		
		Carousel.init();
		
		return Carousel;
	}
	
	
	$.fn.getMaxHeight = function() {
		var height = false;
			
		function int(string) {
			return string ? parseInt(string.replace('px', '')) : false;
		}
	
		this.each(function() {
			var $t = $(this);
			var margin = int($t.css('margin-left')) + int($t.css('margin-right'));
			
			var $height = $t.find('div').outerHeight();
			
			if(height === false || height < $height)
				height = $height;
		});
		
		return height;
	}
