#!/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 ($nospace) = "";

if (!$bird) {
	$dothis = 0;
}

my ($id) = &birds::lookup("species","id","nospace_name","$bird");
my ($code) = &birds::lookup("species","species_code","id","$id");

my ($uc_code) = uc $code;
my ($lc_code) = lc $code;

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

print header,start_html(-title=>"$uc_code Conte Survey Species Account");

print "<CENTER>
<TABLE BORDER=0>\n";

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

if ($dothis == 1) {

	&make_buttons;

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

	my ($sampsize,$intro,$obsv,$uc_name) = "";
	my ($graph_hp) = "_HP.gif";
	my ($graph_sp) = "_SP.gif";
	my ($graph_sh) = "_SH.gif";
	my ($graph_page_suffix) = "_graphs.html";
	my ($graph_link) = "$lc_code$graph_page_suffix";

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

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

		$sampsize = &birds::lookup("species_data","sample_size","species_id","$id");
		$intro = &birds::lookup("species_data","intro_text","species_id","$id");
		$obsv = &birds::lookup("species_data","obsv_text","species_id","$id");

		$uc_name = uc $name;


		print "<TR><TD ALIGN=CENTER>\n\n<!-- content border table -->";
		print "<TABLE BORDER=1><TR>\n   <TD ALIGN=CENTER>\n\n";

		print "<!-- inner content --><TABLE BORDER=0><TR>
   <TD ALIGN=LEFT COLSPAN=2><B>$uc_name <I>($latin)</I> $uc_code $spaces Sample size: $sampsize</B></TD></TR>
<TR>
   <TD ALIGN=LEFT WIDTH=410><A HREF=\"./show-bird.pl?bird=$name\"><IMG SRC=\"../birdpixs/images/$nospace-med.jpg\" WIDTH=400 BORDER=0></A></TD>
   <TD ALIGN=CENTER WIDTH=310><A HREF=\"\#maps\"><IMG SRC=\"../maps/$lc_code.gif\" BORDER=0 WIDTH=200></A></TD></TR>
<TR>
   <TD COLSPAN=2 ALIGN=LEFT><HR>$intro
<HR><BR><A NAME=\"graphs\"><B>Graphs</B></A></TD></TR>
<TR>
   <TD ALIGN=CENTER><A HREF=\"./graphs.pl?bird=$bird\#HP\"><IMG SRC=\"../graphs/$lc_code$graph_hp\" BORDER=0 WIDTH=350 HEIGHT=270></A></TD>
   <TD ALIGN=LEFT VALIGN=TOP><B>Observations</B><BR><BR>$obsv<BR><BR></TD></TR>
<TR>
   <TD ALIGN=CENTER><A HREF=\"./graphs.pl?bird=$bird\#SP\"><IMG SRC=\"../graphs/$lc_code$graph_sp\" BORDER=0 WIDTH=350 HEIGHT=270></A></TD>
   <TD ALIGN=CENTER><A HREF=\"./graphs.pl?bird=$bird\#SH\"><IMG SRC=\"../graphs/$lc_code$graph_sh\" BORDER=0 WIDTH=350 HEIGHT=270></A></TD></TR>
<TR>
   <TD ALIGN=CENTER COLSPAN=2><A NAME=\"maps\"><HR></A><IMG SRC=\"../maps/$lc_code.gif\"></TD></TR>
\n";

		print "</TABLE><!-- end inner content -->

</TR></TABLE><!-- end border table-->\n\n"; 



	}

}

print "</TR></TABLE>\n";

$dbh->disconnect;


# subs
sub make_buttons {

	# make the nav table
   	print "<TR>\n   <TD ALIGN=RIGHT><!-- buttons -->
<TABLE BORDER=0 CELLSPACING=3 CELLPADDING=0>
<TR>
   <TD ALIGN=CENTER><A HREF=\"#graphs\"><IMG SRC=\"../graphics/button-graphs.gif\" BORDER=0></A></TD>
   <TD ALIGN=CENTER><A HREF=\"#maps\"><IMG SRC=\"../graphics/button-maps.gif\" BORDER=0></A></TD>
   <TD ALIGN=CENTER><A HREF=\"./species.pl\"><IMG SRC=\"../graphics/button-species.gif\" BORDER=0></A></TD>
   <TD ALIGN=CENTER><A HREF=\"../index.html\"><IMG SRC=\"../graphics/button-main.gif\" BORDER=0></A></TD></TR>
</TABLE><!-- end buttons -->\n\n</TD></TR>\n";


} # end make_buttons

# 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
