(function($) {
	//
	// Slides for jQuery
	// This plugin is designed to make a slideshow out of a fixed size element's children.
	// Author: Dan P. Smith / additions by Liz Kalter
	//
	$.fn.slides = function(settings) {
		// If the first argument into the plugin is a string, the call should be a method call, so attempt to call a method for each matched element.
		if (typeof arguments[0] == "string") {
			var parameters = $.makeArray(arguments);
			return this.each(function() {
				var methods = $(this).data('slides');
				if (methods && methods[parameters[0]]) {
					methods[parameters[0]].apply(this,parameters.slice(1));
				}
			});
		}
		return this.each(function() {
			var config = $.extend({},$.fn.slides.defaults,settings);
			// Set basic variables
			var slides = $(this).children();
			var slideshow = $(this);
			var currentSlide = 0;
			var interval = null;
			var carousel = null;

			// Publicly accessible methods
			// Called in the following style: $('#slideshow').slides('show',1);
			var methods = {
				show: function(number) {
					number = ((number-1)%slides.length)+1;
					if (currentSlide == number) return;
					
					if (config.filmstrip) { // slide right and left
						//alert('number: ' + number + ' currentSlide: ' + currentSlide);
						if (currentSlide == 0) { // initial load of first slide
							slides.eq(number-1).fadeIn('slow');
						} else {
							if (number == slides.length && currentSlide == 1
									|| (number + 1) == currentSlide) {
								carousel.prev();
							} else if (number == 1 && currentSlide == slides.length
									|| number == (currentSlide + 1)) {
								carousel.next();
							} else {
								carousel.scroll(number, true);
							}
						}
					} else { // fade in and out
						if (currentSlide == 0) { // initial load of first slide
							slides.eq(number-1).show().css('position','static');
						} else if (currentSlide < number) {
							slides.eq(currentSlide-1).fadeOut('slow').css('position','absolute');
							slides.eq(number-1).fadeIn('slow').css('position','static');
						} else {
							slides.eq(currentSlide-1).fadeOut('slow', function() {
								$(this).css('position','absolute');
							});
							slides.eq(number-1).fadeIn('slow', function() {
								$(this).css('position','static');
							});
						}
					}
					
					controlPanel.children().removeClass('selected').eq(number-1).addClass('selected');
					if (config.leftNavContent) {
						$(config.leftNavContainer).find(config.leftNavContent).hide();
						$(config.leftNavContainer).find('.left-nav-content-' + number).show();
					}
					currentSlide = number;
				},
				next: function() {
					methods.show(currentSlide+1);
				},
				prev: function() {
					if (currentSlide == 1) {
						methods.show(slides.length);
					} else {
						methods.show(currentSlide-1);
					}
				},
				play: function() {
					var play = this;
					methods.pause();
					interval = setInterval(function() { return methods.next.call(this); },config.interval);
				},
				pause: function() {
					if (interval) {
						clearInterval(interval);
					}
				},
				setCarousel: function(car) {
					carousel = car;
				}
			};
			$(this).data('slides',methods);
			
			// Create control panel
			if (config.controlContainer) {
				var pageControls = $(config.controlContainer).find('.page-controls-container');
				if (pageControls.length) {
					pageControls.before('<div class="control-panel"></div>');
					var controlPanel = $(config.controlContainer).find('.control-panel');
				} else {
					var controlPanel = $('<div class="control-panel"></div>').appendTo(config.controlContainer);
				}
			} else {
				var controlPanel = $('<div class="control-panel"></div>').css({
					'position':'absolute',
					'z-index':'9999'
				}).prependTo(slideshow);
			}
			slides.addClass('jquery-slide').css('position','absolute').hide().each(function(index) {
				if ($(this).attr('data-href')) {
					// If the div doesn't already have a title, set it to the same thing as the data-href, 
					// so at least the user can have some possible indicator about where clicking the area 
					// is going to send them.
					if (!$(this).attr('title')) {
						$(this).attr('title',$(this).attr('data-href'));
					}
					$(this).hover(function(event) {
						$(this).css('cursor','pointer');
					},
					function() {
						$(this).css('cursor','auto');
					});
					$(this).click(function(event) {
						location.href = $(this).attr('data-href');
					});
				}
				var controlText = $(this).attr('data-control-text') ? $(this).attr('data-control-text') : (index+1);
				
				if (config.controlTooltip) {
					var control = $('<div class="control control'+(index+1)+'"><a href="#">'+controlText+'</a><div class="tooltip">'+controlText+'</div></div>');
				} else {
					var control = $('<div class="control control'+(index+1)+'"><a href="#">'+controlText+'</a></div>');
				}
				if (config.controlOrientation == 'horizontal') {
					control.css('float','left');
				}
				control.appendTo(controlPanel).click(function(event) {
					methods.show(index+1);
					methods.pause();
					event.preventDefault();
				});
				
				if (config.leftNavContent) {
					$(config.leftNavContainer).append($(this).find(config.leftNavContent).addClass('left-nav-content-' + (index+1)));
				}
			});
			// Now that the control panel has all of its elements, we can use its dimensions to calculate exactly where the panel should go.
			controlPanel.css({
				'margin-top':config.controlVAlign == 'bottom' ? slideshow.innerHeight()-controlPanel.outerHeight():'auto',
				'margin-left':config.controlAlign == 'right' ? slideshow.innerWidth()-controlPanel.outerWidth():'auto'
			});
			
			// Create prev/next button
			if (config.prevNextPanel) {
				$(config.prevNextPanel).find('button').click(function() {
					var button = $(this);
					if (button.attr('name') == 'next') {
						methods.pause();
						methods.next();
					} else if (button.attr('name') == 'prev') {
						methods.pause();
						methods.prev();
					}
				});
			}
			
			if (config.filmstrip) {
				$(this).children('div').wrap('<li style="height:' + slideshow.height() + 'px;width:' + slideshow.width() + 'px"></li>');
				$(this).wrapInner('<ul></ul>');
			    $(this).children('ul').jcarousel({
			    	wrap: 'circular',
			    	scroll: 1,
			    	animation: 1000,
			    	buttonNextHTML: null,
			    	buttonPrevHTML: null,
			    	initCallback: methods.setCarousel,
			    	filmstripSlideTooltipFunc: config.filmstripSlideTooltipFunc,
			    	filmstripIconTooltipFunc: config.filmstripIconTooltipFunc
			    });
				slides.show();
			}
			
			methods.show(config.firstSlide);
			methods.play();
		}).addClass('jquery-slides');
	};
	$.fn.slides.defaults = {
		interval: 10000,
		firstSlide: 1,
		controlAlign: 'right',
		controlVAlign: 'bottom',
		controlOrientation: 'horizontal',
		controlContainer: false,
		controlTooltip: false,
		prevNextPanel: false,
		filmstrip: false,
		filmstripSlideTooltipFunc: null,
		filmstripIconTooltipFunc: null,
		leftNavContent: false,
		leftNavContainer: '.left-nav'
	};
})(jQuery);


