// crossword.js
/**********************************************************
        Makes use of extern functions and variables
        which are unique for each crossword! Have a
        look at the corresponding HTML documents to
        get an idea.
**********************************************************/

// Definition of the items as JavaScript objects

function DefItems() {

	Item = new Array();

	for (i = 0; i < height; i++) {

		Item[i] = new Array();

		for (j = 0; j < width; j++) {

			Item[i][j]     = new Image(20,20);
			Item[i][j].src = GetLetter(i,j); // pre-defined
			Item[i][j].clk = false;          // default value (not clicked)
		}
	}
}

// Simple function which returns the ID-code to the appropriate item

function GetItemID(i,j) {

	var pt_a = "";
	var pt_b = "";

	if (i < 10) pt_a += "0";
	pt_a += String(i);

	if (j < 10) pt_b += "0";
	pt_b += String(j);

	return pt_a + pt_b;
}

// Function which joins all items to one string (passed to the Perl script)

function JoinItems() {

	var str = "";

	for (i = 0; i < height; i++) {

		for (j = 0; j < width; j++) {

			str += (Item[i][j].clk) ? "1" : "0";
		}
	}

	var ptr = (DOM) ? document.getElementById("cwstr") : document.exercises.cwstr;
	ptr.value = str;
}

// Output of the crossword

function CrossWord() {

	// head

	document.write("\t\t\t\t\t\t<TABLE BORDER=\"0\" CELLSPACING=\"1\" CELLPADDING=\"0\">\n");

	// body

	for (i = 0; i < height; i++) {

		document.write("\t\t\t\t\t\t\t<TR>\n");

		for (j = 0; j < width; j++) {

			var letter = GetLetter(i,j);
			var item_id = "item" + GetItemID(i,j);
			var arrstr = "Item[" + i + "][" + j + "]";

			document.write("\t\t\t\t\t\t\t\t<TD>\n");

			document.write("\t\t\t\t\t\t\t\t\t");
			document.write("<A HREF=\"#crossword\" ONMOUSEOVER=\"PaintItem('" + item_id + "'," + arrstr + ")\" ONMOUSEOUT=\"PaintItem('" + item_id + "'," + arrstr + ")\" ONCLICK=\"ModifyItem('" + item_id + "'," + arrstr + ")\">");
			document.write("<IMG SRC=\"" + basepath + "img/letters/normal/N_" + letter + "\" ID=\"" + item_id + "\" WIDTH=\"20\" HEIGHT=\"20\" NOSAVE BORDER=\"0\">");
			document.write("</A></TD>\n");
		}

		document.write("\t\t\t\t\t\t\t</TR>\n");
	}

	// foot

	document.write("\t\t\t\t\t\t</TABLE>\n");
}

// Update state of an item

function UpdateItem(id,obj) {

	var chunks = obj.src.split("/");

	var active = basepath + "img/letters/aktiv/A_" + chunks[chunks.length-1].slice(0,6);
	var inactive = basepath + "img/letters/normal/N_" + chunks[chunks.length-1].slice(0,6);

	var newimage = new Image(obj.width,obj.height);
	newimage.src = (mouse_in || obj.clk) ? active : inactive;

	var ptr = (DOM) ? document.getElementById(id) : document.images[id];
	ptr.src = newimage.src;
}

// Possible calls for upper function

function PaintItem(id,obj) {

	mouse_in = (mouse_in) ? false : true;
	UpdateItem(id,obj);
	window.status = (mouse_in) ? 'Klicke hier!' : ' ';
}

function ModifyItem(id,obj) {

	obj.clk = (obj.clk) ? false : true;
	UpdateItem(id,obj);
	// this seems necessary because of Netscape 6.x (wtf??)
	JoinItems();
}
