/*!
* jquery.slideShow. jQuery Slideshow plugin
*
* Copyright (c) 2009 Craig Thompson
* http://craigsworks.com
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*/

"use strict"; // Enable ECMAScript "strict" operation for this function. See more: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
/*jslint browser: true, onevar: true, undef: true, nomen: true, bitwise: true, regexp: true, newcap: true, immed: true, strict: true */
/*global window: false, jQuery: false */
(function($) {
	function SlideShow(target) {
		var self = this,

		/* Current image index */
		current = 0,

		/* Sldieshow timer store */
		timer = null,

		/* Determines speed of slideshow in milliseconds */
		speed = 7000,

		/* Determines duration of fade in milliseconds - ALWAYS LESS THAN THE ABOVE!!! */
		duration = 600;

		// Global target and slideshow references
		this.slideshow = $('.relative', target);
		this.nav = $('.nav', target);

		$.extend(self, {
			'init': function() {
				// Setup nav
				self.nav.empty();
				for(i = 0; i < $('.slide', target).length; i++) {
					$('<li />', {
						'class': !i ? 'active' : '',
						'html': i + 1,
						'title': 'Go to slide ' + (i+1),
						'click': function() {
							self.stop();
							self.goto( $(this).index() );
							
							self.slideshow.one('mouseout', function() {
								timer = setTimeout(function() { self.start(); }, 3000);
							})
						}
					})
					.appendTo(this.nav);
				}

				self.start();
			},
			
			'goto': function(index) {
				var slides = $('.slide', target),
					slide = slides.eq(index);

				self.slideshow.stop(0,0).animate({
					'margin-left': -( slide.position().left ),
					'easing': 'swing',
					'duration': duration
				});

				$('li', self.nav).removeClass('active')
					.eq(slide.hasClass('clone') ? index - slides.length - 2 : index)
					.addClass('active');
			},
			
			'next': function() {
				var over = current >= $('.slide', target).length - 1;

				if(over) {
					$('.slide:first', target).clone().addClass('clone')
						.appendTo(self.slideshow);
				}

				self.goto(++current);

				if(over) {
					self.slideshow.queue(function() {
						$('.slide.clone', target).remove();
						self.slideshow.css({ 'margin-left': 0 });
						current = 0;

						$(this).dequeue();
					})
				}
			},
			
			'prev': function() {
				if(current <= 0) {
					current = $('.slide', target).length;
				}

				self.goto(--current);
			},
			
			'start': function(state) {
				if(state !== false) {
					(function interval() {
						if(timer) { self.next(); }
						timer = setTimeout(interval, speed);
					}());
				}
				else {
					clearTimeout(timer);
				}

				// Toggle playing class
				target.toggleClass('playing', state !== false);
			},

			'stop': function(){ self.start(false); }
		});

		self.init();
	}

	$.fn.slideShow = function() {
		return $(this).each(function() {
			var obj = new SlideShow( $(this) );
			$(this).data('slideShow', obj);
		});
	};
}(jQuery));
