/*	This file serves as the primary JS include file for the main set of pages, and serves to:


	1) Store the strings for the navmenu HTML, etc.  Now formatted nicely as well as functional, thanks to the magic of the 'with' statement. ^_^  Modified and implemented as a single function that makes use of the 'nesting' variable

	2) House the functions for the auto-creation of the link pages for icons

	3) House a function that generates a string of concatenated '../' strings.  Function accepts any integer value; is thus more flexible than if it used strictly the 'nesting' variable

	4) Hold any generalized functions for site-wide use

	5) Hold class definitions (Submenu so far) and such, cleaning up iconset.js

*/


function navmenuInsert()
{

	var neString = neStringCreate(nesting);

	with(document)
	{
		write('<CENTER><TABLE BORDER="3" CELLPADDING="20" WIDTH="100" ');
		writeln('BORDERCOLOR="#000055"><TR><TD ALIGN="CENTER">');
		writeln('<BR>');
		writeln('<FONT FACE="Garamond,Haettenschweiler" SIZE="5">');
		writeln('<A HREF="' + neString + 'index.htm">Home</A><BR><BR>');
		writeln('<A HREF="' + neString + 'aboutme.htm">About Me</A><BR><BR>');
		writeln('<A HREF="' + neString + 'comics.htm">Online Comics</A><BR><BR>');
		writeln('<A HREF="' + neString + 'links.htm">Links</A><BR><BR>');
		writeln('<A HREF="' + neString + 'icons.htm">Buddy Icons</A><BR><BR>');
		//writeln('<A HREF="' + neString + 'resume.htm">R&eacute;sum&eacute;</A><BR><BR>');
		writeln('</FONT></TD>');
		writeln('</TR>');
		writeln('</TABLE></CENTER>');
	}

}

function iconLinkTable()
{
	var currentCell = 0;
	var numRows = rowSizes.length;
	var neString = neStringCreate(nesting);
	var j = 0;	//	Overall picture counter

	document.write('<TABLE BORDER="0" CELLSPACING="20" CELLPADDING="2" ');
	document.write('ALIGN="CENTER" WIDTH="660">');
	document.write('<TR>');

	for( var i = 0 ; i < 16 ; i++ )
	{ with(document){	// Loop inserts blank cells to 'hang' rest of table
		write('<TD WIDTH="20"><IMG SRC="');
		write(neString + 'pics/transparent.gif" ');
		write('BORDER="0" WIDTH="20" HEIGHT="3"></TD>');
	}}

	document.write('</TR>');

	for( var i = 0 ; i < numRows ; i++ )
	{	//		Loop controlling stepping down rows
		document.writeln('<TR>');

		switch(rowSizes[i])
		{
		case 2: case 3:
			with(document)
			{
				write('<TD COLSPAN="' + (8 - 2 * rowSizes[i]) + '">');
				write('<IMG SRC="' + neStringCreate(nesting));
				write('pics/transparent.gif" BORDER="0" WIDTH="3" ');
				writeln('HEIGHT="3"></TD>');
			}
		default:
		}
		for( var k = 0 ; k < rowSizes[i] ; k++ / j++ )
		{	//		Loop controlling stepping across rows
			with(document)
			{
				write('<TD ALIGN="CENTER" VALIGN="TOP" COLSPAN="4">');
				writeln('<BR>');
				writeln('<FONT SIZE="5">');
				write('<A HREF="' + iconTargetURL(picLinks[j]) + '">');
				write('<IMG SRC="' + picLinks[j] + '.gif" ');
				write('BORDER="0" ALT="' + titles[j].replace('<BR>', ' ') + '" ');
				write('WIDTH="50" HEIGHT="50"><BR>' + titles[j] + '</A>');
			}
			switch(titles[j].search('<BR>'))
			{
			case -1:		// no hard return in title
				document.write('<BR><BR>');
			default:
				document.writeln('</FONT></TD>');
			}
		}
		document.write('</TR>');
	}
	document.writeln('</TABLE>');  // Adding automated 'Back' link
	if( nesting > 0) {
		document.write('<BR><BR><CENTER><SPAN CLASS="back-link">');
		document.write('<A HREF="javascript:navUp(1);">');
		document.write('Back</A></SPAN></CENTER>');
	}
}


