$(document).ready(function() {

/* ---- INITIALISE MENU 'LAVA' EFFECT ---- */

	$("#navigation").lavaLamp({
		fx: "swing",
		speed: 300
	});

/* ---- CAROUSEL ---- */

	var inactiveOpacity = 0.35;
	var totalWidth = 0;
	var animating = false;
	var halfwayLine = $(window).width() / 2;
	var $imageList = $("#carousel ul")
	var $currentItem = $("li", $imageList).first();
	var currentItemWidth = $currentItem.outerWidth(true);
	var scrollControls = '<a href="#" id="previous">Previous</a><a href="#" id="next">Next</a>';

	$currentItem.addClass("current");
	$currentItem.prevAll().addClass("previous");
	$currentItem.nextAll().addClass("next");

	if($imageList.find("li").size() > 1) {
		$("#carousel-wrapper").append(scrollControls);
		$("#previous").hide();
	}

/*
	$(window).load(function() {
		$imageList.width(totalWidth);
	});

	$(window).resize(function() {
		halfwayLine = $(window).width() / 2;
	})
*/
	$("#carousel li").not($currentItem).find("img").css({opacity : inactiveOpacity});

	$("#carousel li").each(function() {
		totalWidth = totalWidth + $(this).outerWidth(true);
	});

	$("#previous").click(function() {
		shiftCarousel("previous");
		return false;
	});

	$("#next").click(function() {
		shiftCarousel("next");
		return false;
	});
	
	$(document).keydown(function(e){
		if (e.keyCode == 37) { 
			shiftCarousel("previous");
			return false;
		}
	});
	
	$(document).keydown(function(e){
		if (e.keyCode == 39) { 
			shiftCarousel("next");
			return false;
		}
	});		

	$(".picture-frame").live("click", function() {
		var $parentItem = $(this).parent();

		if($parentItem.hasClass("previous")) {
			shiftCarousel("previous");
		} else if($parentItem.hasClass("next")) {
			shiftCarousel("next");		
		}

	});

	function shiftCarousel(direction) {
		var offset = ($currentItem.outerWidth(true) / 2) + ($currentItem.next().outerWidth(true) / 2);
		var absoluteOffset;
		var $boundaryItem;

		if(direction === "previous") {
			offset = ($currentItem.outerWidth(true) / 2) + ($currentItem.prev().outerWidth(true) / 2);
			absoluteOffset = $imageList.position().left + offset;
			$boundaryItem = $("li", $imageList).first();
		} else if(direction === "next") {
			offset = ($currentItem.outerWidth(true) / 2) + ($currentItem.next().outerWidth(true) / 2);
			absoluteOffset = $imageList.position().left - offset;
			$boundaryItem = $("li", $imageList).last();
		}
/*
		var absoluteOffset = (direction === "next") ? $imageList.position().left - offset : $imageList.position().left + offset;
		var $boundaryItem = (direction === "next") ? $("li", $imageList).last() : $("li", $imageList).first();
*/
		if ($currentItem.get(0) != $boundaryItem.get(0) && animating == false) {
			animating = true;
			$("#previous, #next").fadeIn(350);
			$imageList.animate({left : absoluteOffset}, 400);
			$("img", $currentItem).animate({opacity : inactiveOpacity}, 600, function() { animating = false; });
			$currentItem = (direction === "next") ? $currentItem.next() : $currentItem.prev();
			$("img", $currentItem).animate({opacity : 1}, 600);
			$("li", $imageList).removeClass();
			$currentItem.addClass("current");
			$currentItem.prevAll().addClass("previous");
			$currentItem.nextAll().addClass("next");
			
			if($currentItem.get(0) == $("li", $imageList).last().get(0)) {
				$("#next").delay(200).fadeOut(400);
			}
			
			if($currentItem.get(0) == $("li", $imageList).first().get(0)) {
				$("#previous").delay(200).fadeOut(400);
			}			
		} 
		
		
	}

	$("#carousel li:first img").load(function() {
		currentItemWidth = $currentItem.outerWidth(true);
		$("#carousel").css({"marginLeft" : -currentItemWidth/2}); // Hang on- should this perhaps be the next item's width?	
	});

});
