#!/usr/bin/perl -w

use strict;
use CGI;
use HSB_2010;
use HSB_2009;
use HSB_2008;
use HSB_2007;
use HSB_2006;
use HSB_2005;
use HSB_2004;
use HSB_2003;
use HSB_2002;
use HSB_2001;
use Image::Size;
use FileHandle;

our $DISPLAY_TIMES = 1;

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

    no warnings 'uninitialized';

    $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;
    my $simple = shift;

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

    ### 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);

    if ( ! $simple )
    {
	print <<_EOF_;
<HTML>

<HEAD>
<TITLE>
$title
</TITLE>
<link rel="image_src" href="http://www.hardlystrictlybluegrass.com/2010/images/2010splash.jpg" />
</HEAD>
_EOF_
    }

    print <<_EOF_;

<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 { background: url(/2010/images/green_tile.jpg) fixed; margin: 0; padding: 0; font-family: georgia; font-size: 14px; }
a { color: #323019; }
#page { margin: auto; width: 945px; background: url(/2010/images/bg-920.jpg) repeat-y center 660px; }
/*#page .bg { height: 100%; text-align: center; }*/
#page .page_inside { width: 845px;  margin: auto; }
#page .page_inside .top { text-align: center; }
.lineup {padding-top: 25px;  }
.title { padding-top: 25px; }
.photos { padding-top: 272px;  }
.freeshow { padding-top: 10px; }
.questions { margin-top: 0px; margin-bottom: 5px; }
.volunteer { margin-top: 20px; margin-bottom: 5px; }
.previous { margin-top: 20px; margin-bottom: 5px; }
.previous_opts { padding-bottom: 20px; }
.fbtwitter { margin-top: 15px; }
.hr { width: 300px; border-bottom: 1px dotted; margin: 0px auto 20px auto; padding: 10px 0 10px 0; }
.odd { color: #5E4014; font-style: italic; }
.callout_text {
        width:655px;
        background-image:url(/2010/images/hsb_box_bg.jpg);
        background-repeat:repeat;
        margin:auto;
        text-align:center;
        font-size:15px;
        line-height: 23px;
        color:#231f20;
        font-family: georgia;
}

body, p, table, div {
    font-family: arial, helvetica, sans-serif;
    font-size: 12px;
}
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: 14px;
    color: #4C4C19;
    background-color: #C8C296;
}

// -->
</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;
    }

    if ( ! $simple )
    {
	print <<_EOF_;
<BODY>

<div id="page">
  <div class="page_inside">

    <div class="top">
    <h1 class="title" style="margin:0;"><a href="/2010/"><img border="0" alt="Hardly Strictly Bluegrass 2010" src="/2010/images/hsb10-logo.png"></a></h1>
    <div class="title"><img alt="Speedway Meadow in Golden Gate Park" src="/2010/images/title.png"></div>
    <div class="freeshow"><img alt="FREE SHOW - NO TICKETS NEEDED" src="/2010/images/freeshow.png"></div>
_EOF_

	my $fh = new FileHandle ("printathome-include.html");
	if ( $fh )
	{
	    print <<_EOF_;
	<table cellspacing="0" cellpadding="0" border="0" align="center">
	<tr valign="top"><td>
_EOF_
	    while (<$fh>) { print; }
	    print <<_EOF_;
	</td></tr></table>
_EOF_
	}

	print <<_EOF_;

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

	<B>Note: Schedule and show times are subject to change.</B>

    </div>
    <br clear="all">
_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_;
    <br>
    <div align="center">
    <div class="hr" style="padding: 0px 0 10px 0"></div>

    <div class="faq">
	<h4 style="margin: 5px 0 "><img src="/2010/images/faq.png"></h4>
	<a href="/2010/info.shtml">Click here for general information</a> and 
	our<br> Frequently Asked Questions (including transportation info)
    </div>

    <br>
    <div class="hr" style="padding: 0px 0 10px 0"></div>
    <div class="fbtwitter"><img alt="fbtwitter" border=0 src="/2010/images/fbtwitter.png" usemap="#fbtwitter"></div>

    <map name="fbtwitter">
    <area shape="rect" coords="67,17,185,35" target="_blank" href="http://www.facebook.com/hardlystrictlybluegrass" />
    <area shape="rect" coords="325,17,400,35" target="_blank" href="http://www.twitter.com/hsbfest" />
    </map>

    <div class="hr"></div>
    <div class="questions"><img alt="Questions, Comments and Feedback" src="/2010/images/questions.png"></div>

    <div class="email"><a href="mailto: questions\@hardlystrictlybluegrass.com ">questions\@hardlystrictlybluegrass.com</a></div>

    <div class="hr"></div>
    <div class="volunteer"><img alt="To Enquire About Volunteer Opportunities" src="/2010/images/volunteer.png"></div>
    <div class="email"><A href="mailto:volunteer\@hardlystrictlybluegrass.com">volunteer\@hardlystrictlybluegrass.com</a></div>


    <div class="hr"></div>
    <div class="previous"><img alt="Previous Years" src="/2010/images/previous.png"></div>
    <div class="previous_opts">
      <a href="/2001/">2001</a>,
	 <a href="/2002/">2002</a>,
	 <a href="/2003/">2003</a>,
	 <a href="/2004/">2004</a>,
	 <a href="/2005/">2005</a>,
	 <a href="/2006/">2006</a>,
	 <a href="/2007/">2007</a>,
	 <a href="/2008/">2008</a>,
	 <a href="/2009/">2009</a>

    </div>
  </div>
</div>


  <div style="float: left; padding: 15px 0 0 80px">
  <div style="padding: 5px; width: 500px"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2Fhardlystrictlybluegrass&amp;layout=standard&amp;show_faces=false&amp;width=900&amp;action=like&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:900px; height:35px;" allowTransparency="true"></iframe></div>
  </div>


  <div style="padding: 30px 90px 20px 0; font-size: 11px; text-align: right">
      site by <a target="_blank" href="http://www.chimeinteractive.com/">chime</a>
  </div>

  <div style="clear:both"></div>
</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 $HSB_2010::ARTISTS{$aname}->{NAME})"; }
		    }
		}
	    }
	}
    }

    return $str;
}

