
$(document).ready(function() {

	// -----------------------------------------------------------------
	// SITE VARIABLES
	// -----------------------------------------------------------------
	
	carouselDuration 	= 20000;
	testimonialDuration	= 10000;
	slideFadeIn			= 500;


	// -----------------------------------------------------------------
	// GLOBAL FUNCTIONS
	// -----------------------------------------------------------------
	
	$('#customer-login').hover(function() {
		$('#customer-login-dropdown').slideDown(250).fadeIn(250);										   
	}, function() {
		$('#customer-login-dropdown').slideUp(250).fadeOut(250);
	});


	// -----------------------------------------------------------------
	// TESTIMONIALS
	// -----------------------------------------------------------------

	$('#test-prev, #test-next').click(function() {
		testDirection = ($(this).attr("id") == "test-prev") ? -1 : 1;
		currentTest = $('#testimonials .testimonial.on');
		if (testDirection == 1) {
			if (currentTest.next().length) { 
				currentTest.next().fadeIn(500, function() { $(this).addClass("on"); } );
			} else {
				console.log( $('#tests .testimonial:first-child').attr("id") );
				$('#tests .testimonial:first-child').fadeIn(500, function() { $(this).addClass("on"); } );	
			}
		} else {
			if (currentTest.prev().length) { 
				currentTest.prev().fadeIn(500, function() { $(this).addClass("on"); } );
			} else {
				console.log( $('#tests .testimonial:last-child').attr("id") );
				$('#tests .testimonial:last-child').fadeIn(500, function() { $(this).addClass("on"); } );	
			}
		}		
		
		currentTest.fadeOut(500, function() { $(this).removeClass("on"); } );
	});
	
	function testProgBar() {
		$('#test-progress-bar').remove();
		$('#test-controls').append("<div id='test-progress-bar'><div class='progress'></div></div>");
		$('#test-progress-bar .progress').css({width: "1px"}).animate({width: 55}, testimonialDuration,  function() {
			$('#test-next').trigger("click");
			testProgBar();
		});
	}
	
	testProgBar();
	
	$('#testimonials').hover(function() {
		$('#test-progress-bar .progress').stop(); $('#test-progress-bar').remove();
	}, function() {
		testProgBar();
	});



	// -----------------------------------------------------------------
	// CAROUSEL
	// -----------------------------------------------------------------
	
	// Progress bar function
	function progressBarReset() {
		$('#carousel-progress-bar').remove();
		$('#carousel-controls').prepend("<div id='carousel-progress-bar'><div class='progress'></div></div>"); 
		$('#carousel-progress-bar, #marker').css({marginLeft: $('#carousel-benefits li.on').position().left + parseInt(1)});
		$('#carousel-progress-bar .progress').css({width: "1px"}).animate({width: 145}, carouselDuration,  function() {
			if ($('#carousel-progress-bar').length) {
				if($('#carousel-benefits li.on').next().length == 0) {
					$('#carousel-benefits li:first-child').trigger("click");
				} else { 
					$('#carousel-benefits li.on').next().trigger("click");
				}
				progressBarReset();
			}
		});		
	}

	// On page load, the styles of the following carousel elements will be adjusted
	if (!$('#carousel-benefits li.on').length) {
		$('#carousel-benefits li:first-child').addClass("on").animate({opacity: .95}, 250).siblings().addClass("off").children('.bg').animate({opacity: .55}, 250);
		$('#carousel-slides .slide:first-child').fadeIn(250);
		$('#carousel-next-prev .nextprev').css({opacity: .3});
		progressBarReset();
	}
	
	// The NEXT/PREV hover states
	$('#carousel-next-prev .nextprev').hover(function() {
		$(this).css({opacity: .6});												  
	}, function() {
		$(this).css({opacity: .3});	
	});
	
	// The NEXT/PREV click controls
	$('#carousel-next-prev .nextprev').click(function() {
		currSel = $('#carousel-benefits li.on');
		if ($(this).attr("id") == "carousel-next") { 
			newSlide = currSel.next();
			if (newSlide.length == 0) { newSlide = $('#carousel-benefits li:first-child'); }
		} else { 
			newSlide = currSel.prev();
			if (newSlide.length == 0) { newSlide = $('#carousel-benefits li:last-child'); }
		}
		newSlide.trigger("click");									  
	});
	
	// Hovering over the carousel area will stop the progress bar, kill it, and give the carousel container a class of "on"
	// On mouse-out, the "on" class is removed and the progress bar is recreated
	$('#carousel').hover(function() {
		$(this).addClass("on");	$('#carousel-progress-bar .progress').stop(); $('#carousel-progress-bar').remove();							  
	}, function() {
		$(this).removeClass("on");  progressBarReset();
	});
	
	// Clicking a benefit tab
	$('#carousel-benefits li').click(function() {
		$('#marker').css({marginLeft: $(this).position().left + parseInt(1)});
		if ( $(this).hasClass("on") ) { return false; }
		benefit = $(this).attr("id");
		$('#carousel-slides .slide').fadeOut(slideFadeIn);
		$('#slide-' + benefit).fadeIn(slideFadeIn);
		$(this).addClass("on").removeClass("off").children('.bg').animate({opacity: .95}, 250).parents().siblings().addClass("off").removeClass("on").children('.bg').animate({opacity: .55}, 250);
	});
	
});
