#
# The standard theme used by jobeditor and/or vdr2iso.
# 
# This class is intended to serve as an example for your own themes.
# 
# The theme class is the only class, dependant on a grafics engine.
# It's your choice, which graphics engine to use in your themes.
# This way it is possible to change the graphics engine without the
# need to insulate editor or worker-scripts.
#
# Each theme-module-file must be named as 'Theme<your-theme-name>.pm'
# The ThemeManager looks for that pattern.
#       written by ...: geronimo at vdrportal dot de
# 
# COPYRIGHT:   (c) 2006 Creative Commons by-nc-sa
#               some rights reserved. For details on copyright see:
#               http://creativecommons.org/licenses/by-nc-sa
#               
#
# This class uses GD as graphics engine - so ensure, that your
# perl-installation has support for GD, if you want to use this theme.
#
#
# DISCLAIMER:   NO WARANTY AT ALL. Use at your own risk.
# 
# vi: ts=3
#
package V2I::plugins::ThemeGD_Demo;

use strict;
use base 'V2I::Theme';
use V2I::utils;
use Data::Dumper;
use GD;

my $burnCombat = 0;
my $debug = 0;

sub supportedUserVars {
   my $self = shift;
   my $rv = {};

   $rv->{'jfont'}     = [qw(header title item summary)];
   $rv->{'jimage'}    = [qw(bg)];
   $rv->{'jcolor'}    = [qw(shadow)];
   $rv->{'saspect'}   = [qw(4:3 16:9 lb)];
   $rv->{'sboxes'}    = [qw(0 1)];
   $rv->{'nxMargin'}  = [qw(20 100)];
   $rv->{'nyMargin'}  = [qw(20 100)];
   $rv->{'nimgWidth'} = [qw(150 400)];

   return $rv;
}

sub defaultValues {
   my $self = shift;
   my $rv = {};

   $rv->{'headerfont'}  = {value=>'', size=>90, color=>'white'};
   $rv->{'titlefont'}   = {value=>'', size=>40, color=>'white'};
   $rv->{'itemfont'}    = {value=>'', size=>20, color=>'white'};
   $rv->{'summaryfont'} = {value=>'', size=>16, color=>'white'};
   $rv->{'bgimage'}     = {value=>'', anchor=>'bottom', color=>'black'};
   $rv->{'shadowcolor'} = 'green';
   $rv->{'aspect'}      = '4:3';
   $rv->{'boxes'}       = 1;
   $rv->{'xMargin'}     = 55;
   $rv->{'yMargin'}     = 34;
   $rv->{'imgWidth'}    = 200;

   return $rv;
}

my $imgWidth = 720;

