var currentFocus = null;
var focusTarget = null;
var postsArr = [];
	
function updateScroller(){
	$scroller = $('#scrolling-Div');
	var closest = null;
	var distance = 9000;
	var useX = $(window).scrollLeft() + $(window).width()/2;
		
	for(var i=0; i<postsArr.length; i++){
		if(Math.abs(postsArr[i].x - useX) < distance){
			distance = Math.abs(postsArr[i].x - useX);
			closest = postsArr[i];
		}
	}

	if(closest != null){
		$scroller.find('h4').first().text(closest.label);
		
		var focusItem;

		if(focusTarget == null)
			focusItem = $('#' + closest.id);
		else
			focusItem = $(focusTarget);
		
		var targetLeft = focusItem.offset().left + parseInt(focusItem.css('padding-left'));
		$scroller
			.clearQueue('scrolling')
			.queue('scrolling', function(next){
				$(this).animate({"left": targetLeft}, {duration:"slow", queue: false} );
				next();
			})
			.queue('scrolling', function(next){
				if(focusTarget != null){
					focusTarget = null;
				}
				currentFocus = '#'+focusItem[0].id;
			
			})
			.dequeue('scrolling');

	}
}

$(function() {
	$("#content").wrapInner("<table cellspacing='10'><tr>");
	$(".post").wrap("<td>");
	
	$('.post[id]').each(function(){
		postsArr.push({
			id: this.id,
			x: $(this).offset().left + $(this).width()/2 + parseInt($(this).css('padding-left')),
			label: $(this).attr('label')
		});
		
	});

	var $scrollingDiv = $("#scrolling-Div");
	$scrollingDiv.hide();
	
	var scrollShown = false;

	$(window).scroll(function(){
		updateScroller();
								
		if(!scrollShown){
			$scrollingDiv
				.delay(400)
				.fadeIn(300)
				.delay(600);
				
			$arrows = $('.keyboard-arrow');
			$arrows.each(function(){
				var elem = this;
				$scrollingDiv.delay(125);
				$scrollingDiv.queue(function(next){
					$(elem).addClass('hover');
					next();
				});
				$scrollingDiv.delay(125);
				$scrollingDiv.queue(function(next){
					$(elem).removeClass('hover');
					next();
				});
			});
			
			scrollShown = true;
		}
	});
	
	$(window).resize(function(){
		var winWidth = $(window).width();
		var extra = $('div.extra').first();
		var last = $('.post[id]').last();
		if(winWidth > last.width())
			extra.width(Math.ceil((winWidth - last.width()) / 2));
		else
			extra.width(0);
	});
	$('div.extra').first().css('padding-left', 0);
	$(window).trigger('resize');
	
	$('#selected-works a, .intro-col a, #prevnext a').bind('click', function(event){
		gotoID($(this).attr('href'))

		event.preventDefault();
	});
	
	$('.post[id] .crop').each(function(){
		var $details = $(this).find('.details');
		
		if($details.length == 1){
			var fullHeight = $details.height() + parseInt($details.css('padding-bottom'));
			$details.css('padding-bottom', 0);
			$details.height(0);
			//$details.hide();
			
			$(this).hover(
				//over
				function(event){
					$details.stop();
					$details.animate({'height': fullHeight+'px'}, {duration:"fast"});
				},
				
				//out
				function(event){
					$details.stop();
					$details.animate({'height': '0px'}, {duration:"fast"});
				}
			);
		}
		
		$(this).click(function(){
			var parent = $(this).parents('.post')[0];
			if(parent.id != currentFocus){
				gotoID('#' + parent.id);
			}
		});
	});
	
$(window).keydown(function(event){
               if(Shadowbox){
                       if(Shadowbox.isOpen()){
                               event.preventDefault();
                               return;
                       }
               }
       
               switch(event.which){
                       //left
                       case 37:
                               previousPost();
                               event.preventDefault();
                               break;
                       //up
                       case 38:
                               gotoID('#' + $('.post[id]').last()[0].id);
                               event.preventDefault();
                               break;
                       //right
                       case 39:
                               nextPost();
                               event.preventDefault();
                               break;
                       //down
                       case 40:
                               gotoID('#' + $('.post[id]')[0].id);
                               event.preventDefault();
                               break;
                       //enter
                       case 32:
								$(currentFocus + ' a[rel^="shadowbox"]').first().trigger('click');
                    			break;
               }
       });
	

	$('#arrow-up').click(function(e){
		pseudoKey(38);
	});
	$('#arrow-down').click(function(e){
		pseudoKey(40);
	});
	$('#arrow-left').click(function(e){
		pseudoKey(37);
	});
	$('#arrow-right').click(function(e){
		pseudoKey(39);
	});

});

function pseudoKey(key){
	var event = $.Event('keydown');
	event.which = key;
	$(window).trigger(event);
}
        
function gotoID(id){
	if($(id).length > 0){
		var $anchor = $(id);
		var $window = $(window);

		var useX = $anchor.offset().left + parseInt($anchor.css('padding-left'));
		if($anchor.width() < $window.width()){
			useX -= Math.floor(($window.width() - $anchor.width()) / 2);
		}
		
		$('html, body').stop().animate({
			scrollLeft: useX
		}, 500);
		
		focusTarget = id;
	}
};

function advancePost(direction){
	var $target = $('.post[id]');
	
	if(currentFocus != null){
		var $td = $(currentFocus).parents('td');
		var $temp;
	
		if($td.length > 0){
			if(direction == 'prev'){
				$temp = $td.prev('td');
			}else{
				$temp = $td.next('td');
			}
			
			if($temp.length > 0)
				$target = $temp.find('.post[id]');
		}
	}
	
	if($target.length > 0){
		gotoID('#' + $target[0].id);
	}
}

function previousPost(){
	advancePost('prev');
}
function nextPost(){
	advancePost('next');
}
