var li_width = 756;
var li_margin = 30;
var sidebar_width = 255;
var scroll_speed = 1000;
var currentPage = 0;
var projectMaxNumber = 6;

function setElementsWidth() {
	$('ul.elements').width((li_width+li_margin) * $('ul.elements li').size() + sidebar_width); }

// From previous version, saved just in case. It's tiny.

//function changePage(pageIndex) {
//	$('div.pane').scrollTo($('ul.elements li').eq(pageIndex),scroll_speed, { offset: -sidebar_width });
//}

function flipToProject(projectURL) {
	currentElement = 0;
	$('.bigThumb').mouseout();
	$('#thumbContent').fadeOut('fast', function() {
		$('#projectNav').slideDown('fast', function() {
			$('div.pane').load(projectURL, function() {
				$('#mainDescription').hide();
				$('#projectDescription').html($('#project-desc').html());
				$('#project-desc').remove();
				$('div.pane').fadeIn('slow');

			});
		});
	});
	setElementsWidth();
	document.onkeydown = function(e){ 	
		if (e == null) {
			keycode = event.keyCode;
		} else {
			keycode = e.which;
		}
		if(keycode == 27){
			unFlip();
		} else if(keycode == 39){
			nextPage();
		} else if(keycode == 37){
			prevPage();
		}
	};
}

function unFlip() {
	$('div.pane').fadeOut('fast', function() {
		$('div.pane').html(' ');
		$('#projectNav').slideUp('fast');
		$('#thumbContent').fadeIn('slow');
		$('#projectDescription').html(' ');
		$('#mainDescription').show();
	});
	$('.bigThumb').mouseout();
	document.onkeydown = null;
}

$(document).ready(function() {
	$('#thumbContent a').live('click', function() { flipToProject(this.href); return false; });
	$('#thumbContent a div').hide();
	$('#thumbContent a').hover(function() {
			$(this).children('div').animate({ height: 'show' }, 300);
			}, function() {
			$(this).children('div').animate({ height: 'hide' }, 300);
			}
	);

});

function nextPage() {
	if(currentPage == projectMaxNumber)
	{
		unFlip();
	}
	else
	{
		currentPage = currentPage + 1;
		flipToProject('/projects/project' + currentPage + '.php');
	}
}

function prevPage()
{
	currentPage = currentPage - 1;
	if(currentPage == 0)
	{
		unFlip();
	}
	else
	{
		flipToProject('/projects/project' + currentPage + '.php');
	}

}
