#!/usr/bin/perl

$> = $<;  # set effective user IDs to real id
$) = $(;  # set goup id to real id

$ENV{'PATH'} = '/bin:/usr/bin';
$ENV{'IFS'} = '' if $ENV{'IFS'} ne '';

$num = time;
srand($num);	# seed random number generator 
#$link_number = int(rand(10000));

open(INPUT,"skidata.txt") || die "Can Not open input\n";
while (eof(INPUT) == 0) {
   @items = <INPUT>;
}
close(INPUT);

$total = @items;
$count =1;
$index1=0;
$index2=0;
 
do {
  $line = $items[$count];
  ($type,$asin,$img,$w,$h,$title) = split('\|',$line);
   if($type eq "VHS") {
      $vid_asin[$index1] = "$asin";
      $vid_img[$index1] = "$img";
      $vid_w[$index1] = $w;
      $vid_h[$index1] = $h;
      $vid_title[$index1] = "$title";
      $vid_used[$index1] = 0;
      if($img ne "") { $tot_vid_imgs = $index1+1; }
       ++$index1;
   }
   elsif($type eq "BOOK") {
      $book_asin[$index2] = "$asin";
      $book_img[$index2] = "$img";
      $book_w[$index2] = $w;
      $book_h[$index2] = $h;
      $book_title[$index2] = "$title";
      $book_used[$index2] = 0;
      if($img ne "") { $tot_book_imgs = $index2+1; }
       ++$index2;
   }
} while (++$count < $total);

# randomly pick 6 video images
$count = 0;
do {
    $number = int(rand($tot_vid_imgs));
    if($vid_used[$number] == 0) {
          #found one
          $vid_used[$number] =1;
          $display_vid[$count] = $number;
          ++$count;
    }
} while ($count <6);

# randomly pick 8 book images - make sure images wider than 100 are in the upper 4
$count = 0;
do {
NEXT:
    $number = int(rand($tot_book_imgs));
    if($book_used[$number] == 0) {
          #found one
          #make sure if width is greater than 100 that it is not in the first 4 picked
          if(($book_w[$number] > 100) && ($count < 4)) { goto NEXT; }
          $book_used[$number] =1;
          $display_book[$count] = $number;
          ++$count;
    }
} while ($count <8);

print "Content-type: text/html\n\n";
print "<html><head><title>Skiing Books and Videos</title></head><body>\n";
print "<font size=6><FONT COLOR=\"#0000DF\">F</FONT><FONT COLOR=\"#FF0000\">u</FONT><FONT COLOR=\"#00DB00\">n</FONT><FONT COLOR=\"#FF6600\">G</FONT><FONT COLOR=\"#BC3092\">u</FONT><FONT COLOR=\"#1F1AD9\">i</FONT><FONT COLOR=\"#F80E2C\">d</FONT><FONT COLOR=\"#00C600\">e</FONT><FONT COLOR=\"#FF6600\">:</FONT></FONT> Skiing Videos & Books (from Amazon.com)<br>\n";
#----------------------
# now print videos
#----------------------
print "<table width=100%  border=0 cellpadding=10><tr>\n";
print "<tr bgcolor=\"#9090FF\"><td  align=\"center\"><font size=4><b>Skiing Videos</b></font><br>";
print "<font size=2>[ <a href=\"show_all.pl\">Reloading</a> page displays more pictures ]</td></tr>\n";
print "</table>\n";

print "<table width=100% cols=3 border=0 cellpadding=10><tr>\n";
# First Column
print "<td width = 100 valign=\"top\" align=\"center\">\n";
  for($temp=0;$temp<3;++$temp) {
     print "<center><b>$vid_title[$display_vid[$temp]]</b><br><br><a href = \"http://www.funguide.com/books_video/moreinfo.pl?$vid_asin[$display_vid[$temp]]\" target=\"new\">\n";
    print "<img src = \"skiing/$vid_img[$display_vid[$temp]]\" height=\"$vid_h[$display_book[$temp]]\" width=\"$vid_w[$display_book[$temp]]\"><br clear=all><br>[More / Order]</a><br><br><br></center>\n";
  }
print "</td>\n";

# Middle Column  - display all other titles (random order)
$count = 0;
$todo = @vid_title;

print "<td  bgcolor=\"#E0E0FF\">\n";
do {
    $number = $count;	#int(rand($todo));
    if($vid_used[$number] == 0) {
          #found one
          print "$vid_title[$number]\n";
          print " <a href = \"http://www.funguide.com/books_video/moreinfo.pl?$vid_asin[$number]\" target=\"new\">\n";
          print "[More / Order]</a><br><br>\n";
          $vid_used[$number] =1;
    }

} while (++$count <$todo);
print "</td>\n";

# Third Column
print "<td width = 100 valign=\"top\" align=\"center\">\n";
  for($temp=3;$temp<6;++$temp) {
     print "<center><b>$vid_title[$display_vid[$temp]]</b><br><br>\n";
     print "<a href =  \"http://www.funguide.com/books_video/moreinfo.pl?$vid_asin[$display_vid[$temp]]\" target=\"new\">\n";
    print "<img src = \"skiing/$vid_img[$display_vid[$temp]]\" height=\"$vid_h[$display_book[$temp]]\" width=\"$vid_w[$display_book[$temp]]\"><br clear=all><br>[More / Order]</a><br><br><br></center>\n";
  }
print "</td>\n";
print "</tr></table><BR><BR>\n";

#----------------------
# now print books
#----------------------
print "<table width=100%  border=0 cellpadding=10><tr>\n";
print "<tr bgcolor=\"#E0E0DD\"><td  align=\"center\"><font size=4><b>Skiing Books</b></font><br>";
print "<font size=2>[ <a href=\"show_all.pl\">Reloading</a> page displays more <b>titles</b> / pictures ]</td></tr>\n";
print "</table>\n";

print "<table width=100% cols=3 border=0 cellpadding=10><tr>\n";

# First Column
print "<td width = 100 valign=\"top\" align=\"center\">\n";
  for($temp=0;$temp<4;++$temp) {
     print "<center><b>$book_title[$display_book[$temp]]</b><br><br><a href = \"http://www.funguide.com/books_video/moreinfo.pl?$book_asin[$display_book[$temp]]\" target=\"new\">\n";
    print "<img src = \"skiing/$book_img[$display_book[$temp]]\" height=\"$book_h[$display_book[$temp]]\" width=\"$book_w[$display_book[$temp]]\"><br clear=all><br>[More / Order]</a><br><br><br></center>\n";
  }
print "</td>\n";

# Middle Column  - display all other titles 
$count = 0;
$todo = @book_title;

print "<td  bgcolor=\"#F0F0EE\">\n";
do {
    $number = int(rand($todo));
    if($book_used[$number] == 0) {
          #found one
          print "$book_title[$number]\n";
          print " <a href = \"http://www.funguide.com/books_video/moreinfo.pl?$book_asin[$number]\" target=\"new\">\n";
          print "[More / Order]</a><br><br>\n";
          $book_used[$number] =1;
          ++$count
    }

} while ($count < 24);
print "<br><center><font size=5><b><a href=\"show_all.pl\">Display More Titles</a></b></font></center><br>\n";
print "</td>\n";

# Third Column
print "<td width = 100 valign=\"top\" align=\"center\">\n";
  for($temp=4;$temp<8;++$temp) {
     print "<center><b>$book_title[$display_book[$temp]]</b><br><br><a href = \"http://www.funguide.com/books_video/moreinfo.pl?$book_asin[$display_book[$temp]]\" target=\"new\">\n";
    print "<img src = \"skiing/$book_img[$display_book[$temp]]\" height=\"$book_h[$display_book[$temp]]\" width=\"$book_w[$display_book[$temp]]\"><br clear=all><br>[More / Order]</a><br><br><br></center>\n";
  }
print "</td>\n";
print "</tr></table><BR><BR>\n";

print "</body></html>\n";

