(function($) {
	
	$.fn.slideshow = function(options) {
		
		$this = $(this);
		
		//get the options
		var opts = $.extend({}, $.fn.slideshow.defaults, options);
		
		var opts_hide_title = opts.hideTitle;
		
		if (opts_hide_title) {
			$("#content h2:first").hide();
		}
		
		var thumb = $this.children().find(".slideshow-thumb");
		
		//start
		thumb.click(function() {
			
			var $img = $(this);
			
			$("#content").hide();
			
			var sl_nav = $('<div class="sl-nav"></div>').insertAfter("#nav-wrapper");
			
			$('<span class="sl-back arrow"></span><span class="sl-next arrow"></span><span class="sl-close">Close</span>').appendTo(".sl-nav");
			var sl_close = sl_nav.find(".sl-close");
			var sl_next = sl_nav.find(".sl-next");
			var sl_prev = sl_nav.find(".sl-back");
			
			//append image to body
			var big_img = $img.parent().find(".slideshow-image");
			
			var cloned_img = big_img.clone().insertBefore("#nav-wrapper");
			
			//show
			cloned_img.fadeIn("slow", function() {
				sl_bg = $('<div id="slideshow-bg"></div>').insertBefore("#nav-wrapper");
			});
			
			//next
			sl_next.click(function() {
				cloned_img.remove();

				var next_img = $img.parent().next().find(".slideshow-image");
				
				if (next_img.size() == 0 ) {
					next_img = $this.find(".slideshow-item:first .slideshow-image");
				}
				
				cloned_img = next_img.clone().insertBefore("#nav-wrapper");
				cloned_img.fadeIn("slow");
				$img = next_img;
				
				return $img;
			});
			
			//back
			sl_prev.click(function() {
				cloned_img.remove();

				var prev_img = $img.parent().prev().find(".slideshow-image");
				
				if (prev_img.size() == 0 ) {
					prev_img = $this.find(".slideshow-item:last .slideshow-image");	
				}
				
				cloned_img = prev_img.clone().insertBefore("#nav-wrapper");
				cloned_img.fadeIn("slow");
				$img = prev_img;
				
				return $img;
			});
			
			//close
			sl_close.click(function() {
				cloned_img.fadeOut("slow", function() {
					sl_nav.remove();
					sl_bg.remove();
						
					$("#content").show();
				});
			});
		});
		
		var desc_wrapper_width = function(el) {
			
			//content width
			var win_width = $(window).width();
			var con_width = win_width - 335;
			el.css("width", con_width+"px");
			
			$(window).resize(function() {
				var win_width = $(window).width();
				var con_width = win_width - 335;
				el.css("width", con_width+"px");
			});
		}
	};
	
	$.fn.slideshow.defaults = {
			hideTitle: false
	};
}) (jQuery);
