(function($) {
	$.fn.rotate = function(options) {
		var defaults = {
			pauseSeconds: 10,
			length : 30000,
			minTrail : 20,
			moreText : "more",
			lessText : "less"
		};

		// jquery stuff
		var options = $.extend(defaults, options);
		var instance;

		// buttons ul
		var buttons = $('#buttons');

		//
		var container = $(this);
		var divs = container.children("div");

		// loop divs
		divs.each(function() {
			// fill button ul
			buttons.append("<li class='caption' id='caption"
			+ $(this).attr("id")+"'><a rel='"+($(this).attr("id") -1) +"' href='#'>" + $(this).attr("rel")
			+ "</a></li>");

			// hide image
		});

		$('#buttons li:first').addClass('first');
		
		// shortcuts
		var firstItem = $(container).children("div:first");
		var lastItem = $(container).children("div:last");
		var currentItem = firstItem;


		// fade button
		$('#slideshow div p.overlay').fadeTo(0,0.7);
		var makeSlideshow = function() {


		//	 button is clicked
			buttons.children("li").children("a").click(function(){
				if ( ! $(this).hasClass("current"))
				{
					// select the current item after the pagination click
					currentItem = $(container).children("div:nth(" + ($(this).attr('rel')) + ")");
					makeMagic();
					 return false;

				}
			});

			makeMagic();

			if (currentItem.attr("id") == lastItem.attr("id")) {
				currentItem = firstItem;
			} else {
				currentItem = currentItem.next();
			}

		}

		var makeMagic = function(){
			divs.hide();
			currentItem.show();

			buttons.children('li').removeClass("current");
			$('#caption'+currentItem.attr("id") ).addClass("current");
		}

		var startSlideshow = function() {
			clearInterval(instance);
			makeSlideshow();
			instance = setInterval(makeSlideshow, options.pauseSeconds*1000);
		};

		startSlideshow();

	};
})(jQuery);

