#!/usr/bin/perl -w

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

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

    if ( ! $title )
	{ $title = "Strictly Bluegrass Festival 3: Oct 3 - 5, 2003"; }

    print <<_EOF_;
<HTML>

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

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

function photo_popup (which)
{
    window.open ("gallery/photoshow.cgi?show=" + which + "&num=0", '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; }

.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="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>THANK YOU TO ALL WHO TURNED OUT AND HELPED MAKE THIS YEAR'S
	    FESTIVAL SUCH A HUGE SUCCESS!</B>
	    </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');" TARGET="sbf_pix" HREF="gallery/photoshow.cgi?show=quick&num=0">View Photo Gallery</A>
		</TH>
	    </TR>
	    </TABLE>
	    </DIV>

	    <P>
	    <B>
	    Here is the performance schedule from the 2003 Hardly Strictly
	    Bluegrass Festival:
	    </B>
	    </P>

_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 ( $artist eq $aname )
		{
		    my $ed = CGI::escape ($dref->{DATE});
		    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_2003::ARTISTS{$key}->{NAME}"; }
	else
	    { $title = "SBF: Artists"; }
    }
}

header ($title, $popup);

if ( ! $show ) 
{
    print <<_EOF_;
<P>
Please select one of the following options for the 2003 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_2003::ARTISTS{$key};
	my $path;
	my @plays_text;
	my $tmp;

	my $an = $key;
	$an =~ tr/A-Z/a-z/;
	$path = "/web/www.strictlybluegrass.com/www/2003/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="/2003/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_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>
_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_2003::ARTISTS{$a}->{NAME});
		my $cmp_b = uc($SBF_2003::ARTISTS{$b}->{NAME});

		## skip over any leading html tags
		$a =~ s/^<[^>]+>//;
		$b =~ s/^<[^>]+>//;
		$cmp_a cmp $cmp_b;
	    } keys (%SBF_2003::ARTISTS) )
	{
	    my $art = $SBF_2003::ARTISTS{$key};
	    my $play_2003;

	    $play_2003 = &plays ($key, 1, @SBF_2003::SCHEDULE);

	    if ( ! $play_2003 )
		{ next; }

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

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

	print "</TABLE>\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_2003::SCHEDULE )
    {
	if ( $key && $key ne $dref->{DATE} )
	    { next; }
    
	print <<_EOF_;
<TR>
    <TH COLSPAN=3 BGCOLOR="#C8C296" CLASS="table_header">
    $dref->{DATE} ($dref->{TIME})
    </TH>
</TR>
<TR VALIGN="top">
_EOF_

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

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

		my $aref = $SBF_2003::ARTISTS{$art};
		if ( $showtime ) { print "$showtime "; }
		print "<A HREF='#' onClick='return art_popup(\"$art\");'>$aref->{NAME}</A><BR>\n";
	    }
	    print "</TD>\n";
	}
    }

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

footer ($popup);
