use zUtil;
use POSIX qw(strftime);

sub todayInHistoryHandler
{
   my $html = shift;
   my $maxItems = shift;
   my( @rValue ) = ();
   my( @holder ) = ();
   my( $table, $row, $value, $day, $count, $date, $blurb );
   my( $done );
   my( $item );
   my( $index );
   my( $time );

   print "------------------ todayInHistoryHandler  --------------------\n";
   #print "$html\n";
   #exit;

   $time = strftime( "%B %d", localtime() );
   $time =~ s/0(\d)/$1/gsi;

   print "time: $time\n";

   $index = 6;
   $table = zUtil::getNthItemNew( $index, $html, "<table", "</table>" );
   # bva. as of august 20 2006, we don't need to search anymore, and the
   #      date isn't embedded in the table.
   #while( !($table =~ /$time/gsi) && $index < 40 )
   #{
   #    $table = zUtil::getNthItemNew( $index, $html, "<table", "</table>" );
   #    $index++;
   #}

   print "table: $table\n";
   #exit;

   $count = 0;
   $done = 0;
#   while( ($table =~ /<tr.*?>.*?<td.*?>.*?<b>(.*?)<\/b><\/td>a.*?<td>.*?<\/td>.*?<td.*?>(.*?)<\/td>/gsi) && 
   while( ($table =~ /<tr.*?>.*?<td.*?>.*?<b>(.*?)<\/b><\/td>.*?<td.*?>.*?<\/td>.*?<td.*?>(.*?)<\/td>.*?<\/tr>/gsi) && 
           !$done )
   {
       $date = $1;
       $blurb = $2;
       
       print "date: $date\n";
       
       if( !($date =~ /born on/gsi) )
       {
           if( $blurb =~ /(.*?)<p>/si )
           {
               $blurb = $1;
           }
           $blurb =~ s/\<.+?\>//gs;
           print "date: $date, item: $blurb\n\n";
           
           unshift @holder,
           {
               'year' => $date,
               'fact' => $blurb
           };
           
           $count++;
           if( count > 30  )
           {
               $done = 1;
           }
       }
       else
       {
           $done = 1;
       }
   }
#    exit;
   
   ## holder now has items going from oldest to newest ... we
   ## want to start from the end of it and push "maxItems" onto
   ## the array we are returning. This will give us "x" "latest"
   ## items.
   $count = 0;
   while( $count < $maxItems + 1 )
   {
       $item = shift @holder;
       push @rValue, $item;
       $count++;
   }

#    exit;

   return @rValue;
}

return 1;
