var jobs = [
		'a web developer at <a href="http://www.sephone.com" target="_blank">Sephone</a>',
		'the project manager for <a href="http://www.datavenger.com" target="_blank">datAvenger</a>',
		'a photographer at <a href="http://www.pinetreephotography.com" target="_blank">Pine Tree Photography</a>',
		'a writer',
		'an organizer'
		];
var passions = [
		'technology and its effects on society',
		'making technology usable',
		'educating people for a technological world',
		'how people interact with technology'
		];
var jobIndex = 1;
var passionIndex = 1;
var scrollInterval;
var fadeHalted = false;
var activeRequest = false;
var activeScroll = false;
var maxPage = 1;
setTimeout(changeJob, 10000);
function changeJob() {
	if (!fadeHalted) {
		$('#helloJob').fadeOut('def', function() { 
			$('#helloJob').html(jobs[jobIndex++]);
			$('#helloJob').fadeIn();
		});
		if (!jobs[jobIndex]) jobIndex = 0;
		setTimeout(changePassion, 5000);
	}
}
function changePassion() {
	if (!fadeHalted) {
		$('#helloPassion').fadeOut('def', function() { 
			$('#helloPassion').html(passions[passionIndex++]);
			$('#helloPassion').fadeIn();
		});
		if (!passions[passionIndex]) passionIndex = 0;
		setTimeout(changeJob, 5000);
	}
}
$(document).ready(function() {
	positionStream();
	$('#stream .up').mousedown(function() {
		scrollStreamUp();
		scrollInterval = setInterval(scrollStreamUp, 400);
	});
	$('#stream .down').mousedown(function() {
		scrollStreamDown();
		scrollInterval = setInterval(scrollStreamDown, 400);
	});
	$('#stream').mousewheel(function(event, delta) {
		if (delta > 0) {
			scrollStreamUp();
		} else {
			scrollStreamDown();
		}
	});
	$('#stream .up, #stream .down').mouseup(function() {
		clearInterval(scrollInterval);
	});
	$(window).keydown(function(event) {
		if (!activeScroll && (event.keyCode == 40 || event.keyCode == 32)) {
			scrollStreamDown();
		} else if (!activeScroll && event.keyCode == 38) {
			scrollStreamUp();
		}
	});
	$('#hello').mouseenter(function() {
		fadeHalted = true;
	});
	$('#hello').mouseleave(function() {
		fadeHalted = false;
		setTimeout(changeJob, 10000);
	});
	$('#about ul.projects li a').each(function() {
		if ($(this).attr('class')) {
			$(this).css('background-image', 'url("img/projects/' + 
				$(this).attr('class').split('-')[1] + '.png")');
		}
	});
	$('#about ul.sites li a').each(function() {
		if ($(this).attr('class')) {
			$(this).css('background-image', 'url("img/sites/' + 
				$(this).attr('class').split('-')[1] + '.png")');
		}
	});
});
$(window).resize(function() {
	positionStream();
});
function positionStream() {
	$('#stream').height($(window).height() - $('#stream').offset().top);
	$('#stream .down').css('top', $('#stream').height() - 
			$('#stream .down').height());
}
function scrollStreamDown() {
	scrollStream('down');
}
function scrollStreamUp() {
	scrollStream('up');
}
function scrollStream(direction) {
	if (activeScroll) {
		return false;
	}
	var scrollStep = 200;
	var requestHeight = 1000;
	var currentTop = parseInt($('#stream .items').css('top'));
	var remainingHeight = $('#stream .items').height() + currentTop -
			$('#stream').height();
	if (remainingHeight < requestHeight) {
		activeRequest = true;
		$.get('/items/index/page:' + ++maxPage, function(data) {
			activeRequest = false;
			$('#stream .items').append(data);
			$('#stream .down').fadeIn();
		});
	}
	activeScroll = true;
	if (direction == 'down') {
		if (remainingHeight > scrollStep) {
			$('#stream .items').animate({top: currentTop - scrollStep}, 400,
					null, function() { activeScroll = false; });
		} else {
			$('#stream .items').animate({top: currentTop - remainingHeight}, 
					400, null, function() { activeScroll = false; });
			$('#stream .down').fadeOut();
		}
		$('#stream .up').fadeIn();
	} else {
		if ((-1 * currentTop) > scrollStep) {
			$('#stream .items').animate({top: currentTop + scrollStep}, 400,
					null, function() { activeScroll = false; });
		} else {
			$('#stream .items').animate({top: 0}, 400, null, 
					function() { activeScroll = false; });
			$('#stream .up').fadeOut();
		}
	}
}