(function($){
$.fn.carousel = function(params) {
	var defaults = {
		duration: 500, //transition duration
		delay: 4000, //show time for each element
		element:'a'  //tag element to cycle through
	}
	
	var opts = $.extend(defaults, params);
	
	this.each(function() {
		var $this = $(this);
		var $elem = $this.find(opts.element); //find all elements to fade...
		
		//fix the position...
		$this.css({position:'relative'});
		$elem.css({position:'absolute', top:0, left:0});
		
		if ($elem.length > 1) { //if I have more than an image to show...
			$elem.not(':first').hide();
			
			var $current = $elem.eq(0);
			var timeout = 0;
			loop();
			var $next;	
		}
		
		function next() {
			if ($current.next(opts.element).length) { //if the current element is not the last I get the next element...
				$next = $current.next(opts.element);
			} else { //otherwise I go to the first element...
				$next = $elem.eq(0);
			}
			$current.fadeOut(opts.duration, function() {
				$next.fadeIn(opts.duration);
				$current = $next;
				loop(); //and now we loop...
			});
		}
		
		function loop() {
			if (timeout != 0) { 
				clearTimeout(timeout);
				timeout = setTimeout(next, opts.delay);
			} else { //if it's the first time I loop I shorten the duration (no fade in delay...)
				timeout = setTimeout(next, opts.delay - (opts.duration * 2));
			}		
		}
	});
	
}
})(jQuery)