// JavaScript Document

var currFolder;

var tempPage = document.location.toString();
var thisPage = tempPage.slice(tempPage.lastIndexOf("/") + 1, tempPage.length);
if (thisPage.lastIndexOf("#") != -1)
	thisPage = thisPage.slice(0, thisPage.lastIndexOf("#"));	

function initPage() {	
	var menuSelection = thisPage.slice(0, thisPage.indexOf("."));
	document.getElementById(menuSelection).className = "selected_menu_title";
}

function displayPage(pageName) {
	var nextPage = pageName + ".htm";
	if (nextPage != thisPage)
		document.location = nextPage;	
}

function displaySamples(sampleType) {
	
	currFolder = "samples/";

	switch (sampleType) {
	
		case "canvas_paintings":
			currFolder += "paintings/canvas/";
			/* document.getElementById("paper_paintings").className = "menu_title";					
			document.getElementById("canvas_paintings").className = "selected_menu_title"; */
			break;
			
		case "paper_paintings":
			currFolder += "paintings/paper/";				
			document.getElementById("canvas_paintings").className = "menu_title";			
			document.getElementById("paper_paintings").className = "selected_menu_title";			
			break;	
			
		case "collages":
			currFolder += "collages/";			
			break;		
			
	}
	
	document.getElementById("thumbnails").src = currFolder + "thumbnails.htm";		

}

function enlargeSample(sample) {	
	var thumbnailTable = getThumbnailTable();
	for (var r=0; r < thumbnailTable.rows.length; r++)	{
		for (var c=0; c < thumbnailTable.rows[r].cells.length; c++) {
			if (thumbnailTable.rows[r].cells[c].id == sample)
				thumbnailTable.rows[r].cells[c].className = "table_cell_highlight";
			else
				thumbnailTable.rows[r].cells[c].className = "";	
		}
	} 
	document.getElementById("enlarged_image").src = currFolder + sample + ".htm";	
}

function getFirstSample() {
	var thumbnailTable = getThumbnailTable();
	var firstSample;
	firstLoop:
	for (var a=0; a < thumbnailTable.rows.length; a++)	{
		for (var b=0; b < thumbnailTable.rows[a].cells.length; b++) {
			if (thumbnailTable.rows[a].cells[b].id != "") {
				firstSample = thumbnailTable.rows[a].cells[b].id;
				break firstLoop;
			}
		}
	} 
	return firstSample;	
}

function getLastCellInRow(row) {
	var rowPos = -1;
	var thumbnailTable = getThumbnailTable();	
	for (var x=thumbnailTable.rows[row].cells.length - 1; x >= 0 ; x--) {
		if (thumbnailTable.rows[row].cells[x].id != "") {
			rowPos = x;
			break;
		}
	}
	return rowPos;	
}

function previousSample() {
	var thumbnailTable = getThumbnailTable();
	var previousSample;
	var lastRowPos;
	var lastCellPos;
	previousLoop:
	for (var r=thumbnailTable.rows.length - 1; r >=0; r--)	{
		for (var c=thumbnailTable.rows[r].cells.length - 1; c >=0; c--) {
			if (thumbnailTable.rows[r].cells[c].className == "table_cell_highlight") {
				thumbnailTable.rows[r].cells[c].className = "";	
				if (c > 0) {
					previousSample = thumbnailTable.rows[r].cells[c - 1].id;
					thumbnailTable.rows[r].cells[c - 1].className = "table_cell_highlight";
					break previousLoop;
				}
				else {
					if (r > 0) {
						lastCellPos = getLastCellInRow(r -1);
						if (lastCellPos >= 0) {
							previousSample = thumbnailTable.rows[r -1].cells[lastCellPos].id;
							thumbnailTable.rows[r - 1].cells[lastCellPos].className = "table_cell_highlight";
							break previousLoop;
						}
						else {
							lastRowPos = thumbnailTable.rows.length - 1;
							lastCellPos = thumbnailTable.rows[lastRowPos].cells.length - 1;
							previousSample = thumbnailTable.rows[lastRowPos].cells[lastCellPos].id;
							thumbnailTable.rows[lastRowPos].cells[lastCellPos].className = "table_cell_highlight";
							break previousLoop;						
						}
					}
					else {
						lastRowPos = thumbnailTable.rows.length - 1;
						lastCellPos = thumbnailTable.rows[lastRowPos].cells.length - 1;
						previousSample = thumbnailTable.rows[lastRowPos].cells[lastCellPos].id;
						thumbnailTable.rows[lastRowPos].cells[lastCellPos].className = "table_cell_highlight";
						break previousLoop;
					}
				}
			}
		}
	} 
	document.getElementById("enlarged_image").src = currFolder + previousSample + ".htm";				
}

function nextSample() {
	var thumbnailTable = getThumbnailTable();
	var nextSample;
	nextLoop:
	for (var r=0; r < thumbnailTable.rows.length; r++)	{
		for (var c=0; c < thumbnailTable.rows[r].cells.length; c++) {
			if (thumbnailTable.rows[r].cells[c].className == "table_cell_highlight") {
				thumbnailTable.rows[r].cells[c].className = "";	
				if (c < (thumbnailTable.rows[r].cells.length - 1) && thumbnailTable.rows[r].cells[c + 1].id) {
					nextSample = thumbnailTable.rows[r].cells[c + 1].id;
					thumbnailTable.rows[r].cells[c + 1].className = "table_cell_highlight";
					break nextLoop;
				}
				else {
					if (r < (thumbnailTable.rows.length - 1)) {
						nextSample = thumbnailTable.rows[r + 1].cells[0].id;
						thumbnailTable.rows[r + 1].cells[0].className = "table_cell_highlight";	
						break nextLoop;
					}
					else {
						enlargeSample(getFirstSample());
						return;
					}
				}
			}
		}
	} 
	document.getElementById("enlarged_image").src = currFolder + nextSample + ".htm";		
		
}

function getThumbnailTable() {
	var thumbnailFrame = document.getElementById("thumbnails");
	var thumbnailDoc;
	var thumbnailTable;
    if (thumbnailFrame.contentDocument)  
       thumbnailDoc = thumbnailFrame.contentDocument;  
    else if (thumbnailFrame.contentWindow)  
       thumbnailDoc = thumbnailFrame.contentWindow.document;  
    else if (thumbnailFrame.document)   
       thumbnailDoc = thumbnailFrame.document;  
	thumbnailTable = thumbnailDoc.getElementById("thumbnail_table");	
	return thumbnailTable;
}

function displayDetail(detailType) {
	document.getElementById("artist_statement").className = "menu_title";
	document.getElementById("exhibitions").className = "menu_title";
	document.getElementById("affiliations").className = "menu_title";
	document.getElementById("awards").className = "menu_title";	
	document.getElementById("education").className = "menu_title";
	document.getElementById("bibliography").className = "menu_title";
	document.getElementById("news").className = "menu_title";
	document.getElementById("contact").className = "menu_title";	
	document.getElementById(detailType).className = "selected_menu_title";	
	document.getElementById("detail").src = detailType + ".htm";	
}
