$(function() {
	//Basic navigation
	$('#landing a').click(function() {
		navigateTo($(this).attr('href').substr(1));
		return false;
	});
	
	//Move back to landing position when background is clicked
	$('body').click(function(e) {
		if ($(e.target).is('body') || $(e.target).is('.i-love')) 
			navigateTo('landing');
	});
	
	//Slides to specified target
	function navigateTo(newTarget) {
		var duration = 1000;
		
		//Determine old and new target
		var oldTarget = $('.target').attr('id');
		if (newTarget == oldTarget) newTarget = 'landing';
	
		//Stop all animations
		$('#photos, #coding, #landing').stop(); 
		
		function hide() { $(this).css('display', 'none'); }
	
		//Logic for animations from and to individual targets
		if (newTarget == 'landing') {
			if (oldTarget == 'photos')
				$('#photos').animate({ top: '100%', opacity: 0 }, duration, hide);
			else if (oldTarget == 'coding')
				$('#coding').animate({ top: '0%', opacity: 0 }, duration, hide);
			
			$('#landing').animate({ top: '45%' }, duration);
		}
	
		if (newTarget == 'photos') {
			$('#landing').animate({ top: '0%' }, duration);
			$('#photos').css('display', 'block').animate({ top: '55%', opacity: 1 }, duration);
		}
		else if (newTarget == 'coding') {
			$('#landing').animate({ top: '100%' }, duration);
			$('#coding').css('display', 'block').animate({ top: '45%', opacity: 1 }, duration);
		}
	
		//Set class for new target
		$('#' + oldTarget).removeClass('target');
		$('#' + newTarget).addClass('target');
	}

	//Left and right scrolling for photos and projects
	jQuery.fn.scroll = function(options) {
		//Defaults
		options.container = this; options.position = 0;
	
		//Determines the visibility of an element in the list by checking if it is outside the screen
		function isVisible(element) {
			var left = $(element).offset().left, width = $(element).width();
			return left >= 0 && left + width <= $('body').width();
		}
	
		//Slides in the specified direction ("left" or "right")
		function slide(direction) {
			//Prepare loop
			var target, elements = $(options.container).children(), last = elements.length, start = 0, end = last, step = 1;
			if (direction == 'right') { start = last - 1; end = -1; step = -1; }

			//Move through elements, approaching visible area from the side where the user wants to scroll to
			for (var i = start; i != end; i += step) 
				if (isVisible(elements[i])) //Visible area found, go back to last non-visible element
					if (i - step >= 0 && i - step < last) {
						target = elements[i - step];
						break;
					}
					else return false;
				
			//End is reached, abort
			if (!target) return false;

			//Determine position of target
			var targetLeft = $(target).offset().left, targetRight = targetLeft + $(target).width();

			//Determine value for "left" property of container
			if (targetLeft < 0) options.position += -targetLeft + 80;
			else if (targetRight > $('body').width()) options.position -= (targetRight - $('body').width()) + 80;

			if (options.position > 0) options.position = 0;

			//Scroll smoothly
			$(options.container).stop().animate({ left: options.position + 'px' }, 700);

			return false;
		}
	
		//Left and right arrow button click handlers
		$(options.left).click(function() { return slide('left'); });
		$(options.right).click(function() { return slide('right'); });
		
		//Arrow key handler
		$(document).keydown(function(event) {
			//Abort if this container is not visible
			if ($(options.container).closest('.target').length == 0) return; 
						
			if (event.keyCode == 37) { slide('left'); return false; }
			else if (event.keyCode == 39) { slide('right'); return false; }
		});
	};

	//Set up the two scrolling lists
	$('#photos ul').scroll({ left: $('#photos .left'), right: $('#photos .right') });
	$('#coding ul').scroll({ left: $('#coding .left'), right: $('#coding .right') });
	
	//Collect photo list for fancybox gallery
	var photos = [ ];
	
	$('#photos li a').each(function() {
		photos.push({ href: $(this).attr('href'), title: $(this).find('img').attr('alt') });
	});
	
	//Set up fancybox
	$('#photos li a').click(function() { 
		$.fancybox(photos, { index: $('#photos li a').index(this), padding: 5, loop: false, nextEffect: 'NONE', prevEffect: 'NONE' });
		return false; 
	});
});
