/*
Copyright (C) 2000, 2001 Steve Passiouras.
You may not copy this file or its contents.
You may not distribute this file or its contents in any way.
You may not modify this file or its contents.
You may not modify a copy of this file or its contents.
Basically, you can't do anything with this file without asking me (Steve Passiouras) first!
For further information, please contact steve@eschertiles.com

Now that's out of the way, I hope you have fun!
*/
var current = new Array(4);
current[0] = 0;	// TL = filenames[0]
current[1] = 0;	// TR = filenames[0]
current[2] = 0;	// BL = filenames[0]
current[3] = 0;	// BR = filenames[0]

var corners = new Array(4);
corners[0] = "TL";
corners[1] = "TR";
corners[2] = "BL";
corners[3] = "BR";

function MakeFilename(ind)
{
	var filename = "images/motifs/" + folder + "/" + prefix + ids[ind] + ".gif";
	return filename;
}

function ChangeImage(which, filename)
{
	var i;
	var exp;

	// do the image in the navigation box
	exp = 'document["' + corners[which] + '"].src = ' + '"' + filename + '"';
	eval(exp);

	// do the name in the navigation box
//	exp = 'document.ES.' + corners[which] + '_FILE.options.selectedIndex = ' + current[which];
//	exp = 'document.ES.' + corners[which] + '_FILE.value = "' + ids[current[which]] + '";';
	exp = 'document.ES.' + corners[which] + '_FILE.src = "images/ns_' + ids[current[which]] + '.gif";';
	eval(exp);

	// now do all the other images
	for (i = 0; i < num_cols * num_rows / 4; i++)
	{
		exp = 'document["' + corners[which] + '_' + i + '"].src = ' + '"' + filename + '"';
		eval(exp);
	}
}

// which is 0..3
function ChangePrev(which)
{
	// get the new src filename
	if (current[which] == 0)
		current[which] = num_aspects - 1;
	else
		current[which]--;
	var filename = MakeFilename(current[which]);

	ChangeImage(which, filename);
}

// which is 0..3
function ChangeNext(which)
{
	// get the new src filename
	if (current[which] == num_aspects - 1)
		current[which] = 0;
	else
		current[which]++;
	var filename = MakeFilename(current[which]);

	ChangeImage(which, filename);
}
/*
function SetFile(form, field, val, ind)
{
	var which;

	if (field == "TL_FILE")
		which = 0;
	else if (field == "TR_FILE")
		which = 1;
	else if (field == "BL_FILE")
		which = 2;
	else if (field == "BR_FILE")
		which = 3;

	current[which] = ind;
	var filename = MakeFilename(ind);
	ChangeImage(which, filename);
}
*/
function GetName(row, col)
{
	if (row % 2 == 0)		// it's a top row
	{
		if (col % 2 == 0)	// it's a left column
			return MakeFilename(current[0]);
		else
			return MakeFilename(current[1]);
	}
	else				// it's a bottom row
	{
		if (col % 2 == 0)	// it's a left column
			return MakeFilename(current[2]);
		else
			return MakeFilename(current[3]);
	}
}

//function OnRandom(form, field)
function OnRandom()
{
	var which;

	for (which = 0; which <= 3; which++)
	{
		current[which] = Math.floor(Math.random() * num_aspects);
		ChangeImage(which, MakeFilename(current[which]));
	}
}

// 'which' is 0..3
// 'nav' is:
// 0 -> dot
// 1 -> underscore
// 2 -> a
// 3 -> undescore a
// 4 -> 1
// 5 -> 2
// 6 -> 3
// 7 -> 4
// 8 -> previous
// 9 -> next
function NavImage(which, nav)
{
	var filename;

	if (nav == 8)
		ChangePrev(which);
	else if (nav == 9)
		ChangeNext(which);
	else if (nav >= 0 && nav <= 3)
	{
		current[which] = current[which] % 4 + nav * 4;
		filename = MakeFilename(current[which]);
		ChangeImage(which, filename);
	}
	else if (nav >= 4 && nav <= 7)
	{
		nav -= 4;	// convert from 4..7 => 0..3
		current[which] += (nav - current[which] % 4);
		filename = MakeFilename(current[which]);
		ChangeImage(which, filename);
	}
}

