// http://css-tricks.com/moving-boxes/

$(function() {
	$('#showcase').append('<ol id="pager" class="hide"><li id="previous"><a href="#" title="previous case study" class="left">previous</a></li><li id="next"><a href="#" title="next case study" class="right">next</a></li></ol>');
	$('.description').hide();
	
	var totalPanels			= $('#scroll-container').children().size();
		
	var regWidth			= $('.article').css('width');
	var regHeight			= $('.article').css('height');
	
	var movingDistance	    = 234;
	
	var highWidth			= 360;
	var highHeight			= 216;

	var $panels				= $('#scroll-container > div');
	var $container			= $('#scroll-container');
	

	$('#showcase').data('currentlyMoving', false);
		
	$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 148 )
		.css('left', '0px');

	var scroll = $('#showcase-inner').css('overflow', 'hidden');

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth, height: regHeight, top: '0px' })
			.find('img')
			.animate({ width: regWidth, height: regHeight })
			.end()
			.find('.description')
			.hide();
	};

	function growBigger(element) {
		$(element)
			.animate({ width: highWidth, height: highHeight, top: '-50px' })
			.find('img')
			.animate({ width: highWidth, height: highHeight }, function(){
				$(element).find('.description').show();													
			});
	}

	//direction true = right, false = left
	function change(direction) {
	   
	    //if not at the first or last panel
		if((direction && !(curPanel < totalPanels)) || (!direction && (curPanel <= 1))) { return false; }	
        
        //if not currently moving
        if (($('#showcase').data('currentlyMoving') == false)) {
            
			$('#showcase').data('currentlyMoving', true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $('#scroll-container').css('left');
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;

			$('#scroll-container')
				.stop()
				.animate({
					'left': movement
				}, function() {
					$('#showcase').data('currentlyMoving', false);
				});
			
			returnToNormal('#panel_'+curPanel);
			growBigger('#panel_'+next);
			
			curPanel = next;
			
			//remove all previous bound functions
			$('#panel_'+(curPanel+1)).unbind();	
			
			//go forward
			$('#panel_'+(curPanel+1)).click(function(){ change(true); });
			
            //remove all previous bound functions															
			$('#panel_'+(curPanel-1)).unbind();
			
			//go back
			$('#panel_'+(curPanel-1)).click(function(){ change(false); }); 
			
			//remove all previous bound functions
			$('#panel_'+curPanel).unbind();
		}
	}

	// Set up "Current" panel and next and prev
	growBigger('#panel_2');	
	var curPanel = 2;
	
	$('#panel_'+(curPanel+1)).click(function(){ change(true); });
	$('#panel_'+(curPanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	$('#next').click(function(){ change(true); return false; });	
	$('#previous').click(function(){ change(false); return false; });
	
	$(window).keydown(function(event){
		switch (event.keyCode) {
			case 37: //left arrow
				$('#previous').click();
				break;
			case 39: //right arrow
				$('#next').click();
				break;
		}
	});
	
});
