<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	if ($_SESSION['photo_open_path'] == '') {
		header("Location: index.php");
	}
	if (empty($_SESSION['photo'])) {
		header("Location: index.php");
	}
	if (empty($_SESSION['photo_img_count'])) {
		header("Location: index.php");
	}
	if ($_SESSION['photo_img_count'] < 1) {
		header("Location: index.php");
	}
	$open_path = $_SESSION['photo_open_path'];
	if (defined("photo_LIMITED_IMAGES_FOLDER") and photo_LIMITED_IMAGES_FOLDER <> '') {
		if ($_SESSION['システム管理者'] <> 'YES' or photo_LIMITED_ADMIN_NOLIMIT <> 'YES') {
			$len = strlen(photo_LIMITED_IMAGES_FOLDER);
			if (substr($open_path.'/', 0, $len+1) <> photo_LIMITED_IMAGES_FOLDER.'/') {
				header("Location: index.php");
			}
		}
	}
	if ($_GET['page'].'' <> '') {
		$page = intval($_GET['page']);
		if ($page < 1) {
			$page = 1;
		}
	} else {
		$page = 1;
	}
	$filename = $_SESSION['photo'][$page-1]['file'];
	$filepath = $open_path.'/'.$filename;
	if ($_SESSION['photo'][$page-1]['type'] == 'video' and photo_VIDEO_FFMPEG == 'YES' and $_SESSION['slide_flv_preview'] == 'on' and $slidemode <> 'ajax') {
		// 動画の再生時間を取得 (ffmpegのstderrを利用)
		$ret = shell_exec('ffmpeg -i "'.myfile_ENCODE($filepath).'" 2>&1');
		$time = trim(get_intag($ret, 'duration:', ',', $pos=0));
		$time_ar = explode(":", $time);
		$interval_time = (int)$time_ar[0]*3600+(int)$time_ar[1]*60+(int)$time_ar[2] + 2;
	} else {
		$interval_time = $_COOKIE['slide_interval_time'];
	}
	$add_meta = '';
	if ($_SESSION['slide_fullview_auto'] == 'on' and $slidemode <> 'ajax') {
		if ($page < ceil($_SESSION['photo_img_count'])) {
			$add_meta = '<meta http-equiv="refresh" content="'.$interval_time.';';
			$add_meta .= 'url='.$_SERVER['SCRIPT_NAME'].'?page='.strval($page+1);
			$add_meta .= '&mode=s&slidemode='.$slidemode.'">';
		} elseif ($_SESSION['slide_view_repeat'] == 'on') {
			$add_meta = '<meta http-equiv="refresh" content="'.$interval_time.';';
			$add_meta .= 'url='.$_SERVER['SCRIPT_NAME'].'?page=1';
			$add_meta .= '&mode=s&slidemode='.$slidemode.'">';
		}
	}
	if (VIDEO_PREVIEW == 'YES' && $_SESSION['slide_flv_preview'] == 'on' && $slidemode <> 'ajax' && is_preview_video_filename($filename) && is_ext_filename_str($filename, VIDEO_PREVIEW_CONVERT)) {
		$filepath = $open_path.'/'.$filename.'.flv';	// JWPlayerは拡張子が.flvでないと駄目なため
	} else {
		$filepath = $open_path.'/'.$filename;
	}
	$filepath_url = urlencode($filepath);
	if ($_SESSION['photo'][$page-1]['type'] == 'video') {
		$getimg = "video-jpeg.php?file=".$filepath_url."&ss=".$_SESSION['video_thumb_ss'];
	} else {
		$getimg = "img-view.php?img=".$filepath_url;
	}
	if ($page > 1) {
		$move_prev = $page - 1;
	} else {
		if ($_SESSION['slide_view_repeat'] == 'on') { 
			$move_prev = $_SESSION['photo_img_count'];
		} else {
			$move_prev = $page;
		}
	}
	if ($page < $_SESSION['photo_img_count']) {
		$move_next = $page + 1;
	} else {
		if ($_SESSION['slide_view_repeat'] == 'on') { 
			$move_next = 1;
		} else {
			$move_next = $page;
		}
	}
	if ($page + SLIDE_SMALL_NUM < $_SESSION['photo_img_count']) {
		$skip_next = $page + SLIDE_SMALL_NUM;
	} else {
		$skip_next = $_SESSION['photo_img_count'];
	}
	if ($page - SLIDE_SMALL_NUM > 1) {
		$skip_back = $page - SLIDE_SMALL_NUM;
	} else {
		$skip_back = 1;
	}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="description" content="Powered by http://maroslaw.github.io/rainyday.js/">
