/* 
Author: Chris Schmitz
*/
$(function() {

	// setup testimonial slider
	testimonialsInit();
	
	// testimonail picker functionality
	$('#testimonial-picker li a').click(function() {
		var selected = $(this);
		testimonialTransition(selected);
		return false;
	});
	
	// newsletter signup dropdown form
	$('#newsletter-signup span, #newsletter-signup h2').css('cursor', 'pointer').click(function() {
		var icon = $('#newsletter-signup span');
		icon.addClass('transition').delay(1500).queue(function(next){
		    icon.removeClass('transition');
		    next();
		});
		icon.parent().find('div').delay(600).slideToggle('slow', function() {
			icon.parent().toggleClass('show');
		});
	});
	
});

var testimonialTimeout;

function resetTestimonialTimeout() {
	testimonialTimeout = setTimeout(testimonialTransition, 5000);
}

function testimonialsInit() {
	var i = 1;
	$('#testimonial-picker li').each(function() {
		$(this).find('a').attr('data-position', i);
		i++;
	});
			
	testimonialTransition( $('#testimonial-picker li:first a') );
}

function testimonialTransition( selected ) {
	var active = $('#testimonial-picker li.active').find('a');
		
	if ( typeof(selected) == 'object' ) {
		if ( selected.attr('data-position') === active.attr('data-position') ) {
			var next = null;
		} else {
			var next = selected;
		}
	} else {
		if ( active.parent().is(':last-child') ) {
			var next = $('#testimonial-picker li:first a');
		} else {
			var next = active.parent().next().find('a');
		}
	}
	
	clearTimeout(testimonialTimeout);
	resetTestimonialTimeout();
	
	if ( next != null ) {
		var testimonial = next.attr('data-testimonial');
		var cite 		= next.attr('data-cite');
		var link 		= next.attr('data-link');
		var position	= next.attr('data-position');
	
		if ( $('#testimonial-picker li:visible').attr('data-position') === position ) {
			resetTestimonialTimeout();
		} else {
			$('#testimonial-wrapper').fadeOut(500, function() {
				$('#testimonial').html(testimonial);
				$('#testimonial-cite').html(cite);
				$('#testimonial-link a').html(link).attr('href', link);
				$('#testimonial-wrapper').fadeIn(500);			
			});
		}
		
		$('#testimonial-picker li.active').removeClass('active');
		next.parent().addClass('active');
	}
}
