//============================================================
//                >> jsImagePlayer 1.0 <<
//            for Netscape3.0+, September 1996
//============================================================
//                  by (c)BASTaRT 1996
//             Praha, Czech Republic, Europe
//
// feel free to copy and use as long as the credits are given
//          by having this header in the code
//
//          contact: xholecko@sgi.felk.cvut.cz
//          http://sgi.felk.cvut.cz/~xholecko
//
//============================================================
// Thanx to Karel & Martin for beta testing and suggestions!
//============================================================
//
// Some changes to the appearence made by Kelly Dean at CSU/CIRA.  Jan 1997
//
//============================================================
// DESCRIPTION: preloads number of images named in format
//   "imagename#.ext" (where "#" stands for number and "ext"
//   for file extension of given filetype (gif, jpg, ...))
//   into cache and then provides usual movie player controls
//   for managing these images (i.e. frames). A picture
//   (loading.gif) is displayed while loading the movie.
//   To make it work just set up the variables below.
//   An image "loading.gif" is expected in the directory
//   where this page is located (it asks user to wait while
//   the animation is being downloaded).
//   Enjoy! BASTaRT. (it's spelled with a "T" ! :)
//  KNOWN BUG:  when page is located on a WWW Server that
//   cannot handle the POST form submit method, pressing Enter
//   after changing the frame number in the little input field
//   causes the page to reload and an alert window to appear.
//   This can be avoided by clicking with the mouse somewhere
//   outside the input field rather than hitting the Enter to
//   jump to the desired frame.
//
//**************************************************************************
//********* SET UP THESE VARIABLES - MUST BE CORRECT!!!*********************
image_dir     = "images/animation/";
image_default = "fi_photo_";
image_name    = image_dir + image_default;
                            //the base "path/name" of the image set without the numbers
image_type = "jpg";         //"gif" or "jpg" or whatever your browser can display

first_image = 1;            //first image number
last_image  = 3;            //last image number

   //!!! the size is very important - if incorrect, browser tries to
   //!!! resize the images and slows down significantly
animation_width  = 310;              //width of the images in the animation
animation_height = 200;              //height of the images in the animation

//**************************************************************************
//**************************************************************************



//=== THE CODE STARTS HERE - no need to change anything below ===
//=== global variables ====
theImages = new Array();
normal_delay = 4000;
delay = normal_delay;  //delay between frames in 1/100 seconds
delay_step = 10;
delay_max = 4000;
delay_min = 10;
current_image = first_image;     //number of the current image
timeID = null;
status = 0;            // 0-stopped, 1-playing
play_mode = 0;         // 0-normal, 1-loop, 2-swing
size_valid = 0;

//===> preload the images - gets executed first, while downloading the page (in body)
function slideshow_init(img_dflt, anim_wdth, anim_hght, lst_img)
{
  image_default    = img_dflt;
  animation_width  = anim_wdth;
  animation_height = anim_hght;
  last_image       = lst_img;

  //  message = "last image: " + last_image;
  //  alert(message);

  //===> makes sure the first image number is not bigger than the last image number
  if (first_image > last_image)
  {
     var help = last_image;
     last_image = first_image;
     first_image = help;
  };

  for (var i = first_image; i <= last_image; i++)
  {
     theImages[i] = new Image();
  //   theImages[i].onerror = my_alert("\nError loading ",image_name,i,image_type,"!");
  //   theImages[i].onabort = my_alert("\nLoading of ",image_name,i,image_type," aborted!");
     theImages[i].src = image_dir + image_default + "_" + i + "." + image_type;
  };
}

//===> stop the movie
function stop()
{
   if (status == 1) clearTimeout (timeID);
   status = 0;
}

//===> displays image depending on the play mode in forward direction
function animate_fwd()
{
   current_image++;
   if(current_image > last_image)
   {
      if (play_mode == 0)
      {
         current_image = last_image;
         status=0;
         return;
      };                           //NORMAL
      if (play_mode == 1)
      {
         current_image = first_image; //LOOP
      };
      if (play_mode == 2)
      {
         current_image = last_image;
         animate_rev();
         return;
      };
   };
   document.animation.src = theImages[current_image].src;
   document.control_form.frame_nr.value = current_image;
   timeID = setTimeout("animate_fwd()", delay);
}

//===> displays image depending on the play mode in reverse direction
function animate_rev()
{
   current_image--;
   if(current_image < first_image)
   {
      if (play_mode == 0)
      {
         current_image = first_image;
         status=0;
         return;
      };                           //NORMAL
      if (play_mode == 1)
      {
         current_image = last_image; //LOOP
      };
      if (play_mode == 2)
      {
         current_image = first_image;
         animate_fwd();
         return;
      };
   };
   document.animation.src = theImages[current_image].src;
   document.control_form.frame_nr.value = current_image;
   timeID = setTimeout("animate_rev()", delay);
}

//===> changes playing speed by adding to or substracting from the delay between frames
function change_speed(dv)
{

   delay+=dv;
   if(delay > delay_max) delay = delay_max;
   if(delay < delay_min) delay = delay_min;
}

//===> "play forward"
function fwd()
{
   stop();
   status = 1;
   animate_fwd();
}

//===> jumps to a given image number
function go2image(number)
{
   stop();
   if (number > last_image) number = last_image;
   if (number < first_image) number = first_image;
   current_image = number;
   document.animation.src = theImages[current_image].src;
   document.control_form.frame_nr.value = current_image;
}

