/* Author: Randy Hammons - Optiem, LLC*/
function carouselSetup() {
	//Make up for some WP widget shortcomings.
	//Add a class to first item, remove back-up text
	//Seta delay, and fire off slideshow 
	var firstSlide = $('#breakout img:first');
	var imageHeadlines = $('#breakout h2');
	var transitionDelay = 10000;
	imageHeadlines.remove();
	firstSlide.addClass('show');
	
	
	setTimeout(homeSlideShow, transitionDelay);
	
	function homeSlideShow(){
		var current = $('#breakout .show');
		var next = current.next().length ? current.next() : current.parent().children(':first');
		
		current.fadeOut('slow').removeClass('show');
		next.fadeIn('slow').addClass('show');
		
		setTimeout(homeSlideShow, transitionDelay);
	
	}
	
}

function rotatePics(currentSlide) {
	//Make up for some WP widget shortcomings.
	//Remove back-up text
	//Set a delay, and fire off slideshow 
	var imageHeadlines = $('#breakout h2');
	var pause = 'playing';
	
	if(imageHeadlines[0]){
		imageHeadlines.remove();
		
		var breakout = $('#breakout');
		
		$(breakout).append('<button>pause</button>');
		
		var controls = $('#breakout button');
		
		window.log(controls);
		controls.bind('click', function(event){
			if(pause === 'playing'){
				pause = 'paused';
				controls.text('play');
				clearTimeout(imageAnimation);
				
			} else {
				pause = 'playing';
				controls.text('pause');
				rotation(3);
			}
			controls.toggleClass('paused');
			window.log('pause status:', pause);
		});
		
	} 
	if(pause === 'paused'){
		window.log('checking for pause:', pause);
		return false;
	} else {
		window.log('proceeding with animation:', pause);
		rotation(3);
	}	
	
	function rotation(){
		var transitionDelay = 8000;
		
		var slideCount = $('#breakout img').length;
		currentSlide = currentSlide % slideCount;

		$('#breakout img').eq(currentSlide).fadeOut(function(){
			//re-order the z-index
			$('#breakout img').each(function(i){
				$(this).css(
					'zIndex', ((slideCount - i) + currentSlide) % slideCount
				);
			});
			
			$(this).show();
			imageAnimation = setTimeout(function() { rotation(++currentSlide);}, transitionDelay);
			
			window.log(imageAnimation);
		});	
	}
}

function sectionURL(){
	var pageURL = window.location.pathname;
	var pathArray = window.location.pathname.split( '/' );
	var sectionClass = pathArray[1];
	
	$('body').addClass(sectionClass);
	//window.log(pageURL);
	//window.log(pathArray[1]);
	
}



$(document).ready(function(){
	//carouselSetup();
	rotatePics(3);
	sectionURL();
});



