#!/usr/bin/perl

use strict;

use DBI;

use CGI ":all";
use lib qw(/usr/local/modules/);
use birds;

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

# params
my ($bird) = param('bird');

my ($dothis) = param('dothis');	
if (!$dothis) {
	$dothis = 1;
}

# html
my ($spaces) = "&nbsp;&nbsp;&nbsp;";

# start

my ($id) = &birds::lookup("species","id","nospace_name","$bird");
if (!$id) {
	$dothis = 0;
}

my ($dbh) = &birds::connect_to_db;
my ($cur,$cur2,$timestamp) = "";

print header,start_html(-title=>"Migrant Bird Stopover Habitat Survey");

print "<CENTER>
<TABLE BORDER=0><TR>
  <TD ALIGN=CENTER COLSPAN=2><IMG SRC=\"../graphics/conte_header.jpg\"><BR>
<H2>Bird Photo Directory</H2>\n";

if ($dothis == 0) {
	print "some error";
}

if ($dothis == 1) {

	my ($dbh) = &birds::connect_to_db;
	my ($temp) = "";

        $cur = $dbh->prepare("SELECT common_name,latin_name,sort_int,nospace_name,image_c,species_code FROM species WHERE id=$id");
        $cur->execute;
        $DBI::err and die "Prepare/Execute error: $DBI::err .... $DBI::errstr";

        while (my($name,$latin,$sort_int,$nospace,$imgcopy,$scode) = $cur->fetchrow_array) {

		$temp = lc $scode;
		&make_nav($sort_int,"$temp.html");

		print "<IMG SRC=\"../birdpixs/images/$nospace.jpg\" ALT=\"$name\"><BR>
<B>$name (<I>$latin</I>)</B><BR>(c) $imgcopy\n";


	}


}

print "</TABLE>\n";

$dbh->disconnect;


# subs
sub make_nav {

	my ($sort_int,$acctfile) = @_;
	my ($temp,$temp_name) = 0;

	# make the nav table
	print "<!-- nav -->
 <A HREF=\"./bird-details.pl?bird=$bird\"><IMG SRC=\"../graphics/speciesaccounts.jpg\" WIDTH=126 HEIGHT=28 BORDER=0></A>
 <A HREF=\"./species.pl\"><IMG SRC=\"../graphics/speciesindex.jpg\" WIDTH=126 HEIGHT=28 BORDER=0></A><HR>";

	print "<TABLE BORDER=0><TR>\n";
	if ($sort_int > 1) {
		$temp = ($sort_int - 1);
		$temp_name = &birds::lookup("species","nospace_name","sort_int","$temp");

		print "   <TD WIDTH=60><A HREF=\"./show-bird.pl?bird=$temp_name\"><IMG SRC=\"../birdpixs/images/previous.gif\" HEIGHT=30 WIDTH=30 BORDER=0 ALT=\"previous\"></A></TD>\n";
	}

	print "   <TD WIDTH=60><A HREF=\"./photo-directory.pl\"><IMG SRC=\"../birdpixs/images/home.gif\" HEIGHT=30 WIDTH=30 BORDER=0 ALT=\"home\"></A></TD>\n";

	$temp = ($sort_int + 1);
	$temp_name = &birds::lookup("species","nospace_name","sort_int","$temp");

	if ($temp_name) { 		# if we found something
		print "   <TD WIDTH=60><A HREF=\"./show-bird.pl?bird=$temp_name\"><IMG SRC=\"../birdpixs/images/next.gif\" HEIGHT=30 WIDTH=30 BORDER=0 ALT=\"next\"></A></TD>\n";
	}

	print "</TABLE><!-- end nav -->\n\n";


} # end make_nav

# reference
#
#mysql> desc species;
#+--------------+--------------+------+-----+---------+----------------+
#| Field        | Type         | Null | Key | Default | Extra          |
#+--------------+--------------+------+-----+---------+----------------+
#| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
#| common_name  | varchar(128) | YES  |     | NULL    |                |
#| nospace_name | varchar(64)  | YES  |     | NULL    |                |
#| latin_name   | varchar(128) | YES  |     | NULL    |                |
#| sort_int     | int(11)      | YES  |     | 0       |                |
#| species_code | varchar(8)   | YES  |     | NULL    |                |
#| image_c      | varchar(32)  | YES  |     | NULL    |                |
#| acct_file    | varchar(128) | YES  |     | NULL    |                |
#| graph_file   | varchar(128) | YES  |     | NULL    |                |
#| last_update  | datetime     | YES  |     | NULL    |                |
#+--------------+--------------+------+-----+---------+----------------+
#10 rows in set (0.03 sec)

# revisions
# 2/25/2015 - Suzanne - program created
