var gActiveTab = "rules";
var gConst, gStatusImages, gGame, gUser;
function $(elementId) { return document.getElementById(elementId); }
function $E(tagName) { return document.createElement(tagName); }
function setCookie(name, value) {
document.cookie = name + "=" + escape(value) + "; expires=" + new Date(2222, 2, 2).toGMTString();
}
function getCookie(name, dfltVal) {
var s = document.cookie;
var i = s.indexOf(name + "=");
if (i >= 0) {
i += name.length + 1;
var j = s.indexOf(";", i);
dfltVal = unescape(s.substring(i, j > 0 ? j : s.length));
}
return dfltVal;
}
function getDate(unixTimestamp) {
var c = function(digit) { return (digit > 9 ? "" : "0") + digit; }
var d = new Date(unixTimestamp * 1000);
var today = new Date();
var result = (today.getFullYear() == d.getFullYear() &&
today.getMonth() == d.getMonth() &&
today.getDate() == d.getDate()) ? "Heute" :
c(d.getDate()) + "." + c(d.getMonth() + 1) + "." + (d.getFullYear() + "").substring(2);
return result + ", " + c(d.getHours()) + ":" + c(d.getMinutes());
}
function isCharChosen(charIdx) {
return gGame["chars"].indexOf(String.fromCharCode(charIdx + 97)) >= 0;
}
function isTermComplete() {
return gGame["term"].indexOf("*") == -1;
}
function isLastTerm() {
return gGame["termno"] == gConst["maxterms"];
}
function doHttpRequest(a) {
var http = window.XMLHttpRequest ? new XMLHttpRequest() :                  // browser dependent
new ActiveXObject("Microsoft.XMLHTTP"); // browser dependent
var url = "h.php?a=" + a;
var args = doHttpRequest.arguments;
for (var i=args.length-2; i>0; i-=2)
url += "&" + args[i] + "=" + escape(args[i + 1]);
http.open("GET", url + "&anticache=" + new Date().getTime(), false);
http.send(null);
result = eval("({" + http.responseText + "})");
if (result["status"] == "err")
alert("Error: " + result["message"]);
return result;
}
function handleAction(e) {
if (!e) e = event;                            // browser dependent
var obj = e.target ? e.target : e.srcElement; // browser dependent
if (obj.id == "ntb") {
if (isTermComplete() && !isLastTerm()) {
if (e.type == "mousedown") {
gGame = doHttpRequest("nt", "g", gGame["gameid"]);
updateGui();
} else {
obj.parentNode.className = e.type == "mouseout" ? "btn-n" : "btn-r";
}
}
} else if (obj.id == "ngb") {
if (e.type == "mousedown") {
obj.className = "btn-p";
if (confirm("Soll eine neue Spielrunde gestartet werden?"))
initGame();
} else {
obj.className = e.type == "mouseout" ? "btn-n" : "btn-r";
}
} else if (obj.id == "lookup") {
open("http://www.google.com/search?q=define%3A" + escape(gGame["term"]), "_blank");
} else if (obj.id == "categories2") {
if (gActiveTab == "highscore")
loadHighscore();
} else if (obj.id.charAt(0) == "c") {
var charIdx = parseInt(obj.id.substring(4));
if (!isCharChosen(charIdx) && !isTermComplete()) {
if (e.type == "mousedown") {
gGame = doHttpRequest(obj.id, "g", gGame["gameid"]);
updateGui();
if (isTermComplete() && isLastTerm())
finishGame();
} else {
obj.parentNode.className = e.type == "mouseout" ? "btn-n" : "btn-r";
}
}
} else {
if (obj.id.substring(4) != gActiveTab) {
if (e.type == "mousedown")
setActiveTab(obj.id.substring(4));
else
obj.parentNode.className = e.type == "mouseover" ? "tph-r" : "tph-i";
}
}
}
function setActiveTab(tabId) {
$("tph-" + gActiveTab).parentNode.className = "tph-i";
$(gActiveTab).style.display = "none";
gActiveTab = tabId;
$("tph-" + gActiveTab).parentNode.className = "tph-a";
$(gActiveTab).style.display = "";
}
function updateGui() {
var cats = $("categories").options;
for (var i=cats.length-1; i>=0; i--) {
if (cats[i].value == gGame["catid"]) {
$("catname").innerHTML = cats[i].text;
break;
}
}
$("termno").innerHTML = gGame["termno"];
$("score").innerHTML = gGame["score"];
for (var i=0; i<26; i++)
$("char" + i).parentNode.className = (isCharChosen(i) ? "btn-p" : "btn-n");
$("ntb").parentNode.className = isTermComplete() && !isLastTerm() ? "btn-n" : "btn-p";
var gsText;
var failsRemained = gConst["maxfails"] - gGame["fails"];
if (isTermComplete()) {
gsText = failsRemained > 0 ? "Begriff gel&ouml;st" : "Begriff leider NICHT gel&ouml;st";
if (isLastTerm())
gsText += ". Game over.";
$("lookup").style.display = "";
} else {
gsText = "Noch " + failsRemained + " Fehler bis zum Strick";
$("lookup").style.display = "none";
}
$("gamestatus").innerHTML = gsText;
$("statusimg").style.backgroundImage = "url(" + gStatusImages[gGame["fails"]].src + ")";
var tr = $E("tr");
var term = gGame["term"];
for (var i=0; i<term.length; i++) {
var c = term.charAt(i);
var td = $E("td");
if (c == " ") {
td.className = "spc";
} else {
var span = $E("span");
span.innerHTML = c == "*" ? "&nbsp;" : c == "-" ? "&ndash;" : c.toUpperCase();
var p = $E("p");
p.appendChild(span);
td.appendChild(p);
}
tr.appendChild(td);
}
var tbody = $E("tbody");
tbody.appendChild(tr);
var solPane = $("sol-pane");
solPane.replaceChild(tbody, solPane.lastChild);
}
function loadAndSwitchToHighscore(doSwitch) {
$("categories2").value = gGame["catid"];
loadHighscore();
if (doSwitch)
setActiveTab("highscore");
}
function loadHighscore() {
var hs = doHttpRequest("hs", "c", $("categories2").value).highscore;
var tbody = $E("tbody");
for (var i=0; i<gConst["maxhighscores"]; i++) {
var tr = $E("tr");
function appendCell(html, colNo) {
var td = $E("td");
td.innerHTML = html;
td.className = "hs-col" + colNo;
tr.appendChild(td);
}
appendCell(i + 1 + ".", 1);
if (i < hs.length) {
appendCell(hs[i][0], 2);
appendCell(hs[i][1], 3);
appendCell(getDate(hs[i][2]), 4);
} else {
for (var j=2; j<5; j++)
appendCell("-", j);
}
tbody.appendChild(tr);
}
var hsPane = $("hs-pane");
hsPane.replaceChild(tbody, hsPane.lastChild);
}
function initGame(gameid) {
gGame = doHttpRequest("ig", "g", gameid, "c", $("categories").value);
setCookie("gameid", gGame["gameid"]);
updateGui();
loadAndSwitchToHighscore();
}
function finishGame() {
var checkScore = doHttpRequest("cs", "g", gGame["gameid"]);
if (checkScore.result == 1) {
var name = gUser;
var promptMsg = "Herzlichen Gl\u00FCckwunsch!\n\nMit der erreichten Punktzahl haben Sie " +
"einen Platz in der Rekordliste der gew\u00E4hlten Wortkategorie errungen.\n\nBitte " +
"geben Sie Ihren Namen ein:";
do {
name = prompt(promptMsg, name);
promptMsg = "Der Name sollte nicht l\u00E4nger als " + gConst["maxusernamelength"] +
" Zeichen sein!";
} while (name != null && name.length > gConst["maxusernamelength"]);
if (name != null) {
gUser = name.length > 0 ? name : "Anonymous";
doHttpRequest("sn", "g", gGame["gameid"], "n", gUser);
setCookie("user", gUser);
loadAndSwitchToHighscore(true);
}
} else {
alert("G A M E  O V E R");
}
}
function startInit() {
// disable text selection on all elements. this doesn't work on opera
var body = document.getElementsByTagName("body")[0];
body.onselectstart = function() { return false; }; // ie
body.style.MozUserSelect = "none";                 // geckos
var initBtn = function(o) { o.onmousedown = o.onmouseover = o.onmouseout = handleAction; }
initBtn($("tph-game"));
initBtn($("tph-highscore"));
initBtn($("tph-rules"));
initBtn($("ngb"));
initBtn($("ntb"));
for (var i=0; i<26; i++)
initBtn($("char" + i));
$("lookup").onclick = handleAction;
$("categories2").onchange = handleAction;
$("main").style.backgroundImage = "url(img/hangman.png)";
gConst = doHttpRequest("gc");
gStatusImages = new Array(gConst["maxfails"] + 1);
for (var i=gConst["maxfails"]; i>=0; i--) {
gStatusImages[i] = new Image();
gStatusImages[i].src = "img/" + i + ".jpg";
}
gUser = getCookie("user", "");
var gameid = getCookie("gameid");
initGame(gameid);
if (gameid == gGame["gameid"])
$("categories").value = gGame["catid"];
setActiveTab("game");
$("splash").style.display = "none";
}
function init() {
$("splash").innerHTML = "Laden...";
setTimeout("startInit();", 0);
}

