var images=new Array;
var cellchoices=new Array;
var lastfive=new Array;
var count;
var fullcount;
var cellcount;
var timer;

var cellFallPages=new Array;

function gotoCell(cellnum)
{
//	alert(cellnum+" "+cellFallPages[cellnum]);
	document.location.href=cellFallPages[cellnum];
}

function changeCell()
{
//	clearInterval(timer);
	if (count && cellcount)
	{
		var found;
		do
		{	
			found=1;
			cellnum=Math.round(Math.random()*cellcount);	
			if (cellnum>=cellcount)
				found=0;
			else if (cellcount>10)	
			{	
				for (i=0; i<5 && found; ++i)
				{
					if (cellnum==lastfive[i])
					{
						found=0;
					}
				}
			}	
		}
		while (!found);

		if (cellcount<=5)
			lastfive[cellcount-1]=cellnum;

		do
		{
			imagenum=Math.round(Math.random()*count);
		}
		while (imagenum>=count);
		var cell="cell"+cellchoices[cellnum];
		document[cell].src="images/thumbs/"+images[imagenum][0];	
		document[cell].title=images[imagenum][1];	
		cellFallPages[cellchoices[cellnum]]=images[imagenum][2];

		--count;
		temp=images[imagenum];
		images[imagenum]=images[count];
		images[count]=temp;

		--cellcount;
		temp=cellchoices[cellnum];
		cellchoices[cellnum]=cellchoices[cellcount];
		cellchoices[cellcount]=temp;

		if (cellcount==0)
			cellcount=15;
	}
	else
	{
		clearInterval(timer);
	}
}

function initMosaic()
{
    if ( window.XMLHttpRequest )
    {
        xml = new XMLHttpRequest();
    }
    else
    {
        try
        {
            xml = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xml = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (E)
            {
                document.location.href='mosaic2.shtml';
            }
        }
    }
    xml.open("GET","allthumbs",false);
    xml.send(null);
    lines=xml.responseText.split('\n');
    i=0;
    fullcount=count=lines.length-1;
    cellcount=15;
//    alert(lines.length-1);
    for (i=0; i<lines.length-1; ++i)
    {
        data=lines[i].split(';');
		images[i]=data;
    }
	for (i=0; i<15; ++i)
	{
		var cell="cell"+i;
		var n=Math.round(Math.random()*count);
		document[cell].src="images/thumbs/"+images[n][0];	
		document[cell].title=images[n][1];	
		--count;

		cellFallPages[i]=images[n][2];

		temp=images[n];
		images[n]=images[count];
		images[count]=temp;
		cellchoices[i]=i;
	}
	for (i=0; i<5; ++i)
	{
		lastfive[i]=-1;
	}

	timer=setInterval('changeCell()',1000);	
}


onload=initMosaic;

