(function ($)
{
	$.fn.simpleGallery = function (options)
	{		
		var defaults = {
			width 			: "100%",
			height			: "100%",
			thumbWidth		: "70px",
			thumbHeight		: "50px",
			thumbMargin		: "10px",
			delay			: 2000
		};
		
		var options = $.extend(defaults, options);
		var $this = $(this);
		var slideshowInterval = resetTimer(options.delay, $this);
		// Sort out CSS of list and also add in new divs
		$this.css({
			padding		: 0, 
			margin		: 0,
			listStyle	: "none"
		}).wrap("<div class=\"galleryWrap\" />");
		
		$(".galleryWrap").css({
			position	: "relative",
			width		: options.width,
			height		: options.height
		});
		
		var thumbsDiv = "<ul class=\"galleryThumbs\" /><div class=\"galleryThumbsBar\" />";
		$(thumbsDiv).insertBefore(".galleryWrap ul");
		
		$(".galleryThumbsBar").css({
			 position			: "absolute",
			 bottom				: 0, 
			 left				: 0, 
			 opacity			: 0.5, 
			 height				: parseFloat(options.thumbHeight, 10) + 2 + parseFloat(options.thumbMargin, 10) * 2,
			 width				: "100%",
			 backgroundColor	: "#000000",
			 zIndex				: 2
		});
		
		// Create thumbs
		$($this).find("li").clone().appendTo(".galleryThumbs");
		$(".galleryThumbs li").each(function(index){
			$(this).children("img").css({
				width 			: options.thumbWidth,
				height 			: options.thumbHeight
			});
			$(this).css({
				float			: "left",
				marginRight		: options.thumbMargin,
				listStyle		: "none",
				cursor			: "pointer",
				borderTop		: "1px solid #FFFFFF",
				borderRight		: "1px solid #FFFFFF",
				borderBottom	: "1px solid #FFFFFF",
				borderLeft		: "1px solid #FFFFFF",
				width 			: options.thumbWidth,
				height 			: options.thumbHeight
			});
			$(this).click(function() {
				clearInterval(slideshowInterval);
				slideshowInterval = resetTimer(options.delay, $this);
				swapImages($this, index);
			});
		});
		$(".galleryThumbs li:last-child").css({
			marginRight		: 0
		});
		var thumbsWidth = ((parseFloat(options.thumbWidth) + parseFloat(options.thumbMargin) + 2) * $(".galleryThumbs li").length - parseFloat(options.thumbMargin));
		$(".galleryThumbs").css({
			 position			: "absolute",
			 bottom				: options.thumbMargin,
			 left				: (($(".galleryWrap").innerWidth() - thumbsWidth) * 0.5) + "px",
			 zIndex				: 3,
			 margin				: 0,
			 padding			: 0
		});
		
		// Initially position the list items
		$this.find("li").each(function(index){
			var displayMode = index == 0 ? "block" : "none";
			$(this).css({
				position 	: "absolute", 
				top			: 0,
				left		: 0,
				display		: displayMode,
				padding		: 0, 
				margin		: 0,
				listStyle	: "none"
			});
			index == 0 ? $(this).addClass("selected") : "";
		});
		
		return this;
	}
	
	function advanceSlideshow(images)
	{
		var li = $(images).find("li.selected");
		var index = images.find("li").index(li);
		if (index == images.find("li").length - 1)
		{
			index = 0;
		}
		else
		{
			index ++;
		}
		swapImages(images, index);
	}
	
	function resetTimer(delay, images)
	{
		var interval = window.setInterval(function(){
			advanceSlideshow(images);
		}, delay);
		
		return interval;
	}
	
	function swapImages(images, index)
	{
		if (!$(images).find("li:eq(" + index + ")").hasClass("selected"))
		{
			$(images).find("li.selected").removeClass("selected").fadeOut("slow");
			$(images).find("li:eq(" + index + ")").fadeIn("slow").addClass("selected");
		}
	}
	
	function debug(obj)
	{
		try
		{
			console.log(obj);
		}
		catch (e) {}
	}
	
}) (jQuery);
