
var currentPanel = 1;
var totalPanels = 0;
var autoPlay = true;
var timePassed = 0;
var timeToChange = 15;


function autoAdvance() {
	if( window.timePassed == window.timeToChange ){
		window.timePassed = 0;
		
		if( window.currentPanel == window.totalPanels ) {
			window.currentPanel = 0;
		}
		
		if( autoPlay == true ) {
			$('#rotating-nav a:nth-child('+ (window.currentPanel + 1) +')').trigger('click');
		}
	} else {
		window.timePassed += 1;
	
	}
	// DEBUG $('.autoPlay').html('autoPlay = ' + window.autoPlay);
	// DEBUG $('.timePassed').html('timePassed = ' + window.timePassed	);
}

$(document).ready(function(){
	
	
	// DEBUG $('.autoPlay').html('autoPlay = ' + window.autoPlay);
	// DEBUG $('.timePassed').html('timePassed = ' + window.timePassed);
	// DEBUG $('.timeToChange').html('timeToChange = ' + window.timeToChange);
	// DEBUG $('.currentPanel').html('currentPanel = ' + window.currentPanel);
	
	
	setInterval(autoAdvance, 1000);
	
	$('#box-right').hover(
		function(){
			window.autoPlay = false;
			$(this).attr('title') = 'Navigation Paused';
		},
		function(){
			window.autoPlay = true;
			window.timePassed = 5;
		}
	);
	
	// GENERATE NAVIGATION LINKS
	// - create an a tag for each quote item
	$('.rotating-quotes').each(function(index) {
        $('#rotating-nav').append('<a></a>');
		window.totalPanels = index + 1;
		// DEBUG $('.totalPanels').html('totalPanels = ' + window.totalPanels);
    });
	
	
	// PRELOAD FIRST ITEM
	// - show first quote
	var firstItem = $('.rotating-quotes').get(0);
	$('#rotating-quotes').html( $(firstItem).html() );
	// - set first nav item to class 'on'
	$('#rotating-nav a:first').addClass('on');
	

	
	// SET UP NAVIGATION CLICK
	// - when a navigation item is clicked
	$('#rotating-nav a').click(function(){
		// - remove the class 'on' (current link) from everything
		$('#rotating-nav a').removeClass('on');
		// - add class 'on' to the current link using this
		$(this).addClass('on');
		
		// - get the index of the nav item clicked
		var navClicked =  $(this).index();
		// - get quote index using nav index
		var quoteClicked = $('.rotating-quotes').get(navClicked);
		// - get html from quote index
		var newQuote = $(quoteClicked).html();
		
		window.currentPanel = navClicked + 1;
		// DEBUG $('.currentPanel').html('currentPanel = ' + window.currentPanel);
		
		// fade out old quote and replace with new quote
		$('#rotating-quotes').slideUp(500, function(){
			//if ($.browser.msie){$('#rotating-quotes').style.removeAttribute('filter');}
			$('#rotating-quotes').html(newQuote);
			$('#rotating-quotes').slideDown(500);

			//console.log( newQuote);	
		});
		
	
	});
	

});
