#!/usr/bin/perl -w

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

our $DISPLAY_TIMES = 1;

sub file2caption
{
    my $file = shift;
    my $caption = '';
    my $t;

    $file =~ s/^.*\///g;
    $file =~ s/^IMG_\d+//g;
    $file =~ s/-/, /g;
#    $file =~ s/-.+/ /g;
#    $file =~ s/=(.+)/ - $1/;
    $file =~ s/=(.+)//;
    $file =~ s/\.(jpg|jpeg|gif)$//gi;
    $file =~ s/_/ /g;

    foreach $t ( split (/\s/, $file) )
    {
	if ( $caption ) { $caption .= " "; }
	$caption .= ucfirst ($t);
    }

    $caption =~ s/(\s+)and(\s+)/$1&amp;$2/gi;
    return $caption;
}

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

    if ( ! $title )
#	{ $title = "Hardly Strictly Bluegrass Festival 5: Oct 1 - 2, 2005"; }
#	{ $title = "Hardly Strictly Bluegrass Festival 5: Sep 30 - Oct 2, 2005"; }
	{ $title = "Hardly Strictly Bluegrass Festival 6: Oct 6 - Oct 8, 2006"; }

    ### NEW VERISON
#    my @ary = glob ("gallery/quick/*.jpg");
    my @ary = glob ("gallery/full/*.jpg");
    my $pic1_index = int(rand(scalar(@ary)));
    my $pic1 = $ary[$pic1_index];
    my $pic1_caption = file2caption($pic1);

    my $pic2_index = int(rand(scalar(@ary)));
    my $pic2 = $ary[$pic2_index];
    my $pic2_caption = file2caption($pic2);

    my $pic3_index = int(rand(scalar(@ary)));
    my $pic3 = $ary[$pic3_index];
    my $pic3_caption = file2caption($pic3);

    my $pic4_index = int(rand(scalar(@ary)));
    my $pic4 = $ary[$pic4_index];
    my $pic4_caption = file2caption($pic4);

    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>
	    -->

	    <table cellspacing="0" cellpadding="0" border="0">
	    <tr valign="top"><td>


	    <div style="width: 250px; border: 1px solid; background-color: #E8E2D2; margin-right: 15px; text-align: center; text-align: left;">
		<div style="background-color: #999966; font-size: 12px; font-weight: bold; padding: 3px; border-bottom: 1px solid; text-align: center;">
		SEE YOU NEXT YEAR!
		</div>
		<div style="padding: 10px; font: 11px arial; text-align: justify;">
		Thanks to all who joined us this year! Join us on <b>Oct 6 &amp 7,
		2007</b> when we'll be doing it again -- Doc Watson is already confirmed!
		</div>
	    </div>

<!--
	    <div style="width: 250px; border: 1px solid; background-color: #E8E2D2; margin-right: 15px; text-align: center; text-align: left;">
		<div style="background-color: #999966; font-size: 12px; font-weight: bold; padding: 3px; border-bottom: 1px solid; text-align: center;">
		:: PRINT AT HOME KIT ::
		</div>
		<div style="padding: 10px; font: 11px arial; text-align: justify;">
		Arrive at the park in style with your own copies of the HSB06
		map, schedule, and artist bios! Simply download these PDF files
		and print them out at home before sauntering down to the park.
		<br><br>
		<b>
		<a target="printkit" href="/2006/HSB6_map.pdf">HSB 2006 Map</a><br>
		<a target="printkit" href="/2006/HSB6_schedule.pdf">HSB 2006 Schedule</a><br>
		<a target="printkit" href="/2006/HSB6_bios.pdf">HSB 2006 Bios</a>
		</b>
		<br><br>
		<span style="font-size: 10px">You will need a PDF viewer such
		as <b><a target="printkit" href="http://www.adobe.com/products/acrobat/readstep2.html">Acrobat Reader</a></b>
		to view and print these files.</span>
		</div>
	    </div>
