(function($) {
	$.fn.ssSlideshow = function(options) {
//		var options = $.extend({
//		   	optionOne: 'defaultValue',
//			optionTwo: { partonOne: 'defaultValue' }
//		}, options);
		
		return this.each(function() {
			var container = $(this);
			var items = $(this).children("ul").children();
			function buildSlideshow(items, container) {
			/* buildSlideshow() - Create a left/right scrolling slideshow of element items
			//
			// Parameters
			// - items: jQuery collection of items comprising the slideshow
			// - container: DOM node of the slideshow/item container, wrapped in a jQuery object
			*/
				nextSlide = function() {
					var current = $(items).filter(".active").index();
					if (current == ($(items).length - 1)) {
						var target = 0;
					} else {
						var target = current + 1;
					}
					switchItem(items, $(".pager a"), current, target);
				}
	
			// Initial setup
				items.eq(0).addClass("active");							// Set first item's class attribute to "current" so that it is visible
				var autoSlide = window.setInterval("nextSlide()", 7000);
				buildPager(items, container);							// Create pager w/ links to navigate through items
				setClickHandlers(items, $(".pager"), autoSlide);		// Set up click events on pager links
				autoSlide = setCaptionHovers(items, autoSlide);
			}
	
			// Set up variables
				var activeClass = "active";		// Class attribute value assigned to the current, "active" item
				var oldClass = "out";			// Class attribute value assigned to the item being animated "out", or away from
				var newClass = "in";			// Class attribute value assigned to the item being animated "in", or toward
				var prevClass = "right";		// Class attribute value assigned to both old and new items, determining animation direction
				var nextClass = "left";			// Class attribute value assigned to both old and new items, determining animation direction
			
			function animateHigherIndex(current, target) {
			/* animateHigherIndex() - Create left-to-right directional animation for items in a collection
			//
			// Parameters
			// - current: current element being displayed (a.k.a. the item being replaced) - wrapped in a jQuery object
			// - target: the new element to display (wrapped in a jQuery object)
			*/
				current.addClass(oldClass);
				target.addClass(newClass);
				
				$("."+oldClass).animate({
					"left": "-100%"
				},1000);
				
				$("."+newClass).css({"left": "100%"}).animate({
					"left": "0"
				},1000,'',function() {
					$("."+oldClass).removeClass(oldClass);
					$("."+activeClass).removeClass(activeClass);
					$(this).addClass(activeClass);
					$("."+newClass).removeClass(newClass);
				});
			}
	
			function animateLowerIndex(current, target) {
			/* animateLowerIndex() - Create right-to-left directional animation for items in a collection
			//
			// Parameters
			// - current: current element being displayed (a.k.a. the item being replaced) - wrapped in a jQuery object
			// - target: the new element to display (wrapped in a jQuery object)
			*/
				current.addClass(oldClass);
				target.addClass(newClass);
				
				$("."+oldClass).animate({
					"left": "100%"
				},1000);
				
				$("."+newClass).animate({
					"left": "0"
				},1000,'',function() {
					$("."+oldClass).removeClass(oldClass);
					$("."+activeClass).removeClass(activeClass);
					$(this).addClass(activeClass);
					$("."+newClass).removeClass(newClass);
				});
			}
	
			function setCaptionHovers(items, autoSlide) {
				if(autoSlide) {
					var resetSlide = true;
				}
				items.each(function() {
					$(this).hover(
						function() {
							$(this).children(".caption").queue(function() {
								$(this).addClass("active");
								$(this).dequeue();
							}).queue(function() {
								$(this).animate({"opacity": ".78"},250);
								$(this).dequeue();
							});
							if(resetSlide) {
								clearInterval(autoSlide);
							}
						},
						function() {
							$(this).children(".caption").queue(function() {
								$(this).animate({"opacity": "0"},250);
								$(this).dequeue();
							}).queue(function() {
								$(this).removeClass("active");
								$(this).dequeue();
							});
							if(resetSlide) {
	//							autoSlide = window.setInterval("nextSlide()", 2000);
							}
	//						return autoSlide;
						});
	//				return autoSlide
				});
	//			return autoSlide;
			}
	
			function switchItem(items, links, current, target) {
	/*			if((current < target) || (current == (items.length-2))) {
					animateHigherIndex(items.eq(current), items.eq(target));
					links.removeClass("active");								// Remove "active" class from all pager links
					links.eq(target+1).addClass("active");						// Set current link to "active"
				} else {
					animateLowerIndex(items.eq(current), items.eq(target));
					links.removeClass("active");								// Remove "active" class from all pager links
					links.eq(target+1).addClass("active");						// Set current link to "active"
				}
	*/			
				links.removeClass("active");							// Remove "active" class from all pager links
				items.filter(".active").queue(function() {
					$(this).animate({"opacity":0}, 450);
					$(this).dequeue();
				});
				items.filter(".active").queue(function() {
					$(this).removeClass("active");						// Remove the "active" class from the current item
					$(this).dequeue();
				});
				items.eq(target).queue(function() {
					$(this).css("opacity", 0);
					$(this).dequeue();
				});
				items.eq(target).queue(function() {
					$(this).addClass("active");					// Add the "active" class to the item with a corresponding index
					$(this).dequeue();
				});
				items.eq(target).queue(function() {
					$(this).animate({"opacity":1}, 450);
					$(this).dequeue();
				});
				links.eq(target+1).addClass("active");					// Set current link to "active"
			}
	
			function setClickHandlers(items, linkContainer, autoSlide) {
			// Set click handlers on links
				var pagerLinks = $(linkContainer).children("a");					// Collection of all links in the pager
				var pagerLinksCount = pagerLinks.size();					// Count of all links in the pager
	
			// Set first link in pager to "active" status
				pagerLinks.eq(1).addClass("active");
				
			// Set click-handler to change item to a specific index
				pagerLinks.each(function() {
					if($(this).index() == 0) {
					// Set click-handler for previous item
						$(this).click(function() {
							clearInterval(autoSlide);
							var currentIndex = items.filter(".active").index();		// The currently-active item
							if(currentIndex == 0) {
							// When the first item is active, a click on "Previous" should display the last item
								var itemIndex = parseInt(pagerLinksCount - 3);
							} else {
							// Otherwise, display the item with an index one less than the current index
								var itemIndex = parseInt(currentIndex - 1);
							}
							switchItem(items, pagerLinks, currentIndex, itemIndex);
						});
					} else if ($(this).index() == pagerLinksCount-1) {
					// Set click-handler for next item
						$(this).click(function() {
							clearInterval(autoSlide);
							var currentIndex = items.filter(".active").index();		// The currently-active item
							if(currentIndex == pagerLinksCount-3) {
							// When the last item is active, a click on "Next" should display the first item
								var itemIndex = 0;
							} else {
							// Otherwise, display the item with an index one greater than the current index
								var itemIndex = parseInt(currentIndex + 1);
							}
							switchItem(items, pagerLinks, currentIndex, itemIndex);
						});
					} else {
						$(this).click(function() {
							clearInterval(autoSlide);
							var currentIndex = items.filter(".active").index();		// The currently-active item
							var itemIndex = parseInt($(this).text()) - 1;			// Set current link's numeric text as its index (0-based index)
							switchItem(items, pagerLinks, currentIndex, itemIndex);
						});
					}
				});
			}
	
			function buildPager(items, container) {
			/* buildPager() - Create pager functionality for a collection of items
			//
			// Parameters
			// - items: jQuery collection of items to be "paged"
			// - container: DOM node of the item container, wrapped in a jQuery object
			*/
			
			// Set up variables 
				var itemCount = items.size();
				var linksCount = parseInt(itemCount + 2);				// Number of links with "Previous" and "Next" added
				var pagerWidth = parseInt(container.children("ul").width());			// Width of container
				var linkWidth = Math.floor(pagerWidth / linksCount);	// Total width (width + padding) of each pager link
				
			// Create object of link width/padding properties
				var pagerLink = {
					"width": "27px",												// Links maintain a consistent width of 20px
					"margin": "1px " + Math.floor((linkWidth - 27) / 2) + "px"		// Link padding determined by width of container
				};
	
			// Create pager div
				var pagerDiv = $("<div></div>").attr("class", "pager");
				
			// Create links inside pager
				var pagerContents = "<a class=\"prev\" title=\"Previous\">&lt;</a>";
				for(i=1;i<=itemCount;i++) {
					pagerContents += "<a class=\"pager-link\" title=\"" + $(items).eq(i-1).contents(".caption").text() + "\">"+i+"</a>\n";
				}
				pagerContents += "<a class=\"next\" title=\"Next\">&gt;</a>";
	
			// Add pager links to pager div
				$(pagerDiv).html(pagerContents);
				
			// Set computed width & padding styles on pager links
				$(pagerDiv).children("a").css({
					"width": pagerLink.width,
					"margin": pagerLink.margin
				});
				
			// Append pager to container
				$(pagerDiv).appendTo(container);
			}

			buildSlideshow(items, container);

		});
	}
})(jQuery);
		

