$(document).ready(function() {

	// animated carousel
	
	// basic constants	
	animation_length = 45000;
	container_width = parseInt($('#home-carousel').css('width'));
	num_main_slides = 5;
	num_minor_slides = 3;
	multiple_slide_start = 3;
	
	major_slide_width = container_width / num_main_slides;
	minor_slide_width = major_slide_width / num_minor_slides;
	pixels_per_second = (parseFloat(animation_length) / parseFloat(container_width)).toFixed(2);
	slide_distance_duration = parseInt(container_width / num_main_slides);
	time_per_slide = parseFloat(animation_length / num_main_slides);
	time_per_minor_slide = parseFloat(time_per_slide / num_minor_slides);
	in_minor_slide_loop = false;
	end_of_minor_slides = (multiple_slide_start + num_minor_slides);
	
	$('#carousel li').first().fadeIn('slow').addClass('current');
	
	animate_bar(animation_length, null);	
	loop_slides();
	
	$('#pause, .slide1 a.reserve, .video-slide a').click(function() {
		if ($(this).attr('href') == "#") {
			if ($('#pause span').html() != 'Resume') {
				pause_slides();	
			}
		} else {
			pause_slides();
		}		
	});
	
	// tabbed navigation
	$('#carousel-tabs li').click(function() {
	
		$('#home-video-player-container').hide();
	
		var tab = $(this).attr('rel');
		clearTimeout(timer);
		$('#bar').stop();
		
		// set active slide tab
		if (tab > multiple_slide_start) {
			$('#carousel-tabs ul').css('background-position', '0px ' + (-31 * (parseInt($(this).attr("rel")) - 3)) + 'px');
			var position = (tab - multiple_slide_start) * (container_width / num_main_slides);	
		} else {
			$('#carousel-tabs ul').css('background-position', '0px ' + (-31 * (parseInt($(this).attr("rel")) - 1)) + 'px');
			var position = (tab - 1) * (container_width / num_main_slides);
		}
		
		// set minor loop boolean to false if that's the case
		if (tab != multiple_slide_start) {
			in_minor_slide_loop = false;
		} else {
			in_minor_slide_loop = true;
		}
		
		// set new progress bar position and resume animation
		resume_animation(tab, position, false);
		pause_slides();
		
	});
	
});

function pause_slides() {
	// pause
	if ($('#pause span').html() == 'Pause') {
	
		$('#bar').stop();
		clearTimeout(timer);
		$('#pause span').html('Resume').css('background', 'url("/img/home/carousel/button_play.png")');
		
	// resume
	} else if ($('#pause span').html() == 'Resume') {
		$('#home-video-player-container').hide();
		resume_animation($('#carousel li.current').attr('rel'), null, true);				
	}	
}

// start the initial progress bar animation
function animate_bar(length, position) {

	if (length == undefined || length == null) {
		length = animation_length;
	}
	
	if (position != null) {
		$('#bar').css('width', position);
	}
	
	$('#bar').animate({
		width: container_width
	}, length, 'linear', function() {
		$('#bar').css('width', '0');
		animate_bar(null, null);
	});
	
}

// set up a loop for the main slide content	
function loop_slides(time_left) {

	if (time_left == undefined) {
		time_left = time_per_slide;
	}
	
	timer = setTimeout(function() {
		if (in_minor_slide_loop) {
			$('#carousel li.current').fadeOut('fast').removeClass('current').next().addClass('current').fadeIn('fast');
			if ($('#carousel li.current').attr('rel') == end_of_minor_slides) {
				in_minor_slide_loop = false;
			}
		} else if (parseInt($('#carousel li.current').attr('rel'))+1 == (multiple_slide_start)) {
			// if we're on the slide that contains multiple slides within itself, animate it differently				
			$('#carousel li.current').fadeOut('fast').removeClass('current').next().addClass('current').fadeIn('fast');
			in_minor_slide_loop = true;
		} else {
			if ($('#carousel li.current').is(':last-child')) {
				$('#carousel li.current').fadeOut('fast').removeClass('current');
				$('#carousel li').first().fadeIn('fast').addClass('current');
			} else {
				$('#carousel li.current').fadeOut('fast').removeClass('current').next().addClass('current').fadeIn('fast');
			}
		}
				
		// change active slide tab
		if (!in_minor_slide_loop) {
			if ($('#carousel li.current').attr('rel') > multiple_slide_start) {
				// TODO: have to hard-code some values b/c the images aren't consistent heights
				$('#carousel-tabs ul').css('background-position', '0px ' + (-31 * (parseInt($("#carousel li.current").attr("rel")) - 3)) + 'px');
			} else {
				$('#carousel-tabs ul').css('background-position', '0px ' + (-31 * (parseInt($("#carousel li.current").attr("rel")) - 1)) + 'px');
			}
			time_left = time_per_slide;
		} else {
			if (parseInt($('#carousel li.current').attr('rel')) == (multiple_slide_start)) {
				$('#carousel-tabs ul').css('background-position', '0px ' + (-31 * (parseInt($("#carousel li.current").attr("rel")) - 1)) + 'px');
			}
			// see if we need to change interval for minor slides
			time_left = time_per_minor_slide;
		}
		
		// then continue looping
		loop_slides(time_left);
	}, time_left);
	
}

function resume_animation(tab, position, from_pause) {

	// figure out how many seconds we need to finish the animation,
	// based on our current progress
	if (position != null) {
		var bar_width = position;
	} else {
		var bar_width = $('#bar').width();
	}
		
	if (tab >= multiple_slide_start && tab < end_of_minor_slides) {
		in_minor_slide_loop = true;
	} else {
		in_minor_slide_loop = false;
	}
		
	if (!from_pause) {
		$('#carousel li').each(function() {
			$(this).removeClass('current').fadeOut('fast');
			if ($(this).attr('rel') == tab) {
				$(this).addClass('current').fadeIn('fast');
			}
		});
	}
	
	var pixels_remaining = container_width - bar_width;
	var bar_remaining = pixels_remaining * pixels_per_second;
	
	// get the distance to the next slide, and how long before it needs to fade in
	if (in_minor_slide_loop && !from_pause) {
		var next_distance = tab * major_slide_width;
		var pixels_remaining = (next_distance - bar_width).toFixed(2);
		var percent_slide_remaining = (pixels_remaining / major_slide_width).toFixed(2);
		var slide_time_remaining = time_per_minor_slide * percent_slide_remaining;
	} else if (in_minor_slide_loop && from_pause) {
		var next_distance = ((tab-(multiple_slide_start-1)) * minor_slide_width) + (major_slide_width*(multiple_slide_start-1));
		var pixels_remaining = (next_distance - bar_width).toFixed(2);
		var percent_slide_remaining = (pixels_remaining / (minor_slide_width)).toFixed(2);
		var slide_time_remaining = time_per_minor_slide * percent_slide_remaining;
	} else {
		if ($('#carousel li.current').attr('rel') > multiple_slide_start) {
			var next_distance = ($('#carousel li.current').attr('rel') - (num_minor_slides-1)) * major_slide_width;
		} else {
			var next_distance = $('#carousel li.current').attr('rel') * major_slide_width;
		}
		
		var pixels_remaining = (next_distance - bar_width).toFixed(2);
		var percent_slide_remaining = (pixels_remaining / major_slide_width).toFixed(2);
		var slide_time_remaining = time_per_slide * percent_slide_remaining;
	}
		
	animate_bar(bar_remaining, position);
	loop_slides(slide_time_remaining);
	
	$('#pause span').html('Pause').css('background', 'url("/img/home/carousel/button_pause.png")');

}
