function checkPrev(id,projectId) {
		result = checkProject(projectId);		
		
		maxNo = parseInt(id) + 2;
		//console.info ("id: "+maxNo);
		
		if (result == maxNo) {
			$('.nextLink').css("visibility","hidden");
		}
		else {
			$('.nextLink').css("visibility","visible");
		}
		
		if (id == -1) {
			$('.prevLink').css("visibility","hidden");
		}
		else {
			$('.prevLink').css("visibility","visible");
		}
		
		return result;
	}
	
function checkProject(id) {
		result = $('.elements').find("li");	
		
		size = 1;
		result.each(function (i) {
			rel = $(this).attr("rel");
			if(rel == id) {
				size = $(this).children().size();
				//console.info(size);
			}
		});
		
		return size;
	}
	
function pause(numberMillis)
{
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true)
	{
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}


jQuery(function( $ ){
	$.scrollTo.defaults.axis = 'xy';
	//this one is important, many browsers don't reset scroll on refreshes
	$('#scroller').scrollTo( 0 );
	$.scrollTo( 0 );
	
	$.easing.elasout = function(x, t, b, c, d) {
			var s=1.70158;var p=0;var a=c;
			if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
			if (a < Math.abs(c)) { a=c; var s=p/4; }
			else var s = p/(2*Math.PI) * Math.asin (c/a);
			return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
	
	$.easing.easeOutBounce = function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}

	};

	
	var $paneOptions = $('#scroller');	
	
	$('.nextLink').click(function(){
		projectId = $(this).attr("id").replace("next-", "");
		//console.info ('projectId: '+projectId);
		
		project = $(this).attr("rel");
		//console.info ('project: '+project);
		
		$paneOptions.scrollTo( '#li-'+projectId+' img:eq('+project+')', 1000, { easing:'easeOutBounce' } );
		
		prev = parseInt(project) - 1;
		$('.prevLink').attr("rel",prev);
		project = parseInt(project) + 1;
		$(this).attr("rel",project);
		checkPrev(prev,projectId);
		
		//alert('done');
	});
	
	$('.prevLink').click(function(){
		projectId = $(this).attr("id").replace("prev-", "");
		//console.info ('projectId: '+projectId);
		
		project = $(this).attr("rel");
		//console.info ('project: '+project);
		
		$paneOptions.scrollTo( '#li-'+projectId+' img:eq('+project+')', 1000, { easing:'easeOutBounce' } );

		next = parseInt(project) + 1;
		$('.nextLink').attr("rel",next);
		project = parseInt(project) - 1;
		$(this).attr("rel",project);
		checkPrev(project,projectId);
	});
	
	var projects=new Array();
	i = 0;
	$(".elements li").each(function (i) {
		project = $(this).attr("rel");
		projects[i] = project;
		i++;
	});
	
	//console.info (projects);	
	
	
	
	function changeProject(id) {
		result = $('.elements').find("li");	
		
		result.each(function (i) {
			rel = $(this).attr("rel");
			if(rel == id) {
				var index = $('.elements li').index(this);
				console.log(index);
				//img = getFirst(id);
				//$paneOptions.scrollTo( 'img:eq('+img+')', 1000 );
			}
		});
	}
	
	/*
	
	- user clicks on a project to open it
	- project has an id which matches li id
	- get number of images in project (use to set-up next/prev visability)
	- get total number of images before that project - loop through li
	- add one to total images and move panel to that image
	- think about using next alongside the number of images in a li
	
	*/
	
	
	function getFirst(id) {
		liCount = $('#li-'+id+' > img').size();
		//imageCount = $('.elements li > img').size();
		//liThis = $('.elements > li').size();
		//console.info(liCount);
		//liThis = $('.elements #li-'+id).index(this);
		//$("div").eq(2).addClass("blue");
		//console.info(liCount);
		
		return liCount;
	}
	
	$('#closeLink').click(function() {
		$("#infoBox").slideUp('slow');
		$(".navTitle").slideUp('fast');
		$("#scroller").slideUp('slow');
		$('.projectLink').show();
		$('.hr2').show();
	});
	
	$('#hideInfo').click(function() {
		$("#infoBox").slideUp('slow');
	});
	
	$('.infoLink').click(function() {
		$("#infoBox").slideToggle('slow');
	});	
	
	$('.projectLink .open').click(function() {
		//$('.projectLink').show();
		//$('.hr2').show();
		$('.titleOpen').css("color","");
		
		title = $(this).attr("title");
		id = $(this).attr("rel");
		
		$('#link-'+id+' .titleOpen').css("color","#d0cecc");
		//$('#link-'+id).fadeOut();
		//$('#hr-'+id).fadeOut();
		$("#infoBox").hide();
		$("#openTitle").text(title);
		$("#scroller").slideDown('slow');
		$(".navTitle").slideDown('slow');
		
		//$(".projectLink").show();
		//$("#link-"+id).hide();
		
		excerpt = $(this).attr("id").replace("ex-", "");
		site = $(this).attr("name");
		
		$("#infoExcerpt").text(excerpt);
		
		if (site != '') {
			$("#infoSite").replaceWith('<span id="infoSite"><a href="'+site+'" target="_blank">View site</a></span>');
		}
		else {
			$("#infoSite").replaceWith('<span id="infoSite"></span>');
		}
		
		$paneOptions.scrollTo( '#li-'+id, 2000, {queue:true, easing:'easeOutBounce'} );
		
		$('.nextLink').attr("id","next-"+id);
		$('.prevLink').attr("id","prev-"+id);
		
		$('.nextLink').attr("rel","1");
		$('.prevLink').attr("rel","-1");
		
		liCount = getFirst(id);
		$('.prevLink').css("visibility","hidden");
		if (liCount < 2) {
			$('.nextLink').css("visibility","hidden");
		}
		else {
			$('.nextLink').css("visibility","visible");
		}
		
		return false;
	});
	
	$('.projectLink .titleOpen').click(function() {
		//$('.projectLink').show();
		//$('.hr2').show();
		$('.titleOpen').css("color","");
		
		title = $(this).attr("title");
		id = $(this).attr("rel");
		
		$('#link-'+id+' .titleOpen').css("color","#d0cecc");
		$//('#link-'+id).fadeOut();
		//$('#hr-'+id).fadeOut();
		$("#infoBox").hide();
		$("#openTitle").text(title);
		$("#scroller").slideDown('slow');
		$(".navTitle").slideDown('slow');
		
		//$(".projectLink").show();
		//$("#link-"+id).hide();
		
		excerpt = $(this).attr("id").replace("ex2-", "");
		site = $(this).attr("name");
		
		$("#infoExcerpt").text(excerpt);
		
		if (site != '') {
			$("#infoSite").replaceWith('<span id="infoSite"><a href="'+site+'" target="_blank">View site</a></span>');
		}
		else {
			$("#infoSite").replaceWith('<span id="infoSite"></span>');
		}
		
		$paneOptions.scrollTo( '#li-'+id, 2000, {queue:true, easing:'easeOutBounce'} );
		
		$('.nextLink').attr("id","next-"+id);
		$('.prevLink').attr("id","prev-"+id);
		
		$('.nextLink').attr("rel","1");
		$('.prevLink').attr("rel","-1");
		
		liCount = getFirst(id);
		$('.prevLink').css("visibility","hidden");
		if (liCount < 2) {
			$('.nextLink').css("visibility","hidden");
		}
		else {
			$('.nextLink').css("visibility","visible");
		}
		
		return false;
	});
});