# Album Plugin:	captions/format/hide_shorten
# For info:	'album -plugin_info captions/format/hide_shorten'
# For usage:	'album -plugin_usage captions/format/hide_shorten'
use strict;

sub start_plugin {
  my ($opt,$plugin,$path) = @_;

  # Setup my hooks
  album::hook($opt,'modify_caption',\&modify_caption);
  album::hook($opt,'modify_dir_caption',\&modify_caption);
  album::add_option(1,'length',$album::OPTION_NUM, default=>15, usage=>"Length of captions");
  album::add_option(1,'expand',$album::OPTION_STR, default=>'...', usage=>"Text for the expand button");
  album::add_option(1,'collapse',$album::OPTION_STR, default=>'(collapse)', usage=>"Text for the collapse button");

  return {
    author => 'David Ljung Madison',
    href => 'http://MarginalHacks.com/',
    version => '1.0',
    description =>
"Caption shortener

Shortens captions with a '..' link that reveals the hidden caption.

You can change the option length with: -hide_shorten:length",
  };
}

my $CAP;
sub modify_caption {
  my ($opt, $data, $hookname, $dir, $pic, $cap) = @_;

  # Regen HTML if the option changes.
  album::new_html($opt,$data,$pic)
    if album::option_changed($opt,'length');

	my $len = album::option($opt,'length');
	my $expand = album::option($opt,'expand');
	my $collapse = album::option($opt,'collapse');

  # The new caption with the paypal button
	return undef if length($cap) < $len+3;
	my @cap = album::mysplit($cap);
	return undef if (scalar @cap) <= $len;
	my @a = splice(@cap,0,$len);

	my $id = "CAP".++$CAP;

# Something like this could have been an interesting javascript onclick function..
#  var idx = this.id.split('-')[1];
#  var sp = document.getElementById('cont-'+idx);
#  sp.style.display = ('' == sp.style.display)? 'none':'';

	my $trunc = <<TRUNCATE;
<span id="dotdot$id" style="display: show;">
<a href="#" onclick="document.getElementById('dotdot$id').style.display='none'; document.getElementById('$id').style.display=''; return false;">$expand</a>
</span>
<span id="$id" style="display: none;">
TRUNCATE
	my $end = " <a href='#' onclick=\"document.getElementById('$id').style.display='none'; document.getElementById('dotdot$id').style.display=''; return false;\">$collapse</a></span>\n";

	join('',@a,$trunc,@cap,$end);
}

# Plugins always end with:
1;
