$(function() {
	
	// Default position	 
	var $pos = 1; 
	
	// Number of containers
	var $max = $("#stage > div").length;

	// Set width of Scroller
	$("#stage").width($max * 850); 
	
	// Hide the left control link
	$('.left').hide(); 
	
	// Direct Link
	$('ul.members').each(function() {
		var $list = $(this);
		$('a', $list).click(function() {														
			
			var $position = $(this).attr("rel")-1;
			var $target = $position * -850;
		
			$("#stage").animate({"left": $target}, 0, function() {
																													
				var $xCord = $("#stage").position();
				var $pos = (($xCord.left) / -850)+1;
			
				if ($pos<2) { // Check position to show/hide first left control link
					$('.right').show();
					$('.left').fadeOut(100);
				} else if ($pos==$max) { // Check position to show/hide last control link
					$('.left').show();
					$('.right').fadeOut(100);
				} else {
					if ($pos>1) $('.left').show();
					if ($pos<$max)	$('.right').show();
				}	
			});
			
			return false;
		});
	});
	
	// Left Control Link
	$('a.left').click(function() {
		$("#stage").animate({"left": "+=850px"}, 300, function() {
			var $xCord = $("#stage").position();
			var $pos = (($xCord.left) / -850)+1;
			if ($pos<2) { // Check position to show/hide first left control link
				$('.left').fadeOut(100);
			} else {
				$('.left').show();
				if ($pos<$max)	$('.right').show();
			}
		});
	});

	// Right Control Link
	$('a.right').click(function() {
		$("#stage").animate({"left": "-=850px"}, 300, function() {
			var $xCord = $("#stage").position();
			var $pos = (($xCord.left) / -850)+1;
			if ($pos==$max) { // Check position to show/hide last control link
				$('.right').fadeOut(100);
			} else {
				if ($pos>1) $('.left').show();
				$('.right').show();
			}
		});
	});
});
