// JavaScript Document

$(document).ready( initDocument );

function initDocument(){
	var currentQuoteSlide = 0;
	var quoteSlideWidth = 425;
	var quoteslides = $( '.quote' );
	var numberOfQuoteSlides = quoteslides.length;
	
	var autoPlayQuoteTimeID = 0;
	
	// Remove scrollbar in JS
	$('#quoteSlideContainer').css('overflow', 'hidden');
	
	// Wrap all .slides with #slideInner div
	quoteslides.wrapAll('<div id="quoteSlideInner"></div>')
	// Float left to display horizontally, readjust .slides width
	.css({
	  'float' : 'left',
	  'width' : quoteSlideWidth
	} );
	
	// Set #slideInner width equal to total width of all slides
	$('#quoteSlideInner').css('width', quoteSlideWidth * numberOfQuoteSlides);
	
	// Insert controls in the DOM
	$('#quoteslidemiddle').prepend('<span class="quoteControl" id="leftQuoteControl">Clicking moves left</span>');
	$('#quoteslidemiddle').append('<span class="quoteControl" id="rightQuoteControl">Clicking moves right</span>');
	
	// Create event listeners for .controls clicks
	$('.quoteControl').bind('click', handleSlidernav);
	
	// Create event listeners for .quicklink poge nav clicks
	/*$('.ql').bind('click', quickLink);*/
	
	// Hide left arrow control on first load
	//manageQuoteControls(currentQuoteSlide);
	startTimerQuote();
	
	// manageQuoteControls: Hides and Shows controls depending on currentQuoteSlide
	function manageQuoteControls(position){
		// Hide left arrow if position is first slide
		if( position == 0 ){ 
			$('#leftQuoteControl').fadeOut( 200 );
			
		} else{ 
			$('#leftQuoteControl').fadeIn( 200 );
			 
		}
		// Hide right arrow if position is last slide
		if( position == numberOfQuoteSlides - 1 ){ 
			$('#rightQuoteControl').fadeOut( 200 ); 
			
		} else{ 
			$('#rightQuoteControl').fadeIn( 200 );
			 
		}
	}
	
	//method used by nav arrows - left, right
	function handleSlidernav(){
		 // Determine new position
		 if( $(this).attr('id') == 'rightQuoteControl' ) {
			 currentQuoteSlide = ( currentQuoteSlide < (numberOfQuoteSlides - 1) ) ? (currentQuoteSlide + 1) : 0;
			 
		 }else{
			 currentQuoteSlide = ( currentQuoteSlide > 0 ) ? (currentQuoteSlide - 1) : (numberOfQuoteSlides - 1);
			 
		 }
		
		 stopTimerQuote();
		
		 fadeOutQuoteSlide();
		 //moveQuoteSlide();
	  
	}
	
	function quickLink(){
		// convert page # string to Number data type
		var htmlStr = parseInt( $(this).attr('data') );
		currentQuoteSlide = htmlStr - 1;
		
		//moveQuoteSlide();
		//fadeOutQuoteSlide();
		
	}
	
	function moveQuoteSlide(){
		// Hide / show controls
		//manageQuoteControls(currentQuoteSlide);
		
		// Move slideInner using margin-left
		$('#quoteSlideInner').animate( { 'marginLeft' : quoteSlideWidth * (-currentQuoteSlide) } );
	}
	
	function fadeInQuoteSlide(){
		// Hide / show controls
		$('#quoteSlideInner').css( 'marginLeft', (quoteSlideWidth * (-currentQuoteSlide)) );
		$('#quoteSlideInner').fadeIn( 0 );
	}
	
	function fadeOutQuoteSlide(){
		// Hide / show controls
		//manageQuoteControls(currentQuoteSlide);
		
		$('#quoteSlideInner').fadeOut( 0, fadeInQuoteSlide );
	}
	
	function startTimerQuote(){
		autoPlayQuoteTimeID = setInterval( autoPlayQuote, 6000 );
		
	}
	
	function autoPlayQuote(){
		currentQuoteSlide = ( currentQuoteSlide < (numberOfQuoteSlides - 1) ) ? (currentQuoteSlide + 1) : 0;
		
		//if the sposition is being reset, just fade In the first slide instead of quickly jumping back through the sample
		fadeOutQuoteSlide();
	}
	
	function stopTimerQuote(){
		clearInterval( autoPlayQuoteTimeID );

	}

};