//function OnGenerate(form, field)
function OnGenerate()
{
	var row, col;

	var windowname = 'e' + ids[current[0]] + ids[current[1]] + ids[current[2]] + ids[current[3]];
	var filename = windowname + '.html';
	var newwindow = open("", windowname);
	newwindow.document.writeln('<HTML>');
	newwindow.document.writeln('<HEAD>');
	newwindow.document.writeln('<TITLE>EscherTiles: ' + filename + '</TITLE>');
	newwindow.document.writeln('</HEAD>');
	newwindow.document.writeln('<BODY BGCOLOR="#FFFFFF">');
	newwindow.document.writeln('<TABLE ALIGN="CENTER" WIDTH="' + img_width * num_cols + '" HEIGHT="' + img_height * num_rows + '" CELLPADDING="0" CELLSPACING="0" BORDER="0">');
	for (row = 0; row < num_rows; row++)
	{
		newwindow.document.writeln('  <TR>');
		for (col = 0; col < num_cols; col++)
		{
			newwindow.document.write('    <TD>');
			newwindow.document.write('<IMG SRC="' + GetName(row, col) + '" WIDTH="' + img_width + '" HEIGHT="' + img_height + '" BORDER="0">');
			newwindow.document.writeln('</TD>');
		}
		newwindow.document.writeln('  </TR>');
	}

	newwindow.document.writeln('  <TR>');
	newwindow.document.writeln('    <TD ALIGN="CENTER" COLSPAN="' + num_cols + '">');
	newwindow.document.writeln('    <BR>Generated by EscherTiles V0.1<BR>');
	newwindow.document.writeln('    Copyright &#169; 2000, 2001 <A HREF="mailto:steve@eschertiles.com?subject=Generated by EscherTiles">Steve Passiouras</A>');
	newwindow.document.writeln('    </TD>');
	newwindow.document.writeln('  </TR>');

	newwindow.document.writeln('  <TR>');
	newwindow.document.writeln('    <TD ALIGN="CENTER" COLSPAN="' + num_cols + '">');
	newwindow.document.writeln('    <BR><CENTER><A HREF="http://www.passionelle.com.au/"><IMG BORDER="0" SRC="http://www.passionelle.com.au/cgi-bin/propaganda.cgi?site=eschertiles&page=' + filename + '"></A></CENTER><BR>');
	newwindow.document.writeln('    </TD>');
	newwindow.document.writeln('  </TR>');

	newwindow.document.writeln('</TABLE>');
	newwindow.document.writeln('</BODY>');
	newwindow.document.writeln('</HTML>');
}

function Initialise()
{
	var i;

	for (i = 0; i <= 3; i++)
	{
		var filename = MakeFilename(current[i]);
		ChangeImage(i, filename);
	}
}

function MakeTable()
{
	var row, col;

	document.writeln('<TABLE ALIGN="CENTER" BGCOLOR="#FFFFFF" WIDTH="' + img_width * num_cols + '" HEIGHT="' + img_height * num_rows + '" CELLPADDING="0" CELLSPACING="0" BORDER="0">');
	for (row = 0; row < num_rows; row++)
	{
		document.writeln('  <TR>');
		for (col = 0; col < num_cols; col++)
		{
			var t_or_b = row % 2 ? 'B' : 'T';
			var l_or_r = col % 2 ? 'R' : 'L';
			var num = Math.floor(row / 2) * (num_cols / 2) + Math.floor(col / 2);
			document.writeln('    <TD><IMG NAME="' + t_or_b + l_or_r + '_' + num + '" SRC="" WIDTH="' + img_width + '" HEIGHT="' + img_height + '" BORDER="0"></TD>');
		}
		document.writeln('  </TR>');
	}
	document.writeln('</TABLE>');
}
