//
// Uses AJAX to get the portfolio view
//
function showView(index)
{
	$('content_loader').show();
	
	Effect.Fade('view_image', {duration:0.5});
	Effect.Fade('view_content', {duration:0.5});
	Effect.Fade('view_caption', {duration:0.5});
	
	setCurrentIndex(index);
	project_id = getCurrentID();

	//alert(project_id+" project ID");
	
	var params = 'p='+project_id;
	new Ajax.Request('get_view.php', {method: 'get', parameters: params, onSuccess: showViewComplete, onFailure: showViewFail});
}

function showViewComplete(transport)
{
	$('content_loader').hide();

	if (!transport.responseText.match(/^ERROR\:/)) 
	{
		$('content_target').update(transport.responseText);		
		Effect.Appear('view_image', {duration:0.5});
		Effect.Appear('view_content', {duration:0.5});
		Effect.Appear('view_caption', {duration:0.5});
	}
	else 
	{
		alert(transport.responseText);
	}
}

function showViewFail(transport)
{
	alert("Information about this project could not be retrieved. Please try refreshing your browser, and if the problem persists, contact nine10.");
}


function getIndexByID(project_id)
{
	for (var i=0; i < project_list.length; i++)
	{
		if (project_list[i] == project_id)
		{
			return i;
		}
	}
	return 0;
}

function setCurrentIndex(index)
{
	current_index = index;
}
function getCurrentIndex()
{
	return current_index;
}
function getIDByIndex(index)
{
	return project_list[index];
}
function getCurrentID()
{
	return project_list[getCurrentIndex()];
}

function getNextIndexWithCategory(offset)
{
	if (!offset || offset == undefined) offset = 0;
	if (offset >= project_cat_list.length-1) offset = project_cat_list.length-1
	if (offset < 0) offset = 0;
	
	for (var i=offset; i < project_cat_list.length; i++)
	{
		if (project_cat_list[i] != false)
		{
		
			for (t=0; t < show_cats.length; t++)
			{
				if (project_cat_list[i].toString().indexOf(show_cats[t]) > -1) 
				{
					return i;
				}
			}
		}
	}
	return -1;
}

function getPrevIndexWithCategory(offset)
{
	if (!offset || offset == undefined) offset = 0;
	if (offset >= project_cat_list.length-1) offset = project_cat_list.length-1
	if (offset < 0) offset = 0;
	
	for (var i=offset; i >= 0; i--)
	{
		if (project_cat_list[i] != false)
		{
			for (t=0; t < show_cats.length; t++)
			{
				if (project_cat_list[i].toString().indexOf(show_cats[t]) > -1) 
				{
					return i;
				}
			}
		}
	}
	return -1;
}

function getNextIndex()
{
	index = getCurrentIndex();
	next_index = ((index+1) < project_list.length) ? index+1 : 0;
	next_index_filtered = getNextIndexWithCategory(next_index);
	//alert("First: "+next_index_filtered);
	if (next_index_filtered == -1)
	{
		next_index_filtered = getNextIndexWithCategory(0);
		//alert("Then: "+next_index_filtered);
	}
	return next_index_filtered;
}

function getPrevIndex()
{
	index = getCurrentIndex();
	prev_index = ((index-1) >= 0) ? index-1 : (project_list.length-1);
	prev_index_filtered = getPrevIndexWithCategory(prev_index);
	//alert("First: "+next_index_filtered);
	if (prev_index_filtered == -1)
	{
		prev_index_filtered = getPrevIndexWithCategory(project_list.length-1);
		//alert("Then: "+next_index_filtered);
	}
	return prev_index_filtered;
}

function updateCategories(clicked)
{
	show_cats = [];
	for (var i=0; i < total_cats; i++)
	{
		if ($('cat_'+i).checked == true)
		{
			show_cats.push($F('cat_'+i));
		}
	}
	
	if (clicked != undefined && clicked > 0)
	{
		if (show_cats.length == 0)
		{
			//alert("At least one category must be selected.");
			$('cat_'+clicked).checked = true;
			show_cats.push($F('cat_'+clicked));
		}
	}
}


