#!/usr/bin/perl
use utf8;
use Encode 'decode';
use warnings;
use CGI;
use File::Basename;
#
#	Libre10
#	Copyright 2013 yukikaze
#
$cgi     = new CGI;
$mode    = "";
$mode    = decode( 'utf-8', $cgi->param('mode') );
$pdfpath = decode( 'utf-8', $cgi->param('pdfpath') );
$pagenum = int( $cgi->param('pagenum') );

#$dpi = int($cgi->param('dpi'));
$height = $cgi->param("height");
$width  = $cgi->param("width");
$bpage  = $pagenum - 1;
$npage  = $pagenum + 1;
$blink = "htmlserver.pl?pdfpath=$pdfpath&mode=$mode&width=$width&height=$height";
$nlink = $blink . "&pagenum=" . $npage;
$blink = $blink . "&pagenum=" . $bpage;
$jpeglink = "jpegserver.pl?pdfpath=\"$pdfpath\"&pagenum=$pagenum&mode=$mode&height=$height&width=$width";

#print "Content-type:text/html\n\n";
print $cgi->header( -charset => 'utf-8' );

#print $cgi->start_html(-title=>"page " . $pagenum . " - " . basename $pdfpath);
print "<!DOCTYPE html>\n";
print "<html lang='ja'>\n<head>\n  <meta charset='utf-8'>";
print "<meta name='viewport' content='width=device-width, initial-scale=1'>\n";

$linkfrm  = qq{<hr><div data-role="header" align='center'><a href='$blink' data-prefetch>前へ</a>};
$linkfrm .= qq{page $pagenum　　　　　\n<a href='$nlink' data-prefetch>次へ</a></div><hr>\n};

if ( $mode eq "mobile" ) {
	print "<link rel=\"stylesheet\" href=\"http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css\" />\n";
	print "<script src=\"http://code.jquery.com/jquery-1.9.1.min.js\"></script>\n";
	print "<script src=\"http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js\"></script>\n";
	$linkfrm  = qq{<div data-role=\"header\" align='center'><a href='$blink'data-icon='arrow-l' data-prefetch>前へ</a>　　　　　};
	$linkfrm .= qq{<h3>page $pagenum</h3>};
	$linkfrm .= qq{　　　　　\n<a href='$nlink' data-icon='arrow-r' data-prefetch>次へ</a></div>\n};
	$swipescript = "<script type='text/javascript'>
	\$('#page').live('pagecreate',function(){
	\$('#page').live('swipeleft',image_back);
	}); 
	function image_back(){\$.mobile.changePage('$blink');} 
	</script>";

	print $swipescript;
}
print "</head>\n";
print "<div id='page' data-role='page'>";

#print $cgi->start_html(-title=>"page " . $pagenum . " - " . basename $pdfpath);
#$linkfrm="<div data-role=\"header\" align='center'><h3><a href='" . $blink . "' data-prefetch>前へ</a>　　　　　";
#$linkfrm .="page " . $pagenum . "</h3>" ;
#$linkfrm .="　　　　　\n<a href='" . $nlink . "' data-prefetch>次へ</a></div>\n";
print $linkfrm;
print "<div data-role=\"content\" id='image'><img src='$jpeglink' width='100%'/>";
print "</div>";
print $linkfrm;
print "<div align=center data-role='footer'>Libre10 git '13Q2<BR>Copyright yukikaze,long.inus</div>";
print "</div>";
print $cgi->end_html;

