(function($){
	$.fn.slideshow = function(){
		return $(this).each(function(){
			if($(this).find("img").length > 1){
				var div = $(this);
				var slides = div.find("img");
				var navHtml = "<div class=\"image-carousel-nav\"><a href=\"\" class=\"prev\">prev</a><a href=\"\" class=\"next\">next</a></div>";
				var cursor = 0;
				
				var init = function(){
					div.css({
						height: "214px",
						marginBottom: "2px",
						width: "325px",
						overflow: "hidden",
						position: "relative"
					}).after(navHtml);
					$("div.image-carousel-nav").css({
						backgroundColor: "white",
						height: "26px",
						left: "298px",
						position: "absolute",
						width: "36px"
					}).addClass("clearfix");
					$("div.image-carousel-nav a").css({
						backgroundRepeat: "no-repeat",
						display: "block",
						float: "left",
						height: "26px",
						textIndent: "-4000px",
						width: "18px"
					});
					
					$("div.image-carousel-nav a.prev").css({
						backgroundImage: "url(/images/laquo-blue-01-big.gif)"
					}).click(function(e){
						e.preventDefault();
						showPrev();
					});
					$("div.image-carousel-nav a.next").css({
						backgroundImage: "url(/images/raquo-blue-01-big-with-line.gif)"
					}).click(function(e){
						e.preventDefault();
						showNext();
					});
					
				};
				
				var showPrev = function(){
					var current = cursor;
					cursor = (cursor == 0) ? cursor = slides.length - 1 : cursor = cursor - 1;
					$(slides[current]).fadeOut(function(){
						$(slides[cursor]).fadeIn();
					});   
				};
				
				var showNext = function(){
					var current = cursor;
					cursor = (cursor == slides.length - 1) ? 0 : cursor + 1;
					$(slides[current]).fadeOut(function(){
						$(slides[cursor]).fadeIn();
					});
					
				};
				
				init();
				
			}
		});
	};
	
	$(document).ready(function(){
		$("div.image-carousel").slideshow();
	});
})(jQuery); 
