(function($){

$.fn.carousel = function(opts) {
		
	var defaults = {
		width:	750,
		height:	75,
		offset:	0,
		easing:	'swing',
		speed:	'slow',
		direction:'horizontal'
	}
	
	opts = $.extend(defaults,opts);
	
	return $(this).each(function(){
	
		var $this = $(this);
		
		$this.wrap('<div class="carousel"><div class="carousel_window"><div class="carousel_inner"></div></div></div>');
		
		$this.closest('.carousel').append('<a href="#" class="carousel_left"></a><a href="#" class="carousel_right"></a>');
		$this.closest('.carousel_window').css({width:opts.width+'px',height:opts.height+'px',overflow:'hidden',position:'relative'});
		$this.closest('.carousel_inner').css({position:'absolute'});
		
		$this.closest('.carousel').find('a.carousel_left,a.carousel_right').click(function(){move($(this).hasClass('carousel_left')?-1:1);return false;});
		
		if(opts.direction == 'horizontal') {
			var w = 0;
			$this.find('li').each(function(){w+=$(this).outerWidth();});
			var steps = Math.ceil(w / (opts.width+opts.offset)) - 1;
			$this.closest('.carousel_inner').css({width:w+'px'});
		}
		else {
			var h = 0;
			$this.find('li').each(function(){h+=$(this).outerHeight();});
			var steps = Math.ceil(h / (opts.height+opts.offset)) - 1;
			$this.closest('.carousel_inner').css({height:h+'px'});
		}
		function move(d) {
			var	cur = $this.data('cur') || 0,
				to = Math.max(Math.min(steps,cur+d),0);
			
			if(to != cur) {
				$this.data('cur',to);
				var css = opts.direction=='horizontal'?{left:-1*to*(opts.width+opts.offset)+'px'}:{top:-1*to*(opts.height+opts.offset)+'px'}
				$this.closest('.carousel_inner').animate(css,opts.speed,opts.easing,$this.trigger('carousel.move',to));
			}
		}
	
	});
}

})(jQuery);