<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	if ($_GET["path"] <> '') {
		$open_path = form_str_filepath($_GET["path"]);
	}
	$open_path = str_replace("\\", "/", right_slash_strip($open_path));
	if ($open_path == "") {
		if ($_SESSION['photo_open_path'] <> "") {
			$open_path = $_SESSION['photo_open_path'];
		} else {
			$open_path = $_SESSION['login_friends_album_folder_'.$_SESSION['current_id']];
			if ($open_path == "") {
				$open_path = DEFAULT_IMAGES_FOLDER;
			}
		}
	}
	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) <> photo_LIMITED_IMAGES_FOLDER) {
				$open_path = photo_LIMITED_IMAGES_FOLDER;
			}
		}
	}
	if ($_GET['page'].'' <> '') {
		$page = intval($_GET['page']);
		if ($page < 1) {
			$page = 1;
		}
	} else {
		$page = 1;
	}
	$slidemode = $_GET['slidemode'].'';
	if ($_GET['auto'].'' <> '') {
		if ($_GET['auto'] == 'chg') {
			if ($_SESSION['slide_view_auto'] == 'on') {
				$_SESSION['slide_view_auto'] = 'off';
			} else {
				$_SESSION['slide_view_auto'] = 'on';
			}
		} else {
			$_SESSION['slide_view_auto'] = $_GET['auto'];
		}
	}
	if ($_GET['rep'].'' <> '') {
		if ($_GET['rep'] == 'chg') {
			if ($_SESSION['slide_view_repeat'] == 'on') {
				$_SESSION['slide_view_repeat'] = 'off';
			} else {
				$_SESSION['slide_view_repeat'] = 'on';
			}
		} else {
			$_SESSION['slide_view_repeat'] = $_GET['rep'];
		}
	}
	if ($_SESSION['photo_open_path'] <> $open_path) {
		$_SESSION['photo_open_path'] = $open_path;
		$_SESSION['photo_subdir_count'] = 0;
		$_SESSION['photo_img_count'] = 0;
		unset($_SESSION['photo_subdir']);
		unset($_SESSION['photo_img']);
		unset($_SESSION['photo_img_type']);
		get_target_path_info($open_path);
	}
	$add_meta = '';
	if ($_SESSION['slide_view_auto'] == 'on') {
		if ($page < ceil($_SESSION['photo_img_count'])) {
			$add_meta = '<meta http-equiv="refresh" content="'.$_SESSION['slide_interval_time'].';';
			$add_meta .= 'url='.$_SERVER['SCRIPT_NAME'].'?page='.strval($page+1);
			$add_meta .= '&path='.urlencode($open_path);
			$add_meta .= '&mode=s&slidemode='.$slidemode.'">';
		} elseif ($_SESSION['slide_view_repeat'] == 'on') {
			$add_meta = '<meta http-equiv="refresh" content="'.$_SESSION['slide_interval_time'].';';
			$add_meta .= 'url='.$_SERVER['SCRIPT_NAME'].'?page=1';
			$add_meta .= '&path='.urlencode($open_path);
			$add_meta .= '&mode=s&slidemode='.$slidemode.'">';
		}
	}
	$filename = $_SESSION['photo_img'][$page];
	$filepath = $open_path . "/" . $filename;
	$filepath_url = urlencode($filepath);
	if ($_SESSION['photo_img_type'][$page] == 'video') {
		$getimg = "video-jpeg.php?file=".$filepath_url."&ss=".$_SESSION['video_thumb_ss'];
		$click = "video-view.php?file=".$filepath_url;
		$height_size = $_SESSION['slide_main_size'] - 2;
		$img_style = 'width: 100%;';
	} else {
		list($width, $height) = getimagesize(mb_convert_encoding($filepath, 'SJIS-win', 'UTF-8'));
		$getimg = "img-view.php?img=".$filepath_url;
		if ($width > $height) {
			$img_style = 'width: 100%;';
		} else {
			$img_style = 'height: 100%;';
		}
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
<?php if ($add_meta <> '') echo $add_meta."\n"; ?>
<meta http-equiv="Content-Language" content="ja">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta name="author" content="ok.2nd">
<title><?= $filename ?></title>
<style type="text/css">
* { margin: 0px; padding: 0px; }
html,body {
	width: 100%;
	height: 100%;
	overflow: hidden;
}
img {
	-ms-interpolation-mode: bicubic;
	<?= $img_style ?>
}
div#slide_view {
	text-align: center;
	width: 100%;
	height: 100%;
<?php 	if ($slidemode <> 'wallpaper') { ?>
	background-color: #000000; */
<?php	} ?>
}
div#move_control {
	position: absolute;
	top: 0px; left: 0px;
	width: 100%; height: 100%;
	z-index: 1;
}
#move_control table {
	width:100%; height: 100%;
}
td#left {
	height: 90%;
	text-align: left;
}
td#right {
	height: 90%;
	text-align: right;
}
td#bottom {
	height: 10%;
	text-align: center;
	vertical-align: bottom;
}
.move {
	color: #ffffff;
	text-decoration: none;
	font-size: 200%;
}
.return {
	color: #ffffff;
	text-decoration: none;
	font-size: 200%;
	margin: 0 5px;
}
.auto {
	color: #ffffff;
	text-decoration: none;
	font-size: 100%;
	margin: 0 5px;
}
.page {
	color: #ffffff;
	font-size: 100%;
	margin: 0 5px;
}
</style>
</head>
<?php 	if ($slidemode == 'wallpaper') { ?>
<body style="background-image: url('<?= $getimg ?>');">
<?php	} else {?>
<body onload="pixasticImageOnLoad()">
<script type="text/javascript" src="../scripts/cookie.js"></script>
<script type="text/javascript" src="../scripts/pixastic/pixastic.core.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/blur.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/brightness.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/coloradjust.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/desaturate.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/glow.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/hsl.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/lighten.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/posterize.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/sepia.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/sharpen.js"></script>
<script type="text/javascript" src="../scripts/pixastic/actions/solarize.js"></script>
<script type="text/javascript">
function pixasticImageOnLoad() {
	photo_pixastic = loadCookie("photo_pixastic_stack");
	if (photo_pixastic && photo_pixastic != "false") {
		pixastic = photo_pixastic.split(",");
		for(var ix in pixastic){
			pixasticAction(pixastic[ix]);
		}
	}
}
function pixasticAction(action) {
	if (action == "contrast") {
		Pixastic.process(document.getElementById("img_slide_main"), "brightness", {brightness:0,contrast:0.3});
	} else if (action == "red") {
		Pixastic.process(document.getElementById("img_slide_main"), "coloradjust", {red:0.1,green:0,blue:0});
	} else if (action == "green") {
		Pixastic.process(document.getElementById("img_slide_main"), "coloradjust", {red:0,green:0.1,blue:0});
	} else if (action == "blue") {
		Pixastic.process(document.getElementById("img_slide_main"), "coloradjust", {red:0,green:0,blue:0.1});
	} else if (action == "glow") {
		Pixastic.process(document.getElementById("img_slide_main"), "glow", {amount:0.1,radius:1.0});
	} else if (action == "saturation") {
		Pixastic.process(document.getElementById("img_slide_main"), "hsl", {hue:0,saturation:20,lightness:0});
	} else if (action == "lighten") {
		Pixastic.process(document.getElementById("img_slide_main"), "lighten", {amount:0.1});
	} else if (action == "posterize") {
		posterize = loadCookie("photo_pixastic_posterize");
		Pixastic.process(document.getElementById("img_slide_main"), "posterize", {levels:posterize});
	} else if (action == "sharpen") {
		Pixastic.process(document.getElementById("img_slide_main"), "sharpen", {amount:0.2});
	} else {	// desaturate, sepia, solarize, blur
		Pixastic.process(document.getElementById("img_slide_main"), action);
	}
}
</script>
<?php	} ?>
<div id="slide_view">
<?php 	if ($slidemode <> 'wallpaper') { ?>
	<?php if ($_SESSION['photo_img_count'] <> 0) { ?>
	<img id="img_slide_main" src="<?= $getimg ?>" alt="<?= $filename ?>" title="<?= $filename ?>" />
	<?php } ?>
<?php	} ?>
<div>
<div id="move_control">
<table>
<tr>
<td id="left">
<?php
	if ($page > 1) {
		$move_prev = $page - 1;
	} else {
		if ($_SESSION['slide_view_repeat'] == 'on') { 
			$move_prev = $_SESSION['photo_img_count'];
		} else {
			$move_prev = $page;
		}
	}
