/***********************************************************
* gamedisplay.js                                   by Ralp *
*  Contains functions for drawing specific things, like    *
*  the Playing Board itself.  Image preloading is in here  *
*  too, since that seems awfully display-oriented to me.   *
*  Animated stuff is in a different source file.           *
*  (animate.js, of course)                                 *
***********************************************************/

var pieceImage  = new Array("down.png",  "up.png", "green.png", "yellow.png", "red.png", "xdown.png", "xup.png", "blank.png");

function newLevelNode(lev) {
    var ulEl, liEl, aEl, textEl, spanEl, text2El;
    ulEl = getId("levlist");
    liEl = create("li");
    aEl = create("a");
    aEl.href = "#";
    aEl.className = "tooltip";
    aEl.onclick = new Function("startLevel("+lev+")");
    liEl.appendChild(aEl);
    textEl = document.createTextNode(lev+1);
    aEl.appendChild(textEl);
    spanEl = create("span");
    spanEl.id = "best"+lev;
    aEl.appendChild(spanEl);
    text2El = document.createTextNode("Best score: ??");
    spanEl.appendChild(text2El);

    ulEl.appendChild(liEl);
}

function drawBoard() {
    var boardEl, tbodyEl, rowEl, tdEl, imgEl;
    boardEl = getId("board");
    tbodyEl = boardEl.firstChild;
    boardEl.removeChild(tbodyEl);
    tbodyEl = create("tbody");
    for (var x=0; x<maxX; x++) {
        rowEl = create("tr");
        tbodyEl.appendChild(rowEl);
        for (var y=0; y<maxY; y++) {
            tdEl = create("td");
            rowEl.appendChild(tdEl);
            imgEl = create("img");
            imgEl.id = "i"+x+y;
            imgEl.src = pieceImage[ board[x][y] ].src;
            imgEl.setAttribute("alt", "");
            /* We set alt for XHTML conformance. But not to anything
               useful because IE very annoyingly and incorrectly pops
               up the alt text.  (You guessed it: triplefuck IE.) */
            imgEl.onclick = push;
            if (maxX > 4 || maxY>4) {
                imgEl.setAttribute("width", "96");
                imgEl.setAttribute("height", "96");
            } else {
                imgEl.setAttribute("width", "128");
                imgEl.setAttribute("height", "128");
            }
            tdEl.appendChild(imgEl);
        }
        boardEl.appendChild(tbodyEl);
    }
}

function toggle(x,y) {
    if (x>=0 && x<maxX && y>=0 && y<maxY) {
        board[x][y] = newPiece[ board[x][y] ];
        getId("i"+x+y).src = pieceImage[ board[x][y] ].src;
    }
}
function untoggle(x,y) {
    if (x>=0 && x<maxX && y>=0 && y<maxY) {
        board[x][y] = oldPiece[ board[x][y] ];
        getId("i"+x+y).src = pieceImage[ board[x][y] ].src;
    }
}

function preload(images) {
    /*var argv=preload.arguments, argc=argv.length, preloads = new Array();*/
    var n = images.length;
    for (var i=0; i<n; i++) {
        var temp = images[i];
        images[i] = new Image();
        images[i].src = temp;
    }
}
