$(document).ready(function() {
	$('div.documentHome a').hover(function() {
			$(this).stop().animate({'paddingTop':'5px'}, 150);
		},
		function() {
			$(this).stop().animate({'paddingTop':'0px'}, 150);
			});
	$('ul#navigation a').hover(function() {
			$(this).stop().animate({'marginLeft':'5px'}, 150);
		},
		function() {
			$(this).stop().animate({'marginLeft':'0px'}, 150);
			});
	
	function getProjectId(projectImage) {
		return projectImage.attr('id').substr(13);
		}
	
	var projectAnimationQueue = [];
	
	$('img.projectImage').css('left','850px');
	$('img.projectImage.selected').css('left','0px');
	
	function projectAnimation() {
		if(!$('div#projectImageFrame').hasClass('animated') && projectAnimationQueue.length > 0) {
			$('div#projectImageFrame').addClass('animated');
			current = projectAnimationQueue.shift();
			
			if(current['direction'] == 0) fromPosition = 850;
			else fromPosition = -850;
			
			current['from'].gx({'left':-fromPosition+'px'}, 600, 'Quint', function() {
				$(this).css('left','850px');
				});
			current['to'].css('left',fromPosition+'px').gx({'left':'0px'}, 600, 'Quint', function() {
					$('div#projectImageFrame').removeClass('animated');
					projectAnimation();
					});
			
			$('#projectBar div').gx({'left': ($("div#projectImageFrame img").index(current['to'])/($("div#projectImageFrame img").length-1))*768+1+'px'},600,'Quint');
			
			$('#projectBody_'+getProjectId($(current['from']))).css({'display':'block','opacity':'1'}).removeClass('selected').gx({'opacity':'0'},300, 'Linear', function(){
				$('#projectBody_'+getProjectId($(current['from']))).css('display','none');
				$('#projectBody_'+getProjectId($(current['to']))).css({'display':'block', 'opacity':'0'}).addClass('selected').gx({'opacity':'1'},300, 'Linear', function() {
					});
				});
			
			}
		}
	
	function slideProject(direction,from,to) {
		from.removeClass('selected');
		to.addClass('selected');
		if(!to.next().hasClass('projectImage')) $('div#projectRight').addClass('disabled');
		else $('div#projectRight').removeClass('disabled');
		if(!to.prev().hasClass('projectImage')) $('div#projectLeft').addClass('disabled');
		else $('div#projectLeft').removeClass('disabled');
		
		projectAnimationQueue.push({'direction':direction,
										'from':from,
										'to':to});
		projectAnimation();
		}
	
	$('div#projectRight').click(function() {
		var id = getProjectId($('img.projectImage.selected'));
		if($('#projectImage_'+id).next().hasClass('projectImage')) {
			slideProject(0,$('#projectImage_'+id),$('#projectImage_'+id).next());
			}
		});
	$('div#projectLeft').click(function() {
		var id = getProjectId($('img.projectImage.selected'));
		if($('#projectImage_'+id).prev().hasClass('projectImage')) {
			slideProject(1,$('#projectImage_'+id),$('#projectImage_'+id).prev());
			}
		});
	});
