
function Carousel(selector, interval, speed, frame_selector, pause_selector, no_autostart) {

	var _this = this;

	/*$(pause_selector || selector).hover(
		function() { _this.pause = true; },
		function() { _this.pause = false; }
	);*/

	this.selector = selector + ' ' + (frame_selector || '.frame');
	this.interval = interval;
	this.speed = speed;
	this.current = $(this.selector + ':last');
	this.pause = false;
		
	if (! no_autostart) this.start();

}

Carousel.prototype.start = function() {
	var _this = this;
	this.current.show();
	window.setTimeout(function() { _this.step() }, this.delay());
}

Carousel.prototype.step = function() {
	var _this = this;
	if (! this.pause) {
		_this.current.fadeOut(this.speed, function() {
			_this.rotate();
		});
		window.setTimeout(function() { _this.step() }, _this.delay());
	}
}

Carousel.prototype.current = function() {
	this.current = $(this.selector + ':last');
}

Carousel.prototype.delay = function(frame) {
	var f = frame || this.current;
	if (! f._delay && f.length > 0) {
		var m = f.get(0).className.match(/delay-([0-9]+)/);
		var delay = (m && m.length > 0) ? parseInt(m[1]) : this.interval;
		f._delay = delay > 0 ? delay : this.interval;
	}
	return f._delay;
}

Carousel.prototype.rotate = function() {
	this.current.insertBefore(this.selector + ':first');
	this.current.show();
	this.current = $(this.selector + ':last');
}

Carousel.prototype.forward = function() {
	this.current = $(this.selector + ':last');
	this.rotate();
}

Carousel.prototype.rewind = function() {
	$(this.selector + ':first').insertAfter(this.selector + ':last');
}
		