#!/cygdrive/c/Perl/bin/perl 
# Program to process my bird database and create the web pages for the
# individual birds only.
#
# Original line from excel
#   date filename bird_name bird_category picture_location notes
# after sort
#   bird_name date bird_category filename picture_location notes
#
$new = 1;		# if 1, need a new bird file opened for writing
$count = 1;		# current number of birds pictures in a row
$number = 1;		# number of pictures for a bird
$row_max = 3;		# number of pictures per row
$previous = "";		# name of previous bird processed, new bird = new file.
$filename = "";		# holds filename of the current open bird file
{
   while (chomp($line = <STDIN>)) {
	@name = split /\t/, $line;
	$num = $#name;
			# skip if a zero'd line (excel did this) 
	next if ($num < 0);
			# skip comment lines
	next if ($name[0] =~ /#/);
			# get rid of any extra spaces at the begging or end of
			# the bird name
	$name[2] =~ s/^ +//;		# get rid of any beginning spaces
	$name[2] =~ s/ +$//;		# get rid of any ending spaces
	$newline = $name[2] . "\t" . $name[0] . "\t" . 
		$name[3] . "\t" . $name[1] . "\t " .
		$name[4] . "\t" . $name[5];
	push (@array, $newline);
   }
   			# sort so all the birds of one species are together
   @sorted = (sort {$b cmp $a} @array);

# now read in the class_in file.  From this file I get the detailed
# information on each bird for printing, and create the right menu of
# birds in a specific classification.  I use a hash table and get rid
# of spaces and commas in the classification name

   unless (open CLASS, "<class_in") {
	   die "Cannot open the class_in file\n";
   }
   $class_num = -1;
   foreach (<CLASS>) {
	chomp;
			# skip blank lines
	next if /^(\s)*$/;
			# skip comment lines
	next if (/^#/);
	@in = split /:/;
			# skip sub-class & text lines, they are
			# used only in the makeclass program
	next if ($in[0] =~ /^header/);
	next if ($in[0] =~ /^text/);
			# get rid of any begging or adding extra spaces
	$num = $#name;
	for ($i = 0; $i <= $num; $i++) {
	    $name[$i] =~ s/^\s+//;	# get rid of any beginning spaces
	    $name[$i] =~ s/\s+$//;	# get rid of any ending spaces
	}
			# each bird classification creates a hash table
	if ($in[0] =~ /title/) {
		$class_num ++;
	        $in[1] =~ s/,//g;	# get rid of any beginning spaces
	        $in[1] =~ s/\s+//g;	# get rid of any ending spaces
	        $in[1] =~ s/"//g;	# get rid of any quotes
		$title[$class_num] = $in[1];
			# not put in the html
		$class{$in[1]} =
		'     <td valign="top">' . "\n" .
		'     <table border="0" bgcolor="#FFFFFF"' .
	             ' valign="top" RULES="none">' . "\n" .
		'     <tr valign="top">' . "\n" .
		'     <td width="150" height="2" valign="top" ' . "\n" .
	        '         style="font-family: arial; font-size: small;' .
	             'font-weight: bold;" >' . "\n" .
		"     All  &#147;$in[1]&#148;<hr />\n" .
		"      </tr><tr>\n" .
		"      <ul>";
		$classification = $in[1];
	}
	if ($in[0] =~ /^bird/) {
		$class{"$classification"} = $class{"$classification"} . 
		"      <tr>\n" .
		'      <td width="150" height="2" ' .
		      'style="font-family: arial; font-size: small;' .
		      'font-weight: bold;" >' . "\n" .
		      "<li>\n"  .
		'      <a class="nounder" href="' .
		"     $in[2]\">$in[1]</a></ /li>\n" .
		"      </td></tr>\n";
	}

  }
  close CLASS;
# now do the pages
   while ($line = pop(@sorted)) {
	@name = split /\t/, $line;
		# close previous file if the bird name has changed
	if ($name[0] ne $previous && !$new)
		{
                print OUT "</ul>\n";
                print OUT "</table>\n";
		print OUT $class{"$prev_title"};
		&print_end;
		$new = 1;
		$number = 1;
		}
	if ($new) {
		# need to open file
	   $filename = "bird_" . $name[0] . ".shtml";
	   $filename =~ s/ +//;
	   $filename =~ s/ +//;
	   $filename =~ s/ +//;
	   open OUT, ">$filename" or die "Can't open $filename for writing\n";
	   $new = 0;
	   $count = 1;
	   $previous = $name[0];
	   $prev_title = $name[2];
	   $prev_title =~ s/,//g;	# get rid of any beginning spaces
	   $prev_title =~ s/\s+//g;	# get rid of any ending spaces
	   $prev_title =~ s/"//g;	# get rid of any quotes
	   &print_header1;
	   print OUT "<title> $name[0] </title>\n";
	   &print_header2;
	   print OUT '        <a name="atop"></a>';
	   print OUT "        <h3>$name[0]</h3>\n";
	   print OUT "        <p>\n";
	   print OUT "        </p>\n";
	   print OUT '        <table width="435"', ">\n";
	   print OUT "        <tr><td>\n";
	   print OUT '        <p style="font-size:small;">',"\n";
	   print OUT "        <b>Navigation Hints</b>";
	   print OUT "        Left-clicking on a thumbnail of a picture will",
	             "         bring up a high-resolution image.\n";
	   print OUT "         This may take a couple of minutes to load.",
	             "         Using the left and right arrow keys will \n";
	   print OUT "	       navigate through the high-resolution images.\n";
	   print OUT "         A shift left-key in the Mozilla browser will",
	   	     "         bring up a separate window.\n";
	   print OUT "         </p>";
	   print OUT "         </td>";
	   print OUT "        </tr><tr>\n";
	   print OUT "        </table>\n";
	   print OUT '        <table width="435"', ">\n";
	   print OUT "        <tr>\n";
	}
	if ($count > $row_max) {
		print OUT "        </tr>\n";
		print OUT "        <tr>\n";
		$count=1;
	}
	$dir = &find_dir($name[1]);
		# Now print the individual bird picture
	print OUT '        <td valign="top" width="145"',
                           'style="font-family: arial; font-size: small; "',
	                   ">", "\n";
	print OUT "        <a id=\"thumb$number\" ",
		           "href=\"$dir/Images/$name[3].JPG \"",
			   "class=\"highslide\"\n";
	print OUT '            onclick="return hs.expand(this)">', "\n";
	print OUT "        <img src=\"$dir/Images_sm/$name[3].JPG\" ",
	                   "alt=\"Highslide JS\"\n";
	print OUT '        title="Click to enlarge" /></a>', "\n\n";
	print OUT '	   <!-- Caption for Image -->', "\n";
	print OUT "	   <div class='highslide-caption'",
			   "id=\'caption-for-thumb$number\'>\n";
	print OUT "	   $name[5]\n";
	print OUT "	   </div>\n";
	print OUT "	   <br />\n";
	print OUT '        <p style="font-size:smaller;">', "\n";
	$day = $name[1] - int($name[1]/100)*100;
	$month = int($name[1]/100);
	$month = $month - int($month/100)*100;
	$year = int($name[1]/10000);
	$date = $month . "/" . $day . "/" . $year;
	print OUT "	   $date <br /> $name[4]\n";
	print OUT "        </p>\n";
	print OUT "        </td>\n\n";
	$number = $number +1;
	$count = $count+1;

   }
}
   sub find_dir {
#	finds the directory where the images are.  The directory is a
#	combination of the year and the month
# 	category name of the bird
   $return_dir = int($_[0]/100);
   return $return_dir
   }

sub print_header1 {
	print OUT <<HTML;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
<html>
<head>
HTML
}

sub print_header2 {
	print OUT <<HTML;

<!-- 
    1) Reference to the file containing the javascript. 
       This file must be located on your server. 
-->
<script type="text/javascript" src="../HighSide/highslide/highslide.js"></script>


<!-- 
   2) Initialize the hs object and optionally override the settings
      defined at the top of the highslide.js file. The parameter
      hs.graphicsDir is important!
-->
<script type="text/javascript">

	// remove the registerOverlay call to disable the controlbar
	hs.registerOverlay(
    	{
    		thumbnailId: null,
    		overlayId: 'controlbar',
    		position: 'bottom left',
    		hideOnMouseOut: true,
    		opacity: 0.75
		}
	);
	
    hs.graphicsDir = 'highslide/graphics/';
    hs.outlineType = 'rounded-white';
    window.onload = function() {
        hs.preloadImages(5);
    }
</script>

<link rel=stylesheet href="../HighSide/high.css" type="text/css">

</head>


<body>
<div id="highslide-container"></div>
<!--
   4) Create an empty container for the Highslide popups to live in.
      This container must be defined directly after the body tag.
-->
<!-- Include Header -->
<!--#include virtual="birds_header.html"-->

<!-- Start main table and one row for holding both the menu and the
	body of the text -->
<table>
<tr>
   <td valign="top">

      <!-- Include menu -->
      <!--#include virtual="birds_menu.html"-->
   </td>
   <td width="20"></td>
   <td valign="top">
HTML
}

sub print_end {
	print OUT <<HTML;
        </td>
	</tr>
	</table>
	<br />
	<a href="#atop"><b>Top of Page</b></a>
   </td>
</tr>
</table>
<!-- 
	7 This is the markup for the controlbar. The controlbar is tied
	to the expander in the script tag at the top of the file.
-->
<div id="controlbar" class="highslide-overlay controlbar">
	<a href="javascript:void(0)" onclick="return hs.previous(this)" title="Previous (left arrow key)"></a>
	<a href="javascript:void(0)" onclick="return hs.next(this)" title="Next (right arrow key)"></a>
    <a href="javascript:void(0)" class="highslide-move" title="Click and drag to move"
    	style="margin-left: 10px"></a>
    <a href="javascript:void(0)" onclick="hs.close(this)" title="Close"></a>

</div>
</body>
</html>
HTML
}

