(function($) {
	$.fn.esc_stdslideshow = function(o) {
		return this.each(function() {
			new $esc_stdslideshow(this, o);
		});
	};

	// Default configuration properties.
	var defaults = {
		speed : 1000,
		autoScrollSpeed : 6000,
		autoScroll : false
	};

	/**
	 * The esc_stdslideshow object.
	 * 
	 * @constructor
	 * @name $.esc_stdslideshow
	 * @param Object
	 *            e The element to create the slideshow for.
	 * @param Hash
	 *            o A set of key/value pairs to set as configuration properties.
	 * @cat Plugins/esc_stdslideshow
	 */
	$.esc_stdslideshow = function(e, o) {
		this.options = $.extend({}, defaults, o || {});

		this.container = $(e);
		this.radios = this.container.find('.radiobtns a');
		this.scrollitems = this.container.find('.items-holder li');
		this.running = false;
		this.autoscrolltimer = true;

		var self = this;

		self.setup();
	};

	// Create shortcut for internal use
	var $esc_stdslideshow = $.esc_stdslideshow;

	$esc_stdslideshow.fn = $esc_stdslideshow.prototype = {
		esc_stdslideshow : '0.1'
	};

	$esc_stdslideshow.fn.extend = $esc_stdslideshow.extend = $.extend;

	$esc_stdslideshow.fn.extend({

		/**
		 * Sets up the slideshow.
		 * 
		 * @name setup
		 * @type undefined
		 * @cat Plugins/esc_stdslideshow
		 */
		setup : function() {
			this.options.speed = this.container.width() * 1.2;
			this.setup_radios();
			if (this.options.autoScroll) {
				this.setup_autoscroll();
			}
		},

		/**
		 * Sets up the slideshow radio buttons.
		 * 
		 * @name setup_radios
		 * @type undefined
		 * @cat Plugins/esc_stdslideshow
		 */
		setup_radios : function() {
			var self = this;
			this.radios.click(function() {
				self.slide_to($(this).parent().parent().children('li').index(
						$(this).parent()));

				// Reset autoscroll to avoid immediate follow on scroll
				if (self.options.autoScroll) {
					self.setup_autoscroll();
				}

				return false;
			});
		},

		/**
		 * Sets up the slideshow to autoscroll
		 * 
		 * @name setup_autoscroll
		 * @type undefined
		 * @cat Plugins/esc_stdslideshow
		 */
		setup_autoscroll : function() {
			var self = this;

			// Setup interval
			self.resume_autoscroll();

			// Setup pause on hover
			self.scrollitems.unbind("hover");
			self.scrollitems.hover(function() {
				self.pause_autoscroll();
			}, function() {
				self.resume_autoscroll();
			});

			// Disable scrolling if a video gains focus
			self.scrollitems.find('.videoplayer').parent().focusin(function() {
				clearInterval(self.autoscrolltimer);
				self.options.autoScroll = false;
				self.scrollitems.unbind("hover");
			});
		},

		resume_autoscroll : function() {
			var self = this;
			if (self.options.autoScroll == true) {
				clearInterval(self.autoscrolltimer);
				self.autoscrolltimer = setInterval(function() {
					self.next()
				}, self.options.autoScrollSpeed);
			}
		},

		pause_autoscroll : function() {
			var self = this;
			clearInterval(self.autoscrolltimer);
		},

		/**
		 * Sets up the slideshow slide to the next item
		 * 
		 * @name setup_next
		 * @type undefined
		 * @cat Plugins/esc_stdslideshow
		 */
		next : function() {
			var self = this;
			var next_index = this.radios.parent().index(
					this.radios.parent().filter(".on"));
			next_index++;
			if (next_index == this.radios.length)
				next_index = 0;
			this.slide_to(next_index);
		},

		/**
		 * Slide to a slideshow
		 * 
		 * @name slide_to
		 * @type undefined
		 * @param Int
		 *            index the index of the slideshow item to slide to.
		 * @cat Plugins/esc_stdslideshow
		 */
		slide_to : function(index) {
			var self = this;
			self.radios.parent().removeClass('on').filter(':eq(' + index + ')')
					.addClass('on');
			self.scrollitems.parent().animate({
				'marginLeft' : 0 - (self.container.width() * index)
			}, self.options.speed);
		}
	});

	$esc_stdslideshow.extend({

		/**
		 * Gets/Sets the global default configuration properties.
		 * 
		 * @name defaults
		 * @descr Gets/Sets the global default configuration properties.
		 * @type Hash
		 * @param Hash
		 *            d A set of key/value pairs to set as configuration
		 *            properties.
		 * @cat Plugins/jCarousel
		 */
		defaults : function(d) {
			return $.extend(defaults, d || {});
		}
	});

})(jQuery);
