// JavaScript Document

var current = 0;
var $pImages;
var $pClients;
var $pProjects;

$(document).ready(
function()
{
	$pImages = $("img.portImage", $('#portfolio')); //All project images 
	$pClients = $("span.client", $('#portfolio'));  //All project client names
	$pProjects = $("span.project", $('#portfolio')); //All project names
	
	//Bind navigation events to their buttons
	$("img#next").click(nextProject);
	$("img#previous").click(previousProject);
	
	setCurrent(); //See if there are URL variables to set the current project
	showProject(current); //Show current project (default project 0)
	hideButtons(); //Hide navigation buttons if they are not needed
	
}
);	  
//show all data for a project
function showProject(projectNumber)
{	
	$pImages.eq(projectNumber).show();
	$pClients.eq(projectNumber).show();
	$pProjects.eq(projectNumber).show();
}
//Hide all data for a project
function hideProject(projectNumber)
{
	$pImages.eq(projectNumber).hide();
	$pClients.eq(projectNumber).hide();
	$pProjects.eq(projectNumber).hide();	
}
//Hide current project, go to next project and show it
function nextProject()
{
	hideProject(current);
	current = (current + 1) % $pImages.length;
	showProject(current);
	hideButtons();
	return false;
}
//Hide current project, go to previous project and show it
function previousProject()
{
	hideProject(current);
	current = Math.abs((current - 1)) % $pImages.length;
	showProject(current);
	hideButtons();
	return false; 
}

//Hide navigation buttons if the page is at the start or end of the current portfolio section
function hideButtons()
{
	//If 0, hide previous button, or else show it
	if(current <= 0)
	{
		current = 0;
		$("img#previous").hide();
	}
	else $("img#previous").show();
	
	//If we are at the end of the portfolio, hide the next button. If not be sure to show it
	if(current >= $pImages.length - 1)
	{
		current = $pImages.length - 1;
		$('img#next').hide();
	}
	else $('img#next').show();
}

/* setCurrent looks for an optional variable in the format '..url.html?client=#' where # is the project # to view in the site */
function setCurrent()
{
	var urlVars = window.location.href.split('?'); //We are looking for anything after a ? 
	if(urlVars.length > 1) //If there are any url variables 
	{
		urlVars = urlVars[1].split('='); //separate them at the by '='
		if(urlVars.length > 1) //If there is a variable
		{
			current = urlVars[1] % $pImages.length; //Set the current proejct to that number, as long as it is inside the bounds of the portfolio image length
		}
	}
	else current = 0; //If no variables go to the first image 
}
