

var thePause = {
	interval: null,
	delay: 12000,
	
	doPause: function(){
		clearInterval(this.interval);
		$('#panels').trigger( 'stop' );	
	},
	
	doStart: function(){
		clearInterval(this.interval);
		this.interval = setInterval( function(){
			$('#panels').trigger( 'start' );	
		}, this.delay);
	}
}

$(document).ready( function(){
	// how many panels and how wide
	var panel_count = $('#panels li').size();
	var panel_width = $('#panels li:first').width();
			
	// size the ul to contain them all
	$('#panels ul').width( panel_count * panel_width );
			
	// var rand_start = Math.floor(Math.random()*(panel_count-1));

	$('#panels').serialScroll({				
		items: 'li',
		axis: 'x',
		easing: 'easeInOutQuart',
		interval: thePause.delay,
		force: true,
		navigation: '.panel-nav a',
		stop: true,
		
		onBefore: function( e, elem, $pane, $items, pos ){
			$('a.current').removeClass('current');
			$('.panel-nav a').blur().slice(pos, pos+1).addClass('current');
			$(this).find('.panel-meta').animate({ bottom:"-6em" }, "fast");

		},
		
		onAfter: function( elem ){
			$(elem).find('.panel-meta').animate( {bottom:0}, 1200, "easeOutQuart" );
			$('.panel-meta').not(elem).css({"bottom":"-6em"});	
		}	
	});
	$('.panel-nav a:first').addClass('current');

	$('#panels li').hoverIntent(
		function(){ thePause.doPause(); },
		function(){ thePause.doStart(); }
	);

	$('.panel-nav a').hoverIntent({
		sensitivity: 3,
		interval: 150,
		over: function(){
			thePause.doPause();
			var thumb = $(this).find('span').get(0);
			$(thumb).queue(function(){
				$(this).animate({ opacity:"show", top:"-64px" }, "normal");
				$(this).dequeue();
			});
			return false;
		},
		out: function(){
			thePause.doStart();
			$(this).find('span').queue(function(){
				$(this).animate({ opacity:"hide", top:"-70px" }, "fast");
				$(this).dequeue();
			});
			return false;
		}
	});
});
