var fileName = null;
var maxPhotos = 0;
var theImage = null;
var p = 0;
var preload_pos=0;
var updateMonitor=null;

function preload()
{
    if (preload_pos >= maxPhotos)
    {
         return;
    }
    var image = new Image();
    image.src = fileName+"/photo"+preload_pos+".jpg";
    updateMonitor.innerHTML = "Loading photo"+preload_pos+".jpg out of "+maxPhotos;
    preload_pos++;
    setTimeout("preload()",1000);
}
function initialize(fn,max,id,tid)
{
    updateMonitor = document.getElementById("update-monitor");
    fileName = fn;
    maxPhotos = max;

    //alert("fn="+fn+" ,max="+max+", id="+id+" , tid="+tid);
    theImage = document.getElementById(id);
    theImage.src = fileName+"/photo0.jpg";

    p=0;

    thumbnail = document.getElementById(tid);
    var children = thumbnail.childNodes;
    for(var i=children.length-1; i>=0; --i)
    {
        thumbnail.removeChild(children[i]);
    }

    var table = document.createElement("table");
    var tbody = document.createElement("tbody");
    var tr = document.createElement("tr");
    for(var i=0; i<max; ++i)
    {
        //var image = new Image();
        //image.src = fileName+"/thumbnail"+i+".jpg";
        var td = document.createElement("td");
        updateMonitor.innerHTML = "Loading thumbnail "+i;
        td.innerHTML = "<a href=\"javascript:viewImage("+i+")\"><image src="+fileName+"/thumbnail"+i+".jpg></a>";
        tr.appendChild(td);
    }
    tbody.appendChild(tr);
    table.appendChild(tbody);
    thumbnail.appendChild(table);
    preload_pos=0;
}
function nextPhoto()
{
    if (p+1 >= maxPhotos)
    {
        return;
    }
    ++p;
    theImage.src = fileName+"/photo"+p+".jpg";
}
function previousPhoto()
{

    if (p-1 < 0)
    {
        return;
    }
    --p;
    theImage.src = fileName+"/photo"+p+".jpg";
}
function viewImage(i)
{
    p = i;
    theImage.src = fileName+"/photo"+p+".jpg";
}
