/**
 * This jQuery plugin displays pagination links inside the selected elements.
 *
 * @author Justin Jones (justin(at)jstnjns(dot)com)
 * @version 1
 */
(function($){
	$.fn.simpleslider = function(settings) {
		
		var defaults = {
			width: '',
			height: '',
			auto: false,
			interval: 2000
			},  
			settings = $.extend({}, defaults, settings); 
		
		this.each(function() {

			// ----------------------------------------------------------------|| Setting Vars ||
			$container = $(this);
			// $frame = $container.find('.'+settings.frameClass);
			$frame = $container.children();

			// Find frame width
			if(settings.width !== '') {
				containerWidth = settings.width;
				frameWidthPadding = $frame.outerWidth() - $frame.width();
				frameWidth = settings.width - frameWidthPadding;
			} else {
				containerWidth = $frame.outerWidth();
				frameWidth = $frame.width();
			}
			// Find frame height
			if(settings.height !== '') {
				containerHeight = settings.height;
				frameHeightPadding = $frame.outerHeight() - $frame.height();
				frameHeight = settings.height - frameHeightPadding;
			} else {
				containerHeight = $frame.outerHeight();
				frameHeight = $frame.height();
			}
			// Find frame count
			frameCount = $frame.size();
			
			// Sets frame number to first frame
			frameNumber = 1;
			
			// ----------------------------------------------------------------|| Frame Details ||			

			// Set frames to display correctly
			$frame.css('float', 'left').width(frameWidth);

			// ----------------------------------------------------------------|| Container Details ||			
			// Set structural CSS for container
			$container.css({
						width: containerWidth*frameCount,
						marginLeft: (-1)*(frameNumber - 1)*frameWidth+'px',
						height: containerHeight
						});

			// ----------------------------------------------------------------|| Wrapper Details ||			
			// Wraps entire element
			$container.wrap('<div id="'+$container.attr('id')+'-wrapper" />');
			$wrapper = $('#'+$container.attr('id')+'-wrapper');
			
			// Sets strucural CSS for wrapper
			$wrapper.css({
				width: containerWidth,
				height: containerHeight,
				overflow: 'hidden'
			});
			
				
			// ----------------------------------------------------------------|| Button Details ||	
			// Creates buttons
						$wrapper
				.after('<div id="'+$container.attr('id')+ '-controls"/>');
			
			$controls = $('#'+$container.attr('id')+'-controls');
			$controls
				.append('<table cellpadding="0" cellspacing="0" id="slider_controls_table"><tr><td><input type="button" id="previous_button" class="button" value="" /></td><td id="slider_text">Click on the arrows to scroll through our slideshow. Click on an image to zoom in.</td><td><input type="button" id="next_button" class="button" value="" /></td></tr></table>')

			// Creates buttons functionality
			$('#previous_button').click(function(e){ e.preventDefault(); prevFrame(); stopTimer(); });
			$('#next_button').click(function(e){ e.preventDefault(); nextFrame(); stopTimer(); });
			
			// ----------------------------------------------------------------|| Actions ||			
			// Initializes actions
			checkButtons();
			
			if(settings.auto == true) {
				startTimer(settings.interval);
				checkHover();
			}

			// Moves to frame
			function move(toFrame) {
				if(toFrame !== undefined) {
					$container.animate({
						marginLeft: (-1)*(toFrame - 1)*containerWidth+'px'
					}, 500);
				} else {
					$container.animate({
						marginLeft: (-1)*(frameNumber - 1)*containerWidth+'px'
					}, 500);
				}
			}
			
			// Moves to previous frame
			function prevFrame() {
				frameNumber--;
				if(frameNumber > 0) {
					move();
				} else {
					move(frameCount);
					frameNumber = frameCount;
				}
				checkButtons();
			}
			
			// Moves to next frame
			function nextFrame() {
				frameNumber++;
				if(frameNumber <= frameCount) {
					move();
				} else {
					move(1);
					frameNumber = 1;
				}
				checkButtons();
			}
			
			function checkButtons() {
				// Sets 'previous' button to disabled if on first frame
				if(frameNumber == 1) {
					$('#previous_button').attr('disabled', 'disabled').addClass('disabled');
				} else {
					$('#previous_button').removeAttr('disabled').removeClass('disabled');
				}
					
				// Sets 'next' button to disabled if on last frame
				if(frameNumber == frameCount) {
					$('#next_button').attr('disabled', 'disabled').addClass('disabled');
				} else {
					$('#next_button').removeAttr('disabled').removeClass('disabled');;
				}
			}

			function startTimer(interval) {
				auto = setInterval(nextFrame, interval);
			}
			
			function stopTimer() {
				clearInterval( auto );
			}
			
			function checkHover() {
				$wrapper.hover(function() {
					stopTimer();
				}, function() {
					startTimer(settings.interval);
				});
			}
			
		});
		
		// Allows chainability
		return this;
	}
})(jQuery);