
Picture = new Array;
Caption = new Array;

preloaded = new Array;
LoadedPicture = new Array;

imgdir = "../../Scripts/";

imgPrev = new Image(35, 25);
imgPrev.src = imgdir + "prev.gif";
imgPrev_ro = new Image(35, 25);
imgPrev_ro.src = imgdir + "prev_rollover.gif";
imgPrev_active = new Image(35, 25);
imgPrev_active.src = imgdir + "prev_active.gif";

imgPause = new Image(35, 25);
imgPause.src = imgdir + "pause.gif";
imgPause_ro = new Image(35, 25);
imgPause_ro.src = imgdir + "pause_rollover.gif";
imgPause_active = new Image(35, 25);
imgPause_active.src = imgdir + "pause_active.gif";

imgPlay = new Image(35, 25);
imgPlay.src = imgdir + "play.gif";
imgPlay_ro = new Image(35, 25);
imgPlay_ro.src = imgdir + "play_rollover.gif";
imgPlay_active = new Image(35, 25);
imgPlay_active.src = imgdir + "play_active.gif";

imgNext = new Image(35, 25);
imgNext.src = imgdir + "next.gif";
imgNext_ro = new Image(35, 25);
imgNext_ro.src = imgdir + "next_rollover.gif";
imgNext_active = new Image(35, 25);
imgNext_active.src = imgdir + "next_active.gif";


// These are local to the script file.
num_slides = 0;
first_slide = 0;
slideshow_playing = 0;
rotate_timer_id = "";

// Set the default interval to 5 seconds.
slideshow_interval = 5000;

// These function rely on the proper setup of the Picture
// and Caption arrays in the HTML file.

function add_slide(image_name, caption) {
  Picture[num_slides] = image_name;
  Caption[num_slides] = caption;

  // Indicate that the slide hasn't been preloaded yet.
  preloaded[num_slides] = 0;

  num_slides++;
}

function set_interval(interval) {
  slideshow_interval = interval;
}

function preload_all_slides() {
  for (j = 0; j < num_slides; j++) {
    preload_slide(j);
  }
}

function preload_slide(preload_slide_num) {

  if (preloaded[preload_slide_num] == 2)
    return;

  if (preloaded[preload_slide_num] == 0) {
    LoadedPicture[preload_slide_num] = new Image;
    LoadedPicture[preload_slide_num].src = Picture[preload_slide_num];
    preloaded[preload_slide_num] = 1;  
  } else {
    if (LoadedPicture[preload_slide_num].complete) {
      preloaded[preload_slide_num] = 2;
    }
  }

  // If the slide hasn't been loaded, then check again in 50 ms.
  if (preloaded[preload_slide_num] != 2) {
    setTimeout("preload_slide(" + preload_slide_num + ")", 50);
  }

}

function firstslide() {
  slide_num = first_slide;
  changeslide();
}

function start_slideshow() {
  slideshow_playing = 1;
  
  set_play_button();
  set_pause_button();

  slideshow_rotate("go");
}

function stop_slideshow() {
  slideshow_playing = 0;
  
  set_play_button();
  set_pause_button();

  if (rotate_timer_id != "") {
    clearTimeout(rotate_timer_id);
  }
}

function slideshow_rotate(strStart) {

  if (slideshow_playing) {
    if (strStart != "go") {
      slide_num = next_slide_num();
      changeslide();
    }

    if (strStart != "go") {
      rotate_timer_id = setTimeout("slideshow_rotate()", slideshow_interval);
    } else {
      rotate_timer_id = setTimeout("slideshow_rotate()", slideshow_interval / 2);
    }

    // Preload the next image.
    preload_slide(next_slide_num());
  }

}

function prevslide() {
  slide_num = prev_slide_num();
  changeslide();

  if (slideshow_playing)
    stop_slideshow();
}

function nextslide() {
  slide_num = next_slide_num();
  changeslide();

  if (slideshow_playing)
    stop_slideshow();
}

function next_slide_num() {
  cur_slide_num = slide_num + 1;
  if (cur_slide_num == num_slides) {
    cur_slide_num = first_slide;
  }

  return cur_slide_num;
}
function prev_slide_num() {
  if (slide_num == 0) {
    cur_slide_num = num_slides - 1;
  } else {
    cur_slide_num = slide_num - 1;
  }

  return cur_slide_num;
}


