#!/usr/bin/perl -w
#
# 03/25/01 - Slide_Show
# Author: Thomas Ballard (thomas@esqsoft.com)
# Note: Comments and Suggestions would be appreciated.
#


package O::SlideShow;

use strict;
use vars qw(@ISA @EXPORT_OK);
use O::GET_ARGS;
use O::FORMS qw(GET_HIDDEN);
use O::CGI qw(GET_FORM CONTENT_TYPE);
use O::Wrap qw(wrap);
use O::conf_util qw(conf_read conf_write);
use O::Is qw(isun);
use O::s2dr qw(s2dr);
use O::ENCODE qw(URLDecode URLEncode);
use Exporter;
@ISA = ('Exporter');
@EXPORT_OK = qw(featured_slide list_shows delete_show list_pictures thumbnail_html);



##################################################################################################
### SUBROUTINES
###


#----------------------------------------------------------------------------------------
sub featured_slide{

=head1 USAGE

  Featured Image Usage:

  < ISML TYPE=slide_show SRC=http://[hostname]/[slide_show_path]/[picture1.jpg]>

    The hostname, slide_show_path, and picture key (picture1.jpg is not the actual filename but rather a key in the conf)
    is used by SlideShow.pm to determine which featured image to show.
    (Note: If the user has set rules about how often a featured image is to change then those rules will be honored
     regardless of which picture key is present in the SRC property.)

  Slide Show Usage:

  < ISML TYPE=slide_show>

    Without the presence of source data, SlideShow.pm will attempt to resolve picture1.jpg of the default slide show.
    (This default slide show is stored in /slide_shows/slide_shows.conf as "default_slide_show".)
    If there is not a default show then it will return an HTML page listing available shows at $ENV{HTTP_HOST} site.

=cut

  my $step = '';
  my $success = 0;
  my @slide_items = ();
  my @get_items = ();
  my $partner = new O::Partner($ENV{HTTP_HOST});
  my $user = new O::User($ENV{HTTP_HOST});
  my %CONF_HASH = ();
  my %WINDOW_HASH = ();
  my %FORM = ();


  ### Check for existance of a /slide_shows folder, if not there tell user to set up a slide show
  if (! -d "$user->{directory}/slide_shows"){
    $step='/slide_show_manage/w_intro.htm';
    my $HTML = $partner->load_content($step);
    wrap(text=>\$HTML,partner=>$partner,step=>$step,form=>[{\%FORM,from_slide_show_page=>1}]);
    $partner->{content} = $HTML;
    return $partner->{content};
  }

  ### Figure out what HOST, SHOW, and PICTURE to show
  %CONF_HASH = conf_read( "$user->{directory}/slide_shows/slide_shows.conf" );
  if(
    ($CONF_HASH{sitebuilder}) &&
    (!$CONF_HASH{default_slide_show})
    ){
      $FORM{error} =  "^errortext.no_starting_show^";
      $FORM{errorlink} =  "^errortext.slide_show_usage_link^";
      $step='/slide_show_manage/error_only.htm';
      my $HTML = $partner->load_content($step);
      wrap(text=>\$HTML,partner=>$partner,step=>$step,form=>[\%FORM]);
      $partner->{content} = $HTML;
      return $partner->{content};
  }


  my $SHOW = shift;

  if (!$SHOW){  ### < ISML TYPE=SLIDE_SHOW>  (no SHOW argument)
    # check for the presence of a sitebuilder slide_show file.
    $slide_items[0] = $ENV{HTTP_HOST};
    $slide_items[1] = $CONF_HASH{default_slide_show};  #Note: If empty, then we will show an error
    $slide_items[2] = "picture1.jpg";
    $slide_items[3] = "?path_info=".$ENV{PATH_INFO}   if($CONF_HASH{sitebuilder} && ($CONF_HASH{sitebuilder} eq 'true'));
  } else {         ### < ISML TYPE=SLIDE_SHOW SHOW=blahblahblah>  (SHOW argument)
    $SHOW =~ s@^http://@@;
    #$SHOW =~ s@/cgi-bin/slide_show@@;
    @slide_items = split /\//, $SHOW;

    if(scalar(@slide_items) != 3){
      # Handle error when SHOW= is not of type http://host/show/starting_picture.jpg
      $FORM{error} =  "^errortext.slide_show_usage^";
      $FORM{errorlink} =  "^errortext.slide_show_usage_link^";
      $step='/slide_show_manage/error_only.htm';
      my $HTML = $partner->load_content($step);
      wrap(text=>\$HTML,partner=>$partner,step=>$step,form=>[\%FORM]);
      $partner->{content} = $HTML;
      return $partner->{content};
    }

    $slide_items[3] = "?path_info=".$ENV{PATH_INFO}   if($CONF_HASH{sitebuilder} eq 'true');
  }
  ### End

  ### Handle NOT Featured Image (ie. a regular slide show)
  if ( $slide_items[2] ne 'featured.jpg' ){ #if the filename isn't "featured.jpg" then this is a request for a regular slide
    return slide_show(\@slide_items);
  }

  ### Handle Featured Image
  my $conf_file = s2dr($slide_items[0]) . "/slide_shows/" . $slide_items[1] . "/" . $slide_items[1] . ".conf";
  %CONF_HASH = conf_read( $conf_file );

  if (!scalar keys %CONF_HASH){
    $FORM{error} =  $O::conf_util::error;
    $FORM{errorlink} =  "^errortext.slide_show_usage_link^";
    $step='/slide_show_manage/error_only.htm';
    my $HTML = $partner->load_content($step);
    wrap(text=>\$HTML,partner=>$partner,step=>$step,form=>[\%FORM]);
    $partner->{content} = $HTML;
    return $partner->{content};
  }
  if ($CONF_HASH{create_featured_image} && ($CONF_HASH{create_featured_image} eq 'true')){

    ### Handle change frequency
    if (exists $CONF_HASH{featured_image_frequency} && ($CONF_HASH{create_featured_image} eq 'true')){
      # Note: the featured image code can still be used to pull in a specific image, ie "picture1.jpg", but
      # if the create_featured_image key/value is true then we honor the scheduling constraints within the conf

      my %PIC_HASH = list_pictures($conf_file);
      my $server_time = time;
      my $hammer_time = 0;
      $hammer_time = $CONF_HASH{scheduled_image_change}  if exists $CONF_HASH{scheduled_image_change};

      if ($hammer_time<$server_time){
       # the scheduled change either doesn't exist, or HAS expired
        $CONF_HASH{scheduled_image_change} = $server_time+$CONF_HASH{featured_image_frequency};
        $CONF_HASH{current_featured_image} = int(rand $PIC_HASH{total_images}) + 1;
        #duplicate of below?$slide_items[2] = "picture" . $CONF_HASH{current_featured_image} . ".jpg";

        ### Now store the changes back out to the conf for future visits
        if(!conf_write($conf_file, \%CONF_HASH)){
          return $O::conf_util::error;
        }
      }
      if (exists $CONF_HASH{current_featured_image}){
        $slide_items[2] = "picture" . $CONF_HASH{current_featured_image} . ".jpg";
      }
    }

    %FORM = %CONF_HASH;
    $FORM{featured_image_title} = URLDecode($CONF_HASH{featured_image_title});

    my $img = $CONF_HASH{$CONF_HASH{$slide_items[2]} . "_url"};

    ### Try using a thumbnail version of the featured image first.... thumbnail-ize the filename
    my ($test_for_thumb)    = $img =~ m@/([^\/]+)$@g;  # get everything AFTER the last "/", this should be the filename only.
    my $test_for_thumb_url  = "http://" . $slide_items[0] . "/slide_shows/" . $slide_items[1] . "/thumbnails/th_" . $test_for_thumb; # we'll need this for the <IMG> src property
    my $test_for_thumb_file = s2dr($slide_items[0]) . "/slide_shows/" . $slide_items[1] . "/thumbnails/th_" . $test_for_thumb; # we'll need this to check our file system for the file

    ### Test for the existence of a file at the thumbnail file destination ($test_for_thumb_file)
    if(-e $test_for_thumb_file){
      #YES, power to the thumb, use the smaller, lighter, faster version of the image as the featured image.
      $FORM{featured_image} = $test_for_thumb_url;
    } else {
      #NO, the thumb does not exist, use the bigger, fatter, slower version of the image as the featured image.
      warn "tested for: $test_for_thumb_url";
      $FORM{featured_image} = $CONF_HASH{$CONF_HASH{$slide_items[2]} . "_url"};
    }

    $FORM{slide_show_url} = "http://" . $slide_items[0] . "/slide_show.html?show=" . $slide_items[1] . "&picture=" . $slide_items[2];

    $FORM{featured_dimensions} = " WIDTH='" . $CONF_HASH{featured_image_size} . "'";

    ### Handle Featured Image Popup Link Window Properties
    my $window_conf = s2dr($slide_items[0]) . "/slide_shows/" . $slide_items[1] . "/window_properties.conf";
    if(-e $window_conf ){
      %WINDOW_HASH = conf_read( $window_conf );
      $FORM{featured_window_props} = "";
      $FORM{featured_window_props} .= $WINDOW_HASH{window_toolbar_setting}    if(exists $WINDOW_HASH{window_toolbar_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_location_setting}   if(exists $WINDOW_HASH{window_location_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_scrollbars_setting} if(exists $WINDOW_HASH{window_scrollbars_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_menubar_setting}    if(exists $WINDOW_HASH{window_menubar_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_status_setting}     if(exists $WINDOW_HASH{window_status_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_resizable_setting}  if(exists $WINDOW_HASH{window_resizable_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_width_setting}      if(exists $WINDOW_HASH{window_width_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_height_setting}     if(exists $WINDOW_HASH{window_height_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_top_setting}        if(exists $WINDOW_HASH{window_top_setting});
      $FORM{featured_window_props} .= $WINDOW_HASH{window_left_setting}       if(exists $WINDOW_HASH{window_left_setting});
    }

    $step = "slide_show_manage/featured_image";
    my $HTML = $partner->load_content($step);
    $partner->{content} = $HTML;
    wrap(text=>\$HTML,partner=>$partner,step=>$step,form=>[\%FORM]);

    $HTML =~ s@featured_image@@g;  #not sure how to get the label out of the returned code?

    return $HTML;
  } else { # featured image not enabled for this slide show
    my $HTML = "<!-- featured image not enable for http://". $slide_items[0] . "/slide_shows/" . $slide_items[1] . "-->";
    return $HTML;
  }
}
#----------------------------------------------------------------------------------------






#----------------------------------------------------------------------------------------
sub list_shows{

  #examine all subdirectories of /slide_shows
  #grab files ending in .CONF in each subdirectory
  #get the date/time stamp for each of the confs
  #open each conf
  # 1. to confirm they are actual CONFs (the users have access to this dir and might corrupt it)
  # 2. to read for the following....
  # --slide_show_name
  # --all keys ending in .jpg (this is how I can determine the number of images for that show)

  my $user = new O::User($ENV{HTTP_HOST});
  my $dir = $user->{directory} . "/slide_shows";
  my @slide_show_dirs = ();
  my @file_mod = ();
  my @mtotal_images = ();
  my @slide_show_names = ();
  my @slide_show_url = ();
  my @create_thumbnail_page = ();
  my @conf_filename = ();
  my $index = 1;
  my %TEMP_HASH = ();
  my $image_index = 0;

  ### start by getting a list of all subdirectories of slide_shows/ (non-recursing)
  opendir(DIR, $dir);
  while($_ = readdir(DIR)){
    next if /^\.\.?$/;
    my $temp = "$dir/$_";
    if(!-d $temp){  next; }
    if(-l $temp){   next; }
    push @slide_show_dirs, $_;
  }
  closedir(DIR);

  ### now scan each of those subs for .conf files
  foreach my $subdir (@slide_show_dirs){
    opendir(DIR, $dir . "/" . $subdir);
    my @names = ();
    my @last_mod = ();
    while($_ = readdir(DIR)){
      next unless (
                  ($_ =~ /conf$/) &&
                  ($_ !~ /window_properties.conf$/gi)
                  );
      push @conf_filename, "/" . $subdir . "/" . $_;

      ### get a nicified "last modified" time for the conf file
      push @file_mod, (scalar gmtime ((stat $dir . "/" . $subdir . "/" . $_)[9]))."GMT \n";

      ### create a link to the slide show
      $_ =~ s@.conf$@@;
      push @slide_show_url, "http://" . $ENV{HTTP_HOST} . "/slide_show.html?picture=picture1.jpg&show=" . $_;

      ### read conf, and get the name of the slide show
      my %CONF_HASH = conf_read( $dir . "/" . $subdir . "/" . $_ . ".conf" );
      push @slide_show_names, $CONF_HASH{slide_show_name};
      if($CONF_HASH{create_thumbnail_page} && ($CONF_HASH{create_thumbnail_page} eq 'true')){
        push @create_thumbnail_page, "&refresh_thumbnail_page=".$CONF_HASH{create_thumbnail_page};
      } else {
        push @create_thumbnail_page, "";
      }

      ### determine the number of images within the slide show keys
      $image_index = 0;
      foreach my $key (keys(%CONF_HASH)){
        if($key =~ /^(picture).+(_url)$/){
          $image_index++;
        }
      }
      push @mtotal_images, $image_index;
      ++$index;
    }
    closedir(DIR);
  }
  $TEMP_HASH{total_slide_shows} = ($index-1);

	# examine /slide_shows/slide_shows.conf for a default_slide_show settings
  my %HASH2 = conf_read("$user->{directory}/slide_shows/slide_shows.conf");

  $TEMP_HASH{default_slide_show} = $HASH2{default_slide_show};
  $TEMP_HASH{last_modified} = \@file_mod;
  $TEMP_HASH{slide_show_name_list} = \@slide_show_names;
  $TEMP_HASH{mtotal_images} = \@mtotal_images;
  $TEMP_HASH{slide_show_url} = \@slide_show_url;
  $TEMP_HASH{edit_conf_filename} = \@conf_filename;
  $TEMP_HASH{delete_show_path} = \@slide_show_dirs;
  $TEMP_HASH{refresh_thumbnail_page} = \@create_thumbnail_page;

  return %TEMP_HASH;
}
#----------------------------------------------------------------------------------------





#----------------------------------------------------------------------------------------
sub delete_show{

  require "File/Path.pm";
  my $user 		= O::User->new($ENV{HTTP_HOST});
	my $error 	= "";
  my $success	= 0;

  my $which_show = shift;
  if ($which_show =~ /^all$/i){
    #optional "all" argument (for readability), but an empty argument list accomplishes the same thing.
    $which_show = '';
  }

	#isun "$user->{directory}/slide_shows/$which_show";
  my $path_ok = 0;
  if( -d $user->{directory} . "/slide_shows/" . $which_show ){
    #is path there?
    $success = File::Path::rmtree( $user->{directory} . "/slide_shows/" . $which_show, 0, 0);
  } else {
    warn "$! - couldn't remove /slide_shows/" . $which_show;
		$error="$!";
  }

  return $success;
}
#----------------------------------------------------------------------------------------






#----------------------------------------------------------------------------------------
sub list_pictures{

  ### This sub is intended to return all of the pictures within a single specified slide show.

  #open the current conf
  #read in the url of each image

  my $conf_filename = shift;

  my @temp_image_url = ();  # temporary (unsorted) hash trash can
  my @image_url = ();       # final, pretty, sorted array

  my %TEMP_HASH = ();

  ### read conf, and get the name of the slide show
  my %CONF_HASH = conf_read( $conf_filename );

  ### determine the number of images within the slide show keys and stuff them onto an array
  my $image_index = 0;
  foreach my $key (keys(%CONF_HASH)){
    if($key =~ /^(picture).+(.jpg)$/){
      if(exists $CONF_HASH{$CONF_HASH{$key}."_url"}){
        push @temp_image_url, $key . "=" . $CONF_HASH{$CONF_HASH{$key}."_url"};
      } else {
        push @temp_image_url, "no_conf_hash_key";
      }
      ++$image_index;
    }
  }
  @image_url = sort(@temp_image_url);
  # now rip the "pictureX.jpg=" identifier used for sorting, back OFF of the items in the array
  for (my $index=0; $index < scalar(@image_url); $index++){
    $image_url[$index] =~ s@^.+=@@;
  }

  $TEMP_HASH{thumbnail_size} = $CONF_HASH{thumbnail_size}   if($CONF_HASH{thumbnail_size});
  $TEMP_HASH{image_url} = \@image_url;
  $TEMP_HASH{total_images} = $image_index;

  return %TEMP_HASH;
}
#----------------------------------------------------------------------------------------







#----------------------------------------------------------------------------------------
sub thumbnail_html{

  #examine the thumbnails subdirectory and return urls to each thumbnail

  my $slide_items     = shift;
  my $conf_file       = s2dr($slide_items->[0]) . "/slide_shows/" . $slide_items->[1] . "/" . $slide_items->[1] . ".conf";
  my @uimage_url      = ();
	my @image_url       = ();
  my @uimage_key      = ();
	my @image_key       = ();
  my @uimage_cap      = ();
  my @image_cap       = ();
  my @image_thumbnail = ();
	my @unsorted_keys   = ();
	my @sorted_keys     = ();

  my %CONF_HASH = conf_read( $conf_file );
  if (!scalar keys %CONF_HASH){
    return $O::conf_util::error;
  }

  ### build a (sordid, and sorted) list of image names
  foreach my $key (keys(%CONF_HASH)){
    if($key =~ /^(picture).+(.jpg)$/){
			push @unsorted_keys, $key =~ m@^picture(.+).jpg$@;
      push @uimage_key, $key;
      push @uimage_url, $CONF_HASH{$CONF_HASH{$key}."_url"};
      push @uimage_cap, $CONF_HASH{$CONF_HASH{$key}."_caption"};
    }
	}
	@sorted_keys = sort { $a <=> $b } @unsorted_keys;
	for(my $i1=0; $i1<scalar(@sorted_keys); $i1++){
		for(my $i2=0; $i2<scalar(@unsorted_keys); $i2++){
			if($sorted_keys[$i1] == $unsorted_keys[$i2]){
				push @image_url, $uimage_url[$i2]; #relies on unsorted_keys, uimage_key, and uimage_url arrays index/value's to correspond to one another.
				push @image_key, $uimage_key[$i2];
        push @image_cap, $uimage_cap[$i2];
			}
		}
	}

  #now create a "thumbnail-ized" version of the filename part from @image_url
  for(0..scalar(@image_url)-1){
    my ($thumb_filename) = $image_url[$_] =~ m@.*/(.+)$@;
    $thumb_filename = "/slide_shows/" . $slide_items->[1] . "/thumbnails/th_" . $thumb_filename;
    my $test_thumb = s2dr($slide_items->[0]).$thumb_filename;
    if(!-e $test_thumb){
      $thumb_filename = "http://" . $slide_items->[0] . "/cgi-bin/image/images/slide_show_manage/thumbnail_not_available.gif";
    } else {
      $thumb_filename = "http://" . $slide_items->[0] . $thumb_filename;
    }
    push @image_thumbnail, $thumb_filename;
  }

  my $c = -1;
  my $thumbnail_html = '<TABLE CELLSPACING=15 CELLPADDING=0 BORDER=0>';
  for(0..scalar(@image_key)-1){
    $thumbnail_html .= '<TR>';
    for(1..4){
      if ((++$c) < scalar(@image_key)){
        $thumbnail_html .= '<TD>';
				$thumbnail_html .= '<A HREF="http://' . $slide_items->[0] . '/slide_show.html?show=' . $slide_items->[1] . '&picture=' . $image_key[$c] . '" ONMOUSEOVER="window.status=\'' . URLEncode($image_cap[$c]) . '\';return true;" ONMOUSEOUT="window.status=\'\';return true;" ALT="' . URLEncode($image_cap[$c]) . '">';
				$thumbnail_html .= '<IMG SRC="' . $image_thumbnail[$c] . '" BORDER=0>';
				$thumbnail_html .= '</A>';
				$thumbnail_html .= '</TD>';
      } else {
        $thumbnail_html .= '<TD>&nbsp;</TD>';
      }
    }
    $thumbnail_html .= '</TR>';
    if($c > scalar(@image_key)-1){
      last;
    }
  }
  $thumbnail_html .= '</TABLE>';

  return $thumbnail_html;
}
#----------------------------------------------------------------------------------------







#----------------------------------------------------------------------------------------
sub slide_show{

  my $slide_items = shift;
  my $partner = new O::Partner($ENV{HTTP_HOST});
  my $user = O::User->new($ENV{HTTP_HOST});
  my %CONF_HASH = ();
  my %FORM = GET_FORM;

	#if the data exists in the form telling us which show to use, well then, use it!
	if($FORM{show}){
		$slide_items->[1] = $FORM{show};
	}

  my $step = '';

=head1 WINNER
      Who wins, FORM data, or ISML? (FORM data?!, WINNER!!)
      If FORM data is present assume it's a return trip
        (because the form data should always contain...
          host,
          slide_show,
          picture_key,
          autopilot_running
        )

      If No form data is present, and ISML doesn't specify a SHOW=blah, then we need to examine
        "/slide_shows/slide_shows.conf" for a starting_show value and use it,
      If No starting show data can be found then show the list of available shows.
=cut

  ### Figure out which show to view first
  if(%FORM){
    #$FORM{hidden} = GET_HIDDEN({ path_info => $ENV{PATH_INFO} });
  } else {  # (NO FORM data, probably first visit this session)
    $FORM{path_info} = $ENV{PATH_INFO};
    #$FORM{hidden} = GET_HIDDEN({ path_info => $ENV{PATH_INFO} });
    #use the passed data
  }


  ### Default to the list of shows (if other data is present it will override this default later)
  my %SHOWS_HASH = &list_shows();
  $FORM{total_slide_shows} = $SHOWS_HASH{total_slide_shows};
  $FORM{slide_show_url} = $SHOWS_HASH{slide_show_url};

  $FORM{slide_show_name_list} = $SHOWS_HASH{slide_show_name_list};
  $step='/slide_show_manage/slide_shows.htm';


  %FORM = &construct_show($slide_items,\%FORM);
  if($FORM{error}){
		$FORM{error} = "
			<TABLE CELLSPACING=0 CELLPADDING=1 BORDER=2 BGCOLOR=^prefs.error_bgcolor^ BORDERCOLOR=^prefs.error_bordercolor^>
			<TR><TD BGCOLOR=^prefs.error_bordercolor^><A
			HREF=^form.errorlink?form.errorlink:/cgi-bin/help^ TARGET=_help><IMG
			SRC=/cgi-bin/image/images/error_icon1.gif BORDER=0></A></TD>
			<TD VALIGN=TOP><FONT FACE=^prefs.error_face^ SIZE=^prefs.error_size^ COLOR=^prefs.error_color^><B>&nbsp;" . $FORM{error} . "
			</B></FONT></TD></TR></TABLE>";
  }

  $step='/slide_show_manage/blank_wrapper.htm';
  if($FORM{thumbnail_html}){
    # if we are showing the thumbnail page, turn off the autopilot temporarily
    delete $FORM{autopilot_onload}    if exists $FORM{autopilot_onload}
  }


  ### Create a random number which the content can use for uniquifying itself
  # this is necessary because with < ISML> more than one slide show might exist on a page at a time, but
  # the javascript inside of those slide shows cannot tolerate other controls by the same name, so the
  # random can be used to produce unique names.
  $FORM{random_num} = int(rand 10000)+1;

  ### Generate the output
  my $HTML = $partner->load_content($step);
  wrap(text=>\$HTML,partner=>$partner,step=>$step,form=>[\%FORM,\%CONF_HASH]);
  $partner->{content} = $HTML;
  return $partner->{content};
}
#----------------------------------------------------------------------------------------





#----------------------------------------------------------------------------------------
sub construct_show(){

  ##################################################################################################
  ### SHOW WHICH IMAGE?
  ###
  ### If this is the first request of this CGI for this session then the user MUST feed us the KEY to the first picture, in the URL.
  ### Every subsequent session visit has data in the FORM hash which we can use to extrapolate the next image to show...

  # 2. Successful read of CONF.
  #  - Does the incoming FORM hash contain a specific "picture" request?
  #    -No:
  #      - Set a request for "picture1" (the default starting slide)
  #      - Process with "Yes" (below.)
  #    -Yes:
  #      - Search the conf for a key match.
  #        - Key match found?
  #          -Yes:
  #            - Get the following from the CONF and store into hidden FORM hash:
  #              picture(url), picture_previous(key), picture_next(key), thumbnail(key),
  #              autopilot_running(t/f), autopilot_interval, ref_to_navbar_html,
  #              step, table_characteristics, font_characteristics, title, caption.
  #            - Proceed to step 3.
  #          -No:
  #            - Call sub to Build a list of shows.
  #            - Set step to "slide_shows" page.
  #            - Set error (damaged or partial slide show).
  #            - Print the list and error, exit.

  my $slide_items = shift;
  my $PASSED_FORM = shift;
  my $user = new O::User($slide_items->[0]);
  my $lookup = ""; # this is used to resolve the first part of the key for a given slide (ie. picture1.jpg, becomes "picture1")
  my %FORM = ();
  my $thumbnail_html = "";

	if(!$slide_items->[1]){
    $FORM{error} = "^errortext.no_slide_show_path^";
    return %FORM;
	}

  my $conf_file = $user->{directory} . "/slide_shows/" . $slide_items->[1] . "/" . $slide_items->[1] . ".conf";
  %FORM = conf_read($conf_file);

  if (!%FORM){
    $FORM{error} = "^errortext.find_show^<BR>/slide_shows/" . $slide_items->[1] . "/" . $slide_items->[1] . ".conf";
    return %FORM;
  }

  if(!$PASSED_FORM->{picture}){
    if($slide_items->[2]){
      $FORM{picture} = $slide_items->[2];
    }
  } else {
    $FORM{picture} = $PASSED_FORM->{picture};
  }

  if (!$FORM{picture}){
    $FORM{picture} = "picture1.jpg";
  }

  if ($FORM{$FORM{picture}}){

    if ($FORM{picture} eq 'thumbnail.jpg'){
      $FORM{thumbnail_html} = thumbnail_html($slide_items);
    }

    $lookup = $FORM{$FORM{picture}}									if(exists $FORM{picture});
    if(exists $FORM{$lookup . "_url"}){
      if($FORM{$lookup . "_url"} =~ m@http://@){
        $FORM{url} = $FORM{$lookup . "_url"};
      } else {
        $FORM{url} = "http://" . $slide_items->[0] . $FORM{$lookup . "_url"};
      }
    }
    $FORM{previous} = $FORM{$lookup . "_previous"}	if(exists $FORM{$lookup . "_previous"});
    $FORM{next} = $FORM{$lookup . "_next"}					if(exists $FORM{$lookup . "_next"});
    $FORM{caption} = URLDecode($FORM{$lookup . "_caption"})		if(exists $FORM{$lookup . "_caption"});
    $FORM{thumbnails} = "thumbnail.jpg";
    my $temp_width = "";
		$temp_width .= $FORM{$lookup . "_width"}		if(exists $FORM{$lookup . "_width"});
    if($temp_width =~ m@^(\d)+$@g ){
      $FORM{width} = " WIDTH=" . $temp_width;
    } else {
      $FORM{width} = "";
    }
    my $temp_height = "";
		$temp_height = $FORM{$lookup . "_height"}				if(exists $FORM{$lookup . "_height"});
    if($temp_height =~ m@^(\d)+$@g ){
      $FORM{height} = " HEIGHT=" . $temp_height;
    } else {
      $FORM{height} = "";
    }

		if(
			($FORM{autopilot_running} && ($FORM{autopilot_running} eq 'true')) ||
			($PASSED_FORM->{"autopilot_running"} && ($PASSED_FORM->{"autopilot_running"} eq 'true'))
			){
      $FORM{autopilot_onload} = qq|ONLOAD="setTimeout('autostart^this.num^(^form.autopilot_interval^);',1000);"|;
    }

    if($FORM{disable_mouseovers} && ($FORM{disable_mouseovers} eq 'true')){
      $FORM{toggle} = "false";
    } else {
      $FORM{toggle} = "true";
    }

    $FORM{autopilot_interval} = $FORM{autopilot_interval};
    $FORM{previous_link} = "javascript:validate^this.num^('http://" . $slide_items->[0] . "/slide_show.html?show=" . $FORM{slide_show_path} . "&picture=" . $FORM{$lookup . "_previous"} . "');";
    $FORM{thumbnail_link} = "javascript:validate^this.num^('http://" . $slide_items->[0] . "/slide_show.html?show=" . $FORM{slide_show_path} . "&picture=thumbnail.jpg" . "');";
    $FORM{next_link} = "javascript:validate^this.num^('http://" . $slide_items->[0] . "/slide_show.html?show=" . $FORM{slide_show_path} . "&picture=" . $FORM{$lookup . "_next"} . "');";
    $FORM{play_link} = "javascript:autostart^form.random_num^(" . $FORM{autopilot_interval}. ");";
    $FORM{stop_link} = "javascript:autostop^form.random_num^();";
  } else {
    # We didn't find a key matching the requested action
    $FORM{error} = "^errortext.find_show_data^<BR>$FORM{picture}";
    return %FORM;
  }

  ### Get the content that makes up a slide and its components
  $FORM{slide_show_content} = "^w_settings_tables.slide_show_content^";

  ### Is the navbar_html text or graphical
  $FORM{other_shows_html} = "^w_settings_tables.other_shows_html^";
  $FORM{thumbnail_button_html} = "^w_settings_tables.thumbnail_button_html^";
  $FORM{thumbnail_text_html} = "^w_settings_tables.thumbnail_text_html^";
  $FORM{autopilot_button_html} = "^w_settings_tables.autopilot_button_html^";
  $FORM{autopilot_text_html} = "^w_settings_tables.autopilot_text_html^";

  ### Resolve the navbar html pieces (ie, show thumbnail_button?, show autopilot_buttons?)
  ### Note, this needs to be here since it's dependent on data read from the conf for the FILL_HASH
  $FORM{thumbnail_button_html} = "^w_settings_tables.thumbnail_button_html^";
  if($FORM{create_thumbnail_page} eq 'true'){
    $FORM{thumbnail_chunk_visibility} = "position:relative;visibility:visible;";
  } else {
    $FORM{thumbnail_chunk_visibility} = "position:absolute;visibility:hidden;";
  }

  $FORM{autopilot_button_html} = "^w_settings_tables.autopilot_button_html^";
  if($FORM{enable_autopilot} && ($FORM{enable_autopilot} eq 'true')){
    $FORM{autopilot_chunk_visibility} = "position:relative;visibility:visible;";
  } else {
    $FORM{autopilot_chunk_visibility} = "position:absolute;visibility:hidden;";
  }

  my %TFORM = &list_shows;
  $FORM{slide_show_path} = $TFORM{delete_show_path};
  $FORM{total_slide_shows} = $TFORM{total_slide_shows};
  $FORM{slide_show_url} = $TFORM{slide_show_url};
  $FORM{slide_show_name_list} = $TFORM{slide_show_name_list};

  return %FORM;
}
#----------------------------------------------------------------------------------------