sub createBackground {
   my $self = shift;
   my $cfg = shift;
   my $appCfg = $self->{'work'}{'appConfig'};

   GD::Image->trueColor(1);
   my $imageFN = $self->checkImage($cfg->{'bgimage'}{'value'});
   
   if(not -s $imageFN){
      print(STDERR 'invalid image-path to load ['.$imageFN."]!\n");
      exit;
      }
   print(ref($self).'::createBackground() ...'."\n") if $debug;
   $imgWidth = 720;
   $imgWidth = 1024 if $cfg->{'aspect'} eq '16:9';
   my $imgSrc  = GD::Image->new($imageFN);
   my $image   = GD::Image->new($imgWidth,576);
   my $yMargin = $cfg->{'yMargin'};
   my $xMargin = $cfg->{'xMargin'};
   my @bounds  = GD::Image->stringFT(0, $cfg->{'itemfont'}->{'value'}, $cfg->{'itemfont'}{'size'}, 0,0,0, 'yT');
   my $baseOff = 50;

   $yMargin += 50 if $cfg->{'aspect'} =~ /^lb$/;
   $baseOff = max($bounds[1] - $bounds[5] + 10, 50);

   if($imgSrc->width < 720 and $imgSrc->height < ($image->height - $yMargin)){
      print('gotta teeny image, have to bump it at '.$cfg->{'bgimage'}{'anchor'}."\n") if $debug;     
      my($bR, $bG, $bB) = @{$appCfg->getColor4($cfg->{'bgimage'}{'color'})};     
      my $xBack = $image->colorResolve($bR, $bG, $bB);

      $image->fill(0,0,$xBack);
      for($cfg->{'bgimage'}{'anchor'}){
         /^top$/i    and do { $image->copy($imgSrc
                                          ,$image->width - $imgSrc->width - $xMargin
                                          ,$yMargin
                                          ,0 , 0
                                          ,$imgSrc->width
                                          ,$imgSrc->height);
                              last;
                              };
         /^middle$/i and do { $image->copy($imgSrc
                                          ,$image->width - $imgSrc->width - $xMargin
                                          ,int(($image->height - $imgSrc->height)/2)
                                          ,0 , 0
                                          ,$imgSrc->width
                                          ,$imgSrc->height);
                              last;
                              };
         /^bottom$/i and do { $image->copy($imgSrc
                                          ,$image->width - $imgSrc->width - $xMargin
                                          ,max(0, $image->height - $imgSrc->height - $yMargin - $baseOff)
                                          ,0 , 0
                                          ,$imgSrc->width
                                          ,$imgSrc->height);
                              last;
                              };
         }
      }
   else{ $image->copyResampled($imgSrc,0,0,0,0,$image->width, $image->height, $imgSrc->width, $imgSrc->height); }
   
   my($iR, $iG, $iB) = @{$appCfg->getColor4($cfg->{'itemfont'}{'color'})};
   my($sR, $sG, $sB) = @{$appCfg->getColor4($cfg->{'shadowcolor'})};
   my($hR, $hG, $hB) = @{$appCfg->getColor4($cfg->{'headerfont'}{'color'})};
   
   my $xShadow = $image->colorResolve($sR, $sG, $sB);
   my $xFront  = $image->colorResolve($iR, $iG, $iB);
   my $xTitle  = $image->colorResolve($hR, $hG, $hB);
   my $imgShade = GD::Image->new($image->width, $image->height);
   my $dummy = $imgShade->colorResolve($sR, $sG, $sB);

   $imgShade->fill(0,0,$dummy);
   $image->setThickness(2);
   my $size = $cfg->{'headerfont'}{'size'};
   my $soff = int($size / 20);
   my @box = ($xMargin + 15, $yMargin + 73, $image->width - $xMargin - 122, $image->height - $yMargin - $baseOff -2);

   if($cfg->{'boxes'}){
      $image->copyMerge($imgShade, $box[0], $yMargin +  8, 0, 0, $image->width - 2*$xMargin -  7,  54, 40);
      $image->copyMerge($imgShade, $box[0], $yMargin + 74, 0, 0, $image->width - 2*$xMargin - 127, $image->height - 2*$yMargin - $baseOff - 69, 40);
      $image->rectangle(        $box[0]-15, $yMargin,  $image->width - $xMargin,  $yMargin + 57, $xFront);
      $image->rectangle(        $box[0]-15, $box[1]-7, $box[2]+2, $box[3]+2, $xFront);
      }
   my @bounds = GD::Image->stringFT($xShadow, $cfg->{'headerfont'}->{'value'}, $size , 1.570817, 0, 0, $cfg->{'header'});
   my $hdXBase = $image->width - $xMargin - $bounds[0];
   my $hdYBase = $image->height - $yMargin - $baseOff;

   $image->clip($box[2]+12, $box[1]-10, $image->width, $image->height);
   $image->stringFT($xShadow, $cfg->{'headerfont'}->{'value'}, $size, 1.570817, $hdXBase +6, $hdYBase +6 - $bounds[1], $cfg->{'header'});
   $image->stringFT($xTitle,  $cfg->{'headerfont'}->{'value'}, $size, 1.570817, $hdXBase,    $hdYBase    - $bounds[1] +1, $cfg->{'header'});

   $image->clip(0, 0, $image->width, $image->height);
   if($main::testOnly){
      my $fp;

      if(open($fp, '> GD_Demo-background.png')){
         binmode($fp);
         print($fp $image->png);
         close($fp);
         }
      }
   return $image;
}

sub drawButton {
   my $self = shift;     
   my $image = shift;
   my $x = shift;
   my $y = shift;
   my($red, $green, $blue) = @{$self->{'work'}{'appConfig'}->getColor4(shift)};
   my $dir = shift;
   my $diff = max(255-$red, 255-$green, 255-$blue);
   my $bright = $$image->colorResolve(200,200,200);
   my($start,$end); 

   $dir = 'right' if not $dir;
   ($start, $end) = (225, 135) if $dir =~ /^right$/;
   ($start, $end) = (135,  45) if $dir =~ /^up$/;
   ($start, $end) = (315, 225) if $dir =~ /^down$/;
   ($start, $end) = ( 45, 315) if $dir =~ /^left$/;
   $$image->filledEllipse($x +2, $y +1, 32, 32, $bright);     
   if($diff > 50){
      print('drawButton: bright button #'.$diff.' at ('.$x.'/'.$y.') with colBase=('.$red.','.$green.','.$blue.")\n") if $debug;     
      for my $i (0 .. 1){
         my $color = $$image->colorResolve(min(255,$red  +$i*16)
                                           ,min(255,$green+$i*16)
                                          ,min(255,$blue +$i*16));
         $$image->filledArc($x, $y, 30 - 10*$i, 30 - 10*$i, $start, $end, $color);
         }
      }
   else{
      print('drawButton: dark button #'.$diff.' at ('.$x.'/'.$y.') with colBase=('.$red.','.$green.','.$blue.")\n") if $debug;     
      for my $i (0 .. 1){
         my $color = $$image->colorResolve(max(0,0,$red  - (1-$i)*16)
                                           ,min(0,0,$green- (1-$i)*16)
                                          ,min(0,0,$blue - (1-$i)*16));
         $$image->filledArc($x, $y, 30 - 10*$i, 30 - 10*$i, $start, $end, $color);
         }
      }
   return (int(($x-15) * 720 / $$image->width), $y-16
         , int(($x+19) * 720 / $$image->width), $y+18);   
}