-->

	    </td>
	    <td>

		<div style="width: 250px; border: 1px solid; background-color: #E8E2D2; margin-right: 15px; text-align: center; text-align: left;">
		    <div style="background-color: #999966; font-size: 12px; font-weight: bold; padding: 3px; border-bottom: 1px solid; text-align: center;">
		    2006 EDITORIAL
		    </div>
		    <div style="padding: 10px; font: 11px arial;">
		    Read reports from the festival.<br><br>
		    <a href="editorial/Friday.shtml">Friday</a> |
		    <a href="editorial/Saturday.shtml">Saturday</A> |
		    <a href="editorial/Sunday.shtml">Sunday</A>
		    </div>
		</div>
	    </td>
	</tr>
	</table>

	<P align=center STYLE="background-color: #E8E2D2; font-size: 11px; border: 1px solid #473E16; padding: 2px">random photos from the gallery <a href="javascript:void(photo_popup('full',0));">[ view all ]</a></P>
	<TABLE STYLE="font-size: 10px">
	<TR VALIGN=top ALIGN=center>
	<TD WIDTH="150"><A onClick="return photo_popup('full', $pic1_index);" TARGET="sbf_pix" HREF="gallery/photoshow.cgi?show=full&num=$pic1_index"><IMG
	    ALT=""
	    BORDER=1 HEIGHT=85 xWIDTH=150 VSPACE=2
	    SRC="$pic1"></A><BR>$pic1_caption</TD>
	<TD WIDTH="150"><A onClick="return photo_popup('full', $pic2_index);" TARGET="sbf_pix" HREF="gallery/photoshow.cgi?show=full&num=$pic2_index"><IMG
	    ALT=""
	    BORDER=1 HEIGHT=85 xWIDTH=150 VSPACE=2
	    SRC="$pic2"></A><BR>$pic2_caption</TD>
	<TD WIDTH="150"><A onClick="return photo_popup('full', $pic3_index);" TARGET="sbf_pix" HREF="gallery/photoshow.cgi?show=full&num=$pic3_index"><IMG
	    ALT=""
	    BORDER=1 HEIGHT=85 xWIDTH=150 VSPACE=2
	    SRC="$pic3"></A><BR>$pic3_caption</TD>
	<TD WIDTH="150"><A onClick="return photo_popup('full', $pic4_index);" TARGET="sbf_pix" HREF="gallery/photoshow.cgi?show=full&num=$pic4_index"><IMG
	    ALT=""
	    BORDER=1 HEIGHT=85 xWIDTH=150 VSPACE=2
	    SRC="$pic4"></A><BR>$pic4_caption</TD>
	</TR>
	</TABLE>
	</DIV>

		<P style="font-size: 12px;">
		<B>
		See below for the 2006 Hardly Strictly
		Bluegrass Festival schedule. If you have questions, please 
		check out our <A HREF="info.shtml">Frequent Questions page</A>.
		</B>
		</P>

	    <!--
	    <DIV ALIGN=center>
	    <P STYLE="font-size: 10px; border: 1px solid #473E16; padding: 2px"></P>
	    <TABLE CELLSPACING=0 CELLPADDING=2 BORDER=0>
	    <TR><TD ALIGN=center>
	    <DIV STYLE="border: 1px solid #473E16; background-color: #C8C296; padding: 4px; width: 120px">
	    <A onClick="return photo_popup ('quick', 0);" TARGET="sbf_pix"
		HREF="gallery/photoshow.cgi?show=quick&num=0"><B>View All 
		Pictures</B></A>
	    </DIV>
	    </TD><TD ALIGN=center>
	    <DIV STYLE="border: 1px solid #473E16; background-color: #C8C296; padding: 4px; width: 120px">
	    <A HREF="review.cgi"><B>Read Reviews</B></A>
	    </DIV>
	    </TD>
	    </TR></TABLE>
	    -->

	    <!--
	    -->

_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_keys = 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; }
		     
		for ( my $i = 0; $i < scalar (@$artist_keys); $i++ )
		{
		    my $artist = $$artist_keys[$i];
		    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";
			}
			if ( $i > 0 )
			    { $str .= "<BR>&nbsp;&nbsp;&nbsp;&nbsp;(as $SBF_2006::ARTISTS{$aname}->{NAME})"; }
		    }
		}
	    }
	}
    }

    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_2006::ARTISTS{$key}->{NAME}"; }
	else
	    { $title = "SBF: Artists"; }
    }
}

header ($title, $popup);

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

	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_

	}

	@keys = ( $key );
	if ( $art->{ALIASES} )
	    { push (@keys, @{$art->{ALIASES}}); }

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

	$tmp = &plays (\@keys, 0, @SBF_2005::SCHEDULE);
	if ( $tmp )
	    { push (@plays_text, $tmp); }

	$tmp = &plays (\@keys, 0, @SBF_2004::SCHEDULE);
	if ( $tmp )
	    { push (@plays_text, $tmp); }

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

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

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

	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_2006::ARTISTS{$a}->{NAME});
		my $cmp_b = uc($SBF_2006::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_2006::ARTISTS) )
	{
	    my $art = $SBF_2006::ARTISTS{$key};
	    my $play_2006;

	    my @keys = ( $key );

# not needed since we're only showing current year
#	    if ( $SBF_2006::ARTISTS{$key}->{ALIASES} )
#		{ push (@keys, @{$SBF_2006::ARTISTS{$key}->{ALIASES}}); }

	    $play_2006 = &plays (\@keys, 1, @SBF_2006::SCHEDULE);

	    if ( ! $play_2006 )
		{ 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><NOBR>
_EOF_

	    print $play_2006;
	    print "</NOBR></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_
 
    foreach my $dref ( @SBF_2006::SCHEDULE )
    {
	if ( $key && $key ne $dref->{DATE} )
	    { next; }
    
	print <<_EOF_;
<TABLE CLASS="schedule" CELLPADDING=4 CELLSPACING=1 BORDER=0 WIDTH="550">
<TR>
    <TH COLSPAN=3 WIDTH="25%" 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>
_EOF_
	}

	print <<_EOF_;
</TABLE>
_EOF_

	my $stages_per_line = 2;
	my $cur_stage_num = 0;

	print <<_EOF_;
<TABLE CLASS="schedule" CELLPADDING=4 CELLSPACING=1 BORDER=0 WIDTH="550">
<TR VALIGN="top">
_EOF_

	foreach my $sref ( @{$dref->{STAGES}} )
	{
	    if ( $cur_stage_num == $stages_per_line )
	    {
		$cur_stage_num = 0;
		print <<_EOF_;
</TR>
</TABLE>
<TABLE CLASS="schedule" CELLPADDING=4 CELLSPACING=1 BORDER=0 WIDTH="550">
<TR VALIGN="top">
_EOF_
	    }

	    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_2006::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";
	    $cur_stage_num++;
	}
    }

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

footer ($popup);
