jQuery.fn.slidebar = function(n, settings) {
	settings = jQuery.extend({
		autostart: true,
		duration: 6000, <!-- Shouldn't be less than 2500-->
		pause: true,
		containerClass: "slidebar",
		imageClass: "slidebarthumb",
		textClass: "slidebartext"
	}, settings);

	var currentId = 0;
	var nextId = 0;

	function getNumber() {
		if(jQuery("."+settings.textClass).length == jQuery("."+settings.imageClass).length) {
			n = jQuery("."+settings.textClass).length - 1;
			return true;
		}
		else return false;
	}

	function enableHoverPause() {
		jQuery("."+settings.containerClass).hover(
			function() {
				stop();
			},
			function() {
				start();
			}
		);
	}

	function goTo(id) {
		stop();
		jQuery(document).oneTime(1000, function() {
		nextId = id - 1;
			showNext();
		});
	}

	function showNext() {
		nextId++;
		if(nextId > n) {
			nextId = 0;
		}

		jQuery("."+settings.textClass).eq(currentId).fadeOut("fast", function() {
			jQuery("."+settings.imageClass).eq(currentId).fadeOut("slow", function() {
				jQuery("."+settings.imageClass).eq(nextId).fadeIn("slow", function() {
					jQuery("."+settings.textClass).eq(nextId).fadeIn("slow", function() {
						if(nextId == 0) currentId = 0;
						else currentId++;
					});
				});
			});
		});
	}

	function start() {
		jQuery(document).everyTime(settings.duration, function() {
			showNext();
		}, 0);
	}

	function stop() {
		jQuery(document).stopTime();
	}

	if(getNumber() && settings.autostart) {
		start();
	}

	if(settings.pause) {
		enableHoverPause();
	}

	jQuery("#slidebar0").click(function() {
		goTo(0);
	});

	return this;
}

$(document).ready(function() {
	$().slidebar();
});