function iconTargetURL(st)
{	// Pulls target URL from picture filename
	return st.substr(0,st.indexOf('/',st.indexOf('/') + 1)).concat('.htm');
}	// Can search forward (with indexOf) because picLinks is provided as relative


function neStringCreate(depth)
{	// Function returns the indicated depth of 'parent folder' references
	var workString = '';
	for( var i = 0 ; i < depth ; i++ )
	{ workString = workString.concat('../'); }
	return workString;
}

function navUp(pgs)
{	// Function to navigate up the icons menu tree
	var st = location.href;	
	
	for ( var i = 0 ; i < pgs ; i++ )
	{
		st = st.substr(0, st.lastIndexOf('/'));
	}
	location.href = st.concat('.htm');
}



function spaces(width)
{	//	Function takes numerical parameter, writes multiple '&nbsp;' text
	for( var i = 0 ; i < width ; i++)
	{	document.write('&nbsp;');	}
}

function padNum(num, width)
{	// Takes any numerical input and desired field width and outputs formatted string
	var zeroes = new String;
	zeroes = '';
	
	switch(num)
	{
	 case 0:
		var a = width - 1;
		break;
	// case 1:
	//	num++;
	 default:
	
	with(Math)
	{
		var a = width - ceil((log(num * 1.001)/LN10));
	}	// End with()
	}	//	End switch()
	
	for( var i = 0 ; i < a ; i++ )
	{
		zeroes = zeroes.concat('0');
	}
	
	return zeroes.concat(num.toString(10));
}

/*		****  SUBMENU Object Definition  ****	*/

function Submenu(num, root, pgNam, pgDesc) 
{
	this.numPages = num;
	this.pageNames = pgNam;
	this.pageDesc = pgDesc;
	this.linkRoot = root;
}

function Submenu.prototype.insertHeader(pageNum)
{
	document.write('<CENTER><SPAN CLASS="submenu-header">');
	document.write(this.pageDesc[pageNum]);
	document.writeln('</SPAN></CENTER><BR>');
}

function Submenu.prototype.drawTable(pageNumber)
{		// Function cannot handle numPages == 1 -- DON'T DO THAT!
	var numThrees = Math.floor(this.numPages / 3) - ((this.numPages % 3) % 2);
	var numTwos = (1/2) * ((-3)*Math.pow(this.numPages % 3,2) + 7 * (this.numPages % 3));
		// Regressed quadratic function for {0,1,2} -> {0, 2, 1}
	var n = 0; // Total iterator
	
	navUpPgs++;	//	For each submenu, increase number of pages 'Back' takes 
					//	you back.
	document.writeln('<CENTER><TABLE CLASS="submenu">');

	for( var i = 0 ; i < numThrees ; i++ )
	{
		document.write('<TR>');
		for( var j = 0 ; j < 3 ; j++ / n++ )
		{ with(document){
			write('<TD ALIGN="CENTER" COLSPAN="2" CLASS="submenu"><SPAN CLASS="submenu-links">');
			if( n == pageNumber)
			{ write(this.pageDesc[n]); }
			else {
			write('<A HREF="');
			write(this.linkRoot + this.pageNames[n] + '.htm');
			writeln('">' + this.pageDesc[n] + '</A>');
			}
			write('</SPAN></TD>');
		}}  // Two '}' because of 'with(document)'
		document.writeln('</TR>');
	}

	for ( var i = 0 ; i < numTwos ; i++ )
	{
		document.write('<TR><TD COLSPAN="1" CLASS="submenu">&nbsp;</TD>');  // add neString for transparent.gif?
		for( var j = 0 ; j < 2 ; j++ / n++ )
		{ with(document){
			write('<TD ALIGN="CENTER" COLSPAN="2" CLASS="submenu"><SPAN CLASS="submenu-links">');
			if( n == pageNumber)	//  If you're viewing the current page
			{ write(this.pageDesc[n]); }
			else {	//  If you're not viewing current page - output active link
			write('<A HREF="');
			write(this.linkRoot + this.pageNames[n] + '.htm');
			writeln('">' + this.pageDesc[n] + '</A>');
			}
			write('</SPAN></TD>');
		}}  // Two '}' because of 'with(document)'
		document.writeln('</TR>');
	}
	document.write('</TABLE></CENTER><BR><BR>');
}







