function getRetrospective(index, type) {
	if (!retrospectiveIsTweening()) {
		isTweening = true;
		
		retrospectiveResetMenuBehaviour();

		start(index, type);
	}
}

function start(index, type) {
	var imagePosition = 0;

	switch (type) {
	case 'previous':
		imagePosition = document.documentElement.offsetWidth - 57;

		$("#previous_image").css( {
			display : "block",
			left : -$("#previous_image").width()
		});
		
		$("#previous_image").stop().animate( {
			'left' : 10
		}, 1000, "easeInOutQuart");
		
		$("#next_image").stop().animate( {
			left : document.documentElement.offsetWidth - 37
		});
		break;
	case 'next':
		imagePosition = -$("#retrospectiveImage").width();
		
		$("#next_image").stop().animate( {
			'left' : 10
		}, 1000, "easeInOutQuart");
		break;
	}

	$("#retrospectiveImage").stop().animate( {
		'left' : imagePosition
	}, 1050, "easeInOutQuart", function() {
		startComplete(index, type);
	});
}

function startComplete(index, type) {
	switch (type) {
	case 'previous':
		$.post("ajax/getRetrospectivePrevious.php", {
			index : index
		}, function(data) {
			getRetrospectiveImagesSuccess(data, index, type);
		});
		break;
	case 'next':
		$.post("ajax/getRetrospectiveNext.php", {
			index : index
		}, function(data) {
			getRetrospectiveImagesSuccess(data, index, type);
		});
		break;
	}
}

function getRetrospectiveImagesSuccess(data, index, type) {
	switch (type) {
	case 'previous':
		$("#next_image").attr("id", "");
		$("#retrospectiveImage").attr("id", "next_image");
		$("#previous_image").attr("id", "retrospectiveImage");

		$("#next_image_container").html($("#next_image"));
		$("#current_image_container").html($("#retrospectiveImage"));
		$("#previous_image_container").html(data);
		
		$("#next_image_container img").load(function(){
			isTweening = false;
		});
		break;
	case 'next':
		$("#previous_image").attr("id", "");
		$("#retrospectiveImage").attr("id", "previous_image");
		$("#next_image").attr("id", "retrospectiveImage");

		$("#previous_image_container").html($("#previous_image"));
		$("#current_image_container").html($("#retrospectiveImage"));
		$("#next_image_container").html(data);
		
		$("#next_image_container img").load(function(){
			isTweening = false;
		});
		break;
	}
	
	if( data == "" ) isTweening = false;
	
	loadNav(index);
}

function loadNav(index) {
	$.post("ajax/getRetrospectiveNav.php", {
		index : index
	}, function(data) {
		getRetrospectiveNavSuccess(data, index);
	});

	$.post("ajax/getRetrospectiveNavOverlay.php", {
		index : index
	}, function(data) {
		getRetrospectiveNavOverlaySuccess(data);
	});
	
	//isTweening = false;
}

function getRetrospectiveNavSuccess(data, index) {
	$("#retrospective_nav").html(data);

	retrospectiveNavComplete(index);
}

function getRetrospectiveNavOverlaySuccess(data) {
	$("#nav_overlay").html(data);

	retrospectiveNavOverlayComplete();
}

function retrospectiveNavComplete(index) {
	// ---- PREVIOUS
	// previous button
	if( index > 0 ){
		$("#retrospectivePreviousImage").mouseover(function() {
			overRetrospective("previous");
		});
		$("#retrospectivePreviousImage").mouseout(function() {
			outRetrospective("previous");
		});
	}

	// ---- NEXT
	// next button
	$("#retrospectiveNextImage").mouseover(function() {
		overRetrospective("next");
	});
	$("#retrospectiveNextImage").mouseout(function() {
		outRetrospective("next");
	});
}

function retrospectiveNavOverlayComplete() {
	// ---- PREVIOUS
	// previous block
	$("#retrospectiveImageHoverPreviousContainer").css( {
		display : "block"
	});
	$("#retrospectiveImageHoverPreviousContainer").mouseover(function() {
		overRetrospective("previous");
	});
	$("#retrospectiveImageHoverPreviousContainer").mouseout(function() {
		outRetrospective("previous");
	});

	// ---- NEXT
	// next block
	$("#retrospectiveImageHoverNextContainer").css( {
		display : "block"
	});
	$("#retrospectiveImageHoverNextContainer").mouseover(function() {
		overRetrospective("next");
	});
	$("#retrospectiveImageHoverNextContainer").mouseout(function() {
		outRetrospective("next");
	});
}

function retrospectiveResetMenuBehaviour() {
	// ---- PREVIOUS
	// previous button
	$("#retrospectivePreviousImage").mouseover(null);
	$("#retrospectivePreviousImage").mouseout(null);

	// previous block
	$("#retrospectiveImageHoverPreviousContainer").css( {
		display : "none"
	});
	$("#retrospectiveImageHoverPreviousContainer").mouseover(null);
	$("#retrospectiveImageHoverPreviousContainer").mouseout(null);

	// ---- NEXT
	// next button
	$("#retrospectiveNextImage").mouseover(null);
	$("#retrospectiveNextImage").mouseout(null);

	// next block
	$("#retrospectiveImageHoverNextContainer").css( {
		display : "none"
	});
	$("#retrospectiveImageHoverNextContainer").mouseover(null);
	$("#retrospectiveImageHoverNextContainer").mouseout(null);
}

function overRetrospective(type) {
	if (!retrospectiveIsTweening()) {
		switch (type) {
		case 'previous':
			$("#retrospectiveImage").stop().animate( {
				'left' : 10 + 10
			}, 300);
			break;
		case 'next':
			 $("#next_image").stop().animate( {
				 'left' : document.documentElement.offsetWidth - 77
			 }, 300);
			break;
		}
	}
}

function outRetrospective(type) {
	if (!retrospectiveIsTweening()) {
		$("#retrospectiveImage").stop().animate( {
			'left' : 10
		}, 300);
		
		switch (type) {
		case 'previous':
			
			break;
		case 'next':
			 $("#next_image").stop().animate( {
				 'left' : document.documentElement.offsetWidth - 57
			 }, 300);
			break;
		}
	}
}

var isTweening = false;

function retrospectiveIsTweening() {
	return isTweening;
	//return !($("#retrospectiveImageHoverPreviousContainer").css("display") == "block" || $("#retrospectiveImageHoverNextContainer").css("display") == "block");
}

function positionImage(type){
	switch (type) {
	case 'previous':
		
		break;
	case 'next':
		 $("#next_image").css({
			 display : "block",
			 'left' : document.documentElement.offsetWidth - 37
		 });

		 $("#next_image").stop().animate({
			 'left' : document.documentElement.offsetWidth - 57
		 });
		break;
	}
	
	isTweening = false;
}