#!/usr/bin/perl -w

use strict;
use CGI;
use HSB_2012;
use HSB_2011;
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 12: Oct 5 - 7, 2012"; }

    ### 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://hardlystrictlybluegrass.com/2012/images/hsb12.jpg" />
<style type="text/css">
body .schedule { font: bold 15px georgia; }
body .schedule .table_header { font: 20px georgia; padding: 7px; }
.schedule a { text-decoration: none; }
.stage_col { padding-right: 20px; }
.stage { 
  margin-top: 10px;
  text-indent: -999em;
  width: 200px;
  height: 75px;
  background-repeat: no-repeat;
}
.stage_towers { background-image: url(/2012/images/stage-towers-sm.png); }
.stage_star { background-image: url(/2012/images/stage-star-sm.png); }
.stage_rooster { background-image: url(/2012/images/stage-rooster-sm.png); }
.stage_arrow { background-image: url(/2012/images/stage-arrow-sm.png); }
.stage_porch { background-image: url(/2012/images/stage-porch-sm.png); }
.stage_banjo { background-image: url(/2012/images/stage-banjo-sm.png); }
</style>

</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=320,height=400');
    win.focus();
    return false;
}
// -->
</SCRIPT>

<link type="text/css" rel="stylesheet" media="all" href="/2012/style.css" />
</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 style="background: url(/2003/images/frame-left.jpg) repeat-y -8px top;"></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_;
<div id="container"> 
  <div id="main">
    <div id="content" class="middle"><div class="inside">
    <br>
    </div></div>

    <div class="redbox"><div class="inside">
      <h1><a href="/2012/"><img border="0" alt="HARDLY STRICTLY BLUEGRASS 12: Fr
i Oct 5, Sat Oct 6 &amp; Sun Oct 7" src="/2012/images/hsb12-info-hdr.png"></a></h1>
    </div></div> 
    <div class="redbox_bottom"><img src="/2012/images/framebottom.png"></div>

    <div id="content" class="middle"><div class="inside">

_EOF_
      my $fh = new FileHandle("opts.html");
      while (<$fh>) { print ; }
      $fh->close;

      print <<_EOF_;

      <h3 style="position: relative; top: 0px; margin: 0px auto 10px auto; padding: 0; font: 15px georgia; ">
      Neighborhood questions / concerns during the festival? Call our Neighborhood Support Line - 415-386-8584
      </h3>

      <h3 style="margin: 0px 0 10px 0; padding: 0; font: 15px georgia">
      <div style="font: bold 15px georgia">LEARN MORE ABOUT OUR FOUNDER, WARREN HELLMAN - COME TO OUR "WARREN MUSEUM"!</div>
      The museum is at 1479A Folsom St, SF, CA &amp; open to the public for 1 week: Wed. 10/3 - Wed. 10/10, between 2 and 8pm.
      </h3>

      <h3 style="margin: 0px 0 10px 0; padding: 0; font: 17px georgia">
      2012 HSB MOBILE APPS ARE HERE! Click to download:
      <a target="_mobile" href="http://itunes.apple.com/us/app/hsb-festival/id563451644?mt=8">IPHONE</a> or <a target="_mobile" href="https://play.google.com/store/apps/details?id=sdei.hsbapp.activity&feature=search_result#?t=W251bGwsMSwxLDEsInNkZWkuaHNiYXBwLmFjdGl2aXR5Il0.">ANDROID</a>
      </h3>

      <div style="text-align: left; padding: 00px 30px; ">
_EOF_

	$fh = new FileHandle ("printathome-include-artistpage.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_;

      <h3 style="margin: 0px auto 10px auto; padding: 0; font: 15px georgia; width: 700px">
      ATTENTION! San Francisco is experiencing MANY large events on the weekend of HSB which will make parking extremely limited - PLEASE USE PUBLIC TRANSPORTATION!
      </h3>

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

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

	<div style="margin-top:10px; padding: 10px; font: 17px georgia; border: 7px double black; background-color: #B91722; color: #f6efd8">
	NEW! ARROW STAGE will be STREAMED LIVE Friday through Sunday! <a style='color: #000; text-deocration: underline' href ="/2012/stream.shtml">Click here</a> to watch!
	</div>

    </div>
    <br clear="all">
_EOF_
    }
}

sub footer
{
    my $popup = shift;

    if ( $popup )
    {
	print <<_EOF_;
    </TD>
    <TD ROWSPAN=2 WIDTH=12 style="background: url(/2003/images/frame-right.jpg) repeat-y -8px top"></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_;

    <div align="center" style="padding: 20px 0;"><img src="/2012/images/chevron.png"></div>
    <div class="previous"><img alt="Previous Years" src="/2012/images/previous.p
ng"></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>,
      <a href="/2010/">2010</a>,
      <a href="/2011/">2011</a>
    </div>

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

    <div style="padding: 30px 15px 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></div><!-- /inside, /content -->

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

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

if ( ! $show && ! $simple )
{
    print <<_EOF_;
<P>
Please select one of the following options for the 2012 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#schedule">View Full Schedule</A>
    </TH>
    <TH BGCOLOR="#C8C296" CLASS="table_header">
    <A HREF="$ENV{SCRIPT_NAME}?show=artists#schedule">View Full Artist List</A>
    </TH>
</TR>
</TABLE>
_EOF_
}

if ( $show eq 'artists' )
{
    if ( $key )
    {
	my $art = $HSB_2012::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 ( 0 && -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_2012::SCHEDULE);

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

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

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

	    my @keys = ( $key );

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

	    $play_2012 = &plays (\@keys, 1, @HSB_2012::SCHEDULE);

	    if ( ! $play_2012 )
		{ 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_2012;
	    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>

<div id="schedule"></div>
_EOF_
    }
 
    foreach my $dref ( @HSB_2012::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="100%" 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 class='stage_col' WIDTH='50%'><div class='stage stage_$sref->{STAGE_ID}'><U><B>$sref->{NAME}</B></U></div>\n";
	    if ( $sref->{NOTES} ) {
	      print $sref->{NOTES};
	    }
	    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_2012::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);
}
