// JavaScript Document

$(document).ready( initDocument );

function initDocument(){
	var currentFeatureSlide = 0;
	var featureSlideWidth = 950;
	var featureslides = $( '.slide' );
	var numberOfFeatureSlides = featureslides.length;
	
	var autoPlayFeatureTimeID = 0;
	
	// Remove scrollbar in JS
	$('#featureSlideContainer').css('overflow', 'hidden');
	
	// Wrap all .slides with #slideInner div
	featureslides.wrapAll('<div id="featureSlideInner"></div>')
	// Float left to display horizontally, readjust .slides width
	.css({
	  'float' : 'left',
	  'width' : featureSlideWidth
	} );
	
	// Set #slideInner width equal to total width of all slides
	$('#featureSlideInner').css('width', featureSlideWidth * numberOfFeatureSlides);
	
	// Insert controls in the DOM
	$('#featureslidemiddle').prepend('<span class="featureControl" id="leftFeatureControl">Clicking moves left</span>');
	$('#featureslidemiddle').append('<span class="featureControl" id="rightFeatureControl">Clicking moves right</span>');
	
	// Create event listeners for .controls clicks
	$('.featureControl').bind('click', handleSlidernav);
	
	// Create event listeners for .quicklink poge nav clicks
	/*$('.fl').bind('click', quickLink);*/
	
	// Hide left arrow control on first load
	//manageFeatureControls(currentFeatureSlide);
	//check global hasFlash Boolean Value before starting timer
	if( hasFlash ){
		startTimerFeature();
	}
	
	// manageFeatureControls: Hides and Shows controls depending on currentFeatureSlide
	function manageFeatureControls(position){
		// Hide left arrow if position is first slide
		if( position == 0 ){ 
			$('#leftFeatureControl').fadeOut( 200 );
			
		} else{ 
			$('#leftFeatureControl').fadeIn( 200 );
			 
		}
		// Hide right arrow if position is last slide
		if( position == numberOfFeatureSlides - 1 ){ 
			$('#rightFeatureControl').fadeOut( 200 ); 
			
		} else{ 
			$('#rightFeatureControl').fadeIn( 200 );
			 
		}
	}
	
	//method used by nav arrows - left, right
	function handleSlidernav(){
		 // Determine new position
		// Determine new position
		 if( $(this).attr('id') == 'rightFeatureControl' ) {
			 currentFeatureSlide = ( currentFeatureSlide < (numberOfFeatureSlides - 1) ) ? (currentFeatureSlide + 1) : 0;
			 
		 }else{
			 currentFeatureSlide = ( currentFeatureSlide > 0 ) ? (currentFeatureSlide - 1) : (numberOfFeatureSlides - 1);
			 
		 }
		
		fadeOutFeatureSlide();
		//moveFeatureSlide();
	  
	}
	
	function quickLink(){
		// convert page # string to Number data type
		var htmlStr = parseInt( $(this).attr('data') );
		currentFeatureSlide = htmlStr - 1;
		
		//moveFeatureSlide();
		fadeOutFeatureSlide();
		
	}
	
	function moveFeatureSlide(){
		// Hide / show controls
		//manageFeatureControls(currentFeatureSlide);
		
		// Move slideInner using margin-left
		$('#featureSlideInner').animate( { 'marginLeft' : featureSlideWidth * (-currentFeatureSlide) } );
	}
	
	function fadeInFeatureSlide(){
		// Hide / show controls
		$('#featureSlideInner').css( 'marginLeft', (featureSlideWidth * (-currentFeatureSlide)) );
		$('#featureSlideInner').fadeIn( 500 );
	}
	
	function fadeOutFeatureSlide(){
		// Hide / show controls
		//manageFeatureControls(currentFeatureSlide);
		
		$('#featureSlideInner').fadeOut( 250, fadeInFeatureSlide );
	}
	
	function startTimerFeature(){
		autoPlayFeatureTimeID = setInterval( autoPlayFeature, 5000 );
		
	}
	
	function autoPlayFeature(){
		currentFeatureSlide = ( currentFeatureSlide < (numberOfFeatureSlides - 1) ) ? (currentFeatureSlide + 1) : 0;
		
		//if the sposition is being reset, just fade In the first slide instead of quickly jumping back through the sample
		fadeOutFeatureSlide();
	}
	
	function stopTimerFeature(){
		clearInterval( autoPlayFeatureTimeID );

	}

};
