/*
 * Plugin: Jquery Slider
 * created: 6.22.2011	
 * by Jay Pilgreen
 * copyright River City Studio
 */


(function($) {
	// This is a shell for the plugin .. prevents cross contamination of the $ with prototype .. etc
	$.fn.rcsSlider = function(options) {
		
		// Set up the Defaults
		$.fn.rcsSlider.defaults = {
			previousButtonClass: "previous",
			nextButtonClass: "next",
			containerClass: "outer-shell",
			interiorContainerClass: "inner-shell",
			wrap: false,
			animationTime: 1000,
			animationEffect: "swing",
			complete: null // Currently not implemented
		};
		
		// Extend the options with whatever the user may set up
		options = $.extend($.fn.rcsSlider.defaults, options);
		
		return this.each(function() {
			// We return each because we want to roll through all matched elements instead of working on all them at once. This is the default behavior of jQuery and allows chaining.
			
			var master = $(this);
			var container = master.find("." + options.containerClass);
			var innerContainer = $("<div class='" + options.interiorContainerClass + "'></div>");
			var previousButton = master.find("." + options.previousButtonClass);
			var nextButton = master.find("." + options.nextButtonClass);
			var sliderWidth = container.width();
			
			// Create a new container to slide back and forth inside our main container that will usually have a defined width and cannot be made larger
			container.children().wrapAll(innerContainer);
			
			// Re-pull it for new info
			innerContainer = $("." + options.interiorContainerClass);
			
			var totalChildren = innerContainer.children().length;
			var firstChild = innerContainer.children(1);
			var childMarginTotal = parseInt(firstChild.css("marginLeft")) + parseInt(firstChild.css("marginRight"));
			var masterWidth = (firstChild.width() + childMarginTotal) * (totalChildren)
			
			// We need to re-pull the elements to style them with children
			innerContainer.css({
				position: "absolute",
				height: container.height(),
				width: masterWidth,
				left: 0
			});
			
			// Set up the event handlers
			nextButton.click(function(event) {
				event.preventDefault();
				slide("next");
			});
			
			previousButton.click(function(event) {
				event.preventDefault();
				slide("previous");
			});
			
			
			// Helper Functions
		
			function slide(direction) {
				var standardWidth = container.width() - childMarginTotal;
				var innerContainerWidth = innerContainer.width();
				var leftPosition = innerContainer.position().left;
				var moveToPoint = null; // This will be the full string
				var moveDistance = standardWidth; // This will be the numeric value
				
				switch(direction) {
					case "next":
						// This finds the end
						if((standardWidth * 2) + Math.abs(leftPosition) > innerContainerWidth) {
							moveDistance = innerContainerWidth - Math.abs(leftPosition) - standardWidth;
						}
						
						// If we are wrapping and at the end
						if(options.wrap == true) {
							if(standardWidth + Math.abs(leftPosition) >= innerContainerWidth) {
								moveToPoint = 0;
							} else {
								moveToPoint = "-=" + moveDistance;
							}
						} else {
							moveToPoint = "-=" + moveDistance;
						}
						
						break;
						
					case "previous":
						// Finds the beginning
						if(moveDistance > Math.abs(leftPosition)) {
							moveDistance = Math.abs(leftPosition)
							atOneEnd = true
						}
						
						// If we are wrapping and at the beginning
						if(options.wrap == true) {
							if(leftPosition >= 0) {
								moveToPoint = -(innerContainerWidth - standardWidth);
							} else {
								moveToPoint = "+=" + moveDistance;
							}
						} else {
							moveToPoint = "+=" + moveDistance;
						}
						
						break;
				}
				
				console.log("Move To: " + moveToPoint + " | Left Position: " + leftPosition + " | Width: " + innerContainerWidth );
				
				// This will loop all of them at the same time
				innerContainer.animate({
					left: moveToPoint
				}, options.animationTime, options.animationEffect, function() {
					// Did we mash the button 16 times?
					if(innerContainer.position().left > 0) {
						innerContainer.animate({ 
							left: 0 
						});
					}
					
					if(standardWidth + Math.abs(innerContainer.position().left) > innerContainerWidth) {
						innerContainer.animate({
							left: -(innerContainerWidth - standardWidth)
						}).clearQueue();
					}
					
					// Run the complete function
					$.isFunction(options.complete) && options.complete.call(this);
				});
				
			}
		});
	}
})(jQuery);
