
//
// matrix.js
//
// Coypright 2008 by Thomas Hammel. All rights reserved.
//


var PhotoSlot = new Array(new Array(0,0,0,0,0,0,0,0,0,0),new Array(0,0,0,0,0,0,0,0,0,0));

var CaptionSlot = new Array(new Array(0,0,0,0,0,0,0,0,0,0),new Array(0,0,0,0,0,0,0,0,0,0));

var RefSlot = new Array(new Array(0,0,0,0,0,0,0,0,0,0),new Array(0,0,0,0,0,0,0,0,0,0));

var TdSlot = new Array(new Array(0,0,0,0,0,0,0,0,0,0),new Array(0,0,0,0,0,0,0,0,0,0));

var NextRef=0

var BackRef=0;

var Nx=5;

var Ny=2;

var Nxh=5;

var Nyh=2;

var PhotoCurrent=0;

var Pwidth=240;

var Pheight=240;


function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function PhotoClear(ix,iy)
{
    PhotoSlot[iy][ix].src='button/none.gif'; 
    PhotoSlot[iy][ix].alt=' ';  
//    PhotoSlot[iy][ix].style.visibility='collapse';
    PhotoSlot[iy][ix].style.width=0;
    PhotoSlot[iy][ix].style.paddingLeft=0;
    PhotoSlot[iy][ix].style.marginLeft=0;
    PhotoSlot[iy][ix].style.paddingRight=0;
    PhotoSlot[iy][ix].style.marginRight=0;
    CaptionSlot[iy][ix].innerHTML='';
//    CaptionSlot[iy][ix].style.visibility='collapse';
    CaptionSlot[iy][ix].style.width=0;
    CaptionSlot[iy][ix].style.paddingLeft=0;
    CaptionSlot[iy][ix].style.marginLeft=0;
    CaptionSlot[iy][ix].style.paddingRight=0;
    CaptionSlot[iy][ix].style.marginRight=0;
//    TdSlot[iy][ix].style.visibility='collapse';
    TdSlot[iy][ix].style.width=1;
    TdSlot[iy][ix].style.paddingLeft=0;
    TdSlot[iy][ix].style.marginLeft=0;
    TdSlot[iy][ix].style.paddingRight=0;
    TdSlot[iy][ix].style.marginRight=0;
}


function PhotoLoadBlank(ix,iy)
{
    PhotoSlot[iy][ix].src='button/blank.gif';
    PhotoSlot[iy][ix].alt=' ';  
    CaptionSlot[iy][ix].innerHTML=' ';
//    PhotoSlot[iy][ix].visibility='visible'; 
//    CaptionSlot[iy][ix].visibility='visible';
//    TdSlot[iy][ix].style.visibility='visible';
    TdSlot[iy][ix].style.width=Pwidth;
    TdSlot[iy][ix].style.height=Pheight;
}


function PhotoLoad(ix,iy)
{
    var spot=ix+Nx*iy;
    var field=Photo[GalleryCurrent][spot+PhotoCurrent].split(" ",6);
    PhotoSlot[iy][ix].src=PhotoDir[GalleryCurrent] + 'small/' + field[0] + '.jpg'; 
    PhotoSlot[iy][ix].alt=field[0];  
    RefSlot[iy][ix].href='image.htm?image=' + field[0];
//    CaptionSlot[iy][ix].innerHTML='x';
    CaptionSlot[iy][ix].innerHTML=field[0];
//    PhotoSlot[iy][ix].width=Pwidth; 
//    PhotoSlot[iy][ix].visibility='visible'; 
//    CaptionSlot[iy][ix].visibility='visible';
//    TdSlot[iy][ix].style.visibility='visible';
    TdSlot[iy][ix].style.width=Pwidth;
    TdSlot[iy][ix].style.height=Pheight;
}