?>
	<a class="move" href="?path=<?= urlencode($open_path) ?>&page=<?= $move_prev ?>&slidemode=<?= $slidemode ?>">&lt;</a>
</td>
<td id="right">
<?php
	if ($page < $_SESSION['photo_img_count']) {
		$move_next = $page + 1;
	} else {
		if ($_SESSION['slide_view_repeat'] == 'on') { 
			$move_next = 1;
		} else {
			$move_next = $page;
		}
	}
?>
	<a class="move" href="?path=<?= urlencode($open_path) ?>&page=<?= $move_next ?>&slidemode=<?= $slidemode ?>">&gt;</a>
</td>
</tr>
<tr>
<td id="bottom" colspan=2>
	<a class="return" href="index.php?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&mode=s&auto=off">×</a>
	<?php if ($_SESSION['slide_view_auto'] == 'on') { ?>
	<a class="auto" href="?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&auto=off&slidemode=<?= $slidemode ?>">Auto <span style="color: #ffff40">On</span></a>
	<?php } else { ?>
	<a class="auto" href="?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&auto=on&slidemode=<?= $slidemode ?>">Auto Off</a>
	<?php } ?>
	<?php if ($_SESSION['slide_view_repeat'] == 'on') { ?>
	<a class="auto" href="?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&rep=off&slidemode=<?= $slidemode ?>">Repeat <span style="color: #ffff40">On</span></a>
	<?php } else { ?>
	<a class="auto" href="?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&rep=on&slidemode=<?= $slidemode ?>">Repeat Off</a>
	<?php } ?>
	<?php if ($slidemode == 'wallpaper') { ?>
	<a class="auto" href="?path=<?= urlencode($open_path) ?>&page=<?= $page ?>">Wallpaper <span style="color: #ffff40">On</span></a>
	<?php } else { ?>
	<a class="auto" href="?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&slidemode=wallpaper">Wallpaper Off</a>
	<?php } ?>
	<span class="page"><?= $page ?> / <?= $_SESSION['photo_img_count'] ?></span>
