// JavaScript Document

var paintingTag = "pt=";
var categoryTag = "cat=";
var imageDirTag = "Paintings";
var thumbDirTag = "ThumbNails"
var numTableCols = 5;
var numGalleriesCols = 12; //Number of colums in a row of the Gallery table
var numGalleriesNameCols = 2;//Number of columns in Gallery table used by gallery name

function getGalleryCol(category,col)
{
  return(Galleries[((category-1)*numGalleriesCols)+ col-1]);
}

//Get the number of colums for a given row on the Gallery thumbnail page
function getNumColsForGalleryRow(category,row)
{
  return(Galleries[((category-1)*numGalleriesCols)+ numGalleriesNameCols + row-1]);
}

function getGalleryCol2(i, category)
{
  i = i-0 + getFirstPaintingForCat(category);
  return(Paintings[(i*numTableCols)+0]);
}

function getNumberOfPaintings()
{
  return(Paintings.length/numTableCols);
}

function getNumberOfPaintingsForCat(category)
{
	
	var numPaintings = getNumberOfPaintings();
	var numPaintingsForCat = 0;
	
	
    for (i = 0; i < numPaintings; i++)
  	{
    	if (Paintings[(i*numTableCols)+2] == category)
			numPaintingsForCat++;
	}
	
 
  return(numPaintingsForCat);
}

function getFirstPaintingForCat(category)
{
	
	var numPaintings = getNumberOfPaintings();
		
    for (i = 0; i < numPaintings; i++)
  	{
    	if (Paintings[(i*numTableCols)+2] == category)
			return i;
	}
	return (0);
  
}

function getPaintingName(i, category)
{
  i = i-0 + getFirstPaintingForCat(category);
  return(Paintings[(i*numTableCols)+0]);
}

function getPaintingFilename(i,category)
{

   //Subtract 0 to ensure numeric calculation, rather than concatenation
   i = i-0 + getFirstPaintingForCat(category);	
   var name = Paintings[(i*numTableCols)+1];
  return(name);
}

function getPaintingSize(i,category)
{
  i = i-0 + getFirstPaintingForCat(category);	
  return(Paintings[(i*numTableCols)+3]);
}

function getPaintingMaterial(i,category)
{
  i = i-0 + getFirstPaintingForCat(category);	
  return(Paintings[(i*numTableCols)+4]);
}



function showThumbnail(category, paintingNumber)
{
	//alert(thumbnailFilename+ ";" + paintingName + ";" + paintingNumber);
	var paintingFileName = getPaintingFilename(paintingNumber,category);
	paintingFileName = thumbDirTag + "/" + paintingFileName;
	var paintingName = getPaintingName(paintingNumber,category);

  	document.write("<TD ALIGN=CENTER><a href = painting.html?" + paintingTag + paintingNumber + "," + categoryTag + category);
  	document.write("><IMG SRC='" +  paintingFileName +  "' alt='" + paintingName + "'   BORDER=0></TD>");

}

function getVariable(defaultVal, tagStr)
{
//alert ("tag:"+tagStr);
  
  var searchStr = window.location.search;
  var index = searchStr.indexOf(tagStr);
  if (index >= 0)
  {
	var tempStr =  searchStr.substring(index+tagStr.length+1);
	//alert("1st:"+tempStr);
	var index2 = tempStr.indexOf(",");
	if (index2 >= 0)
		tempStr = tempStr.substring(0,index2);
	//alert("2nd:"+tempStr);		
	return tempStr;
  }
  else return(defaultVal);
}