// This function changes the slide and the description box
// according to the slide_num
function changeslide(){

  // Make sure the slide is loaded.
  if (preloaded[slide_num] != 2) {
    setTimeout("changeslide()", 100);
    return;
  }

  //  Changes the slide
  if (document.images) {
    document.images.picture.src = LoadedPicture[slide_num].src;
  }
  //eval('document.picture.src = "' + Picture[slide_num] + '"');

  //  Changes the description box
  objDescription = document.getElementById("description");
  objDescription.innerText = Caption[slide_num];

  // Preload the slide before and after this one.
  preload_slide(next_slide_num());
  preload_slide(prev_slide_num());

}

function draw_image_frame() {

  document.write('<table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1>');

  draw_buttons();

  document.write('<tr>');
  document.write('<td ALIGN=CENTER VALIGN=BOTTOM>');
  document.write('<img SRC="' + Image[0] + '" NAME="picture" width="600">');
  document.write('</td>');
  document.write('</tr>');

  document.write('<tr>');
  document.write('<td ALIGN=CENTER VALIGN=CENTER>');
  document.write('<p>&nbsp;');
  document.write('<b><p id="description" align="center">&nbsp;</p></b>');
  document.write('<p>&nbsp;');

  document.write('</td></tr>');
  document.write('</table>');

  // Preload the first slide.
  preload_slide(first_slide);

}

function draw_buttons() {

  document.write('<tr>');
  document.write('<td ALIGN=CENTER VALIGN=TOP>');

  document.write('<center><table BORDER=0 CELLSPACING=0 CELLPADDING=10 COLS=4 WIDTH="250">');
  document.write('<tr>');

  document.write('<td ALIGN=CENTER VALIGN=CENTER><a href="javascript:prevslide()"');
  document.write(' onMousedown="change_button(' + "'btnPrev', 'imgPrev_active')" + '"');
  document.write(' onMouseup="change_button(' + "'btnPrev', 'imgPrev_ro')" + '"');
  document.write(' onMouseover="change_button(' + "'btnPrev', 'imgPrev_ro')" + '"');
  document.write(' onMouseout="change_button(' + "'btnPrev', 'imgPrev')" + '">');
  document.write(' <img name="btnPrev" SRC="" BORDER=0 height=25 width=35></a></td>');

  document.write('<td ALIGN=CENTER VALIGN=CENTER><a href="javascript:stop_slideshow()"');
  document.write(' onMouseover="set_pause_button(' + "'over')" + '"');
  document.write(' onMouseout="set_pause_button(' + "'out')" + '">');
  document.write(' <img name="btnPause" SRC="" BORDER=0 height=25 width=35></a></td>');

  document.write('<td ALIGN=CENTER VALIGN=CENTER><a href="javascript:start_slideshow()"');
  document.write(' onMouseover="set_play_button(' + "'over')" + '"');
  document.write(' onMouseout="set_play_button(' + "'out')" + '">');
  document.write(' <img name="btnPlay" SRC="" BORDER=0 height=25 width=35></a></td>');

  document.write('<td ALIGN=CENTER VALIGN=CENTER><a href="javascript:nextslide()"');
  document.write(' onMousedown="change_button(' + "'btnNext', 'imgNext_active')" + '"');
  document.write(' onMouseup="change_button(' + "'btnNext', 'imgNext_ro')" + '"');
  document.write(' onMouseover="change_button(' + "'btnNext', 'imgNext_ro')" + '"');
  document.write(' onMouseout="change_button(' + "'btnNext', 'imgNext')" + '">');
  document.write(' <img name="btnNext" SRC="" BORDER=0 height=25 width=35></a></td>');

  document.write('</tr>');
  document.write('</table>');

  // Set up the buttons.
  change_button('btnPrev', 'imgPrev');
  change_button('btnPause', 'imgPause_active');
  change_button('btnPlay', 'imgPlay');
  change_button('btnNext', 'imgNext');

}

function set_play_button(strAction) {
  if (!slideshow_playing) {
    if (strAction == "over")
      change_button('btnPlay', 'imgPlay_ro');
    else if (strAction == "out")
      change_button('btnPlay', 'imgPlay');
    else
      change_button('btnPlay', 'imgPlay');
  } else {
    change_button('btnPlay', 'imgPlay_active');
  }
}

function set_pause_button(strAction) {
  if (slideshow_playing) {
    if (strAction == "over")
      change_button('btnPause', 'imgPause_ro');
    else if (strAction == "out")
      change_button('btnPause', 'imgPause');
    else
      change_button('btnPause', 'imgPause');
  } else {
    change_button('btnPause', 'imgPause_active');
  }
}

function change_button(image_name, new_image) {

  if (document.images) {
    document.images[image_name].src = eval(new_image + ".src");
  }
}