sub writeSummary {
   my $self = shift;     
   my $image = shift;
   my $rec = shift;
   my $sumText = shift;
   my @words   = split(/\s/, $sumText);
   my $boxRef = shift;
   my $subRef = shift;
   my $x1 = $boxRef->[0];
   my $y1 = $boxRef->[1];
   my $x2 = $boxRef->[2];
   my $y2 = $boxRef->[3];
   
   push(@words, $sumText) if not $#words;
   push(@words, ' ');

   my $ttfFN = $rec->{'settings'}{'summaryfont'}{'value'};
   my $fntSize = $rec->{'settings'}{'summaryfont'}{'size'};
   my($red,$green,$blue) = @{$self->{'work'}{'appConfig'}->getColor4($rec->{'settings'}{'summaryfont'}{'color'})}; 
   my $mCol = $$image->colorResolve($red, $green, $blue);
   my @bnds = GD::Image->stringFT($mCol, $ttfFN, $fntSize, 0,0,0, 'yT');
   my $h = int(($bnds[1]-$bnds[5])*1.01);
   my $w = $x2 - $x1;
   my $test = 'rednose';
   my $i = 1;
 
   while(@words){
         my $curY = $y1 + $i*$h;
         my $curX = $x1;
         my $scratch = '';
   
         last if($curY > $y2 or not $test);
         $w  = $x2 - $x1;
         if(($curY + 10) > $subRef->[1] and ($curY-$h) < $subRef->[3]){
            $w -= $subRef->[2] - $subRef->[0] -10; 
            $curX = $subRef->[2] + 10 if $subRef->[0] eq $x1;
            }
         while($test = shift(@words)){
               @bnds = GD::Image->stringFT(0, $ttfFN, $fntSize, 0,0,0, $scratch.' '.$test);
               last if ($bnds[4]-$bnds[0]) >= $w;
               $scratch .= $test.' ';
               }
         if(not $test and $#words){
            unshift(@words, $test) if $scratch;
            $test = 'rednose';
            }
         else{ unshift(@words, $test); }
         $$image->stringFT($mCol, $ttfFN, $fntSize, 0, $curX, $curY, $scratch);
         $i++;
         }
   return join(' ', @words);
}