//===> "play reverse"
function rev()
{
   stop();
   status = 1;
   animate_rev();
}

//===> changes play mode (normal, loop, swing)
function change_mode(mode)
{
   play_mode = mode;
}

//===> sets everything once the whole page and the images are loaded (onLoad handler in <body>)
function launch()
{
   stop();
   current_image = first_image;
   document.animation.src = theImages[current_image].src;
   document.control_form.frame_nr.value = current_image;
   // this is trying to set the text (Value property) on the START and END buttons
   // to S(first_image number), E(last_image number). It's supposed (according to
   // JavaScrtipt Authoring Guide) to be a read only value but for some reason
   // it works on win3.11 (on IRIX it doesn't).
   document.control_form.start_but.value = " F(" + first_image + ") ";
   document.control_form.end_but.value = " L(" + last_image + ") ";
   // this needs to be done to set the right mode when the page is manualy reloaded
   change_mode (0);
}

function animation()
{
  src = image_dir + "fi_logo" + "." + image_type;

    // HEIGHT=",animation_height, " WIDTH=", animation_width, "\"
  document.write(" <table class=imagePlayer width=",animation_width, "><tr height=",animation_height, " valign=center><td width=",animation_width, " align=center>");
  document.write("   <IMG NAME=\"animation\" SRC=\"", src, "\" ALT=\"[JavaScript Image Player]\">"); //"
  document.write(" </table>");

  document.write(" <FORM Method=POST Name=\"control_form\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Name=\"start_but\" Value=\"  First  \" onClick=\"go2image(first_image)\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" -1 \" onClick=\"go2image(--current_image)\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" REW \" onClick=\"rev()\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" STOP \" onClick=\"stop()\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" FWD \" onClick=\"fwd()\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" +1 \" onClick=\"go2image(++current_image)\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Name=\"end_but\" Value=\"   Last   \" onClick=\"go2image(last_image)\"> ");
  //document.write(" <BR> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" Once \" onClick=\"change_mode(0)\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" Repeat \" onClick=\"change_mode(1)\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" Sweep \" onClick=\"change_mode(2)\"> ");
  //document.write(" <BR> ");
  //document.write("    <B>Image #</B> ");
  document.write("    <INPUT TYPE=\"hidden\" NAME=\"frame_nr\" VALUE=\"0\" SIZE=\"4\" ");
  document.write("    onFocus=\"this.select()\" onChange=\"go2image(this.value)\"> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" Slower \" onClick=\"change_speed(delay_step)\"> ");
  //document.write("    <B>Speed</B> ");
  document.write("    <INPUT TYPE=\"hidden\" Value=\" Faster \" onClick=\"change_speed(-delay_step)\"> ");
  document.write(" </FORM> ");
}


function setDataType(cValue)
  {
    var isDate = new Date(cValue);
    if (isDate == "NaN")
      {
        if (isNaN(cValue))
          {
            cValue = cValue.toUpperCase();
            return cValue;
          }
        else
          {
            var myNum;
            myNum = String.fromCharCode(48 + cValue.length) + cValue;
            return myNum;
          }
        }
  else
      {
        var myDate = new String();
        myDate = isDate.getFullYear() + " " ;
        myDate = myDate + isDate.getMonth() + " ";
        myDate = myDate + isDate.getDate() + " ";
        myDate = myDate + isDate.getHours() + " ";
        myDate = myDate + isDate.getMinutes() + " ";
        myDate = myDate + isDate.getSeconds();
        return myDate ;
      }
  }
function sortTable(col, tableToSort, ReverseMe, totCols)
{
  var iCurCell = col + totCols;
  var totalRows = tableToSort.rows.length;
  var bSort = 0;
  var tColor = 0;
  var colArray = new Array();
  var oldIndex = new Array();
  var indexArray = new Array();
  var bArray = new Array();
  var newRow;
  var newCell;
  var i;
  var c;
  var j;

  var cellClassRoot = 'tbInfo_';
  var cellClass;

  for (i=1; i < tableToSort.rows.length; i++)
  {
    colArray[i - 1] = setDataType(tableToSort.cells(iCurCell).innerText);
    iCurCell = iCurCell + totCols;
  }

  for (i=0; i < colArray.length; i++)
  {
    bArray[i] = colArray[i];
  }

  colArray.sort();
  if (ReverseMe == 1)
  {
     colArray.reverse();
  }

  for (i=0; i < colArray.length; i++)
  {
    indexArray[i] = (i+1);
    for(j=0; j < bArray.length; j++)
    {
      if (colArray[i] == bArray[j])
      {
        for (c=0; c<i; c++)
        {
          if ( oldIndex[c] == (j+1) )
          {
            bSort = 1;
          }
        }
        if (bSort == 0)
        {
          oldIndex[i] = (j+1);
        }
          bSort = 0;
      }
    }
  }

  for (i=0; i<oldIndex.length; i++)
  {
    newRow = tableToSort.insertRow();
    cellClass = cellClassRoot + (i&1);

    for (c=0; c<totCols; c++)
    {
      newCell = newRow.insertCell();
      newCell.innerHTML = tableToSort.rows(oldIndex[i]).cells(c).innerHTML;
      newCell.align = tableToSort.rows(oldIndex[i]).cells(c).align;
      newCell.className = cellClass;
    }
  }

  for (i=1; i<totalRows; i++)
  {
    tableToSort.moveRow((tableToSort.rows.length -1),1);
  }

  for (i=1; i<totalRows; i++)
  {
    tableToSort.deleteRow();
  }
}


