/**

	Author: Matt Brewer
	Version 1.0 beta
	Date: June 16, 2010
	
	Slideshow Plugin

*/


(function($) {
	
	$.fn.slideshow = function(settings) {
		
		settings = $.extend({
			autoplay: true,
			autoplayInterval: 5,
			slideshowSpeed: '2000',
			paneClass: "art",
			disabledClass: "disabled",
			containerSelector: "#slider",
			leftButtonSelector: "ul li.previousArrow:first",
			rightButtonSelector: "ul li.previousArrow:last",
			activeButtonClass: "selected",
			controlStripSelector: "div#myController",
			buttonSelector: "ul li:not(.previousArrow)"
		}, settings);
		
		return this.each(function() {
			$.data(this, "slideshow", new Slideshow(this));
		});
								
		function Slideshow(domElement) {
				
			this.setup = function() {
				
				this.dom.css({
					position: 'relative',
					overflow: 'hidden'
				});
				
				this.current = 0;
				this.previous = 0;
				this.length = this.dom.find("div."+settings.paneClass).length;
				this.one_entry_width = this.dom.find("div."+settings.paneClass+':first').outerWidth();
				this.container = this.dom.find(settings.containerSelector);
				this.control_strip = $(settings.controlStripSelector);
				this.left_button = this.control_strip.find(settings.leftButtonSelector);
				this.right_button = this.control_strip.find(settings.rightButtonSelector);
								
				this.per_page = this.control_strip.find(settings.buttonSelector).length;
				this.num_pages = Math.ceil(this.length / this.per_page);
				this.current_page = 0;
				

				// Stick the first element on the very end to show a peak of it when we're on the last one before we wrap back around to the first one anyway
				// var first = this.dom.find("div."+settings.paneClass+':first').clone();
				// first.appendTo(this.container);
				
				// Resize the container to hold all of the elements and make positionable
				this.container.css({
					position: 'absolute',
					width: this.one_entry_width * (this.length+1)	// Added one b/c I've added on a duplicate first item to the end of the list so it partially shows up
				});
				
				// Make the buttons actually do something
				this.setup_buttons();
				
				if ( settings.autoplay ) {
					this.setup_autoplay();
				}
				
			}
			
			this.next = function() {
				if ( ++this.current >= this.length ) this.current = 0;
				this.scroll(this.current);
			}
			
			this.prev = function() {
				if ( --this.current < 0 ) this.current = this.length - 1;
				this.scroll(this.current);
			}
			
			this.next_pane = function() {
				
				this.control_strip.find(settings.buttonSelector).filter("."+settings.activeButtonClass).removeClass(settings.activeButtonClass);
				this.control_strip.find(settings.buttonSelector+":first").addClass(settings.activeButtonClass);
				
				this.scroll(++this.current_page * this.per_page);	// Go to the first one on the next page
				
				if ( (this.current_page+1) >= this.num_pages ) {
					this.right_button.addClass(settings.disabledClass);
				}
				
				if ( this.current_page >= 1 ) {
					this.left_button.removeClass(settings.disabledClass);
				}
				
				this.evaluate_tab_buttons();
				
			}
			
			this.prev_pane = function() {
			
				this.control_strip.find(settings.buttonSelector).filter("."+settings.activeButtonClass).removeClass(settings.activeButtonClass);
				this.control_strip.find(settings.buttonSelector+":first").addClass(settings.activeButtonClass);
			
				this.scroll(--this.current_page * this.per_page);	// Go to the first one on the previous page
				
				if ( this.current_page < 1 ) {
					this.left_button.addClass(settings.disabledClass);
				}
				
				if ( this.current_page < this.num_pages ) {
					this.right_button.removeClass(settings.disabledClass);
				}
				
				this.evaluate_tab_buttons();
			
			}
			
			this.scroll = function(i) {
				
				// Bounds checking
				if ( this.current_page >= this.num_pages || this.current_page < 0 ) {
					// console.info("slideshow.current_page = 0;");
					this.current_page = 0;
					this.scroll(0);
				} 
				
				// Perform the animation
				this.container.animate({
					left: "-"+(i * this.one_entry_width)+"px"
				}, settings.slideshowSpeed, 'swing');
				
				this.select_button(i);
				
			}
			
			this.select_button = function(i) {
								
				// Figure out which of the buttons we're selecting, then un-select the old one and select the new one
				var idx = i % this.per_page;
				this.control_strip.find(settings.buttonSelector).filter("."+settings.activeButtonClass).removeClass(settings.activeButtonClass);
				this.control_strip.find(settings.buttonSelector+":eq("+idx+")").addClass(settings.activeButtonClass);
								
			}
			
			this.setup_buttons = function() {
				
				var slideshow = this;
				
				// Setup the left button
				slideshow.left_button.click(function(e) {
					slideshow.slideshow_enabled = false;
					e.preventDefault();
					if ( !$(this).hasClass(settings.disabledClass) ) {
						slideshow.prev_pane();
					}
					
				});
				
				// Setup the right button
				slideshow.right_button.click(function(e) {
					slideshow.slideshow_enabled = false;
					e.preventDefault();
					if ( !$(this).hasClass(settings.disabledClass) ) {
						slideshow.next_pane();
					}
					
				});
				
				// Disabled the left/right button if needed on launch
				slideshow.left_button.addClass(settings.disabledClass);
				if ( this.num_pages == 1 ) {
					slideshow.right_button.addClass(settings.disabledClass);
				}
				
				// Put hover titles on the arrow buttons
				slideshow.left_button.attr("title", "Previous Page");
				slideshow.right_button.attr("title", "Next Page");
				
				// Add an "index" attribute to all of the buttons now
				var buttons = this.control_strip.find(settings.buttonSelector);
				var i = 0;
				buttons.each(function() {
					$(this).attr("index", i++);
				});
				
				// Now setup the actual tabbed buttons to do something
				buttons.click(function(e) {
					
					e.preventDefault();
					if ( !$(this).hasClass(settings.disabledClass) ) {
						var real_idx = slideshow.current_page * slideshow.per_page + parseInt($(this).attr("index"));
						slideshow.scroll(real_idx);
					}
					
					slideshow.slideshow_enabled = false;
					
				});
				
				// Now make sure that I haven't enabled too many of the tab buttons
				this.evaluate_tab_buttons();
				
			}
			
			this.evaluate_tab_buttons = function() {
				
				var top = (this.current_page+1) * this.per_page;
				if ( top > this.length ) {
					
					var bottom = this.current_page * this.per_page;
					var effective_top = this.length - 1;
					var range = effective_top - bottom;
					this.control_strip.find(settings.buttonSelector+':gt('+range+')').addClass(settings.disabledClass);
					
				} else {
					this.control_strip.find(settings.buttonSelector).removeClass(settings.disabledClass);
				}
				
			}
			
			this.setup_autoplay = function() {
				
				this.slideshow_enabled = true;
				var slideshow = this;
				
				slideshow.timer = setInterval(function() {
					if ( slideshow.slideshow_enabled ) {
						slideshow.next();
					} else {
						clearInterval(slideshow.timer);
						slideshow.timer = null;
					}
				}, settings.autoplayInterval*1000);
								
			}
			
			this.dom = $(domElement);
			this.setup();				
			
		}	
		
	}
	
})(jQuery);