///////////////////////
//The method displays an individual painting
function showPainting(paintingNumber,category)
{
	
	var paintingNumber = paintingNumber-0;
	var category = getVariable("1", "cat")-0;
	var paintingFileName = getPaintingFilename(paintingNumber,category);
	paintingFileName = imageDirTag + "/" + paintingFileName;
	var paintingName = getPaintingName(paintingNumber, category);
	var paintingSize = getPaintingSize(paintingNumber, category);
	var paintingMaterial = getPaintingMaterial(paintingNumber, category);
	var galleryUrl = "gallery.html?" + categoryTag + category;
	
    //Set up prev and next 
    var totalPaintings = getNumberOfPaintingsForCat(category)-0;
	var prev = 0;
	var next = 0;
	
	
	if (totalPaintings > 1)
	{
	    if (paintingNumber == 0)
		{
			prev =  totalPaintings - 1;
			next = paintingNumber+ 1;
		}
		else if (paintingNumber == totalPaintings - 1)		
		{
	    	next = 0;
			prev =  paintingNumber - 1;
		}
		else
		{
			next = paintingNumber + 1;
			prev =  paintingNumber - 1;
		
		}
	}

	
	var prevUrl = "painting.html?" + paintingTag + prev + "," + categoryTag + category;
	var nextUrl = "painting.html?" + paintingTag + next + "," + categoryTag + category;
	//alert("total:"+totalPaintings);
	//alert("cur:"+paintingNumber);
	//alert("prev:"+prevUrl);
	//alert("next:"+nextUrl);
	//alert(paintingFileName);
	writeHeader("painting");
	
	document.write("<table class='nrcTable' width='850' border='0' cellpadding='0' cellspacing='0' height='100%' >");
    document.write("<tr>");
    document.write("<td width='30'></td>");
    document.write("<td valign='top' align='center' width='600'><img src='" + paintingFileName + "'/></td>");
	document.write("<td width='25'></td>");
		
	//-->Title and navigation table
	document.write("<td valign='top' >");
	document.write("<table cellpadding='0' cellspacing='0' border='0'>");
	document.write("<tr><td width='100'>&nbsp;</td></td><tr/>");
	
	//Painting name
   	document.write("<tr><td align='center' valign='top' width='175' height='50' class='navInfo'>" + paintingName + "</td></tr>");
	
	//Painting Material
    document.write("<tr><td align='center' class='navInfo'>"+paintingMaterial+"</td></tr>");
	
	//Painting size
	document.write("<tr><td align='center' class='navInfo'>"+paintingSize+"</td></tr>");
	document.write("<tr><td align='center' >&nbsp;</td></tr>");
	
	//Gallery link line 1
	document.write("<tr><td align='center'>");
	document.write("<a href='" + galleryUrl + "' title='Gallery' class='link1'>" + getGalleryCol(category,1) + "</a>");
	document.write("</td></tr>");
	//Gallery link line 2
	document.write("<tr>");
	document.write("<tr><td align='center'>");
	document.write("<a href='" + galleryUrl + "' title='Gallery' class='link1'>" + getGalleryCol(category,2) + "</a>");
	document.write("</td></tr>");
	
	//Buttons
	document.write("<tr>");
	document.write("<td  align='center'>");
	document.write("<a href='" + prevUrl + "' title='Prev' >");
    document.write("<img src='Utility/previous.gif' alt='Prev' border='0'/>");
	document.write("</a>"); 
	document.write("<a href='" + nextUrl + "' title='Next' >");
    document.write("<img src='Utility/next.gif' alt='Next' border='0'/>");
	document.write("</a>"); 
	document.write("</td>");
    document.write("</tr>");
	
	document.write("</table>");
	document.write("</td>");
	//-->End Title and navigation table
	
	document.write("</tr></table>");
	
}

//This method displays artist name/ navigation headrer
function writeHeader(pageType)
{
	
	document.write("<table  border='0' class='nrcTable' width='850'>");
	document.write("<tr><td height='5'><td/><tr/>");
	document.write("<tr><td  class='artistName' >&nbsp;&nbsp;NORA CHARNEY ROSENBAUM</td></tr>");
	document.write("<tr><td><hr/></td></tr>");
	document.write("<tr>");
   document.write("<td align='right'><a href='index.html'  class='link1' title='Home'>HOME</a>   		&nbsp;&nbsp;");
   
   if (pageType != "galleries")
		document.write("<a href='galleries.html'  class='link1' title='Galleries'>GALLERIES</a>&nbsp;&nbsp;");
	if (pageType != "statement")
		document.write("<a href='statement.html'  class='link1' title='Statement'>STATEMENT</a>&nbsp;&nbsp;");
			if (pageType != "resume")
		document.write("<a href='resume.html'  class='link1' title='Resume'>RESUME</a>&nbsp;&nbsp;");
	if (pageType != "contact")
		document.write("<a href='contact.html'  class='link1' title='Contact'>CONTACT</a>&nbsp;&nbsp;");
	if (pageType != "links")
		document.write("<a href='links.html'  class='link1' title='Links'>LINKS</a>&nbsp;&nbsp&nbsp;&nbsp&nbsp;&nbsp");		
	document.write("</td></tr>");
	document.write("<tr><td height='15'><td/><tr/>");
	document.write("</table>");
	
}

//This method writes the name of a Gallery. If 0 is passed as the gallery number, "GALLERY" is
//written. 
function writeGalleryName(category)
{
	var name;
	if (category == 0)
		name = "GALLERIES";
	else
		name = getGalleryCol(category,1) +  getGalleryCol(category,2);
			
	document.write("<table border='0' width='850' class='nrcTable' >");
	document.write("<tr><td  class='artistName' align='right'>"+name+"&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>");
	document.write("</table>");
}

//This method writes the name of a Gallery. If 0 is passed as the gallery number, "GALLERY" is
//written. 
function writeCaption(caption)
{
				
	document.write("<table border='0' width='850' class='nrcTable' >");
	document.write("<tr><td  class='artistName' align='right'>"+caption+"&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>");
	document.write("</table>");
}
