$(document).ready(function() { 
	rotateMovies = function(){	
        var $slider = $('#movieSlider'),
            $items = $slider.find('> div'),
            $single = $items.filter(':first'),			 
            singleWidth = $items.outerWidth(),
			sliderPos = ($slider.position().left - (singleWidth*2));
			
            $slider.css({'width' : ($items.length * singleWidth)});				
			$slider.css({'left' : sliderPos});
						
			var Imgs = $("#movieSlider div").get(),
			thisPos = 0;	
		   	for (i=0;i<=$items.length;i=i+1)
			{
				$(Imgs[i]).css({'left' : thisPos});
				var thisPos = thisPos + singleWidth;	
			}
			//set 5th on as active			
			//$(Imgs[4]).addClass('active');
			
		gotoPage = function(dir){	
			var left = singleWidth * dir //determine distance to move
			$("#movieSlider div").removeClass('active'); //Remove all active class
			//animate
			$('#movieSlider div').filter(':not(:animated)').animate({ 
				left: '+=' + left
				}, 450, function () {		
					//after animation, highlight center movie	
					//var newImgs = $("#movieSlider div").get();
					//$(newImgs[4]).addClass('active');
		  		});					
			//rearrange		
	  		if (dir == 2) {	
				$('#movieSlider div:last-child').clone().addClass('cloned').prependTo($('#movieSlider')).css({'left' : (0+singleWidth)});
               	$('#movieSlider div:last-child').remove();
				$('#movieSlider div:last-child').clone().addClass('cloned').prependTo($('#movieSlider')).css({'left' : 0});
               	$('#movieSlider div:last-child').remove();
             } else if  (dir == -2){
				$('#movieSlider div:first-child').clone().addClass('cloned').appendTo($('#movieSlider')).css({'left' : ($slider.width()-(singleWidth*2))});
				$('#movieSlider div:first-child').remove();		
				$('#movieSlider div:first-child').clone().addClass('cloned').appendTo($('#movieSlider')).css({'left' : ($slider.width()-singleWidth)});
				$('#movieSlider div:first-child').remove();
			} else if (dir == 1) {	
				$('#movieSlider div:last-child').clone().addClass('cloned').prependTo($('#movieSlider')).css({'left' : 0});
               	$('#movieSlider div:last-child').remove();
             } else {
				$('#movieSlider div:first-child').clone().addClass('cloned').appendTo($('#movieSlider')).css({'left' : ($slider.width()-singleWidth)});
				$('#movieSlider div:first-child').remove();
            };
        };		
	};		
	//rotateMovies(); //Run function on launch				
	// forward and back buttons
	$('#movieLeft').click(function () {
		//check if moving
		var imgs = $("#movieSlider div").get();
		var direction = $('#movieLeft').attr("rel");
		if(!$(imgs[3]).is(':animated')){
		gotoPage(direction);                       
		}
	});
	
	$('#movieRight').click(function () {
		//check if moving
		var imgs = $("#movieSlider div").get();
		var direction = $('#movieRight').attr("rel");
		if(!$(imgs[3]).is(':animated')){
		gotoPage(direction);                       
		}
	}); 
});  