<title>雨降り</title>
<style media="screen" type="text/css">
img { display: none; }
body { overflow: hidden; }
#canvas { position: absolute; top: 0px; left: 0px; }
#sound { position: absolute; width: 30%; height: 30%; overflow-y: auto; margin-left: 35%; -moz-border-radius: 15px; border-radius: 15px; background-color: #C4C4C4; opacity: 0.3; margin-top: 5%; }
.video { position: absolute; width: 90%; height: 80%; margin-left: 5%; margin-top: 5%; }
</style>
<script src="rainyday/rainyday.js"></script>
<script src="../scripts/jquery.js"></script>
<script src="../scripts/jquery.cookie.js"></script>
<script src="../scripts/ok2nd.js"></script>
<script>
function rain() {
	var preset = 1;
	var blur = 21;
	if (getURLParameter('preset')) {
		preset = getURLParameter('preset');
	} else if ($.cookie('rainyday_preset')) {
		preset = Number($.cookie('rainyday_preset'));
	}
	if (preset < 1 || preset > 9) {
		preset = 1;
	}
	if (getURLParameter('blur')) {
		blur = getURLParameter('blur');
	} else if ($.cookie('rainyday_blur')) {
		blur = Number($.cookie('rainyday_blur'));
	}
	if (blur < 1) {
		blur = 1;
	}
	if (blur > 31) {
		blur = 31;
	}
	$.cookie('rainyday_preset',preset,{path:'<?= MY_SESSION_PATH ?>', expires:365});
	$.cookie('rainyday_blur',blur,{path:'<?= MY_SESSION_PATH ?>', expires:365});
	var img = new Image();
	img.src = $('#background').attr('src');
	var canvasWidth = window.innerWidth;
	var canvasHeight = parseInt(canvasWidth * img.height / img.width);
	var engine = new RainyDay('canvas','background', canvasWidth, canvasHeight, 1, blur);
	if (preset == 1) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_DROPS;
		engine.rain([ engine.preset(3, 3, 0.88), engine.preset(5, 5, 0.9), engine.preset(6, 2, 1) ], 100);
	} else if (preset == 2) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_DROPS;
		engine.VARIABLE_GRAVITY_ANGLE = Math.PI / 8;
		engine.rain([ engine.preset(0, 2, 0.5), engine.preset(4, 4, 1) ], 50);
	} else if (preset == 3) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_SMUDGE;
		engine.rain([ engine.preset(0, 2, 0.5), engine.preset(4, 4, 1) ], 50);
	} else if (preset == 4) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_DROPS;
		engine.rain([ engine.preset(4, 4, 0.88), engine.preset(8, 8, 0.9), engine.preset(9, 4, 1) ], 100);
	} else if (preset == 5) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_DROPS;
		engine.VARIABLE_GRAVITY_ANGLE = Math.PI / 8;
		engine.rain([ engine.preset(0, 2, 0.5), engine.preset(8, 8, 1) ], 50);
	} else if (preset == 6) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_SMUDGE;
		engine.rain([ engine.preset(0, 2, 0.5), engine.preset(8, 8, 1) ], 50);
	} else if (preset == 7) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_DROPS;
		engine.rain([ engine.preset(6, 6, 0.88), engine.preset(12, 12, 0.9), engine.preset(9, 4, 1) ], 100);
	} else if (preset == 8) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_DROPS;
		engine.VARIABLE_GRAVITY_ANGLE = Math.PI / 8;
		engine.rain([ engine.preset(0, 2, 0.5), engine.preset(12, 12, 1) ], 50);
	} else if (preset == 9) {
		engine.gravity = engine.GRAVITY_NON_LINEAR;
		engine.trail = engine.TRAIL_SMUDGE;
		engine.rain([ engine.preset(0, 2, 0.5), engine.preset(12, 12, 1) ], 50);
	}
};
/*	→ ok2nd.js
function getURLParameter(name) {
	return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}
*/
</script>
<script>
document.onkeydown = keyPress;
function keyPress(e) {
	e = e || window.event;;
	if (e.keyCode == 27) {	// Esc
		window.close();
	} else if (e.keyCode >= 49 && e.keyCode <= 57) {	// 1～9
		location.href = '?page='+getURLParameter("page")+'&preset='+(Number(e.keyCode)-48);
	} else if (e.keyCode >= 97 && e.keyCode <= 105) {	// (Num Lock 10キー)1～9
		location.href = '?page='+getURLParameter("page")+'&preset='+(Number(e.keyCode)-96);
	} else if (e.keyCode == 38) {	// ↑:38
		location.href = '?page='+getURLParameter("page")+'&blur='+blur_cookie(-5);
	} else if (e.keyCode == 40) {	// ↓:40
		location.href = '?page='+getURLParameter("page")+'&blur='+blur_cookie(5);
	} else if (e.keyCode == 37 || e.keyCode == 100 || e.keyCode == 33 || e.keyCode == 29) {	// ←, Page Up, 無変換 ：Page Prev
		window.location.href = "?page=<?= $move_prev ?>";
	} else if (e.keyCode == 39 || e.keyCode == 102 || e.keyCode == 34) {	// →, Page Down ：Page Next
		window.location.href = "?page=<?= $move_next ?>";
	} else if (e.keyCode == 13 || e.keyCode == 32 || e.keyCode == 229) {	// Enter, Space (229:日本語モード) ：Page Next
		window.location.href = "?page=<?= $move_next ?>";
	} else if (e.keyCode == 9 ) {					// Tab ：Page Skip(10)
		window.location.href = "?page=<?= $skip_next ?>";
	} else if (e.keyCode == 240 ) {					// Caps Lock ：Page Skip(10) Back
		window.location.href = "?page=<?= $skip_back ?>";
	} else if (e.keyCode == 36) {					// Home ：Page Top
		window.location.href = "?page=1";
	} else if (e.keyCode == 35) {					// End ：Page Last
		window.location.href = "?page=<?= $_SESSION['photo_img_count'] ?>";
	} else if (e.keyCode == 122) {					// F11 ：全画面
		window.location.href = "?page=<?= $page ?>";		// リロード
		return true;
	} else {	// その他 (ファンクションキーなどを有効にするため)
		return true;
	}
	return false;
}
function blur_cookie(upDown) {
	if ($.cookie('rainyday_blur')) {
		var blur = Number($.cookie('rainyday_blur'));
	} else {
		var blur = 21;
	}
	if ((blur += upDown) < 1) {
		blur = 1;
	}
	if (blur > 31) {
		blur = 31;
	}
	return blur;
}
</script>
</head>
<body onload="rain();">
<div id="sound" style="z-index: -1;"></div>
<img id="background" src="<?= $getimg ?>">
<div id="cholder">
<canvas id="canvas"></canvas>
</div>
</body>
</body>
</html>
