# Album Plugin:	social/facebook-like
# For info:	'album -plugin_info social/facebook-like'
# For usage:	'album -plugin_usage social/facebook-like'

# Version history:
# v1.01 David Ljung Madison <contactRequest at GetDave.com>
# 			+ Cleaned up code by moving it to format/opengraph
# v1.00 David Ljung Madison <contactRequest at GetDave.com>
#       + released

use strict;

my $DESCRIPTION = << 'DESCRIPTION';
Plugin: social/facebook-like

Add facebook like/share buttons!

	https://developers.facebook.com/docs/plugins/like-button

Facebook may update/change their code, in which case this plugin
will no longer work without being updated!  If you find that the
code this plugin generates is out-of-date, make sure you have the
most current version of album and it's plugins from:

  http://MarginalHacks.com/

And if so, please contact us and let us know!

This plugin requires the use of the 'format/opengraph' plugin

DESCRIPTION

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

	# Requires 'format/opengraph' plugin
	album::get_plugin($opt,'facebook-comments','format/opengraph');

	# Setup the options
	album::add_option(1, 'layout', album::OPTION_STR, default=>"button_count", usage=>"Layout (standard, button_count, box_count)");
	album::add_option(1, 'share', album::OPTION_BOOL, default=>1, usage=>"Include a 'share' button");
	album::add_option(1, 'colorscheme', album::OPTION_STR, usage=>"Colorscheme (either 'light' or 'dark')");

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

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

sub setup_head {
	my ($opt,$data, undef,  $dir, $album) = @_;

	# Add it to the head
	$data->{head} .= <<FACEBOOK_JS unless $data->{head} =~ /fb-root/;
    <!-- Added by plugin social/facebook-like -->
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk')); </script>
		<!-- END code for plugin social/facebook-like   -->
FACEBOOK_JS

	# Still do the album!
	return 0;
}

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

	# Calculated by the opengraph plugin (which we require)
	my $image_page_url = $obj->{opengraph_image_url};

	# Options
	my $layout = album::option($opt, 'layout');
	my $share = album::option($opt, 'share') ? 'true' : 'false';
	my $colorscheme = album::option($opt, 'colorscheme') ||
		album::option($opt, 'social/facebook-comments:colorscheme');
	$colorscheme = ($colorscheme eq 'dark') ? 'dark' : 'light';

	# Add the Facebook div
	$obj->{cap_image} .= <<LIKE;
<br />
<div class="fb-like" data-href="$image_page_url" data-layout="$layout" data-action="like" data-colorscheme="$colorscheme" data-share="$share"></div>
LIKE
}

# Plugins always end with:
1;
