$(document).ready( function($) {
							
	var regWidth		=	$('#carousel ul li').width();
	var items			=	$('#carousel ul li');
	var center			=	parseInt(items.length / 2);
	
	var viewableWidth	=	$('#carousel').width();	
	var maxVisible		=	parseInt($('#carousel').width()/regWidth);
	
	var curWidth		=	regWidth + (viewableWidth - (maxVisible * regWidth));
	
//	var curImgWidth		=	parseInt(curWidth * .9);
	var curImgWidth		=	307;
	
	var offset			=	(parseInt(items.length - maxVisible) / 2) * regWidth;
	offset				+=	((parseInt(items.length - maxVisible) % 2) == 0)?0:parseInt(regWidth/2);
	
	$('#carousel ul').width((regWidth*(items.length-1))+curWidth).height(274).css({left:'-'+offset+'px', overflow:'visible'});
	$('#carousel  ul li').removeClass('center');
	$('#carousel  ul li:eq('+center+')')
		.addClass('center')
		.width(curWidth)
		.find("img")
		.css({'width' : curImgWidth, 'paddingTop' : '0' });

	$(".scrollbutton, #carousel ul").animate(
		{
			opacity: 1
		},
		750,
		function() {
			$(".scrollbutton, #carousel ul").css('visibility', 'visible');
			$("#carousel").data("currentlyMoving", false).css({backgroundPosition:'380px -9999px', visibility:'visible'});
		}
	);

//	.css('visibility','visible');
	
	function rotate(direction) {

        if (($("#carousel").data("currentlyMoving") == false)) {
			
			$("#carousel").data("currentlyMoving", true);			
			switch(direction) {
				case 'right':
					$('#carousel ul li:first-child').animate(
						{
							width: 0
						},
						1000,
						function() {
							$(this).remove().clone().appendTo("#carousel ul").width(regWidth);
							$("#carousel").data("currentlyMoving", false);
						});
						animateItems($('#carousel  ul li.center').next());
				break;
				case 'left':
				default:
					$('#carousel ul li:last-child').width(0).remove().clone().prependTo("#carousel ul").animate({width: regWidth}, 1000, function() { $("#carousel").data("currentlyMoving", false); });
					animateItems($('#carousel  ul li:eq('+center+')'));
				break;
			}	
		
		}
		
	}
	
	function animateItems(curItem) {
		$('#carousel  ul li.center')
			.removeClass('center')
			.animate({'width' : regWidth }, 1000)
			.find("img")
			.animate({ 'width' : regWidth, 'paddingTop' : '15px' }, 1000);
		curItem		
			.addClass('center')
			.animate({'width' : curWidth }, 1000)
			.find("img")
			.animate({'width' : curImgWidth, 'paddingTop' : '0' }, 1000);
	}

	$(window).keydown(function(event){
		switch (event.keyCode) {
//			case 13: //enter
//			case 32: //space		
			case 39: //right arrow			
				$(".scroll-right img").click();
			break;
			case 37: //left arrow
				$(".scroll-left img").click();
			break;
		}
	});

	$(".scroll-right img").click(function() { rotate('right') });
	$(".scroll-left img").click(function() { rotate('left') });

});