if(jQuery)(
	
	function(jQuery){
		
		// Create the base object to store common settings
		var base = {};
				
		jQuery.extend(jQuery.fn,{
			
			/**
			* Creates a slideshow
			*/
			slideshow:function(options) {
				
				// Some default options
				defaults = jQuery.extend({
					
					"autoplay" 		: true, 			// Auto start the slideshow
					"controls" 		: true, 			// Show navigation controls
					"direction" 	: "horizontal", 	// Direction of slideshow
					"numeric" 		: false,			// Show numeric navigation controls
					"pause" 		: 5000, 			// Set the pause period for each slide
					"speed" 		: 1000 				// Set the slideshow animation speed
					
				}, options);
				
				base.options = $.extend(defaults, options);
				
				// Set some variables
				base.element = $(this); // Set the base slideshow element for use throughout
				base.numSlides = base.element.find("li").size(); // Get the number of slides
				base.width = base.element.width(); // Get the slideshow width
				base.height = base.element.height(); // Get the slideshow height
				base.marginElement = (base.options.direction == "horizontal" ? "left" : "top"); // Set which margin to manipulate to slide
				base.shiftPx = (base.options.direction == "horizontal" ? base.width : base.height); // Set the initial margin for the next slide
				base.margin = "-="+base.shiftPx+"px"; // Set the initial margin for the next slide
				base.currentSlide = 1; // Set the initial slide counter
				base.animationDirection = "forward";
				
				// Create a slideshow only for more than one item
				if (base.numSlides > 1) {
					
					// Clone the first slide and set dimensions
					base.element.find("li:first").clone(true).appendTo(base.element.find("ul"));
					base.element.find("li").css({ "width": base.width });
					base.element.find("li").css({ "height": base.height });
					base.numSlides += 1;
					
					// Float each slide for horizontal scrolling
					if (base.options.direction == "horizontal") {
						
						base.element.find("ul").css({ "width": (base.numSlides * base.shiftPx)+"px" });
						base.element.find("li").css({ "float": "left" });
						
					}
					
					// Add the control buttons
					if (base.options.controls) {
						
						// Numeric controls
						if (base.options.numeric) {
							
							base.element.append("<ol class=\"pager\"></ol>");
							
							// Add the controls
							for (s = 1; s <= (base.numSlides - 1); s ++ ) base.element.find(".pager").append("<li><a href=\""+s+"\">"+s+"</a></li>");
							
							// Detect navigation selection
							base.element.find(".pager a").click(function(event) {
								
								event.preventDefault();
								
								if ($(this).attr("href") != base.currentSlide) {
									
									// Clear the slide timer and set base settings
									window.clearTimeout(base.slideTimer);
									base.options.autoplay = false;
									
									// Set the slide counter
									base.currentSlide = $(this).attr("href");
									
									// Set the new margin
									base.margin = "-"+((base.currentSlide - 1) * base.shiftPx)+"px";
									
									// Animate
									jQuery(this).animate_slide();
									
								}
								
							});
							
						}
						
						// Prev/next controls
						else {
							
							// Add the controls
							base.element.append("<div class=\"prev prev-"+base.options.direction+"\"><a href=\"#\">Previous</a></div>");
							base.element.append("<div class=\"next next-"+base.options.direction+"\"><a href=\"#\">Next</a></div>");
							
							// Detect navigation selection
							base.element.find(".prev, .next").click(function(event) {
								
								event.preventDefault();
								
								// Make sure an animationis not in progress
								if (!base.animating) {
									
									// Clear the slide timer and set base settings
									window.clearTimeout(base.slideTimer);
									base.options.autoplay = false;
									
									base.animationDirection = $(this).hasClass("next") ? "forward" : "backward";
									
									// Set the starting animation position
									jQuery(this).reset_slide();
									
									// Increment the slide counter
									$(this).hasClass("next") ? base.currentSlide ++ : base.currentSlide --;
																		
									// Set the new margin
									base.margin = $(this).hasClass("next") ? "-="+base.shiftPx+"px" : "+="+base.shiftPx+"px";
									
									// Animate
									jQuery(this).animate_slide();
									
								}
								
							});
							
						}
						
					}
					
					// Animate!
					if (base.options.autoplay) base.slideTimer = window.setTimeout(jQuery(this).animate_slide, base.options.pause);
					
				}
				
			},
			
			
			
			/**
			* Animate the slideshow
			*/
			animate_slide:function() {
				
				// Set the animation flag
				base.animating = true;
				
				// Set the animation margin properties
				base.anim = {};
				base.anim["margin"+base.marginElement.charAt(0).toUpperCase() + base.marginElement.slice(1)] = base.margin;
								
				// animate to the next slide
				base.element.find("ul").animate(base.anim, base.options.speed, false, function() {
					
					// Unset the animation flag
					base.animating = false;
					
					if (base.options.autoplay) {
						
						// Increment for autoplay
						base.currentSlide ++;
						base.animationDirection = "forward";
						
						// Set the starting animation position
						jQuery(this).reset_slide();
						
						// Set the margin and continue the animation
						base.margin = "-="+base.shiftPx+"px";
						base.slideTimer = window.setTimeout(jQuery(this).animate_slide, base.options.pause);
						
					}
					
				});
				
			},
			
			
			
			/**
			* Reset the slide position for continual animation
			*/
			reset_slide:function() {
				
				// Return to the start of the slideshow for forward navigation
				if (base.currentSlide >= base.numSlides && base.animationDirection == "forward") {
					
					if (base.options.direction == "vertical") base.element.find("ul").css({ "margin-top": "0px" });
					else base.element.find("ul").css({ "margin-left": "0px" });
					base.currentSlide = 1;
					
				}
				
				// Go to the end of the slideshow for backward navigation
				else if (base.currentSlide <= 1 && base.animationDirection == "backward") {
					
					margin = "-"+((base.numSlides - 1) * (base.options.direction == "horizontal" ? base.width : base.height))+"px";
					if (base.options.direction == "vertical") base.element.find("ul").css({ "margin-top": margin });
					else base.element.find("ul").css({ "margin-left": margin });
					base.currentSlide = base.numSlides;
					
				}
				
			}
			
		})
		
	}
	
)(jQuery);