function Refresh()
{
    var ix, iy, spot;
    var button;

    for(ix=0; ix<Nxh; ix++)
    {
        for(iy=0; iy<Nyh; iy++)
        {
            if(ix>=Nx || iy>=Ny)
            {
                PhotoClear(ix,iy);
            }
            else
            {
                spot=ix+Nx*iy;
                if(spot+PhotoCurrent<Photo[GalleryCurrent].length)
                {
                    PhotoLoad(ix,iy);
                }
                else
                {
                    PhotoLoadBlank(ix,iy);
                }
            }
        }
    }
    //
    // set the back button action
    //
    button = PhotoCurrent-(Nx*Ny);
    if(button>=Photo[GalleryCurrent].length) button=0;
    if(button<0) button=Math.floor((Photo[GalleryCurrent].length-1)/(Nx*Ny))*(Nx*Ny);
    BackRef.href='matrix.htm?gallery=' + Gallery[GalleryCurrent] + '&image=' + button;
    //
    // set the back button action
    //
    button = PhotoCurrent+(Nx*Ny);
    if(button>=Photo[GalleryCurrent].length) button=0;
    if(button<0) button=Math.floor((Photo[GalleryCurrent].length-1)/(Nx*Ny))*(Nx*Ny);
    NextRef.href='matrix.htm?gallery=' + Gallery[GalleryCurrent] + '&image=' + button;

}


function Setup()
{
    Refresh();
}


function NextDirectory()
{
    PhotoCurrent+=(Nx*Ny)
    if(PhotoCurrent>=Photo[GalleryCurrent].length) PhotoCurrent=0;
    if(PhotoCurrent<0) PhotoCurrent=Math.floor((Photo[GalleryCurrent].length-1)/(Nx*Ny))*(Nx*Ny);

    Refresh();
}


function BackDirectory()
{
    PhotoCurrent-=(Nx*Ny)
    if(PhotoCurrent>=Photo[GalleryCurrent].length) PhotoCurrent=0;
    if(PhotoCurrent<0) PhotoCurrent=Math.floor((Photo[GalleryCurrent].length-1)/(Nx*Ny))*(Nx*Ny);

    Refresh();
}


function GalleryHighlight()
{
    GalleryButton.src="button/upw.gif";
}


function GalleryNormal()
{
    GalleryButton.src="button/up.gif";
}


function NextDirectoryHighlight()
{
    NextDirectoryButton.src="button/rightw.gif";
}


function NextDirectoryNormal()
{
    NextDirectoryButton.src="button/right.gif";
}


function BackDirectoryHighlight()
{
    BackDirectoryButton.src="button/leftw.gif";
}


function BackDirectoryNormal()
{
    BackDirectoryButton.src="button/left.gif";
}


// 
// Search for a particular gallery.
//
function GallerySearch(pname) 
{
    var j= -1;
    //
    //first check to see if it is an index
    //
    j=Math.floor(pname);
    if(j>=0 && j<PhotoDir.length)
    {
        return j;
    }
    //
    // then check to see if it matches a gallery name
    //
    for(j=0; j<PhotoDir.length; j++)
    {
        if(Gallery[j]==pname)
        {
            return j;
        }
    }
    //
    // didn't find it
    //
    return -1;
}

// 
// Search for a particular gallery.
//
function ImageSearch(pname) 
{
    var j= -1;
    //
    //first check to see if it is an index
    //
    j=Math.floor(pname);
    if(j>=0 && j<Photo[GalleryCurrent].length)
    {
        return j;
    }
    //
    // then check to see if it matches a gallery name
    //
    for(j=0; j<PhotoDir.length; j++)
    {
            for (i=0; i<Photo[j].length; i++) 
            {
                var field=Photo[j][i].split(" ",6);
                if(field[0]==pname)
                {
                    GalleryCurrent=j;
                    return i;
                }
            }
   }
    //
    // didn't find it
    //
    return -1;
}