sub createSpecialPage {
   my $self = shift;
   my $curPage = shift;
   my $work = shift;
   my $pgNr = shift;
   my $special = shift;
   my $summary = shift;
   my $spuRef = shift;
   my $appCfg = $self->{'work'}{'appConfig'};
   my $cfg  = $curPage->{'settings'};
   my $imgMenu   = $self->createBackground($cfg);
   my $imgButton = GD::Image->newPalette($imgMenu->width, $imgMenu->height);
   my $imgEmpty  = GD::Image->newPalette(720, 576);
   my $fnSpecial = $self->checkImage($curPage->{'image0'}{'value'});
   my $imgSpecial;
   my $titleFont = $cfg->{'titlefont'};
   my $itemFont  = $cfg->{'itemfont'};
   my $summaryFont = $cfg->{'summaryfont'};
   my($iR, $iG, $iB) = @{$appCfg->getColor4($itemFont->{'color'})};
   my($tR, $tG, $tB) = @{$appCfg->getColor4($titleFont->{'color'})};
   my($sR, $sG, $sB) = @{$appCfg->getColor4($summaryFont->{'color'})};
   my($xR, $xG, $xB) = @{$appCfg->getColor4($curPage->{'image0'}{'color'})};
   my $txtMain = $appCfg->getLangToken('mainmenue');
   my $txtPlay = $appCfg->getLangToken('specmenue');
   my $yMargin = $cfg->{'yMargin'};
   my $xMargin = $cfg->{'xMargin'};
   my @bounds  = GD::Image->stringFT(0, $cfg->{'itemfont'}->{'value'}, $cfg->{'itemfont'}{'size'}, 0,0,0, 'yT');
   my $baseOff = max($bounds[1] - $bounds[5] + 10, 50);

   $yMargin += 50 if $cfg->{'aspect'} =~ /^lb$/;
   my $xShadow = $imgMenu->colorResolve(255-$iR, 255-$iG, 255-$iB);
   my $xItem   = $imgMenu->colorResolve($iR, $iG, $iB);
   my $xTitle  = $imgMenu->colorResolve($tR, $tG, $tB);
   my $xSpec   = $imgMenu->colorResolve($xR, $xG, $xB);
   my $xTest   = $imgMenu->colorResolve(255,  0, 0);
   my $dummy   = $imgEmpty->colorAllocate(0,0,0);
   
   $imgEmpty->transparent($dummy);
   $dummy = $imgButton->colorAllocate(0,0,0);
   $imgButton->transparent($dummy);

   my @bounds = GD::Image->stringFT(0, $itemFont->{'value'}, $itemFont->{'size'},  0, 0, 0, 'yT');
   my @bkBnds = GD::Image->stringFT(0, $itemFont->{'value'}, $itemFont->{'size'},  0, 0, 0, $txtMain);
   my @box = ($xMargin + 15, $yMargin + 73, $imgMenu->width - $xMargin - 122, $imgMenu->height - $yMargin - $baseOff -2);
   my $wBack  = $bkBnds[4] - $bkBnds[0];
   my $height = max(21, $bounds[1] - $bounds[5]);
   my $base = $imgMenu->height - $yMargin - 10;
   my $buttonBase = $base - int($baseOff/2 - 11);
   my @subDim;
   my $fp;

   $imgMenu->clip($xMargin,  $yMargin, $imgMenu->width - $xMargin,  $yMargin + 57);
   $imgMenu->stringFT($xShadow, $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 20, $yMargin + 52, $curPage->{'title'});
   $imgMenu->stringFT($xTitle,  $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 18, $yMargin + 50, $curPage->{'title'});
   $imgMenu->clip(0, 0, $imgMenu->width, $imgMenu->height);

   if(-s $fnSpecial){
      $imgSpecial = GD::Image->new($fnSpecial); 
      my $factor = $cfg->{'imgWidth'} / max($imgSpecial->width, $imgSpecial->height);
      my @newDim = (int($imgSpecial->width * $factor), int($imgSpecial->height * $factor));

      for ($curPage->{'image0'}{'anchor'}){
          /tl/    and do { @subDim = ($box[0], $box[1], $box[0] + $newDim[0] + 20, $box[1] + $newDim[1] + 20);
                           $imgMenu->filledRectangle($subDim[0], $subDim[1], $subDim[2], $subDim[3], $xSpec);
                           $imgMenu->copyResampled($imgSpecial, $subDim[0] + 10, $subDim[1] + 10, 0, 0, $newDim[0], $newDim[1]
                                                  ,$imgSpecial->width, $imgSpecial->height); 
                           last; };
          /tr/    and do { @subDim = ($box[2]-20-$newDim[0], $box[1], $box[2], $box[1] + $newDim[1] + 20);
                           $imgMenu->filledRectangle($subDim[0], $subDim[1], $subDim[2], $subDim[3], $xSpec);
                           $imgMenu->copyResampled($imgSpecial, $subDim[0] + 10, $subDim[1] + 10, 0, 0, $newDim[0], $newDim[1]
                                                  ,$imgSpecial->width, $imgSpecial->height); 
                           last; };
          /bl/    and do { @subDim = ($box[0], $box[3]-20-$newDim[1], $box[0] + $newDim[0] + 20, $box[3]);
                           $imgMenu->filledRectangle($subDim[0], $subDim[1], $subDim[2], $subDim[3], $xSpec);
                           $imgMenu->copyResampled($imgSpecial, $subDim[0] + 10, $subDim[1] + 10, 0, 0, $newDim[0], $newDim[1]
                                                  ,$imgSpecial->width, $imgSpecial->height); 
                           last; };
          /br/    and do { @subDim = ($box[2]-20-$newDim[0], $box[3]-20-$newDim[1], $box[2], $box[3]);
                           $imgMenu->filledRectangle($subDim[0], $subDim[1], $subDim[2], $subDim[3], $xSpec);
                           $imgMenu->copyResampled($imgSpecial, $subDim[0] + 10, $subDim[1] + 10, 0, 0, $newDim[0], $newDim[1]
                                                  ,$imgSpecial->width, $imgSpecial->height); 
                           last; };
          }
      }
   print('gonna write specials summary ...'."\n") if $debug;
   $imgMenu->clip($box[0], $box[1], $box[2], $box[3]) if $cfg->{'boxes'};
   my $text = $self->writeSummary(\$imgMenu, $curPage, $summary, \@box, \@subDim);

   $imgMenu->clip(0, 0, $imgMenu->width, $imgMenu->height);
   $imgMenu->stringFT($xShadow, $itemFont->{'value'}, $itemFont->{'size'}, 0,  $xMargin + 42, $base +1, $txtPlay);
   $imgMenu->stringFT($xItem,   $itemFont->{'value'}, $itemFont->{'size'}, 0,  $xMargin + 40, $base   , $txtPlay);
   $imgMenu->stringFT($xShadow, $itemFont->{'value'}, $itemFont->{'size'}, 0, $imgWidth - $xMargin - $wBack +2, $base +1, $txtMain);
   $imgMenu->stringFT($xItem,   $itemFont->{'value'}, $itemFont->{'size'}, 0, $imgWidth - $xMargin - $wBack, $base, $txtMain);
   $self->drawButton(\$imgMenu, $xMargin + 10, $buttonBase, $itemFont->{'color'}) if $main::testOnly;
   my @dim = $self->drawButton(\$imgButton,  $xMargin + 10, $buttonBase, $itemFont->{'color'});

   $$spuRef .= '<button name="play" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
              .'up="main" left="main" down="';
   my $oBt  = 'main';

   if($text){    $oBt = 'next'; }
   elsif($pgNr){ $oBt = 'prev'; }
   $$spuRef .= $oBt.'" right="'.$oBt.'"/>'."\n";  

   $self->drawButton(\$imgMenu,   $imgWidth - $xMargin - $wBack - 30, $buttonBase, $itemFont->{'color'}) if $main::testOnly;
   @dim = $self->drawButton(\$imgButton, $imgWidth - $xMargin - $wBack - 30, $buttonBase, $itemFont->{'color'});
   $$spuRef .= '<button name="main" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
              .'down="play" right="play" up="';
   $oBt = 'play';           
   if($pgNr){    $oBt = 'prev'; }
   elsif($text){ $oBt = 'next'; }
   $$spuRef .= $oBt.'" left="'.$oBt.'"/>'."\n";  
   
   if($text){
      $imgMenu->filledArc(int($imgMenu->width /2) - 20, $buttonBase, 30, 30, 225, 315, $xItem) if not $main::testOnly;
      $self->drawButton(\$imgMenu,   int($imgMenu->width /2) - 20, $buttonBase, $itemFont->{'color'}, 'down') if $main::testOnly;
      @dim = $self->drawButton(\$imgButton, int($imgMenu->width /2) - 20, $buttonBase, $itemFont->{'color'}, 'down');
      $$spuRef .= '<button name="next" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
                 .'up="play" left="play" down="';
      $oBt = 'main';
      $oBt = 'prev' if $pgNr; 
      $$spuRef .= $oBt.'" right="'.$oBt.'"/>'."\n";  
      }
   if($pgNr){
      $imgMenu->filledArc(int($imgMenu->width /2) + 20, $buttonBase, 30, 30,  45, 135, $xItem) if not $main::testOnly;
      $self->drawButton(\$imgMenu,   int($imgMenu->width /2) + 20, $buttonBase, $itemFont->{'color'}, 'up') if $main::testOnly;
      @dim = $self->drawButton(\$imgButton, int($imgMenu->width /2) + 20, $buttonBase, $itemFont->{'color'}, 'up');
      $$spuRef .= '<button name="prev" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
                 .'down="main" right="main" up="';
      $oBt = 'play';           
      $oBt = 'next' if $text; 
      $$spuRef .= $oBt.'" left="'.$oBt.'"/>'."\n";  
      }

   print('gonna save menu images to ['.$work."]\n") if $debug;
   if(open($fp, '> '.$work.'/menu'.$special.'-bg-'.$pgNr.'.png')){
      binmode($fp);
      print($fp $imgMenu->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu'.$special.'-bt-'.$pgNr.'.png')){
      binmode($fp);
      print($fp $imgButton->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu-no-buttons.png')){
      binmode($fp);
      print($fp $imgEmpty->png);
      close($fp);
      }
   return $text;
}

sub createSpecialMenu {
   my $self = shift;
   my $special = shift;
   my $work = shift;
   my $spuRef = shift;
   my $cfg  = $special->{'settings'};
   my $appCfg = $self->{'work'}{'appConfig'};
   my $imgMenu   = $self->createBackground($cfg);
   my $imgButton = GD::Image->newPalette($imgMenu->width, $imgMenu->height);
   my $imgEmpty  = GD::Image->newPalette(720, 576);
   my $titleFont = $cfg->{'titlefont'};
   my $itemFont  = $cfg->{'itemfont'};
   my($iR, $iG, $iB) = @{$appCfg->getColor4($itemFont->{'color'})};
   my($tR, $tG, $tB) = @{$appCfg->getColor4($titleFont->{'color'})};
   my $yMargin = $cfg->{'yMargin'};
   my $xMargin = $cfg->{'xMargin'};
   my $txtMain = $appCfg->getLangToken('mainmenue');
   my @bounds  = GD::Image->stringFT(0, $cfg->{'itemfont'}->{'value'}, $cfg->{'itemfont'}{'size'}, 0,0,0, 'yT');
   my @bkBnds  = GD::Image->stringFT(0, $itemFont->{'value'}, $itemFont->{'size'},  0, 0, 0, $txtMain);
   my $baseOff = max($bounds[1] - $bounds[5] + 10, 50);

   $yMargin += 50 if $cfg->{'aspect'} =~ /^lb$/;
   my $xShadow = $imgMenu->colorResolve(255-$iR, 255-$iG, 255-$iB);
   my $xItem   = $imgMenu->colorResolve($iR, $iG, $iB);
   my $xTitle  = $imgMenu->colorResolve($tR, $tG, $tB);
   my $dummy   = $imgEmpty->colorAllocate(0,0,0);
   my $wBack  = $bkBnds[4] - $bkBnds[0];
   my $base = $imgMenu->height - $yMargin - 10;
   my $buttonBase = $base - int($baseOff/2 - 11);

   $imgEmpty->transparent($dummy);
   $dummy = $imgButton->colorAllocate(0,0,0);
   $imgButton->transparent($dummy);
   $imgMenu->clip($xMargin,  $yMargin, $imgMenu->width - $xMargin,  $yMargin + 57);
   $imgMenu->stringFT($xShadow, $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 20, $yMargin + 52, $special->{'title'});
   $imgMenu->stringFT($xTitle,  $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 18, $yMargin + 50, $special->{'title'});
   $imgMenu->clip($xMargin +5, $yMargin + 70, $imgMenu->width - 172, $imgMenu->height - $yMargin - $baseOff);

   my @bounds = GD::Image->stringFT(0, $itemFont->{'value'}, $itemFont->{'size'}, 0, 0, 0, 'yT');
   my $hLine = max(21, int(($bounds[1] - $bounds[5]) * 1.1));
   my $x0 = $xMargin +18;
   my $yTop = $yMargin + 70;
   my $x1 = $imgMenu->width - 172;
   my $yBottom = $imgMenu->height - $yMargin - $baseOff-2;
   my @buttons;
   my @dim;
   my $fp;
   my $i=0;

   foreach my $page (@{$special->{'pages'}}){
           next if not $page->{'title'};     
           my $curY = $yTop + $bounds[1] - $bounds[5] + $i * $hLine;

           $imgMenu->stringFT($xItem, $itemFont->{'value'}, $itemFont->{'size'}, 0, $xMargin + 50, $curY, $page->{'title'});
           $self->drawButton(\$imgMenu,   $xMargin + 25, $curY - int(($bounds[1] - $bounds[5])*0.45), $itemFont->{'color'}) if $main::testOnly; 
           @dim = $self->drawButton(\$imgButton, $xMargin + 25, $curY - int(($bounds[1] - $bounds[5])*0.45), $itemFont->{'color'}); 
           push(@buttons, [@dim]);
           $i++;
           }
   $imgMenu->clip(0, 0, $imgMenu->width, $imgMenu->height);
   my $mx = $#buttons +1;

   $i=1;
   foreach my $dim (@buttons){
           $$spuRef .= '<button name="item'.$i.'" x0="'.$dim->[0].'" y0="'.$dim->[1].'" x1="'.$dim->[2].'" y1="'.$dim->[3].'" ';
           $$spuRef .= 'up="main" left="main" '                     if $i eq 1;
           $$spuRef .= 'up="item'.($i-1).'" left="item'.($i-1).'" ' if not $i eq 1;
           $$spuRef .= 'down="main" right="main" '                     if $i eq $mx;
           $$spuRef .= 'down="item'.($i+1).'" right="item'.($i+1).'" ' if $i < $mx;
           $$spuRef .= '/>'."\n";
           $i++;
           }
   $imgMenu->stringFT($xShadow, $itemFont->{'value'}, $itemFont->{'size'}, 0, $imgWidth - $xMargin - $wBack +2, $base +1, $txtMain);
   $imgMenu->stringFT($xItem,   $itemFont->{'value'}, $itemFont->{'size'}, 0, $imgWidth - $xMargin - $wBack, $base, $txtMain);
   $self->drawButton(\$imgMenu, $imgWidth - $xMargin - $wBack - 30, $buttonBase, $itemFont->{'color'}) if $main::testOnly;
   @dim = $self->drawButton(\$imgButton, $imgWidth - $xMargin - $wBack - 30, $buttonBase, $itemFont->{'color'});

   $$spuRef .= '<button name="main" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" ';
   $$spuRef .=  'up="item'.$mx.'" left="item'.$mx.'" '
               .'down="item1" right="item1"/>'."\n";
   
   print('gonna save menu images to ['.$work."]\n") if $debug;
   if(open($fp, '> '.$work.'/menu-main.png')){
      binmode($fp);
      print($fp $imgMenu->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu-main-bt.png')){
      binmode($fp);
      print($fp $imgButton->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu-main-empty.png')){
      binmode($fp);
      print($fp $imgEmpty->png);
      close($fp);
      }
}

sub createSubMenu {
   my $self = shift;
   my $rec  = shift;
   my $work = shift;
   my $page = shift;
   my $summary = shift;
   my $spuRef = shift;
   my $appCfg = $self->{'work'}{'appConfig'};
   my $cfg  = $rec->{'settings'};
   my $imgMenu   = $self->createBackground($cfg);
   my $imgButton = GD::Image->newPalette($imgMenu->width, $imgMenu->height);
   my $imgEmpty  = GD::Image->newPalette(720, 576);
   my $titleFont = $cfg->{'titlefont'};
   my $itemFont  = $cfg->{'itemfont'};
   my $summaryFont = $cfg->{'summaryfont'};
   my($iR, $iG, $iB) = @{$appCfg->getColor4($itemFont->{'color'})};
   my($tR, $tG, $tB) = @{$appCfg->getColor4($titleFont->{'color'})};
   my($sR, $sG, $sB) = @{$appCfg->getColor4($summaryFont->{'color'})};
   my $txtMain = $appCfg->getLangToken('mainmenue');
   my $txtPlay = $appCfg->getLangToken('playRec');
   my $yMargin = $cfg->{'yMargin'};
   my $xMargin = $cfg->{'xMargin'};
   my @bounds  = GD::Image->stringFT(0, $cfg->{'itemfont'}->{'value'}, $cfg->{'itemfont'}{'size'}, 0,0,0, 'yT');
   my $baseOff = max($bounds[1] - $bounds[5] + 10, 50);

   $yMargin += 50 if $cfg->{'aspect'} =~ /^lb$/;
   my $xShadow = $imgMenu->colorResolve(255-$iR, 255-$iG, 255-$iB);
   my $xItem   = $imgMenu->colorResolve($iR, $iG, $iB);
   my $xTitle  = $imgMenu->colorResolve($tR, $tG, $tB);
   my $dummy   = $imgEmpty->colorAllocate(0,0,0);
   
   $imgEmpty->transparent($dummy);
   $dummy = $imgButton->colorAllocate(0,0,0);
   $imgButton->transparent($dummy);

   my @bounds = GD::Image->stringFT(0, $itemFont->{'value'}, $itemFont->{'size'},  0, 0, 0, 'yT');
   my @bkBnds = GD::Image->stringFT(0, $itemFont->{'value'}, $itemFont->{'size'},  0, 0, 0, $txtMain);
   my $wBack  = $bkBnds[4] - $bkBnds[0];
   my $height = max(21, $bounds[1] - $bounds[5]);
   my $base = $imgMenu->height - $yMargin - 10;
   my $buttonBase = $base - int($baseOff/2 - 11);
   my $fp;

   $imgMenu->clip($xMargin,  $yMargin, $imgMenu->width - $xMargin,  $yMargin + 57);
   $imgMenu->stringFT($xShadow, $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 20, $yMargin + 52, $rec->{'title'});
   $imgMenu->stringFT($xTitle,  $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 18, $yMargin + 50, $rec->{'title'});
   $imgMenu->clip(0, 0, $imgMenu->width, $imgMenu->height);
   $imgMenu->stringFT($xShadow, $itemFont->{'value'}, $itemFont->{'size'}, 0,  $xMargin + 42, $base +1, $txtPlay);
   $imgMenu->stringFT($xItem,   $itemFont->{'value'}, $itemFont->{'size'}, 0,  $xMargin + 40, $base   , $txtPlay);
   $imgMenu->stringFT($xShadow, $itemFont->{'value'}, $itemFont->{'size'}, 0, $imgWidth - $xMargin - $wBack +2, $base +1, $txtMain);
   $imgMenu->stringFT($xItem,   $itemFont->{'value'}, $itemFont->{'size'}, 0, $imgWidth - $xMargin - $wBack, $base, $txtMain);

   $self->drawButton(\$imgMenu, $xMargin + 10, $buttonBase, $itemFont->{'color'}) if $main::testOnly;
   my @dim = $self->drawButton(\$imgButton,  $xMargin + 10, $buttonBase, $itemFont->{'color'});
   $$spuRef .= '<button name="play" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
              .'up="main" left="main" down="';
   print('gonna write summary ...'."\n") if $debug;
   my @box = ($xMargin + 15, $yMargin + 73, $imgMenu->width - $xMargin - 122, $imgMenu->height - $yMargin - $baseOff -2);
   $imgMenu->clip($xMargin,  $yMargin + 66, $imgMenu->width - 170, $imgMenu->height - $yMargin - $baseOff) if $cfg->{'boxes'};
   my $text = $self->writeSummary(\$imgMenu, $rec, $summary, \@box);
   my $oBt  = 'main';

   $imgMenu->clip(0, 0, $imgMenu->width, $imgMenu->height);
   if($text){    $oBt = 'next'; }
   elsif($page){ $oBt = 'prev'; }
   $$spuRef .= $oBt.'" right="'.$oBt.'"/>'."\n";  

   $self->drawButton(\$imgMenu,   $imgWidth - $xMargin - $wBack - 30, $buttonBase, $itemFont->{'color'}) if $main::testOnly;
   @dim = $self->drawButton(\$imgButton, $imgWidth - $xMargin - $wBack - 30, $buttonBase, $itemFont->{'color'});
   $$spuRef .= '<button name="main" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
              .'down="play" right="play" up="';
   $oBt = 'play';           
   if($page){    $oBt = 'prev'; }
   elsif($text){ $oBt = 'next'; }
   $$spuRef .= $oBt.'" left="'.$oBt.'"/>'."\n";  
   
   if($text){
      $imgMenu->filledArc(int($imgMenu->width /2) - 20, $buttonBase, 30, 30, 225, 315, $xItem) if not $main::testOnly;
      $self->drawButton(\$imgMenu,   int($imgMenu->width /2) - 20, $buttonBase, $itemFont->{'color'}, 'down') if $main::testOnly;
      @dim = $self->drawButton(\$imgButton, int($imgMenu->width /2) - 20, $buttonBase, $itemFont->{'color'}, 'down');
      $$spuRef .= '<button name="next" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
                 .'up="play" left="play" down="';
      $oBt = 'main';
      $oBt = 'prev' if $page; 
      $$spuRef .= $oBt.'" right="'.$oBt.'"/>'."\n";  
      }
   if($page){
      $imgMenu->filledArc(int($imgMenu->width /2) + 20, $buttonBase, 30, 30,  45, 135, $xItem) if not $main::testOnly;
      $self->drawButton(\$imgMenu,   int($imgMenu->width /2) + 20, $buttonBase, $itemFont->{'color'}, 'up') if $main::testOnly;
      @dim = $self->drawButton(\$imgButton, int($imgMenu->width /2) + 20, $buttonBase, $itemFont->{'color'}, 'up');
      $$spuRef .= '<button name="prev" x0="'.$dim[0].'" y0="'.$dim[1].'" x1="'.$dim[2].'" y1="'.$dim[3].'" '
                 .'down="main" right="main" up="';
      $oBt = 'play';           
      $oBt = 'next' if $text; 
      $$spuRef .= $oBt.'" left="'.$oBt.'"/>'."\n";  
      }

   print('gonna save menu images to ['.$work."]\n") if $debug;
   if(open($fp, '> '.$work.'/menu-bg-'.$page.'.png')){
      binmode($fp);
      print($fp $imgMenu->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu-bt-'.$page.'.png')){
      binmode($fp);
      print($fp $imgButton->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu-no-buttons.png')){
      binmode($fp);
      print($fp $imgEmpty->png);
      close($fp);
      }
   return $text;
}

sub createMainMenu {
   my $self = shift;
   my $disk = shift;
   my $work = shift;
   my $spuRef = shift;
   my $cfg  = $disk->{'settings'};
   my $appCfg = $self->{'work'}{'appConfig'};
   my $imgMenu   = $self->createBackground($cfg);
   my $imgButton = GD::Image->newPalette($imgMenu->width, $imgMenu->height);
   my $imgEmpty  = GD::Image->newPalette(720, 576);
   my $titleFont = $cfg->{'titlefont'};
   my $itemFont  = $cfg->{'itemfont'};
   my($iR, $iG, $iB) = @{$appCfg->getColor4($itemFont->{'color'})};
   my($tR, $tG, $tB) = @{$appCfg->getColor4($titleFont->{'color'})};
   my $yMargin = $cfg->{'yMargin'};
   my $xMargin = $cfg->{'xMargin'};
   my @bounds  = GD::Image->stringFT(0, $cfg->{'itemfont'}->{'value'}, $cfg->{'itemfont'}{'size'}, 0,0,0, 'yT');
   my $baseOff = max($bounds[1] - $bounds[5] + 10, 50);

   $yMargin += 50 if $cfg->{'aspect'} =~ /^lb$/;
   my $xShadow = $imgMenu->colorResolve(255-$iR, 255-$iG, 255-$iB);
   my $xItem   = $imgMenu->colorResolve($iR, $iG, $iB);
   my $xTitle  = $imgMenu->colorResolve($tR, $tG, $tB);
   my $dummy   = $imgEmpty->colorAllocate(0,0,0);

   $imgEmpty->transparent($dummy);
   $dummy = $imgButton->colorAllocate(0,0,0);
   $imgButton->transparent($dummy);
   $imgMenu->clip($xMargin,  $yMargin, $imgMenu->width - $xMargin,  $yMargin + 57);
   $imgMenu->stringFT($xShadow, $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 20, $yMargin + 52, $disk->{'title'});
   $imgMenu->stringFT($xTitle,  $titleFont->{'value'}, $titleFont->{'size'}, 0,  $xMargin + 18, $yMargin + 50, $disk->{'title'});
   $imgMenu->clip($xMargin +5, $yMargin + 70, $imgMenu->width - 172, $imgMenu->height - $yMargin - $baseOff);
   
   my @bounds = GD::Image->stringFT(0, $itemFont->{'value'}, $itemFont->{'size'}, 0, 0, 0, 'yT');
   my @tsBnds = GD::Image->stringFT(0, $itemFont->{'value'}, int($itemFont->{'size'}/8*5), 0, 0, 0, localizeTS($disk->{'recordings'}[0]{'ts'}));
   my $hLine = max(21, $bounds[1] - $bounds[5] + $tsBnds[1] - $tsBnds[5]);
   my $x0 = $xMargin +18;
   my $yTop = $yMargin + 70;
   my $x1 = $imgMenu->width - 172;
   my $yBottom = $imgMenu->height - $yMargin - $baseOff-2;
   my @buttons;
   my @dim;
   my $fp;
   my $i=0;

   foreach my $rec (@{$disk->{'recordings'}}){
           next if not $rec->{'title'};     
           my $curY = $yTop + $bounds[1] - $bounds[5] + $i * $hLine;

           $imgMenu->stringFT($xItem, $itemFont->{'value'}, $itemFont->{'size'}, 0, $xMargin + 50, $curY, $rec->{'title'});
           $imgMenu->stringFT($xItem, $itemFont->{'value'}, int($itemFont->{'size'}/8*5), 0, $imgMenu->width - $xMargin - ($tsBnds[4]-$tsBnds[0]) - 122, $curY + ($tsBnds[1] - $tsBnds[5]), localizeTS($rec->{'ts'}));
           $self->drawButton(\$imgMenu,   $xMargin + 25, $curY - int(($bounds[1] - $bounds[5])*0.45), $itemFont->{'color'}) if $main::testOnly; 
           @dim = $self->drawButton(\$imgButton, $xMargin + 25, $curY - int(($bounds[1] - $bounds[5])*0.45), $itemFont->{'color'}); 
           push(@buttons, [@dim]);
           $i++;
           }
   if($disk->{'special'}{'title'}){
      my $curY = $yTop + $bounds[1] - $bounds[5] + $i * $hLine;
      
      $imgMenu->stringFT($xItem, $itemFont->{'value'}, $itemFont->{'size'}, 0, $xMargin + 50, $curY, $disk->{'special'}{'title'});
      $self->drawButton(\$imgMenu,   $xMargin + 25, $curY - int(($bounds[1] - $bounds[5])*0.45), $itemFont->{'color'}) if $main::testOnly; 
      @dim = $self->drawButton(\$imgButton, $xMargin + 25, $curY - int(($bounds[1] - $bounds[5])*0.45), $itemFont->{'color'}); 
      push(@buttons, [@dim]);
      }
   my $mx = $#buttons +1;

   $i=1;
   foreach my $dim (@buttons){
           $$spuRef .= '<button name="item'.$i.'" x0="'.$dim->[0].'" y0="'.$dim->[1].'" x1="'.$dim->[2].'" y1="'.$dim->[3].'" ';
           $$spuRef .= 'up="item'.$mx.'" left="item'.$mx.'" '       if $i eq 1;
           $$spuRef .= 'up="item'.($i-1).'" left="item'.($i-1).'" ' if not $i eq 1;
           $$spuRef .= 'down="item1" right="item1" ' if $i eq $mx;
           $$spuRef .= 'down="item'.($i+1).'" right="item'.($i+1).'" ' if $i < $mx;
           $$spuRef .= '/>'."\n";
           $i++;
           }
   print('gonna save menu images to ['.$work."]\n") if $debug;
   if(open($fp, '> '.$work.'/menu-main.png')){
      binmode($fp);
      print($fp $imgMenu->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu-main-bt.png')){
      binmode($fp);
      print($fp $imgButton->png);
      close($fp);
      }
   if(open($fp, '> '.$work.'/menu-main-empty.png')){
      binmode($fp);
      print($fp $imgEmpty->png);
      close($fp);
      }
}

1;     
__END__
