(function($) {
	var currentIndex = 0;
	var settings = {};
	var container;
	var count;
	var loader;
	var img;
	$.fn.slideshow = function(s) {
		settings = $.extend({ }, s);
		return this.each(function() {
			container = $(this);
			count = container.find("li").size();
			$prev = $("<span>").addClass("slideshow-prev").attr('href', 'javascript:void(0);');
			$next = $("<span>").addClass("slideshow-next").attr('href', 'javascript:void(0);');
			
			img = $("<img>");
			loader = $("<div>").addClass("slidehow-loader");
			var viewport = $("<div>").addClass("slideshow-view").append(img).append(loader).append($next).append($prev);
			container.prepend(viewport).find("li").each(function(index, el) {
				$(el).find("a").click(function() {
					$.fn.slideshow.goto(index);
					return false;
				});
			});
			img.load(function() {
				loader.hide();
			});
			
			$next.click(function(arg) {
				$.fn.slideshow.next();
			});
			$prev.click(function(arg) {
				$.fn.slideshow.prev();
			});
			
			if (count > 0) {
				$.fn.slideshow.goto(0);
			}
		});
	};
	$.fn.slideshow.next = function() {
		$.fn.slideshow.goto(currentIndex < count - 1 ? currentIndex + 1 : 0);
	};
	$.fn.slideshow.prev = function() {
		$.fn.slideshow.goto(currentIndex > 0 ? currentIndex - 1 : count - 1);
	}
	$.fn.slideshow.goto = function(index) {
		currentIndex = index;
		loader.show();
		img.hide();
		container.find("li a").removeClass("active");
		var anchor = container.find("li").eq(index).find("a").addClass("active");
		img.attr("src", anchor.attr('href')).attr("alt", anchor.attr('title')).attr("title", anchor.attr('title')).show();
	};
})(jQuery);
