/*

  Functions to show/hide image in the cells of the homepage

  Requires:
    jquery.js

  Define elsewhere:
    nrRows            <- Number of rows
    nrColums          <- Number of columns
    dirs              <- Two dimensional array of directories for the thumbs
    titles            <- Two dimensional array of the directory titles
    links             <- Two dimensional array of the links
    nrthumbs          <- Two dimensional array of the number of alternative thumbs
    thumbs            <- Three dimensional array of the names of the thumbs

  v1.0 24/10/2009, Hayo Baan
       Initial version

  v2.0 18/07/2010, Hayo Baan
       Setup outside of html page
       Added randomisation of images shown

  v2.1 27/07/2010, Hayo Baan
       jQuery adaptations

  v2.2 25/10/2010, Hayo Baan
       Added Wildlife

  v3.0 20/12/2010, Hayo Baan
       Multiple images per element

  v3.1 01/01/2010, Hayo Baan
       Longer delay after completing set
       Not skipping empty elements anymore   

*/


// Initialise 
// Shown: a two dimensional array of thumb shown (-1 indicates not shown) 
var shown = new Array();
for (i=0; i<nrRows; i++) {
    shown[i] = new Array();
    for (ii=0; ii<nrColumns; ii++) {
        shown[i][ii] = -1;
    }
}


// Show/hide random image at position (curRow, curCol), then hop to next position
// Returns true when whole set has been completed
var curRow=0;
var curCol=0;
function showHideRandom() {
    var newSet = 0;
    if (Math.random()<0.8) {
        showElt(curRow, curCol);
    } else {
        hideElt(curRow, curCol);
    }
    //do {
        curCol++;
        if (curCol == nrColumns) {
            curCol = 0;
            curRow++;
            if (curRow == nrRows) {
                curRow = 0;
                newSet = 1;
            }
        }
    //} while (dirs[curRow][curCol] == "");
    return newSet;
}


// Show and hide random images and repeat after timeout
var timeout = 5000;
function showHideRandomTimed() {
    timeout = 100;
    if (showHideRandom()) {
        timeout = 5000;
    } 
    t = setTimeout("showHideRandomTimed()", timeout);
}


// Initialise the mouse and click functions of each cell
$(document).ready(function () {
    $("td[id^=elt]").click(clickElt);
    for (i = 0; i<nrRows*nrColumns; i++) {
        showHideRandom();
    }
    t = setTimeout("showHideRandomTimed()", timeout);
});


// Return the row from an element ID
function eltRow(id) {
    return parseInt(id.replace(/elt_/, "").replace(/_.*/, ""));
}


// Return the column from an element ID
function eltColumn(id) {
    return parseInt(id.replace(/elt_/, "").replace(/.*_/, ""));
}


// Return the element ID
function eltID(row, col) {
    return "elt_" + row + "_" + col;
}


// Show the cell image
function showElt(row,col) {
    if (dirs[row][col] != "") {
        prev = shown[row][col];
        do {
            thmb = Math.floor(Math.random()*nrthumbs[row][col]);
        } while (nrthumbs[row][col] > 1 && (prev == thmb || (col > 0 && (thumbs[row][0][shown[row][0]] == thumbs[row][col][thmb] || thumbs[row][col-1][shown[row][col-1]] == thumbs[row][col][thmb]))));
        file = thumbs[row][col][thmb] + ".jpg";
        if (dirs[row][col] != "/") {
            file = "/_Thumbs/" + file;
        }
        file = dirs[row][col] + file;
        $("#" + eltID(row,col)).css("backgroundImage", "url("+file+")");
        $("#" + eltID(row,col)).addClass("shown");
        shown[row][col] = thmb;
    }
}


// Hide the cell image
function hideElt(row,col) {
    if (dirs[row][col] != "") {
        $("#" + eltID(row,col)).css("backgroundImage", "");
        $("#" + eltID(row,col)).removeClass("shown");
        shown[row][col] = -1;
    }
}


// Follow the cell link
function clickElt() {
    row = eltRow(this.id);
    col = eltColumn(this.id);
    if (links[row][col] != "") {
        parent.location = links[row][col];
    }
}
