$(document).ready(function() {
	
	var settings = {
		duration: 200,
		opacity: .8,
		offset: -165,
		easing: 'easeInQuad'
	};	
	
	$.fn.drawNav = function(dudebro) {
		if ($(dudebro).is(':first-child')) {
			// we're at the first entry
			$('#previous').fadeOut(settings.duration);
			$('#next').fadeIn(settings.duration);
		} else if ($(dudebro).is(':last-child')) {
			// we're at the last entry
			$('#next').fadeOut(settings.duration);
			$('#previous').fadeIn(settings.duration);
		} else if ($('.project').hasClass('full')) {
			// if there's a full.project, show 'general' nav
			$('.navigator').fadeIn(settings.duration);
		} else {
			// in the middle, draw all
			// check for visibility
			$('.navigator').fadeOut(settings.duration);
		}
		
	}
	
	//$('.navigator').hide();	
	$().drawNav();
	
	$('.navigator').live('click', function() {
		$().navigate($(this).attr('id'), '#'+$('.portfolio .project.full').attr('id'));
	});
	
	$.fn.showBox = function(bro, action) {
		if (action == 'open') {
			$(bro).slideDown(settings.duration / 2, settings.easing);
		} else if (action == 'close') {
			$(bro).slideUp(settings.duration / 2, settings.easing);
		} else {		
			return false;
		}
	}
	
	
	
	$.fn.navigate = function(direction, placeholder) {
		// use this to control navigator behavior later on
		if (direction == 'next') {
			next = '#'+$(placeholder).next('.project').attr('id')
			$().expand(next);
			setTimeout(function() {
				$().collapse(placeholder);
			}, settings.duration / 2);
		} else if (direction == 'previous') {
			previous = '#'+$(placeholder).prev('.project').attr('id')
			$().expand(previous);
			setTimeout(function() {
				$().collapse(placeholder);
			}, settings.duration / 2);
		}
	}	
	
	$.fn.collapse = function(victim) {		
		$(victim).children('.wrapper').children('.expander').slideUp(settings.duration / 2, settings.easing);
		$(victim).removeClass('full')
		setTimeout(function() {			
			$(victim+' .tags').hide();
			$(victim+' .close-project').hide();
			setTimeout( function() {
				$(victim).children('.wrapper').children('.gallery').css('opacity', settings.opacity);			
			}, settings.duration);
		});
	};
	
	$.fn.expand = function(newguy) {
		var x = $(window).height() * .66;
		$('#bottom-spacer').animate({height:x}, settings.easing);		
		$(newguy).addClass('full');	
		$(newguy+' .wrapper').children('.expander').slideDown(settings.duration / 2, settings.easing);
		setTimeout( function() {
			$.scrollTo(newguy, settings.duration / 2, {offset:settings.offset});
			$().drawNav(newguy);
			setTimeout(function() {
				$(newguy+' .tags').fadeIn(settings.duration);
				$(newguy+' .close-project').fadeIn(settings.duration);
			}, settings.duration);
		}, settings.duration * 1.5);		
	};			
					
	$('.gallery').hover( function() {
		$(this).stop().animate({opacity:1}, settings.duration);
	}, function() {
		if ($(this).parent('.wrapper').parent('.project').hasClass('full')) {
			return false;
		}		
		$(this).stop().animate({opacity:settings.opacity}, settings.duration);
	});
		
	$('.close-project').live('click', function() {
		victim = '#'+$(this).parent('.wrapper').parent('.project').attr('id');		
		$().collapse(victim);
		$('#bottom-spacer').css('height',0);
		setTimeout(function() {
			$.scrollTo(victim, settings.duration, {offset:settings.offset});		
		});
		$().drawNav();
		return false;
	});
	
	$('.title').live('click',function() {		
		$.scrollTo(0, settings.duration, {
			onAfter: function() {
				setTimeout( function() { $().showBox('.about', 'open');}, settings.duration);
			}
		});
		victim = '#'+$('.project.full').attr('id');
		$().delay(settings.duration).collapse(victim);
		$('#bottom-spacer').css('height','0');
		$().drawNav();
	});
			
	$('.project .gallery').live('click', function() {
		if ($(this).parent().parent().hasClass('full')) {
			return false;
		}
		$().showBox('.about','close');
		newguy = '#'+$(this).parent().parent().attr('id');		
		setTimeout(function() {
			$().expand(newguy)}, settings.duration);

		$().collapse('.project');
	});
});
