var ViewWindow = 0;

function ClearView()
{
   if( ViewWindow != 0 && !ViewWindow.closed )
      ViewWindow.close();
}

function ViewImage( idstr, width, height )
{
   ClearView();
   if( navigator.appName == "Microsoft Internet Explorer" ) 
   {
      width += 20;
      height += 25;
   }
   ViewWindow = window.open( "images/" + idstr + ".jpg", "Image" , "status=false, width=" + width + ", height=" + height );
   ViewWindow.focus();
}

function TableItem( filestr, picwidth, picheight, description )
{
   this.filestr = filestr;
   this.picwidth = picwidth;
   this.picheight = picheight;
   this.description = description;
}

function TableParams( ncolumns, colordark, colorlight, thumbwidth, thumbheight, border, center, text )
{
   this.ncolumns    = ncolumns;
   this.colordark   = colordark;
   this.colorlight  = colorlight;
   this.thumbwidth  = thumbwidth;
   this.thumbheight = thumbheight;
   this.border      = border;
   this.center      = center;
   this.text        = text;
   this.columnwidth = Math.round( 100 / ncolumns );
}

function WriteField( str )
{
   document.write( "<BR>" );
   document.write( "<FONT FACE=\"Comic Sans MS, Verdana, Arial, Helvetica\">" );
   if( str.length > 0 )
      document.write( str );
   else
      document.write( "&nbsp;" );
   document.write( "</FONT>" );
}

function DisplayImage( Item, Params )
{
   document.write( "<A HREF=\"images/" + Item.filestr + ".jpg\"" 
                   + "onClick=\"ViewImage( '"
                   + Item.filestr
                   + "', "
                   + Item.picwidth + ", " + Item.picheight
                   + " ); return false;\"><IMG SRC=\"thumbnails/"
                   + Item.filestr
                   + "_t.jpg\" WIDTH=" + Params.thumbwidth + " HEIGHT=" + Params.thumbheight + " BORDER=" + Params.border + " ALT=\""
                   + Item.filestr
                   + ".jpg - Click to enlarge.\"></A>" );
}

function WriteItem( Item, Params )
{
   document.write( "<TD VALIGN=\"TOP\" WIDTH=\"" + Params.columnwidth + "\%\"><SMALL>");
   if( Params.center )
      document.write( "<CENTER>" );
   DisplayImage( Item, Params );
   if( Params.center )
      document.write( "</CENTER>" );
   if( Params.text )
      WriteField( Item.description );
   document.write( "</SMALL></TD>" );
}

function CreateTable( ItemList, Params )
{
   nfullrows = Math.floor( ItemList.length / Params.ncolumns );
   document.write( "<P><TABLE WIDTH=\"100%\" BORDER=1 BORDERCOLORDARK=\"" + Params.colordark + "\" BORDERCOLORLIGHT=\"" + Params.colorlight + "\">" );
   for( count = 0; count < nfullrows * Params.ncolumns; count++ )
   {
      if( count % Params.ncolumns == 0 )
         document.write( "<TR>" );
      WriteItem( ItemList[count], Params )
      if( ( count + 1 ) % Params.ncolumns == 0 )
         document.write( "</TR>" );
   }
   nlast  = ItemList.length % Params.ncolumns;
   nempty = Params.ncolumns - nlast;
   if( nlast > 0 )
   {
      document.write( "<TR>" );
      for( i = 0; i < Params.ncolumns; i++ )
         if( ( ( Params.ncolumns + nlast ) % 2 == 0 && i < Math.floor( nempty / 2 ) ) ||
             ( ( Params.ncolumns + nlast ) % 2 != 0 && i < Math.floor( nempty / 2 ) - 1 ) || 
             ( Params.ncolumns % 2 != 0 && nlast % 2 == 0 && i == Math.floor( Params.ncolumns / 2 ) ) ||
             count >= ItemList.length
           )
            document.write( "<TD>&nbsp;</TD>" );
         else
            WriteItem( ItemList[count++], Params )
      document.write( "</TR>" );
   }
   document.write( "</TABLE>" );
}

