/**
 * 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: '',
			timer: 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;
				framePadding = $frame.outerWidth() - $frame.width();
				frameWidth = settings.width - framePadding;
			} else {
				containerWidth = $frame.outerWidth();
				frameWidth = $frame.outerWidth();
			}
			// Find frame height
			frameHeight = $frame.outerHeight();
			// Find frame count
			frameCount = $frame.size();
			
			// Sets frame number to first frame
			frameNumber = 1;
			
			// ----------------------------------------------------------------|| Frame Details ||			

			// Set frames to display correctly
			$frame.css('display', 'inline').width(frameWidth);

			// ----------------------------------------------------------------|| Container Details ||			
			// Set structural CSS for container
			$container.css({
						width: containerWidth*frameCount,
						marginLeft: (-1)*(frameNumber - 1)*frameWidth+'px'
						});

			// ----------------------------------------------------------------|| 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,
				overflow: 'hidden'
			});
			
				
			// ----------------------------------------------------------------|| Button Details ||	
			// Creates buttons
			$wrapper
				.before('<input type="button" id="previous_button" class="button" value="&lt;" />')
				.before('<input type="button" id="next_button" class="button" value="&gt;" />')

			// Creates buttons functionality
			$('#previous_button').click(function(e){ e.preventDefault(); prevFrame(); stopTimer(); });
			$('#next_button').click(function(e){ e.preventDefault(); nextFrame(); stopTimer(); });
			
			checkButtons();
			// ----------------------------------------------------------------|| Header Details ||	
			// Initialize headers
			$header = $wrapper.parent().find('.header');
			$container.find('.frame h4').hide();
			$container.children().find('h4').clone().appendTo('.header');

			updateHeader();
			// ----------------------------------------------------------------|| Actions ||			
			
			if(settings.timer == 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();
				updateHeader();
			}
			
			// Moves to next frame
			function nextFrame() {
				frameNumber++;
				if(frameNumber <= frameCount) {
					move();
				} else {
					move(1);
					frameNumber = 1;
				}
				checkButtons();
				updateHeader();
			}
			
			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 updateHeader() {
				$header.find('h4').hide().eq(frameNumber - 1).css('display', 'inline');
				
				// Adds background image to H4's based on their ID
				/*$header.find('h4').each(function() {
					id = $(this).attr('id');
					src = '/images/hdr-spotlight-'+id+'.gif';
					$(this).css('backgroundImage','url(' + src +')');
				});*/
				
			}
			
			function startTimer(interval) {
				timer = setInterval( nextFrame, interval);
			}
			
			function stopTimer() {
				clearInterval( timer );
			}
			
			function checkHover() {
				$wrapper.hover(function() {
					stopTimer();
				}, function() {
					startTimer(settings.interval);
				});
			}
			
		});
		
		// Allows chainability
		return this;
	}
})(jQuery);