$(document).ready(function(){
	$(".nav").css("visibility","hidden");
	//define the index of the li elements
	var first = $("ol>li:eq(0)");
	var second = $("ol>li:eq(1)");
	var length = $("ol>li").length - 1;
	var last = $("ol>li").eq(length);
	var nextToLast = $("ol>li").eq(length - 1);
	
	first.addClass("first");
	second.addClass("second");
	last.addClass("last");
	nextToLast.addClass("nextToLast");
	
	//animate prev and next links
	$(".prev a").each(function(){
		var active = $("li.active");
		
		$(this).hover(function(){
			//$(this).animate({backgroundPosition: "0 0"},100);
			active.animate({marginLeft: "+=20px"},100);
		}, function(){
			//$(this).animate({backgroundPosition: "-20px 0"},100);
			active.animate({marginLeft: "-=20px"},100);
		});
		
		$(".next a").hover(function(){
			//$(this).animate({backgroundPosition: "-100px 0"},100);
			active.animate({marginLeft: "-=20px"},100);
		}, function(){
			//$(this).animate({backgroundPosition: "-80px 0"},100);
			active.animate({marginLeft: "+=20px"},100);
		});
	});
	
	$(".prev a, .next a").click(function(){
		var active = $("li.active");
		var index = $("ol>li").index(active);
		
		if ($(this).parent("div").hasClass("prev")) {
			//second is active
			if (second.hasClass("active")) {
				$(".next a").show();
				$("ol").animate({marginLeft: "-" + ((index - 1)  * 920) + "px"},100);
				active.removeClass("active").prev("li").addClass("active");
				$(".nav").find(".current").removeClass("current").prev("li").addClass("current");
				$(this).hide();
			//first is active
			} else if (first.hasClass("active")) {
				//do nothing
			} else {
				$(".next a").show();
				$("ol").animate({marginLeft: "-" + ((index - 1)  * 920) + "px"},100);
				active.removeClass("active").prev("li").addClass("active");
				$(".nav").find(".current").removeClass("current").prev("li").addClass("current");
			}
		} else {
			//next to last is active
			if (nextToLast.hasClass("active")) {
				$(".prev a").show();
				$("ol").animate({marginLeft: "-" + ((index + 1)  * 920) + "px"},100);
				active.removeClass("active").next("li").addClass("active");
				$(".nav").find(".current").removeClass("current").next("li").addClass("current");
				$(this).hide();
			} 
			//last is active
			else if (last.hasClass("active")) {
				//do nothing
			}
			//some other item besides next to last or last is active
			else {
				$(".prev a").show();
				$("ol").animate({marginLeft: "-" + ((index + 1)  * 920) + "px"},100);
				active.removeClass("active").next("li").addClass("active");
				$(".nav").find(".current").removeClass("current").next("li").addClass("current");
			}
		}
		return false;
	});
	
	//control clicking of NAV items
	
	$(".nav a").click(function(){
		var text = parseFloat($(this).text());
		var navLength = $(".nav a").length;
		var index = text - 1;
		var parent = $(this).parent("li");
		var current = $("li.current");
		var idxCurrent = $(".nav li").index(current) + 1;
		
		if (idxCurrent > text) {
			var speed = (idxCurrent - text) * 200;
		} else {
			var speed = (text - idxCurrent) * 200;
		}
		
		//show prev link
		if (text == 1) {
			$(".prev a").hide();
			$(".next a").show();		
		} else if (text == navLength){
			$(".next a").hide();
			$(".prev a").show();
		} else {
			$(".prev a, .next a").show();
		}
		
		$("ol").animate({marginLeft: "-" + (index * 920) + "px"}, speed);
		$("ol").find(".active").removeClass("active");
		$("ol>li").eq(index).addClass("active");
		current.removeClass("current");
		$(".nav li").eq(index).addClass("current");
		$(".navup li").eq(index).addClass("current");
		return false;
	});
	
	$(".links a").attr("target","_blank");
	
	
});