###
## MAIN BODY
###

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

print $query->header;

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

header ($title, $popup, $simple);

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

<TABLE WIDTH="800" align="center" 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 = $HSB_2010::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, @HSB_2010::SCHEDULE);

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

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

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

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

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

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

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

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

	$tmp = &plays (\@keys, 0, @HSB_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($HSB_2010::ARTISTS{$a}->{NAME});
		my $cmp_b = uc($HSB_2010::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 (%HSB_2010::ARTISTS) )
	{
	    my $art = $HSB_2010::ARTISTS{$key};
	    my $play_2010;

	    my @keys = ( $key );

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

	    $play_2010 = &plays (\@keys, 1, @HSB_2010::SCHEDULE);

	    if ( ! $play_2010 )
		{ 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_2010;
	    print "</NOBR></TD>\n</TR>\n";
	}

	print "</TABLE><BR>\n";
    }
}
elsif ( $show eq 'day' )
{
    if ( ! $simple )
    {
	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 ( @HSB_2010::SCHEDULE )
    {
	if ( $key && $key ne $dref->{DATE} )
	    { next; }
    
	print <<_EOF_;
<TABLE CLASS="schedule" CELLPADDING=4 CELLSPACING=1 BORDER=0 WIDTH="850">
<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="850">
<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="850">
<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 = $HSB_2010::ARTISTS{$art};
		if ( $showtime )
		    { print "$showtime "; }

		my $art_display = '';
		if ( $aref ) {
		    $art_display = $aref->{NAME};
		} else {
		    foreach my $t ( split (/_/, lc($art)) )
		    {
			if ( $art_display ) { $art_display .= " "; }
			$art_display .= ucfirst ($t);
		    }
		    $art_display = $art;
		}

		printf "<A HREF='#' onClick='return art_popup(\"$art\");'>%s</A><BR>\n",
		    $art_display;
	    }
	    print "</UL></TD>\n";
	    $cur_stage_num++;
	}
    }

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

if ( ! $simple )
{
    footer ($popup);
}
