;(function ($, window, document, undefined) {

	// Create the defaults once
	var pluginName = 'slideshow',
		defaults = {
			transitionSpeed: 200,
			imageWidth: 640
		};

	// The actual plugin constructor
	function Slideshow (element, options) {
		this.element = element;
		this.options = $.extend({}, defaults, options) ;
		this._defaults = defaults;
		this._name = pluginName;
		this.init();
	}

	Slideshow.prototype.init = function () {
		
		var ss = this;
		this.container = $(this.element).find(".slideshow");
		this.thumbs = $(this.element).find(".thumbs > li");
		this.current = 0;

		// initialize event listeners on thumbnails
		if (this.thumbs.length > 1) {

			var count = 0;
			this.thumbs.each(function(){
				// if (count == 0) {
				// 	ss.show($(this));
				// }
				$(this).data("position", count).bind("click", function(){
					ss.show(this);
				});
				count++;
			}).css({
				"cursor": "pointer"
			});
			
			this.container.css({
				"width": this.thumbs.length * this.options.imageWidth
			});

			if (this.container.find("p").length) {
				$(this.element).find("> .slideshow-container").css({
					"height": 404
				});
			}

		// remove thumbnails from page if there's only one image
		} else {
			$(this.element).find(".thumbs").remove();
		}

	};
	
	Slideshow.prototype.show = function (thumb) {
		var position = $(thumb).data("position");
		if (position != this.current) {
			this.current = position;
			$(this.container).stop().animate({
				"left": -(this.options.imageWidth * position)
			}, this.options.transitionSpeed);
		}
		this.thumbs.css({
			"opacity": .4
		});
		$(thumb).animate({
			"opacity": 1
		}, this.options.transitionSpeed);
	}

	$.fn[pluginName] = function (options) {
		return this.each(function () {
			if (!$.data(this, 'plugin_'+pluginName)) {
				$.data(this, 'plugin_'+pluginName, new Slideshow(this, options));
			}
		});
	}

})(jQuery, window);
