
/*** Initialize the variables ( these can be edited as necessary ) ***/
var autoRunSeconds = 5;
var doesAutoRun = true; // set to false to stop the autorun
var easing = 'jswing'; // Check here for your other options -> http://gsgd.co.uk/sandbox/jquery/easing/
var easingDuration = .4; // In seconds
var fadeDuration = .5; // In seconds
var currentFeatured = 1; // If for some reason you want the first featured item to be something other than 1


/*** Finish initializing the variables ( nothing to be edited here ) ***/
var timer;
var autoRunTimer;
var totalHeight = 0;
var clicked = false;
var animating = false;
var totalItems = 0;


/*** Start jQuery ***/
$(document).ready(
	function() {
		
		if (doesAutoRun) autoRunTimer = window.setInterval( function() { featureMove(); }, autoRunSeconds * 1000 );
		
		$("#up").click(
			function() {
				
				featureMove('up');
				clicked = true;
				return false;
				
			}
		)
		
		$("#down").click(
			function() {
				
				featureMove('down');
				clicked = true;
				return false;
				
			}
		)
		
		$("#featured-items-container a").each(
			function() {
				totalItems++;
				totalHeight = totalHeight + 105;
			}
		)
		
		/*** Function to move the featured content up or down ***/
		function featureMove( way ) {
			
			if ( way == undefined ) way = 'down';

			if ( !animating ) {

				clearInterval( autoRunTimer );
				clearInterval( timer );
				
				var prevCurr = currentFeatured;
				if ( way == 'up' && currentFeatured - 1 > 0 ) {
					
					currentFeatured = currentFeatured - 1;
					showNextImage( currentFeatured, prevCurr );
					
				} else if ( way == 'down' && currentFeatured + 1 <= totalItems ) {
					
					currentFeatured = currentFeatured + 1;
					showNextImage( currentFeatured, prevCurr );
					
				} else if ( currentFeatured == totalItems ) {
					
					currentFeatured = 1;
					showNextImage( currentFeatured, totalItems );
					
					$("#featured-items-inner").animate({ 
						top: '0px'
					}, 
					easingDuration * 1000, 
					easing, 
						function() { 
							animating = false; 
						}
					);
					
				}
				
				var container = $("#featured-items-container");
				var items = $("#featured-items-inner");
				var itemsTop = $(items).css('top').replace('px', '');
				var stopHeight = -(totalHeight-211);
				var moveHeight = ( way == 'up' ) ? parseInt(itemsTop) - parseInt(-(105)) : parseInt(itemsTop) - parseInt(105);

				if ( (moveHeight+105) > stopHeight && moveHeight < 105 ) {

					$(items).animate({ 
						top: moveHeight
					}, 
					easingDuration * 1000, 
					easing, 
						function() { 
							animating = false; 
						}
					);

				}

				if ( !clicked ) {
					timer = setInterval( function() { featureMove(); }, (autoRunSeconds * 1000) );
				}

			}

		}
		
		
		/*** Function to animate the featured image ***/
		function showNextImage( curr, lastCurr ) {
			
			if( curr > 1 ) {
				$('.stop').removeClass('stop');
			} else if ( curr == 1 ) {
				$('#up').addClass('stop');
			}
			
			$(".featured-item[rel='" + lastCurr + "']").removeClass('current');
			
			$(".featured-item[rel='" + curr + "']").addClass('current');
			
			$("#featured-" + lastCurr).fadeOut( 
				fadeDuration * 1000, 
				function() {
					$("#featured-" + curr).fadeIn('slow');
				}
			);
			
		}
		
	}
);
