# Album Plugin:	format/image_protect
# For info:	'album -plugin_info format/image_protect'
# For usage:	'album -plugin_usage format/image_protect'

use strict;

my $PLUGIN = 'format/image_protect';

my $DESCRIPTION = << 'DESCRIPTION';
Plugin: format/image_protect

Protects images from downloading.

No image protection scheme is foolproof!

If you publish it, and someone knows what they're doing, then
you won't be able to stop them from keeping it.

Method:

1) Disables "Image Toolbar" for image
2) Disables right clicking for image
3) Turns off link to full-size image

Also see album's "-credit" option.

DESCRIPTION

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

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

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

sub do_album {
	my ($opt) = @_;
	# Just a sanity check (we skip 'end_handle_file' otherwise)
	# ..arguably this is a bug that should be fixed in album??
	album::usage($opt,"[$PLUGIN] plugin requires '-thumbs' option")
		unless $opt->{thumbs};

	# Don't link to images, just have image pages
	print STDERR "[$PLUGIN] Implies -image_pages\n" unless $opt->{image_pages};
	$opt->{image_pages} = 1;

	# Still do the album
	return 0;
}

sub protect_file {
	my ($opt,$data, undef,  $dir, $pic, $obj, $path) = @_;

	$obj->{URL}{image_page}{image} = 'no-image-downloads';
	$obj->{full}{intag} .= ' galleryimg="no" oncontextmenu="return false" ';
	$obj->{medium}{intag} .= ' galleryimg="no" oncontextmenu="return false" ';

	album::add_head($opt,$data,<<END_OF_BODY);
<script type="text/javascript">

var clickmessage="Right click disabled on images!"

function disableclick(e) {
	if (document.all) {
		if (event.button==2||event.button==3) {
			if (event.srcElement.tagName=="IMG") {
				alert(clickmessage);
				return false;
			}
		}
	} else if (document.layers) {
		if (e.which == 3) {
			alert(clickmessage);
			return false;
		}
	} else if (document.getElementById)
		if (e.which==3&&e.target.tagName=="IMG")
			setTimeout("alert(clickmessage)",0)
}

function associateimages() {
	for(i=0;i<document.images.length;i++)
		document.images[i].onmousedown=disableclick;
}

if (document.all) {
	document.onmousedown=disableclick;
	for (var i_tem = 0; i_tem < document.images.length; i_tem++)
		document.images[i_tem].galleryimg='no'
} else if (document.getElementById)
	document.onmouseup=disableclick;
else if (document.layers)
	associateimages()
</script>
END_OF_BODY

	# Don't skip the file!
	return 0;
}

# Plugins always end with:
1;