</td>
</tr>
</table>
</div>
<?php
	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;
	}
?>
<script language="JavaScript">
document.onkeydown = keyPress;
function keyPress( e ) {
	// IEでは e は未定義
	// 第一引数の e が未定義なら、window.event を使う。
	// by http://d.hatena.ne.jp/language_and_engineering/20081124/1227474876
	e = e || window.event;
	//alert( "キーコード：" + e.keyCode );
	if (e.keyCode == 27) {						// ESC ：Exit, Return to index.php
		location.href = "index.php?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&mode=s&auto=off";
	} else if (e.keyCode == 37 || e.keyCode == 100 || e.keyCode == 33) {	// ←, Page Up ：Page Prev
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $move_prev ?>&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 39 || e.keyCode == 102 || e.keyCode == 34) {	// →, Page Dow ：Page Next
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $move_next ?>&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 13 || e.keyCode == 32) {		// Enter, Space ：Page Next
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $move_next ?>&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 9 ) {					// Tab ：Page Skip(10)
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $skip_next ?>&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 243 ) {					// 半角/全角 ：Page Skip(10) Back
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $skip_back ?>&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 36) {					// Home ：Page Top
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=1&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 35) {					// End ：Page Last
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $_SESSION['photo_img_count'] ?>&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 65) {					// A ：Auto On/Off
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&auto=chg&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 82) {					// R ：Repeat On/Off
		window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&rep=chg&slidemode=<?= $slidemode ?>";
	} else if (e.keyCode == 87) {					// W ：Wallpaper On/Off
		<?php if ($slidemode == 'wallpaper') { ?>
			window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $page ?>";
		<?php } else { ?>
			window.location.href = "?path=<?= urlencode($open_path) ?>&page=<?= $page ?>&slidemode=wallpaper";
		<?php } ?>
	} else {					// その他 (ファンクションキーなどを有効にするため)
		return true;
	}
	return false;
}
</script>
</body>
</html>
<?php
function get_target_path_info($open_path) {
	$path_SJIS = mb_convert_encoding($open_path, 'SJIS-win', 'UTF-8');
	if ($dir = opendir($path_SJIS)) {
		$subdir_count = 0;
		$img_count = 0;
		$files = scandir($path_SJIS);
		foreach ($files as $file) {
			if ($file != "." && $file != "..") {
				if (is_dir($path_SJIS . "/" . $file)) {
					$_SESSION['photo_subdir'][++$subdir_count]
						= mb_convert_encoding($file, 'UTF-8', 'SJIS-win');
				} elseif (is_img_filename($file)) {
					$_SESSION['photo_img'][++$img_count]
						= mb_convert_encoding($file, 'UTF-8', 'SJIS-win');
				} else {
					if (photo_VIDEO_FFMPEG == "YES") {
						if (is_video_filename($file)) {
							$_SESSION['photo_img'][++$img_count]
							= mb_convert_encoding($file, 'UTF-8', 'SJIS-win');
							$_SESSION['photo_img_type'][$img_count] = 'video';
						}
					}
				}
			}
		}
		closedir($dir);
	}
	$_SESSION['photo_subdir_count'] = $subdir_count;
	$_SESSION['photo_img_count'] = $img_count;
}
?>
