/**
 * 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: '390',
			height: '210',
			autoScroll: false,
			interval: 10000
			},  
			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;
			
			
			
			$("#right_wrapper #page").text(frameNumber+' av '+frameCount);
			
			// ----------------------------------------------------------------|| 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 functionality
			$("#right_wrapper #prev").click(function(e){ e.preventDefault(); prevFrame(); stopTimer(); });
			$("#right_wrapper #next").click(function(e){ e.preventDefault(); nextFrame(); stopTimer(); });
			
			// ----------------------------------------------------------------|| Actions ||			
			// Initializes actions
			checkButtons();
			
			/*
			if(settings.autoScroll == true) {
				startTimer(settings.interval);
				checkHover();
			}
			*/

			// Moves to frame
			function move(toFrame) {
				
				if(toFrame !== undefined) {
					$container.animate({
						marginLeft: (-1)*(toFrame - 1)*containerWidth+'px'
					}, 500);
					
					nextSlide = $container.attr('id')+'_slide'+toFrame;
				} else {
					$container.animate({
						marginLeft: (-1)*(frameNumber - 1)*containerWidth+'px'
					}, 500);
					
					nextSlide = $container.attr('id')+'_slide'+frameNumber;
				}
				
				$('#'+nextSlide+' a span').each(function() {
					src = $(this).attr('title');
					$(this).after('<img src="'+src+'" alt="" />').remove();
				});
			}
			
			// Moves to previous frame
			function prevFrame() {
				frameNumber--;
				if(frameNumber > 0) {
					move();
					
				} 
				else {
					move(frameCount);
					frameNumber = frameCount;
				}
				checkButtons();
				$("#right_wrapper #page").text(frameNumber+' av '+frameCount);
			}
			
			// Moves to next frame
			function nextFrame() {
				frameNumber++;
				if(frameNumber <= frameCount) {
					move();
				} else {
					move(1);
					frameNumber = 1;
				}
				checkButtons();
				$("#right_wrapper #page").text(frameNumber+' av '+frameCount);
			}
			
			function checkButtons() {
				// Sets 'previous' button to disabled if on first frame
				if(frameNumber == 1) {
					$("#right_wrapper #prev").addClass('inactive');
				} else {
					$("#right_wrapper #prev").removeClass('inactive');
				}
					
				// Sets 'next' button to disabled if on last frame
				if(frameNumber == frameCount) {
					$("#right_wrapper #next").addClass('inactive');
				} else {
					$("#right_wrapper #next").removeClass('inactive');;
				}
			}
			
			function startTimer(interval) {
				autoScroll = setInterval(nextFrame, interval);
			}
			
			function stopTimer() {
				clearInterval( autoScroll );
			}
			
			/*
			function checkHover() {
				$wrapper.hover(function() {
					stopTimer();
				}, function() {
					startTimer(settings.interval);
				});
			}
			*/
			
		});
		
		// Allows chainability
		return this;
	}
})(jQuery);