package plugin::presentation::PresentationHandler;
################################################################################
#
# JavaScriptを使ってプレゼンテーションを行います。
#
################################################################################
use strict;

sub new {
    my $class = shift;
    my $self = {};
    return bless $self,$class;
}

sub do_action {
	my $self = shift;
	my $wiki = shift;
	my $cgi = $wiki->get_CGI;
	my $pagename = $cgi->param("page");
	
	$wiki->set_title("プレゼンテーション: $pagename");
	
	my @lines = split(/\n/, $wiki->get_page($pagename));
	my @pages = ();
	my $title = '';
	my $content = '';
	
	foreach my $line (@lines){
		if(index($line, '!!!')==0){
			if($title ne ''){
				push(@pages, {title=>$title, content=>$wiki->process_wiki($content)});
			}
			$title = $wiki->process_wiki($line);
			$content = '';
		} else {
			$content .= $line . "\n";
		}
	}
	if($title ne ''){
		push(@pages, {title=>$title, content=>$wiki->process_wiki($content)});
	}
	
	print "Content-Type: text/html;charset=EUC-JP\n";
	print "Pragma: no-cache\n";
	print "Cache-Control: no-cache\n\n";
	print <<EOD;
<html>
<head>
	<title>プレゼンテーション： @{[Util::escapeHTML($pagename)]}</title>
</head>
<body>
<style type="text/css">
@{[stylesheet()]}
</style>
<script language="JavaScript">
var presen_page = 0;
var presen_data = new Array($#pages);
EOD
	my $count = 0;
	foreach my $page (@pages){
		print <<EOD;
var data = new Object();
data.title   = '@{[&escapeJavaScript($page->{title})]}';
data.content = '@{[escapeJavaScript($page->{content})]}';
presen_data[$count] = data;
EOD
		$count++;
	}
	
	print <<EOD;
function showNextPage(){
	var div = document.getElementById('presentation');
	if(presen_page == presen_data.length){
		div.innerHTML = "スライドショーは終了しました。";
		presen_page++;
	} else {
		div.innerHTML = presen_data[presen_page].title + 
		                presen_data[presen_page].content;
		presen_page++;
	}
	div.focus(false);
}

function backToFirst(){
	presen_page = 0;
	showNextPage();
}

function showPrevPage(){
	if(presen_page > 1){
		presen_page--;
		presen_page--;
		showNextPage();
	}
}

function exitPresentation(){
	location.href='@{[$wiki->config('script_name').'?page='.Util::escapeHTML($pagename)]}'
}
</script>
<div class="presen_menu">
  <a href="javascript:backToFirst();">最初</a>
  <a href="javascript:showPrevPage();">前へ</a>
  <a href="javascript:showNextPage();">次へ</a>
  <a href="javascript:exitPresentation();">終了</a>
</div>
<div id="presentation" onMouseUp ="javascript:showNextPage();">
クリックするとスライドショーを開始します。
</div>
</body>
</html>
EOD
	
	exit;
}

#===============================================================================
# プレゼンモードのCSSを返却します。
#===============================================================================
sub stylesheet {
	return <<END_OF_CSS;
div#presentation {
	font-size: 300%;
	height: 80%;
}

div.presen_menu {
	text-align: right;
	margin: 0px;
	font-size: 10px;
}
END_OF_CSS
}

#===============================================================================
# JavaScriptの文字列にエスケープします。
#===============================================================================
sub escapeJavaScript {
	my $value = shift;
	$value =~ s/\n/\\n/g;
	$value =~ s/'/\\'/g;
	return $value;
}

#===============================================================================
# メニューを有効にします。
#===============================================================================
sub hook {
	my $self = shift;
	my $wiki = shift;
	my $pagename = $wiki->get_CGI()->param("page");
	
	$wiki->add_menu("プレゼンテーション",$wiki->config('script_name').
	                "?action=PRESEN&amp;page=".&Util::url_encode($pagename));
}

1;
