// crossword.js
/**********************************************************
        Makes use of global variables specified within the
        corresponding HTML document:

	letters[][]   - array of letters making up the
	                crossword matrix
	width, height - dimensions of that matrix
**********************************************************/

basepath = "http://www.langenscheidt.de/lehrwerke_onlineprojekte/optimal_A1/";
DOM = (document.getElementById('DOM-Test')) ? true : false;
if (!DOM) alert ("Warnung: Ihre Browser unterstuetzt kein DOM. Es kann zu Beeintraechtigungen bei der Loesung bestimmter Aufgabentypen kommen.\n");

mouse_in = false;

// build "proper" array from the crossword matrix (we'll work with that one)

function DefItems() {

	Item = new Array();

	for (var 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 = basepath + "img/letters/normal/N_" + FormatLetter(letters[i][j]); // pre-defined
			Item[i][j].clk = false; // default value (not selected)
		}
	}
}

// return formated string of an item's coordinates ("id")

function GetItemID(i,j) {

	var x = String(i);
	var y = String(j);

	if (i < 10) x = "0" + x;
	if (j < 10) y = "0" + y;

	return x + y;
}

// function that joins the crossword's items into 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;
}

// print crossword

function PrintCrossword() {

	// head

	document.write("\t\t\t\t\t\t<TABLE BORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"0\" ALIGN=\"CENTER\">\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 img = FormatLetter(letters[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_" + img + "\" ID=\"" + item_id + "\" WIDTH=\"20\" HEIGHT=\"20\" NOSAVE  CLASS=\"charfield\">");
			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");
}

// retreive filename of image

function FormatLetter(letter) {

	var c_letter = letter.charCodeAt(0) - 64;

	if (c_letter > 0 && c_letter < 27) {

		var str = c_letter.toString(10);
		return (c_letter < 10) ? ("0" + str + ".gif") : (str + ".gif");
	}

	if (letter == 'Ä') return "27.gif";
	if (letter == 'Ü') return "28.gif";
	if (letter == 'Ö') return "29.gif";

	return "30.gif";
}

// update selection of item

function UpdateItem(id,obj) {

	var chunks = obj.src.split("/");

	var active = basepath + "img/letters/aktiv/A_" + chunks[chunks.length-1].slice(2,8);
	var inactive = basepath + "img/letters/normal/N_" + chunks[chunks.length-1].slice(2,8);

	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;
}

// various ways to call 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);
	// Netscape bug, using a work-around
	JoinItems();
}