/*			** Note: Formatting will be off as a result of the modification to tab size
function iconNavmenuInsert()
{
	document.write("<CENTER><TABLE BORDER=3 CELLPADDING=20 WIDTH=100 BORDERCOLOR=#000055><TR><TD ALIGN=CENTER><BR><FONT FACE=Garamond,Haettenschweiler SIZE=5><A HREF=../index.htm>Home</A><BR><BR><A HREF=../aboutme.htm>About Me</A><BR><BR><A HREF=../comics.htm>Online Comics</A><BR><BR><A HREF=../links.htm>Links</A><BR><BR><A HREF=../icons.htm>Buddy Icons</A><BR><BR><A HREF=../resume.htm>R&eacute;sum&eacute;</A><BR><BR></FONT></TD></TR></TABLE></CENTER>");  // Outdated, after the 'with(document)' statement below.

  with(document)
  {
	write('<CENTER><TABLE BORDER="3" CELLPADDING="20" WIDTH="100" ');
	writeln('BORDERCOLOR="#000055"><TR><TD ALIGN="CENTER">');
		  writeln('<BR>');
		  writeln('<FONT FACE="Garamond,Haettenschweiler" SIZE="5">');
		writeln('<A HREF="../index.htm">Home</A><BR><BR>');
		writeln('<A HREF="../aboutme.htm">About Me</A><BR><BR>');
		writeln('<A HREF="../comics.htm">Online Comics</A><BR><BR>');
		writeln('<A HREF="../links.htm">Links</A><BR><BR>');
		writeln('<A HREF="../icons.htm">Buddy Icons</A><BR><BR>');
		writeln('<A HREF="../resume.htm">R&eacute;sum&eacute;</A><BR><BR>');
		writeln('</FONT></TD>');
		writeln('</TR>');
	writeln('</TABLE></CENTER>');
  }

}
*/

/*
function stringCompress(squish)
{
	while(squish.indexOf("\"") != -1)
	{
		squish = squish.substr(0,squish.indexOf("\"") - 1) + squish.substr(squish.indexOf("\"")+1);
	}

	while(squish.indexOf("\'") != -1)
	{
		squish = squish.substr(0,squish.indexOf("\"") - 1) + squish.substr(squish.indexOf("\"")+1);
	}

	while(squish.indexOf("\t") != -1)
	{
		squish = squish.substr(0,squish.indexOf("\t") - 1) + squish.substr(squish.indexOf("\t")+1);
	}

	while(squish.indexOf("\n") != -1)
	{
		squish = squish.substr(0,squish.indexOf("\n") - 1) + squish.substr(squish.indexOf("\n")+1);
	}

	while(squish.indexOf("\r") != -1)
	{
		squish = squish.substr(0,squish.indexOf("\r") - 1) + squish.substr(squish.indexOf("\r")+1);
	}

	return squish.toLowerCase();
}
*/

// location.href = location.href.substr(0,location.href.lastIndexOf('/')+1) + 'chorale.html'
// function can be used to change active navigation
// consider altering and formalizing to allow access to parent folders
// self/parent for use in frames?