//*ELEGANT JQUERY SLIDER + PRELOADER
//*AUTHOR: CALVIN FROEDGE
//*URL: WWW.CALVINFROEDGE.COM
//Setup some vars we're going to reuse, so we don't have to change our code in multiple places if we want to update the logic later = )
var hero = $('#hero');
var slideContainer = $('#hero .slides');
var slides = $('#hero .slide');
var images = $('#hero .slide img')
var firstSlide = $('#hero .slide:first-child');
var lastSlide = $('#hero .slide:last-child');
var slideControl = $('#hero .slideControl a');
var intervalDuration = 5000;
var progressIndicatorWidth = 500;
var statusBar = $('#status-bar');

$(document).ready(function(){
	//on page load, start by hiding all containers
	/*slideContainer.hide().css({'position' : 'absolute', 'margin-top' : '-1000px'});*/
	slides.hide();
	/*slideControl.hide();
	
	//Set up the preloader
	var preloadLength = images.length;
	var fullyLoaded = 1;
	var loadStatus = new Object;
	var loadInstanceValue = 1 / preloadLength;
	loadStatus.current = 0;
	//Insert the status bar
	$('<div id="pre-loader" style="width:'+progressIndicatorWidth+'px;height:5px;margin:0 auto;"><h3 id="status-title" style="margin:360px auto 10px auto;color:#fff;font-family:Gotham, Helvetica;font-weight:normal;font-size:14px;text-align:center;" class="cufontext">Loading Media...</h3><div id="status-container" style="width:'+progressIndicatorWidth+'px;padding:2px;border:1px solid #7B6F6D;background:black;"><div id="status-bar" style="background:#6A9336;height:5px;width:0px;"></div><p style="color:#fff;" id="status-text"></p></div></div>').appendTo(hero);
	//Run the loop for preloading
	$.each(images, function(){
		var src = $(this).attr('src');
  		$.ajax({
  			url: src,
  			context: document.body,
  			success: function(){
  				loadStatus['current'] = loadStatus['current'] + loadInstanceValue;
    			statusUpdate(loadStatus['current']);
  			}
		});	
	});
	*/
	//if the sliders are hovered over
	slideContainer.hover(
		function(){
			stopjQueryInterval();
		},
		//and start again if hovered off
		function(){
			jQueryInterval();
		}
	);	
	
	slideControl.click(function(){
		slideController($(this).attr('title'));
	});
	
	firstSlide.addClass('active').show();
	jQueryInterval();
	
});

//function for preloader status update
function statusUpdate(loadProgress)
{
	$('#status-text').html(newWidth);
	var newWidth = loadProgress * progressIndicatorWidth;
	$('#status-bar').animate({
    	width: newWidth+'px'
  	}, 1000, function() {
    	if(newWidth == progressIndicatorWidth){
    		finishPreload();
    	}
  	});
}

//the preload is finished!
function finishPreload()
{
	$('#pre-loader').animate({
		opacity: 0
	}, 1000, function(){
	
	});
	$('#pre-loader').remove();
	//show the first slide and set a class of active-slide
	slideContainer.css({'position' : 'absolute', 'margin-top' : '0px'}).fadeIn();
	firstSlide.addClass('active').show();
	slideControl.show();
	
	//set the timeout
	jQueryInterval();	
}

//function for controlling slides with arrows
function slideController(type){
	//stop the timeout
	stopjQueryInterval();
	
	//if next
	if (type == 'next'){ 
		//go to next slide
		nextSlide();
		
		//turn off the timer
		stopjQueryInterval();
	}
	//if prev
	else if (type == 'prev')
	{		
		//go to prev slide
		prevSlide();
		
		//turn off the timer
		stopjQueryInterval();
	}
}

//the function that swaps to the previous slide
function prevSlide(){

	if ( $('#hero .active').is(':first-child'))
	//checks to see if this is the last slide
	{
		$('#hero .active').removeClass('active').hide();
		lastSlide.fadeIn('slow').addClass('active')
	}
	else
	//this is not the last slide
	{
		$('#hero .active').removeClass('active').hide().prev().fadeIn('slow').addClass('active');
	}
	
}

//the function that swaps to next slide
function nextSlide(){

	if ( $('#hero .active').is(':last-child'))
	//checks to see if this is the last slide
	{
		$('#hero .active').removeClass('active').hide();
		firstSlide.fadeIn('slow').addClass('active');
	}
	else
	//this is not the last slide
	{
		$('#hero .active').removeClass('active').hide().next().fadeIn('slow').addClass('active');
	}
}

//the function that sets the timer which activates the nextSlide function
function jQueryInterval(){
	slideTimer = setInterval(nextSlide, intervalDuration);
}

//the timeout stop function
function stopjQueryInterval(){
	clearInterval(slideTimer);
}
