// Search and replace: promo_box

$(document).ready(function() {
	// update stats total
	$('.promo_box .stats .total').html( $('.promo_box .entry').size() );
	
	// bind nav buttons 
	$('.promo_box .nav .button').click( function() {
		if( $('.promo_box .entry').size() > 1 ) {
			clearInterval(promo_box_interval);
		}
		change_promo_box( $(this).attr('rel') );
	});
	
	// auto toggle
	if( $('.promo_box .entry').size() > 1 ) {
		promo_box_interval = setInterval(function() {
			change_promo_box('next');
		},3500);
	}
	
	// init promo_box
	change_promo_box(1);
	
	// CUSTOM NAV ANIMATION
	if( $('.promo_box .entry').size() > 1 ) {
		// init navigation
		$('.promo_box').hover( function() {
			$(this).find('.next').stop();
			$(this).find('.prev').stop();
			$(this).find('.next').delay(200).animate({ opacity: 0.5, right: -50 }, 150 );
			$(this).find('.prev').delay(200).animate({ opacity: 0.5, left: -50 }, 150 );
		});
		$('.promo_box').mouseleave( function() {
			$(this).find('.next').stop();
			$(this).find('.prev').stop();
			$(this).find('.next').delay(500).animate({ opacity: 0, right: -20 }, 150 );
			$(this).find('.prev').delay(500).animate({ opacity: 0, left: -20 }, 150 );
		});
		
		// buttons
		$('.promo_box .nav .button').show();
		$('.promo_box .nav .button').css({ opacity: 0 });
		
		$('.promo_box .nav .button.next').hover( function() { 
			$(this).stop();
			$(this).animate({ opacity: 1, right: -50 }, 200 );
		});
		$('.promo_box .nav .button.prev').hover( function() { 
			$(this).stop();
			$(this).animate({ opacity: 1, left: -50 }, 200 );
		});
		
		$('.promo_box .nav .button.next').mouseleave( function() { 
			$(this).stop();
			$(this).animate({ opacity: 0.5, right: -50 }, 200 );
		});
		$('.promo_box .nav .button.prev').mouseleave( function() { 
			$(this).stop();
			$(this).animate({ opacity: 0.5, left: -50 }, 200 );
		});
	}
});

function change_promo_box(rel) {
	// get variables
	aCurrent 	= $('.promo_box .stats .current').html()*1;
	aTotal		= $('.promo_box .stats .total').html()*1;
	// decide settings
	if( rel == 'next' ) {
		showId = aCurrent == aTotal ? 1 : aCurrent+1;
	} else if( rel == 'prev' ) {
		showId = aCurrent == 1 ? aTotal : aCurrent-1;
	} else {
		showId = rel;
	}
	// update stats
	$('.promo_box .stats .current').html(showId);
	// update navigation
	$('.promo_box .nav .button').removeClass('current');
	$('.promo_box .nav .button_'+showId).addClass('current');
	// change promo
	$('.promo_box .entry').hide();
	$('.promo_box .stats .current').html(showId);
	$('.promo_box .entry_'+showId).fadeIn('fast');
}

