#!/usr/bin/perl -w

use strict;
use CGI;
use SBF_2004;
use SBF_2003;
use SBF_2002;
use SBF_2001;
use Image::Size;

our $DISPLAY_TIMES = 1;

sub header
{
    my $title = shift;
    my $popup = shift;

    if ( ! $title )
	{ $title = "Hardly Strictly Bluegrass Festival 4: Oct 1 - 3, 2004"; }

    print <<_EOF_;
<HTML>

<HEAD>
<TITLE>
$title
</TITLE>
</HEAD>

<SCRIPT LANGUAGE="JavaScript">
<!--

function photo_popup (which, num)
{
    window.open ("gallery/photoshow.cgi?show=" + which + "&num=" + num, 'sbf_pix',
        'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=630,height=630');
    return false;
}

function art_popup (artist)
{
    var win = window.open ("$ENV{SCRIPT_NAME}?show=artists&key=" + artist + "&popup=1",
	'art_popup',
	'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=300,height=300');
    win.focus();
    return false;
}
// -->
</SCRIPT>

<STYLE TYPE="text/css">
<!-- 
body, p, table, div {
    font-family: Trebuchet MS, arial, helvetica, sans-serif;
    font-size: 11px;
}
b.artist_name { font-size: 14px; }

ul.artist_list { margin: 0.5em 0 0 0; padding: 0; }
ul.artist_list li {
    list-style-type: none;
    margin: 0 0 0.5em 1em;
    padding: 0;
    text-indent: -1em;
}

.table_header {
    border: 1px solid black;
    text-transform: uppercase;
    font-size: 11px;
    color: #4C4C19;
    background-color: #C8C296;
}

a:link { color: #473E16; text-decoration: none; }
a:link:hover { color: #0000FF; text-decoration: underline; }
a:visited { color: #473E16; text-decoration: none; }
a:visited:hover { color: #0000FF; text-decoration: underline; }

// -->
</STYLE>

</HEAD>


_EOF_

    if ( $popup )
    {
	print <<_EOF_;
<BODY BGCOLOR="#000000" TOPMARGIN=0 LEFTMARGIN=0 MARGINHEIGHT=0 MARGINWIDTH=0>
<TABLE WIDTH="300" BORDER="0" CELLSPACING="0" CELLPADDING="0">
<TR>
    <TD COLSPAN=3><IMG WIDTH="300" HEIGHT=12 SRC="/2003/images/frame-top.jpg"></TD>
</TR>
<TR>
    <TD ROWSPAN=2><IMG WIDTH=12 HEIGHT=275 SRC="/2003/images/frame-left.jpg"></TD>
    <TD WIDTH="276" BGCOLOR="#F2E9C0" VALIGN="top">
    <DIV ALIGN="center">
    <IMG
	ALT="Strictly Bluegrass"
	HEIGHT=26 WIDTH=275
	SRC="/2003/images/sbf-popup-hdr.gif">
    </DIV>
_EOF_
	return;
    }

    print <<_EOF_;
<BODY BGCOLOR="#000000">
<DIV ALIGN="center">

<TABLE WIDTH="600" BORDER="0" CELLSPACING="0" CELLPADDING="0">
<TR>
    <TD WIDTH="600" COLSPAN="3"><IMG ALT="" SRC="/2003/images/frame-top.jpg" WIDTH=600 HEIGHT="25"></TD>
</TR>

<TR>
    <TD BACKGROUND="/2003/images/frame-left.jpg" WIDTH="25"><IMG
	ALT=""
	WIDTH=25 HEIGHT="350"
	SRC="/2003/images/frame-left.jpg"></TD>
    <TD BGCOLOR="#F2E9C0" WIDTH="550">
	<IMG SRC="/2003/images/spacer.gif" WIDTH="550" HEIGHT="1"><BR>
	<FONT FACE="times">
	<TABLE WIDTH=550 BORDER=0 CELLPADDING=0 CELLSPACING=0>
	<TR>
	    <TD><IMG
		BORDER="0"
		ALT=""
		SRC="/2003/images/header-pic.gif"
		WIDTH=118 HEIGHT=150 BORDER=0></TD>
	    <TD><A HREF="/"><IMG
		BORDER="0"
		ALT=""
		WIDTH=432 HEIGHT=126
		SRC="images/header-txt.gif"></A></TD>
	</TR>
	</TABLE>

	<BR>

	<TABLE WIDTH="550" HEIGHT="444" BACKGROUND="/2003/images/bg-house.gif" CELLSPACING="0" CELLPADDING="0">
	<TR VALIGN="top">
	    <TD WIDTH="550">
	    <!--
	    <B>Note: Schedule and show times are subject to change.</B>

	    <P>
	    <B>
	    Here is the performance schedule from the 2004 Hardly Strictly
	    Bluegrass Festival. If you have questions, please 
	    check out our <A HREF="info.shtml">Frequent Questions page</A>.
	    </B>
	    </P>
	    -->

	    <TABLE ALIGN=center BORDER=0 CELLSPACING=0 CELLPADDING=0>
	    <TR><TD><IMG
		SRC="crowd-sm.jpg"
		ALT="" BORDER=1
		WIDTH=545 HEIGHT=126></TD></TR>
	    <TR><TD ALIGN=right>
		<FONT SIZE=1>Photo by Ray Bussolari</FONT>
	    </TD></TR>
	    </TABLE>
	    <P ALIGN=center>
	    <FONT SIZE=3>
	    Thank you and we'll see you next year!
	    (Doc Watson is already confirmed)
	    </FONT>
	    </P>

	    <P>
	    Congratulations to <A HREF="http://www.ibma.org/">IBMA</A>
	    winners Del McCoury (Entertainer of the
	    Year, Album of the Year), Ricky Skaggs &amp; Kentucky
	    Thunder (Instrumental Group of the Year), Bryan Sutton
	    of Hot Rize (Guitarist of the Year), and original
	    Strictly Bluegrass participant
	    Michael Cleveland of Coon Creek (Fiddler of the Year,
	    Instrumental Album of the Year)!
	    </P>


	    <P>
	    While we put together our full archive presentation,
            please enjoy browsing through a quick photo gallery we've
            put together for you.
	    </P>

	    <DIV ALIGN="center">
	    <TABLE WIDTH="400" CELLSPACING=5 CELLPADDING=3>
	    <TR>
		<TH BGCOLOR="#C8C296" CLASS="table_header">
		<A onClick="return photo_popup ('quick', 0);" TARGET="sbf_pix"
		HREF="gallery/photoshow.cgi?show=quick&num=0">View Photo
		Gallery</A>
		</TH>
		<TH BGCOLOR="#C8C296" CLASS="table_header">
		<A HREF="review.cgi">Read Show Reviews</A>
		</TH>
	    </TR>
	    </TABLE>
	    </DIV>

_EOF_
}

sub footer
{
    my $popup = shift;

    if ( $popup )
    {
	print <<_EOF_;
    </TD>
    <TD ROWSPAN=2 WIDTH=12><IMG WIDTH=12 HEIGHT=275 SRC="/2003/images/frame-right.jpg"></TD>
</TR>
<TR>
    <TD ALIGN='center' VALIGN=bottom BGCOLOR="#F2E9C0">
	[ <A HREF="javascript:window.close()">close window</A> ]
    </TD>
</TR>
<TR>
    <TD COLSPAN=3><IMG WIDTH=300 HEIGHT=12 SRC="/2003/images/frame-bottom.jpg"></TD>
</TR>
</TABLE>
_EOF_
    }
    else
    {
	print <<_EOF_;
	    <P>
	    </TD>
	</TR>
	<TR>
	    <TD VALIGN=bottom>

	    <TABLE WIDTH="550" BORDER="0" CELLSPACING="0" CELLPADDING="0">
	    <TR>
		<TD ALIGN=left>
		    [ <A HREF="/">home page</A> ]
		</TD>
		<TD ALIGN=right>
		    [ site by <A TARGET="chime"
			HREF="http://www.chimeinteractive.com/">chime interactive</A> ]
		</TD>
	    </TR>
	    </TABLE>

	    </TD>
	</TR>
	</TABLE>
	</FONT>
    </TD>
    <TD BACKGROUND="/2003/images/frame-right.jpg" WIDTH="25"><IMG
	ALT="" WIDTH="25" HEIGHT="350"
	SRC="/2003/images/frame-right.jpg"></TD>
</TR>

<TR>
    <TD WIDTH="600" COLSPAN="3"><IMG ALT="" SRC="/2003/images/frame-bottom.jpg" WIDTH=600 HEIGHT="25"></TD>
</TR>
</TABLE>

</DIV>
_EOF_
    }

    print <<_EOF_;
</BODY>
</HTML>
_EOF_
}

sub plays
{
    my $artist = shift;
    my $inc_link = shift;
    my @schedule = (@_);
    my $str = '';

    foreach my $dref ( @schedule )
    {
#	print "DATE: $dref->{DATE}\n";
	foreach my $sref ( @{$dref->{STAGES}} )
	{
#	    print "Stage: $sref->{NAME}\n";
	    foreach my $art ( @{$sref->{ARTISTS}} )
	    {
		my $st = '';
		my $aname = $art;

		if ( $art =~ /^(.+)\|(.+)$/ )
		{
		    $aname = $1;
		    $st = " ($2)";
		}

		if ( ! $DISPLAY_TIMES )
		    { undef $st; }
		     
		if ( $artist eq $aname )
		{
		    my $ed = CGI::escape ($dref->{DATE});
		    if ( $str ) { $str .= "<BR>\n"; }
		    if ( $inc_link )
		    {
			$str .= "  <A TARGET='sbf_main' HREF=\"$ENV{SCRIPT_NAME}?show=day&key=$ed\">$dref->{DATE}</A>, $sref->{NAME}${st}\n";
		    }
		    else
		    {
			$str .= "  $dref->{DATE}, $sref->{NAME}${st}\n";
		    }
		}
	    }
	}
    }

    return $str;
}

###
## MAIN BODY
###

my $query = new CGI;
my $show = $query->param('show');
my $popup = $query->param('popup');
my $key = $query->param('key');
my $title;

print $query->header;

if ( $show )
{
    if ( $show eq 'artists' )
    {
	if ( $key )
	    { $title = "SBF: $SBF_2004::ARTISTS{$key}->{NAME}"; }
	else
	    { $title = "SBF: Artists"; }
    }
}

header ($title, $popup);

if ( ! $show ) 
{
    print <<_EOF_;
<P>
Please select one of the following options for the 2004 lineup:
</P>

<TABLE WIDTH="400" CELLSPACING=5 CELLPADDING=3>
<TR>
    <TH BGCOLOR="#C8C296" CLASS="table_header">
    <A HREF="$ENV{SCRIPT_NAME}?show=day">View Full Schedule</A>
    </TH>
    <TH BGCOLOR="#C8C296" CLASS="table_header">
    <A HREF="$ENV{SCRIPT_NAME}?show=artists">View Full Artist List</A>
    </TH>
</TR>
</TABLE>
_EOF_
}

if ( $show eq 'artists' )
{
    if ( $key )
    {
	my $art = $SBF_2004::ARTISTS{$key};
	my $path;
	my @plays_text;
	my $tmp;

	my $an = $key;
	$an =~ tr/A-Z/a-z/;
	$path = "/web/www.strictlybluegrass.com/www/2004/images/artists/fr-$an.gif";
	if ( -e $path )
	{
	    my $align = "";
	    my $br = "<BR>\n";
	    my ($width, $height) = Image::Size::imgsize ($path);
	    if ( $width < $height ) { $align = "ALIGN='left'"; }
	    else { $br = "<BR>\n"; }
	    print <<_EOF_;
<IMG
    ALT="$art->{NAME}"
    $align
    WIDTH="$width" HEIGHT="$height"
    VSPACE=3 HSPACE=3
    SRC="/2004/images/artists/fr-$an.gif">$br
_EOF_

	}

	print "<B CLASS='artist_name'>$art->{NAME}</B><BR>\n";
#	print "<B>Plays:</B> ";
	print &plays ($key, 1, @SBF_2004::SCHEDULE);

	$tmp = &plays ($key, 0, @SBF_2003::SCHEDULE);
	if ( $tmp )
	    { push (@plays_text, &plays ($key, 0, @SBF_2003::SCHEDULE)); }

	$tmp = &plays ($key, 0, @SBF_2002::SCHEDULE);
	if ( $tmp )
	    { push (@plays_text, &plays ($key, 0, @SBF_2002::SCHEDULE)); }

	$tmp = &plays ($key, 0, @SBF_2001::SCHEDULE);
	if ( $tmp )
	    { push (@plays_text, &plays ($key, 0, @SBF_2001::SCHEDULE)); }

	if ( $art->{URL} )
	{
	    my @urls = @{$art->{URL}};
	    print "<BR>\n";
#	    printf "<B>URL%s:</B> ",
#		scalar (@urls) > 1 ? "s" : "";
	    foreach my $url ( @urls )
	    {
		my $show_url = $url;
		$show_url =~ s|^http://||i;
		$show_url =~ s|^([^/]+)/.*|$1|;
		print "<A TARGET=\"sbf_artist\" HREF=\"$url\">$show_url</A>\n";
	    }
	}

	if ( scalar (@plays_text) > 0 )
	{
	    print "<P>\n<U>Also played</U>:<BR>\n" . join ("<BR>\n", @plays_text);
	}
    }
    else
    {
	print <<_EOF_;
<TABLE CELLSPACING=5 CELLPADDING=3 ALIGN="right">
<TR>
    <TH BGCOLOR="#C8C296" CLASS="table_header">
    <A HREF="$ENV{SCRIPT_NAME}?show=day">View Schedule by Day</A>
    </TH>
</TR>
</TABLE>
<BR CLEAR=all>
_EOF_

	print "<TABLE CELLSPACING=0 CELLPADDING=1>\n";
	print "<TH ALIGN=left>Artist</TH><TH ALIGN=left>Date/Stage</TH>\n";
	foreach my $key ( sort { 
		my $cmp_a = uc($SBF_2004::ARTISTS{$a}->{NAME});
		my $cmp_b = uc($SBF_2004::ARTISTS{$b}->{NAME});

		## skip over any leading html tags
		$cmp_a =~ s/^<[^>]+>//;
		$cmp_b =~ s/^<[^>]+>//;

		## skip leading "the"
		$cmp_a =~ s/^The //i;
		$cmp_b =~ s/^The //i;

		$cmp_a cmp $cmp_b;
	    } keys (%SBF_2004::ARTISTS) )
	{
	    my $art = $SBF_2004::ARTISTS{$key};
	    my $play_2004;

	    $play_2004 = &plays ($key, 1, @SBF_2004::SCHEDULE);

	    if ( ! $play_2004 )
		{ next; }

	    print <<_EOF_;
<TR VALIGN=top>
    <TD><A HREF="$ENV{SCRIPT_NAME}?show=artists&key=$key"
	onClick='return art_popup("$key");'>$art->{NAME}</A></TD>
    <TD>
_EOF_

	    print $play_2004;
	    print "    </TD>\n</TR>\n";
	}

	print "</TABLE><BR>\n";
    }
}
elsif ( $show eq 'day' )
{
    print <<_EOF_;
<DIV ALIGN="right">
<TABLE CELLSPACING=5 CELLPADDING=3>
<TR>
_EOF_

    if ( $key )
    {
	print <<_EOF_;
    <TH BGCOLOR="#C8C296" CLASS="table_header">
    <A HREF="$ENV{SCRIPT_NAME}?show=day">View Full Schedule</A>
    </TH>
_EOF_
    }

    print <<_EOF_;
    <TH BGCOLOR="#C8C296" CLASS="table_header">
    <A HREF="$ENV{SCRIPT_NAME}?show=artists">View Artist List</A>
    </TH>
</TR>
</TABLE>
</DIV>
_EOF_
 
    print <<_EOF_;
<TABLE CLASS="schedule" CELLPADDING=4 CELLSPACING=1 BORDER=0 WIDTH="550">
_EOF_

    foreach my $dref ( @SBF_2004::SCHEDULE )
    {
	if ( $key && $key ne $dref->{DATE} )
	    { next; }
    
	print <<_EOF_;
<TR>
    <TH COLSPAN=4 WIDTH="25%" BGCOLOR="#C8C296" CLASS="table_header">
    $dref->{DATE} ($dref->{TIME})
    </TH>
</TR>
<TR VALIGN="top">
_EOF_

	if ( $dref->{NOTES} )
	{
	    print <<_EOF_;
<TD COLSPAN=4>
$dref->{NOTES}
</TD>
</TR>
<TR VALIGN="top">
_EOF_
	}

	foreach my $sref ( @{$dref->{STAGES}} )
	{
	    print "<TD WIDTH='25%'><U><B>$sref->{NAME}</B></U><BR>\n";
	    print "<UL CLASS='artist_list'>\n";
	    foreach my $art ( @{$sref->{ARTISTS}} )
	    {
		my $showtime;
		if ( $art =~ /^(.+)\|(.+)$/ )
		{
		    $art = $1; 
		    $showtime = $2;
		}

		if ( ! $DISPLAY_TIMES )
		    { undef $showtime; }

		print "<LI>";
		my $aref = $SBF_2004::ARTISTS{$art};
		if ( $showtime )
		    { print "$showtime "; }
		printf "<A HREF='#' onClick='return art_popup(\"$art\");'>%s</A><BR>\n",
		    $aref ? $aref->{NAME} : $art;
	    }
	    print "</UL></TD>\n";
	}
    }

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

footer ($popup);
