//
//
//
// DO THIS WITH ONE FILE AND A URL VARIABLE THAT INDICATES WHICH IMAGE TO SHOW.
//
//

// MAIN FUNCTION
function main()
{

  // Set the page's title
  document.title = getTitle();

  // Define Local Variables
  var cur_slide_num = new Number();
  var prev_slide_num = new Number();
  var next_slide_num = new Number();

  var prev_slide_page = new String();
  var next_slide_page = new String();
  var command = new String();
  var no_slide = new String();

  var img_type = new String();
  var last_slide_num = new Number();
  var lag_time = new Number();

  img_type = getImageType();
  last_slide_num = getLastSlideNum();
  lag_time = getLagTime();

  cur_slide_num = getCurSlideNum();

  // Set the current Image
  document.slide_image.src = "slide_images/" + cur_slide_num + "." + img_type;

  // Set next and prev variables
  if(cur_slide_num == last_slide_num){ // We're at the end
    next_slide_num = 1;
    prev_slide_num = cur_slide_num*1 - 1;
  }
  else if(cur_slide_num == 1){ // We're at the beginning
    next_slide_num = 2;
    prev_slide_num = last_slide_num;

  }else{
    next_slide_num = cur_slide_num*1 + 1;
    prev_slide_num = cur_slide_num*1 - 1;
  }

  // Build the next and prev links.
  prev_slide_page = "?slide=" + prev_slide_num;
  next_slide_page = "?slide=" + next_slide_num;

  // Check to see if we should auto refresh the slideshow and and a flag to the url accordingly
  if(!isSlidding()){
    prev_slide_page = prev_slide_page + "&no_slide=true";
    next_slide_page = next_slide_page + "&no_slide=true";
  } // else add nothing.


  // Set the next and prev links.
  document.links[2].href = next_slide_page;
  document.links[1].href = prev_slide_page;

  // Set the start/stop slideshow link's values
  //autoSlide();

  // If we are NOT set to STOP the Auto Refresh, then refresh
  if(isSlidding()){
    // Set the timeout to reload the page
    var command = "loadPage('" + next_slide_page + "')"; //new String;
    setTimeout(command, lag_time);
  }
  // Else, do nothing.

}



// SUB FUNCTIONS

function loadPage(page) {

//alert(page);

  document.location.href=page;
}



function getCurSlideNum(){
  var last_slide_num = getLastSlideNum();
  var args = getArgs();  // Get the URL string variables

  // Set the current slide number
  if(args.slide){
    if(args.slide > last_slide_num){
      return last_slide_num;
    }
    else{
      return args.slide;
    }
  }
  else{
    return 1;
  }
}


function isSlidding(){
  var args = getArgs();  // Get the URL string variables

  if(args.no_slide){
    return 0;  // Auto advance the slide show IS OFF
  }
  else{
    return 1; // Auto advance the slide show IS ON
  }
}


function generateStartStopLink(){

//alert("yay");
  var link_url = new String();
  var link_text = new String();
  var link_mouse_over_code = new String();
  var regexp1 = new RegExp("&no_slide=[^&]*", "gi"); // Catches all occurances but first position
  var regexp2 = new RegExp("no_slide=[^&]*&", "gi"); // Cathces first position
  var regexp3 = new RegExp("no_slide=[^&]*", "gi"); // Cathces first position when there are no other string variables
//alert("yay");

  if(isSlidding()){

    // Get the current page name and add the no slide flag to it
    link_url = String(window.location.href);
    link_url = link_url.substring(link_url.lastIndexOf("/")+1, link_url.length);
    if(link_url.indexOf("?") == -1){
      link_url = link_url + "?no_slide=true";
    }
    else{
      link_url = link_url + "&no_slide=true";
    }
    link_text = "<img name=\"nav_stop\" width=\"99\" height=\"16\" src=\"/images/slideshow_dc_stop_on.gif\" alt=\"Stop Slideshow\" border=\"0\">";
	link_mouse_over_code = "onMouseOver=\"nav_stop.src='/images/slideshow_dc_stop_over.gif';\" onMouseOut=\"nav_stop.src='/images/slideshow_dc_stop_on.gif';\"";

  }
  else{

    // Get the current page name
    link_url = String(window.location.href);
    link_url = link_url.substring(link_url.lastIndexOf("/")+1, link_url.length);

    // Clear out all the no_slide variables in all postions of the string
    link_url = link_url.replace(regexp1, "");
    link_url = link_url.replace(regexp2, "");
    link_url = link_url.replace(regexp3, "");

//alert("link_url = |"+link_url+"|");


    link_text = "<img name=\"nav_start\" width=\"99\" height=\"16\" src=\"/images/slideshow_dc_start_on.gif\" alt=\"Start Slideshow\" border=\"0\">";
	link_mouse_over_code = "onMouseOver=\"nav_start.src='/images/slideshow_dc_start_over.gif';\" onMouseOut=\"nav_start.src='/images/slideshow_dc_start_on.gif';\"";

  }

  return ("<a href=\""+link_url+"\"" + link_mouse_over_code + ">"+link_text+"</a>");

}

//************************************************************
// This example is from JavaScript: The Definitive Guide, 3rd Edition.
// That book and this example were Written by David Flanagan.
// They are Copyright (c) 1996, 1997, 1998 O'Reilly & Associates.
// This example is provided WITHOUT WARRANTY either expressed or implied.
// You may study, use, modify, and distribute it for any purpose,
// as long as this notice is retained.
//<!-- Modified by:  Kenneth C. Devoe, WildStorm Productions -->

/*
 * Function getArgs() parses comma-separated name=value argument pairs from
 * the query string of the URL. It stores the name=value pairs in 
 * properties of an object and returns that object.
 */

function getArgs() {
    var args = new Object();                   // Create the object.
    var query = location.search.substring(1);  // Get query string.
    var pairs = query.split("&");              // Break at ampersand.
    for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');           // Look for "name=value".
	if (pos == -1){
	 continue;                                 // If not found, skip.
	}
	var argname = pairs[i].substring(0,pos);  // Extract the name.
	var value = pairs[i].substring(pos+1);    // Extract the value.
	args[argname] = unescape(value);          // Store as a property.
    }
    return args;                              // Return the object.
}


//------------------------------