(function($){
	$.fn.simpleslider = function(settings) {
		
		var defaults = {
			auto			: true,
			interval		: 3500,
			speed			: 1000,
			easing			: 'swing',
			direction		: 'vertical',
			continuous		: 'true',
			buttons			: false,
			autobuttonsize	: false,
			previoustext	: 'Previous',
			nexttext		: 'Next'
		};
		
		settings = $.extend({}, defaults, settings); 
		
		$(this).each(function() {
			// ----------------------------------------------------------------|| Setting Vars ||
			var $this, $frame, $wrapper, wrapperHeight, containerHeight, frameHeight, wrapperWidth, containerWidth, frameWidth, frameCount, frameNumber;
			
			$this = $(this);

			// Modifies frames			
			$frame = $this.children().addClass('frame');
			
			// Wraps entire element
			$wrapper = $this.wrap('<div id="'+$this.attr('id')+'-wrapper" />').parent('#'+$this.attr('id')+'-wrapper')

			// Find frame count
			frameCount = $frame.size();

			// Find the greatest height of all frames
			wrapperHeight = 0;
			frameHeight = 0;
			
			$frame.each(function() {
				if($(this).height() > frameHeight) frameHeight = $(this).height();
				if($(this).outerHeight(true) > wrapperHeight) wrapperHeight = $(this).outerHeight(true);
			});
			
			// Find frame width
			wrapperWidth = $this.width();
			frameWidth = $frame.width();
			
			// Set to first frame
			frameNumber = 1;
			
			// ----------------------------------------------------------------|| Wrapper Details ||			
			
			// Sets strucural CSS for wrapper
			$wrapper.css({
				width: wrapperWidth,
				height: wrapperHeight,
				overflow: 'hidden'
			});		
				
			// ----------------------------------------------------------------|| Container Details ||			
			// Set structural CSS for container
			if(settings.direction == 'horizontal') {
				containerWidth = wrapperWidth * frameCount;
				containerHeight = wrapperHeight;
			} else if(settings.direction == 'vertical') {
				containerWidth = wrapperWidth;
				containerHeight = wrapperHeight * frameCount;
			}
			
			$this.css({
				width: containerWidth,
				height: containerHeight
			});

			// ----------------------------------------------------------------|| Frame Details ||			

			// Set frames to display correctly
			$frame.css({
				display: 'block',
				float: 'left',
				height: frameHeight,
				width: frameWidth
			});
			
			// ----------------------------------------------------------------|| Button Details ||	
			// Creates buttons
			if(settings.buttons) {
				$wrapper
				.before('<input type="button" id="'+$this.attr('id')+'-previous_button" class="button" value="'+settings.previoustext+'" />')
				.after('<input type="button" id="'+$this.attr('id')+'-next_button" class="button" value="'+settings.nexttext+'" />');
			}
			
			if(settings.autobuttonsize && settings.direction == 'horizontal') {
				$wrapper.parent().find('.button').css({
					height: containerHeight
				});
			} else if(settings.autobuttonsize && (settings.direction == 'vertical')) {
				$wrapper.parent().find('.button').css({
					width: containerWidth
				});
			}

			// Creates buttons functionality
			$('#'+$this.attr('id')+'-previous_button').click(function(e){ e.preventDefault(); prevFrame(); stopTimer(); });
			$('#'+$this.attr('id')+'-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) {
					if(settings.direction == 'horizontal') {
						$this.animate({
							marginLeft: (-1)*(toFrame - 1)*wrapperWidth+'px'
						}, settings.speed, settings.easing);
					} else if(settings.direction == 'vertical') {
						$this.animate({
							marginTop: (-1)*(toFrame - 1)*wrapperHeight+'px'
						}, settings.speed, settings.easing);
					}
				} else {
					if(settings.direction == 'horizontal') {
						$this.animate({
							marginLeft: (-1)*(frameNumber - 1)*wrapperWidth+'px'
						}, settings.speed, settings.easing);
					} else if(settings.direction == 'vertical') {
						$this.animate({
							marginTop: (-1)*(frameNumber - 1)*wrapperHeight+'px'
						}, settings.speed, settings.easing);						
					}
				}
			}
			
			// 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) {
					$('#'+$this.attr('id')+'-previous_button').attr('disabled', 'disabled').addClass('disabled');
				} else {
					$('#'+$this.attr('id')+'-previous_button').removeAttr('disabled').removeClass('disabled');
				}
					
				// Sets 'next' button to disabled if on last frame
				if(frameNumber == frameCount) {
					$('#'+$this.attr('id')+'-next_button').attr('disabled', 'disabled').addClass('disabled');
				} else {
					$('#'+$this.attr('id')+'-next_button').removeAttr('disabled').removeClass('disabled');;
				}
			}

			function startTimer(interval) {
				auto = setInterval(nextFrame, interval);
			}
			
			function stopTimer() {
				if(settings.auto !== false){
					clearInterval(auto);
				}
			}
			
			function checkHover() {
				$wrapper.hover(function() {
					stopTimer();
				}, function() {
					startTimer(settings.interval);
				});
			}
		});
	}
})(jQuery);