<?php
if (isset($_POST['do'])) {
	header('Location: opt.php?csel='.$_POST['col_sel'].'&bg='.$_POST['bg'].'&num='.$_POST['num'].'&fnum='.$_POST['fnum'].'&size='.$_POST['size'].'&speed='.$_POST['speed'].'&flake='.urlencode($_POST['flake']).'&cl='.$_POST['cl'].'&ch='.$_POST['ch'].'&r1='.$_POST['r1'].'&r2='.$_POST['r2'].'&g1='.$_POST['g1'].'&g2='.$_POST['g2'].'&b1='.$_POST['b1'].'&b2='.$_POST['b2']);
}
	define("SESSION_PREFIX", "jsnow");
	require("../../__common__/__define_common.php");
	require("../../__common__/include-common-all.php");
	$col_sel = post_to_cookie('csel', 'col_sel', 'rgb', 'col_sel');
	$background = post_to_cookie('bg', 'bg', '000000', 'bg');
	$flakes = post_to_cookie('num', 'num', 30, 'num');
	$fnum = post_to_cookie('fnum', 'fnum', 5, 'fnum');
	$flakeMaxSize = post_to_cookie('size', 'size', 20, 'size');
	$fallingSpeedMax = post_to_cookie('speed', 'speed', 3, 'speed');
	$flake_str = post_to_cookie('flake', 'flake', '*,&hearts;,♪,★', 'flake');
	$cl = post_to_cookie('cl', 'cl', 0, 'cl', 'i');
	$ch = post_to_cookie('ch', 'ch', 255, 'ch', 'i');
	$r1 = post_to_cookie('r1', 'r1', 0, 'r1', 'i');
	$r2 = post_to_cookie('r2', 'r2', 255, 'r2', 'i');
	$g1 = post_to_cookie('g1', 'g1', 0, 'g1', 'i');
	$g2 = post_to_cookie('g2', 'g2', 255, 'g2', 'i');
	$b1 = post_to_cookie('b1', 'b1', 0, 'b1', 'i');
	$b2 = post_to_cookie('b2', 'b2', 255, 'b2', 'i');
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>jSnow</title>
<?php
	if ($col_sel == 'rgb') {
		if ($r1 > $r2 || $g1 > $g2 || $b1 > $b2) {
			error_exit('薄片の色の最大値は最小値より大きくしてください。');
		}
	} else {
		if ($cl > $ch) {
			error_exit('薄片の色の最大値は最小値より大きくしてください。');
		}
	}
	$flake_ary = explode(',', $flake_str);
	foreach ($flake_ary as $flake) {
		if ($flakeCode <> '') $flakeCode .= ',';
		$flakeCode .= '"'.$flake.'"';
	}
	for ($ix=0; $ix<$fnum; $ix++) {
		if ($flakeColor <> '') $flakeColor .= ',';
		if ($col_sel == 'rgb') {
			$flakeColor .= '"rgb('.mt_rand($r1, $r2).','.mt_rand($g1, $g2).','.mt_rand($b1, $b2).')"';
		} else {
			$flakeColor .= '"rgb('.mt_rand($cl, $ch).','.mt_rand($cl, $ch).','.mt_rand($cl, $ch).')"';
		}
	}
?>
<script type="text/javascript" src="../../scripts/jquery.js"></script>
<script type="text/javascript" src="jsnow.js"></script>
<style type="text/css">
html,body {background:#<?= $background ?>;position:relative;padding:0;margin:0;}
</style>
<script type="text/javascript">
$(function() {
	$().jSnow({
	flakes:<?= $flakes ?>,
	flakeMaxSize:<?= $flakeMaxSize ?>,
	flakeMinSize:10,
	fallingSpeedMax:<?= $fallingSpeedMax ?>,
	fallingSpeedMin:1,
	flakeCode:[<?= $flakeCode ?>],
	flakeColor:[<?= $flakeColor ?>]});
});
</script>
<script>
document.onkeydown = keyPress;
function keyPress( e ) {
	e = e || window.event;
	if (e.keyCode == 32 || e.keyCode == 229 || e.keyCode == 13) {	// SPACE, Return
		window.location.reload();
		return false;
	}
	if (e.keyCode == 27) {				// ESC
		top.location.href = 'input.php?<?= $_SERVER['QUERY_STRING'] ?>';
		return false;
	}
	return true;					// その他 (ファンクションキーなどを有効にするため)
}
document.onmousedown = mouseDown;
function mouseDown( e ) {
	window.location.reload();
}
</script>
</head>
<body>
<div id="body">
</div>
</body>
</html>
