#!/usr/bin/perl

# Kijk of er gigs in de toekomst zijn
# Zo ja, tot 4 afbeelden (de 4 eerstvolgende welteverstaan)



use lib "C:/files/Projecten/AccesSite2/lib";
#use lib "/dev/a/staff/roelof/lib";
use lib "../lib";
use AccesSite;

my $AS = new AccesSite(
  data_path => "../data",
) || die "new AccesSite: $!";

# news = 3
# tour = 4

#print "\n\n";

my $now = time;
my $tour = $AS->LoadRecord(4)->Childs('datum','nr','0');
my ($gig1,$gig2,$gig3,$gig4);
foreach $gig (@{$tour}) {
  my $hidden  = $gig->GetData('hidden');
  next if $hidden;
  # See if this is in the future
  # We need the FIRST four gigs so we need to search them all.
  # However the gigs are sorted so we can stop if ne is in the past
  # and use the last two
  #print "$gig\n";
  if ($gig->GetData('datum') > $now + (60 * 60 * 2)) { # two hours extra
    $gig4 = $gig3;
    $gig3 = $gig2;
    $gig2 = $gig1;
    $gig1 = $gig;
    }
  else {
    last;
    }
}

my @gigs;
push @gigs, $gig1 if $gig1;
push @gigs, $gig2 if $gig2;
push @gigs, $gig3 if $gig3;
#push @gigs, $gig4 if $gig4;

# OK, we have selected the stuff. Output
print "content-type: text/javascript\n\n";
#print <<"LINE";
#var newstxt = "<font size=2 face=\\"Arial, Helvetica, sans-serif\\" color=\\"#FFFFFF\\">" +
#LINE
if (@gigs) {

#//"<div class=Box>" +
#              //"<a href=\\"/index.pl/en/list/4\\" target=kyumain class=RedText " +
#              //"style=\\"font-size:10pt;\\">UPCOMING GIGS</a><br>\\n" +
              
print <<"LINE";
var tourtxt = "<font class=Text style=\\"font-size:7pt;color:#999999;\\">" +
LINE

my @day = qw(SUN MON TEU WED THU FRI SAT);
my @month = qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC);
my $dot = "<font class=RedText>.</font>";

foreach my $gig (@gigs) { #($gig1,$gig2,$gig3) {
  next unless $gig;
  #2|gig||||||title;text;;Title|datum;datetime;;Date|support;paragraph;;Support act(s)|supporting;paragraph;;Headline(s)|body;html_medium;;Main text|hyperlink;webaddress;;Hyperlink
  my $date  = $gig->GetData('datum');
  my $venue = uc($gig->GetData('title'));
  my $city  = uc($gig->GetData('city'));
  $venue =~ s/<[^>]*>/ /g; # no HTML
  $venue =~ s/\n/ /g; # no newlines
  $venue =~ s/"/&quot;/g; # no quotes
  $city  =~ s/<[^>]*>/ /g; # no HTML
  $city  =~ s/\n/ /g; # no newlines
  $city  =~ s/"/&quot;/g; # no quotes

  my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($date);

  $date = "$day[$wday]$dot$month[$mon]$dot".
          sprintf("%02d$dot%04d<font class=RedText>\@</font>%02d<font class=RedText>:</font>%02d",
                  $mday,$year + 1900,$hour,$min);

  my $url = "/index.pl/en/list/$gig->{id}";

  print <<"  LINES";
        "$br<a href=\\"$url\\" target=kyumain style=\\"text-decoration:none;font-size:7pt;color:#999999;\\">$date<br>" +
        "<font class=RedText>$venue</font> $city</a><br>\\n" +
  LINES
  $br = "<span style=font-size:3pt;>&nbsp;</span><br>";
  }
print <<"LINE";
    "</font>\\n";
LINE
} else {
print <<"LINE";
var tourtxt = false;
LINE
}



