# Album Plugin:	captions/exif/mouseover
# For info:	'album -plugin_info captions/exif/mouseover'
# For usage:	'album -plugin_usage captions/exif/mouseover'
use strict;
use File::Copy;

my $DESCRIPTION = << 'DESCRIPTION';
Add a mouseover window to images that displays photo EXIF info.
DESCRIPTION

# later..
my @KEY_ORDER = qw();

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

  # Setup the options
#  album::add_option(1, 'embed', album::OPTION_BOOL, default=>1,
#                    usage=>'Embed the sound in the page');

  # Setup the hooks
  album::hook($opt, 'do_album', \&setup_script);
  album::hook($opt, 'modify_caption', \&modify_caption);

  return {
    author => 'David Madison',
    href => 'http://MarginalHacks.com/',
    version => '1.0',
    description => $DESCRIPTION,
  };
}

# Come up with a legal 'id="name"' given a file.  We may lose uniqueness
# here if, for example we have two files: [ab;ba.jpg] and [ab!ba.jpg].
# Personally I'm not terribly bothered by this bug.
#
# From w3.org HTML 4.01 spec:
# Legal ID and NAME tokens are of the format:  [A-Za-z][A-Za-z0-9_:.-]*
sub id {
	my ($file) = @_;

	# Remove illegal characters
  $file =~ s/[^a-z0-9_:\.-]/_/ig;
	# Make sure it starts with a letter
	$file =~ /^[a-z]/i ? $file : "Z$file";
}

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

	# Get the exif info
	#########################
  my $pic_file = $data->{obj}{$pic}{full}{path} || "$dir/$pic";
  my $exif = album::get_exif_info($opt,$data,$pic_file);
	return $cap unless $exif && ref $exif eq 'HASH';

	my $id = id($pic);

	# Create the exif <div>
	#########################
	$cap .= <<EXIF_TOP;
<div id="shLayer"
    style="position:absolute; z-index:1; background-color: #FFFFFF; border: 1px solid #660000; visibility: hidden"
    onMouseOver="showHideLayers('shLayer','','show')"
    onMouseOut="showHideLayers('shLayer','','hide')"> 
	<table id="exif_info">
EXIF_TOP

	foreach my $key ( keys %$exif ) {
		$cap .= <<ELEMENT;
		<tr>
			<td class=exif_key>$key</td>
			<td class=exif_val>$exif->{$key}</td>
		</tr>
ELEMENT
	}

	$cap .= <<EXIF_BOTTOM;
	</table>
</div>
EXIF_BOTTOM

	# Modify the <img> tag
	#########################
  my $obj = $data->{obj}{$pic};
	$obj->{intag} .= <<IMGTAG;
id='$id'
onMouseOver="showHideLayers('shLayer','','show'); placeMenu('shLayer','$id')"
onMouseOut="showHideLayers('shLayer','','hide')" style="position: relative;"
IMGTAG

	return $cap;
}

sub setup_script {
  my ($opt, $data, $hookname, $dir, $album) = @_;
  
  #my $embed = album::option($opt, 'embed');

	$data->{head} .= <<HEAD;
<style type="text/css">
<!--
#exif_info {
	font-size:11px;
	font-family:Verdana, Arial, Helvetica, sans-serif;
	color:#777777;
	letter-spacing:0px;
	border: none;
	padding: 1px;
    }
-->
</style>

<script language="JavaScript" type="text/JavaScript">
<!--
function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function placeMenu(layerID,placementID){
	layer_obj = document.getElementById(layerID);
	placement_obj = document.getElementById(placementID); 
		
//	h=placement_obj.offsetLeft+placement_obj.offsetWidth-layer_obj.offsetWidth;
	h=placement_obj.offsetLeft+55;
	v=placement_obj.offsetTop+38;

	layer_obj.style.left=h;
	layer_obj.style.top=v;
}

function showHideLayers() { //v6.0
  var i,p,v,obj,args=showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
//-->
</script>
HEAD
  
  return 0;	# Don't skip the album!
}

# Plugins always end with:
1;


