// Management of a mini gallery of photos.
//
// Author:  OrdinaSoft
//          Patrick Lanz
//          Lausanne
//          info@ordinasoft.ch
//
// First version: August 7, 2008


// Shows the mini gallery.
//  - idElement:     id of the elementin which to display the gallery.
//  - NbRows:        number of rows in the gallery.
//  - NbColumns:     number of columns in the gallery.
//  - RotateTime:    rotation time, in seconds.
//  - HSpace:        horizontal space, in pixels.
//  - VSpace:        vertical space, in pixels.
//  - ThumbnailSize: size of the thumbnails.
//  - NoImages:      array with the number of the images.
//  - Captions:      array with the captions of the images.

function ShowMiniGallery (idElement, NbRows, NbColumns, RotateTime, HSpace, VSpace,
                          ThumbnailSize, NoImages, Captions) {

  // Wait for the element to be accessible
  var Elmt = document.getElementById (idElement);
  if (Elmt == null) {
    setTimeout (function () {
                  ShowMiniGallery (idElement, NbRows, NbColumns, RotateTime, HSpace, VSpace,
                                   ThumbnailSize, NoImages, Captions);
                  return true;
                }, 50);
    return false;
  } // Elmt == null

  // Initialize the table
  var s = [];
  s [s.length] = '<table border="0" cellpadding="0" cellspacing="0">';
  s [s.length] = '  <tbody>';
  for (var ixRow = 0; ixRow < NbRows; ixRow++) {
    s [s.length] = '    <tr>';
    for (var ixCol = 0; ixCol < NbColumns; ixCol++) {
      var ix = ixRow * NbColumns + ixCol;
      var No = NoImages [ix].toString ();
      s [s.length] =  '      <td style="height: ' + ThumbnailSize.toString () + 'px; ' +
                                       'width: ' + ThumbnailSize.toString () + 'px;" ' +
                                 'align="center" valign="middle">' +
                             '<img src=' +
                                       '"http://www.fivb.org/Vis2009/Images/GetImage.asmx' +
                                       '?No=' +  No +
                                       '&MaxSize=' + ThumbnailSize.toString () + '" ' +
                                   'style="cursor: pointer;" ' +
                                   'id="mg_' + No + '" ' +
                                   'title="' + Captions [ix] + '" ' +
                               'onclick="open (\'http://www.fivb.org/visasp/ShowImage.aspx?No=' +
                                           No + '\', \'\', \'resizable=yes,scrollbars=no\');" ' +
                      '/></td>';
      if (ixCol != (NbColumns - 1))
        s [s.length] = '      <td style="width: ' + HSpace.toString () + 'px;"></td>';
    }  // for each column
    s [s.length] = '    </tr>';
    if (ixRow != (NbRows - 1))
      s [s.length] = '    <tr style="height: ' + VSpace.toString () + 'px;"><td></td></tr>';
  }  // for each row
  s [s.length] = '  </tbody>';
  s [s.length] = '</table>';

  // Show the gallery
  Elmt.innerHTML = s.join ('\n\r');

  // Starts the rotation of the photos
  setTimeout (function () {
                MiniGalleryRotate (RotateTime, ThumbnailSize, NbRows * NbColumns, NoImages,
                                   Captions);
                return true;
              },
              1000 * RotateTime);

  return true;
}  // ShowMiniGallery


// Do the rotation of the images.
//  - RotateTime:    rotation time, in seconds.
//  - ThumbnailSize: size of the thumbnails.
//  - NbDisplayed:   number of displayed images.
//  - NoImages:      array with the number of the images.
//  - Captions:      array with the captions of the images.

function MiniGalleryRotate (RotateTime, ThumbnailSize, NbDisplayed, NoImages, Captions) {

  // Get the image to change
  var ix = Math.floor (Math.random () * NbDisplayed);

  // Get an image that is not displayed
  var ixImage = Math.floor (Math.random () * (NoImages.length - NbDisplayed) + NbDisplayed);

  // Updates the NoImages array
  var No = NoImages [ixImage];
  var OldNo = NoImages [ix];
  NoImages [ixImage] = OldNo;
  NoImages [ix] = No;

  // Updates the Captions array
  var Caption = Captions [ixImage];
  var OldCaption = Captions [ix];
  Captions [ixImage] = OldCaption;
  Captions [ix] = Caption;

  // Updates the image
  Elmt = document.getElementById ('mg_' + OldNo.toString ());
  Elmt.src = 'http://www.fivb.org/Vis2009/Images/GetImage.asmx' +
                '?No=' +  No +
                '&MaxSize=' + ThumbnailSize.toString ();
  Elmt.id = 'mg_' + No.toString ();
  Elmt.onclick = function () {
                   open ('http://www.fivb.org/visasp/ShowImage.aspx?No=' +
                           No.toString (), '', 'resizable=yes,scrollbars=no');
                   return true;
                 };
  Elmt.title = Caption;

  // Prepares the next rotation
  setTimeout (function () {
                MiniGalleryRotate (RotateTime, ThumbnailSize, NbDisplayed, NoImages, Captions);
                return true;
              },
              1000 * RotateTime);

  return true;
}  // MiniGalleryRotate