function FindAndSet(pname)
{
    var j;
    //
    // first check to see if it matches a gallery name
    //
    for(j=0; j<PhotoDir.length; j++)
    {
        if(Gallery[j]==pname)
        {
            GalleryCurrent=j;
            PhotoCurrent=0;
            return;
        }
    }
    //
    // then see if it matches a specific image
    //
        for(j=0; j<PhotoDir.length; j++)
        {
            for (i=0; i<Photo[j].length; i++) 
            {
                var field=Photo[j][i].split(" ",6);
                if(field[0]==pname)
                {
                    GalleryCurrent=j;
                    PhotoCurrent=i;
                    return;
                }
            }
        }
}


function Load()
{
    PhotoSlot[0][0] = document.getElementById('s00');
    PhotoSlot[0][1] = document.getElementById('s01');
    PhotoSlot[0][2] = document.getElementById('s02');
    PhotoSlot[0][3] = document.getElementById('s03');
    PhotoSlot[0][4] = document.getElementById('s04');
//    PhotoSlot[0][5] = document.getElementById('s05');
//    PhotoSlot[0][6] = document.getElementById('s06');
//    PhotoSlot[0][7] = document.getElementById('s07');
//    PhotoSlot[0][8] = document.getElementById('s08');
//    PhotoSlot[0][9] = document.getElementById('s09');
    PhotoSlot[1][0] = document.getElementById('s10');
    PhotoSlot[1][1] = document.getElementById('s11');
    PhotoSlot[1][2] = document.getElementById('s12');
    PhotoSlot[1][3] = document.getElementById('s13');
    PhotoSlot[1][4] = document.getElementById('s14');
//    PhotoSlot[1][5] = document.getElementById('s15');
//    PhotoSlot[1][6] = document.getElementById('s16');
//    PhotoSlot[1][7] = document.getElementById('s17');
//    PhotoSlot[1][8] = document.getElementById('s18');
//    PhotoSlot[1][9] = document.getElementById('s19');

    CaptionSlot[0][0] = document.getElementById('c00');
    CaptionSlot[0][1] = document.getElementById('c01');
    CaptionSlot[0][2] = document.getElementById('c02');
    CaptionSlot[0][3] = document.getElementById('c03');
    CaptionSlot[0][4] = document.getElementById('c04');
//    CaptionSlot[0][5] = document.getElementById('c05');
//    CaptionSlot[0][6] = document.getElementById('c06');
//    CaptionSlot[0][7] = document.getElementById('c07');
//    CaptionSlot[0][8] = document.getElementById('c08');
//    CaptionSlot[0][9] = document.getElementById('c09');
    CaptionSlot[1][0] = document.getElementById('c10');
    CaptionSlot[1][1] = document.getElementById('c11');
    CaptionSlot[1][2] = document.getElementById('c12');
    CaptionSlot[1][3] = document.getElementById('c13');
    CaptionSlot[1][4] = document.getElementById('c14');
//    CaptionSlot[1][5] = document.getElementById('c15');
//    CaptionSlot[1][6] = document.getElementById('c16');
//    CaptionSlot[1][7] = document.getElementById('c17');
//    CaptionSlot[1][8] = document.getElementById('c18');
//    CaptionSlot[1][9] = document.getElementById('c19');

    RefSlot[0][0] = document.getElementById('x00');
    RefSlot[0][1] = document.getElementById('x01');
    RefSlot[0][2] = document.getElementById('x02');
    RefSlot[0][3] = document.getElementById('x03');
    RefSlot[0][4] = document.getElementById('x04');
//    RefSlot[0][5] = document.getElementById('x05');
//    RefSlot[0][6] = document.getElementById('x06');
//    RefSlot[0][7] = document.getElementById('x07');
//    RefSlot[0][8] = document.getElementById('x08');
//    RefSlot[0][9] = document.getElementById('x09');
    RefSlot[1][0] = document.getElementById('x10');
    RefSlot[1][1] = document.getElementById('x11');
    RefSlot[1][2] = document.getElementById('x12');
    RefSlot[1][3] = document.getElementById('x13');
    RefSlot[1][4] = document.getElementById('x14');
//    RefSlot[1][5] = document.getElementById('x15');
//    RefSlot[1][6] = document.getElementById('x16');
//    RefSlot[1][7] = document.getElementById('x17');
//    RefSlot[1][8] = document.getElementById('x18');
//    RefSlot[1][9] = document.getElementById('x19');


    TdSlot[0][0] = document.getElementById('t00');
    TdSlot[0][1] = document.getElementById('t01');
    TdSlot[0][2] = document.getElementById('t02');
    TdSlot[0][3] = document.getElementById('t03');
    TdSlot[0][4] = document.getElementById('t04');
//    TdSlot[0][5] = document.getElementById('t05');
//    TdSlot[0][6] = document.getElementById('t06');
//    TdSlot[0][7] = document.getElementById('t07');
//    TdSlot[0][8] = document.getElementById('t08');
//    TdSlot[0][9] = document.getElementById('t09');
    TdSlot[1][0] = document.getElementById('t10');
    TdSlot[1][1] = document.getElementById('t11');
    TdSlot[1][2] = document.getElementById('t12');
    TdSlot[1][3] = document.getElementById('t13');
    TdSlot[1][4] = document.getElementById('t14');
//    TdSlot[1][5] = document.getElementById('t15');
//    TdSlot[1][6] = document.getElementById('t16');
//    TdSlot[1][7] = document.getElementById('t17');
//    TdSlot[1][8] = document.getElementById('t18');
//    TdSlot[1][9] = document.getElementById('t19');


    BackRef = document.getElementById('back');
    GalleryButton = document.getElementById('ButtonGallery');
//    BackDirectoryButton.onclick=BackDirectory;
    GalleryButton.onmouseover=GalleryHighlight;
    GalleryButton.onmouseout=GalleryNormal;
    GalleryNormal();
    
    BackRef = document.getElementById('back');
    BackDirectoryButton = document.getElementById('ButtonBackDirectory');
//    BackDirectoryButton.onclick=BackDirectory;
    BackDirectoryButton.onmouseover=BackDirectoryHighlight;
    BackDirectoryButton.onmouseout=BackDirectoryNormal;
    BackDirectoryNormal();
    
    NextRef = document.getElementById('next');
    NextDirectoryButton = document.getElementById('ButtonNextDirectory');
//    NextDirectoryButton.onclick=NextDirectory;
    NextDirectoryButton.onmouseover=NextDirectoryHighlight;
    NextDirectoryButton.onmouseout=NextDirectoryNormal;
    NextDirectoryNormal();

//    par=gup('gallery');
//    if(par!="") GalleryCurrent=Math.floor(par);

    par=gup('gallery');
//    if(par!="") GalleryCurrent=Math.floor(par);
    if(par!="") GalleryCurrent=GallerySearch(par);
    if(GalleryCurrent<0 || GalleryCurrent>=PhotoDir.length) GalleryCurrent=0;

    par=gup('image');
    if(par!="") 
    {
//        PhotoCurrent=Math.floor(par);
        PhotoCurrent=ImageSearch(par);
        if(PhotoCurrent>=Photo[GalleryCurrent].length) PhotoCurrent=0;
        if(PhotoCurrent<0) PhotoCurrent=Math.floor((Photo[GalleryCurrent].length-1)/(Nx*Ny))*(Nx*Ny);
    }


    par=gup('name');
    if(par!="") FindAndSet(par);

    Nx=Mx[GalleryCurrent];
    if(Nx<0) Nx=1;
    if(Nx>Nxh) Nx=Nxh;

    Ny=My[GalleryCurrent];
    if(Ny<0) Ny=1;
    if(Ny>Nyh) Ny=Nyh;

    Setup();
}


onload = function()
{ 
    Load();
}



