#!/usr/bin/perl

use strict;
use CGI ':standard';
use Image::Magick;
use LWP::Simple;
use Data::Dumper;

my $params = CGI::Vars();

my $action = $params->{action} || 'display_images';
exit if(!grep($_ eq $action, qw(display_images)));

my $site_path = '/var/www/html/twr360';

my $content = '';
eval "\$content=$action();";

if($params->{output} eq 'template'){
	$content = templateFill('player_pdf.html',{body=>$content});
}
	
print "content-type: text/html\n\n$content";
exit;

sub display_images{
	my $id = $params->{id};
	my $dir = $params->{dir} || "/programs_data/pdf_images/$id/";
	if(!-d "$site_path$dir"){
		$dir = "/programs_data/pdf_images2/$id/";
	}
	my $path = "$site_path$dir";
	my @files = <$path*.jpg>;
	my $count = scalar(@files)/2;
	
	return '' if(!$count);
	
	my $width = 500;
	my $cwidth = 500;
	my $img = Image::Magick->new();
	$img->Read(filename=>"${path}img_1.jpg");
	my ($cimg_width, $cimg_height) = $img->Get('width','height');
	my $img2 = Image::Magick->new();
	my ($img_width, $img_height);
	my $skip_flag = 0;
	if(-e "${path}img_2.jpg"){
		$img2->Read(filename=>"${path}img_2.jpg");
		($img_width, $img_height) = $img2->Get('width','height');
	}else{
		$skip_flag = 1;
		$img_width = $cimg_width;
		$img_height = $cimg_height;
	}
	
	my $resize_flag = 0;
	if(!$skip_flag && ($cimg_width != $img_width || $cimg_height != $img_height)){
		$resize_flag = 1;
		my $image_binary = get("https://www.twr360.org/images/sized/img_2/l${width}x1000/img.jpg?p=$dir");
		my $img2 = Image::Magick->new();
		$img2->BlobToImage($image_binary);
		($img_width, $img_height) = $img2->Get('width','height');
		$width = $img_width if($img_width < $width);
	}
	
	my $cover_height = $cimg_height*($cwidth/$cimg_width);
	my $height = $img_height*($width/$img_width);
	
	my $time = time();
	
	my $thumbs = '';
	my $out = qq~
	
	<div id="bookflip-container">
		<a class="thumbnail-toggle" href="javascript:void(0);" onclick="document.getElementById('thumbnails').style.display = (document.getElementById('thumbnails').style.display == 'block')?'none':'block';"><!lang:show_hide_thumbnails></a>
		
		<div class="controls">
			<a href="javascript:void(0)" class="previous" onclick="clipmeR();"><!lang:previous></a>
			<a href="javascript:void(0)" class="next" onclick="clipmeL();"><!lang:next></a>
		</div>
		
		<div id="shadow-left" class="shadow"></div>
		<div id="shadow-right" class="shadow"></div>
		
		<div id="bookflip">
		</div>
		
	</div>

	<script type="text/javascript">
		var pageTranslated = '<!lang:Page>';
		var \$ = jQuery;
		
		\$('.controls > a').hover(function(){
			\$(this).fadeTo(200,1);
		},function(){
			\$(this).fadeTo(100,0);
		});
	</script>
	
	<script type="text/javascript" src="/javascript/bookflip.js"></script>
		<div id="pages" style="display: none;">~;
	for(1..$count){
		my $url = ($_ == 1 || !$resize_flag)?"${dir}img_$_.jpg?ts=$time":"/images/sized/img_$_/l${cwidth}x1000/img.jpg?p=$dir&ts=$time";
		$out .= qq~<div style="background:url('$url');"></div>\n~;
		my $page_num = int($_)-1;
		$thumbs .= qq~<li><a href="javascript:void(0);" onclick="autoFlip($page_num);"><img src="${dir}thumb_$_.jpg" alt="" /> <span>$_</span></a></li>~;
	}
	$out .= qq~</div>
	<div id="thumbnails">
		<ul>
			$thumbs
		</ul>
	</div>
	<script type="text/javascript">
		var \$ = jQuery;
		pWidth=$img_width; //width of each page
		pHeight=$img_height; //height of each page
		
		numPixels=10;  //size of block in pixels to move each pass
		pSpeed=12; //speed of animation, more is slower
		
		startingPage="0";//select page to start from, for last page use "e", eg. startingPage="e"
		allowAutoflipFromUrl = true; //true allows querystring in url eg bookflip.html?autoflip=5
		
		pageBackgroundColor="#CCCCCC";
		pageFontColor="#ffffff";
		
		pageBorderWidth="1";
		pageBorderColor="#3D4D5D";
		pageBorderStyle="solid";  //dotted, dashed, solid, double, groove, ridge, inset, outset, dotted solid double dashed, dotted solid
		
		pageShadowLeftImgUrl ="/bookflip/black_gradient.png";
		pageShadowWidth = 80;
		pageShadowOpacity = 100;
		pageShadow=0 //0=shadow off, 1= shadow on left page
		
		allowPageClick=true; //allow page turn by clicking the page directly
		allowNavigation=true; //this builds a drop down list of pages for auto navigation.
		
		ini();
		</script>
	~;
	
	recordHit();
	
	return $out;
}

sub recordHit{
	#my ($id) = $dbh->selectrow_array("SELECT id FROM ministry_program_hits WHERE ministry_id = ? AND language_id = ? AND program_file_id = ? AND program_file_type = 'pdf'");
}

sub templateFill{
	my ($template,$rep)=@_;
	$rep||={};
	my $path=$site_path."/templates";
	open(IN,"$path/$template");
	my $shell;
	while(<IN>){
		$shell.=$_;
	}
	close(IN);
	foreach(keys %{$rep}){
		$shell=~s/<!$_>/$rep->{$_}/g;
	}
	$shell=obfuscate($shell) if($rep->{obfuscate});
	return $shell;
}
