$(document).ready(function(){

	//Variables
	var fcheight = $("#featured-content").height();
	var controlsheight = $("#controls").height();
	var navigationheight = $("#navigation").height();
	
	var delaytime = 5000;

	//Startup stuff
	$(one);

	sliderIntervalID = setInterval(function(){
		next();
	}, delaytime);
	
	//Dynamic CSS
	$("#content").height(fcheight);
	
	//Generic Animations
	$("#featured-content").hover(function(){
		$("#controls").animate({
			top: "0px"
		}, 300 );
		$("#navigation").animate({
			top: "0px"
		}, 300);
		$("#content").animate({
			height: fcheight - navigationheight - 1
		}, 300);
		clearInterval(sliderIntervalID);
	}, function() {
		$("#controls").animate({
			top: -controlsheight - 1
		}, 300 );
		$("#navigation").animate({
			top: navigationheight - 1
		}, 300);
		$("#content").animate({
			height: fcheight
		}, 300);
		if ( $('#play').css("display") == "none" ) {
			sliderIntervalID = setInterval(function(){
				next();
			}, delaytime);
		}
	});
	
	//Links
	$("#next").click(next);
	$("#previous").click(previous);
	
	$("#pause").click(function(){
		clearInterval(sliderIntervalID);
		$(this).fadeOut(150, function(){
			$("#play").fadeIn(150);
		});
	});
	$("#play").click(function(){
		sliderIntervalID = setInterval(function(){
			next();
		}, 6000);
		$(this).fadeOut(150, function(){
			$("#pause").fadeIn(150);
		});
	});

	$("#one-link").click(one);
	$("#two-link").click(two);
	$("#three-link").click(three);
	$("#four-link").click(four);
	
});

//Functions

function one() {
	$("currentli").removeClass("currentli");
	$(".current").find(".caption").animate({bottom: "-80px"}, 500, function(){
		$(this).parent().parent().fadeOut(250, function(){
			$(this).removeClass("current").parent().find("#one").addClass("current").fadeIn(250, function(){
				$(this).find(".caption").delay('100').animate({bottom: "0px"}, 500).parent().parent();
			});
		});
	});
}

function two() {
	$(".current").find(".caption").animate({bottom: "-80px"}, 500, function(){
		$(this).parent().parent().fadeOut(250, function(){
			$(this).removeClass("current").parent().find("#two").addClass("current").fadeIn(250, function(){
				$(this).find(".caption").delay('100').animate({bottom: "0px"}, 500).parent().parent();
			});
		});
	});
}

function three() {
	$(".current").find(".caption").animate({bottom: "-80px"}, 500, function(){
		$(this).parent().parent().fadeOut(250, function(){
			$(this).removeClass("current").parent().find("#three").addClass("current").fadeIn(250, function(){
				$(this).find(".caption").delay('100').animate({bottom: "0px"}, 500).parent().parent();
			});
		});
	});
}

function four() {
	$(".current").find(".caption").animate({bottom: "-80px"}, 500, function(){
		$(this).parent().parent().fadeOut(500, function(){
			$(this).removeClass("current").parent().find("#four").addClass("current").fadeIn(500, function(){
				$(this).find(".caption").delay('100').animate({bottom: "0px"}, 500).parent();
			});
		});
	});
}

function next() {
	
	var currentid = $(".current").attr("id");
	
	if (currentid == "one") {
		$(two);
	} else if (currentid == "two") {
		$(three);
	} else if (currentid == "three") {
		$(four);
	} else {
		$(one);
	}
	
}

function previous() {
	
	var currentid = $(".current").attr("id");
	
	if (currentid == "one") {
		$(four);
	} else if (currentid == "two") {
		$(one);
	} else if (currentid == "three") {
		$(two);
	} else {
		$(three);
	}
	
}
