#!/usr/bin/perl --

use strict;

require "../../cgi-bin/book/common.pl";

# 現在日時取得
my ($sec, $min, $hour, $day, $mon, $year) = localtime();
$year += 1900;
$mon += 1;

# おはなし会データ取得 (今月)
my @hashlist1 = getTalkEventInfo($year, $mon);
my @hashlist2 = getTalkEventInfo($year + int($mon / 12), ($mon % 12) + 1);

# カレンダー表作成
my $bodyleft = getCalendarTable($year, $mon, @hashlist1);	# 今月

my $bodyright = getCalendarTable($year + int($mon / 12), ($mon % 12) + 1, @hashlist2);	# 来月


# ポップアップ作成
my $popup = "";
$popup .= "<div id=\"popup\" style=\"display: none;\">\n";
if ($ENV{"HTTP_USER_AGENT"} =~ /MSIE/)		# ユーザーエージェント確認 (Internet Explorer)
{
	$popup .= "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"width: 64px;\">\n";
	$popup .= "<tr>";
	$popup .= "<td style=\"width: 15px;\"><img src=\"./balloon_img/balloon-top-left.gif\" /></td>";
	$popup .= "<td style=\"width: 50%;\" background=\"./balloon_img/balloon-top.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
	$popup .= "<td style=\"width: 25px;\" background=\"./balloon_img/balloon-top.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
	$popup .= "<td style=\"width: 50%;\" background=\"./balloon_img/balloon-top.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
	$popup .= "<td style=\"width: 15px;\"><img src=\"./balloon_img/balloon-top-right.gif\" /></td>";
	$popup .= "</tr>\n";
}
else
{
	$popup .= "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"width: auto;\">\n";
	$popup .= "<tr>";
	$popup .= "<td style=\"width: 15px;\"><img src=\"./balloon_img/balloon-top-left.gif\" /></td>";
	$popup .= "<td style=\"width: auto;\" background=\"./balloon_img/balloon-top.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
	$popup .= "<td style=\"width: 25px;\" background=\"./balloon_img/balloon-top.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
	$popup .= "<td style=\"width: auto;\" background=\"./balloon_img/balloon-top.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
	$popup .= "<td style=\"width: 15px;\"><img src=\"./balloon_img/balloon-top-right.gif\" /></td>";
	$popup .= "</tr>\n";
}
$popup .= "<tr>";
$popup .= "<td style=\"background-color: #f5f0dd; border-left: 2px #7ca20d solid;\"><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "<td colspan=\"3\" style=\"background-color: #F5F0DD;\" align=\"left\" nowrap=\"nowrap\"><div id=\"popup_text\" style=\"font-size: 80%\"></div></td>";
$popup .= "<td style=\"background-color: #f5f0dd; border-right: 2px #7ca20d solid;\"><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "</tr>\n";
$popup .= "<tr>";
$popup .= "<td><img src=\"./balloon_img/balloon-bottom-left.gif\" /></td>";
$popup .= "<td background=\"./balloon_img/balloon-bottom.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "<td style=\"background-color: #f5f0dd;\"><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "<td background=\"./balloon_img/balloon-bottom.gif\"><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "<td><img src=\"./balloon_img/balloon-bottom-right.gif\" /></td>";
$popup .= "</tr>\n";
$popup .= "<tr>";
$popup .= "<td><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "<td><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "<td><img src=\"./balloon_img/balloon-point.gif\" /></td>";
$popup .= "<td><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "<td><img src=\"./balloon_img/dummy.gif\" /></td>";
$popup .= "</tr>\n";
$popup .= "</table>\n";
$popup .= "</div>\n";

sub getCalendarTable
{
	my ($year, $mon, @hashlist) = @_;

	# 月末日

	my @days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	# うるう年判定

	if ((($year % 4 == 0) && ($year % 100 != 0)) || ($year % 400 == 0))
	{
		$days[1] = 29;
	}

	# 月初め曜日取得
	my $start = wday($year, $mon, 1);

	# 月末日取得
	my $last = $days[$mon - 1];

	# カレンダー表作成
	my $cal = "";
	my $index = 0;
	$cal .= "\t\t\t\t\t\t<table class=\"carender\" summary=\"カレンダー\">\n";
	$cal .= "\t\t\t\t\t\t<caption>$mon月</caption>\n";
	$cal .= "\t\t\t\t\t\t\t<tr>\n";
	$cal .= "\t\t\t\t\t\t\t\t<th class=\"sun\">日</th>\n";
	$cal .= "\t\t\t\t\t\t\t\t<th>月</th>\n";
	$cal .= "\t\t\t\t\t\t\t\t<th class=\"tue\">火</th>\n";
	$cal .= "\t\t\t\t\t\t\t\t<th>水</th>\n";
	$cal .= "\t\t\t\t\t\t\t\t<th>木</th>\n";
	$cal .= "\t\t\t\t\t\t\t\t<th>金</th>\n";
	$cal .= "\t\t\t\t\t\t\t\t<th class=\"sat\">土</th>\n";
	$cal .= "\t\t\t\t\t\t\t</tr>\n";

	my $val = "";
	for (my $i = 0; $i < 42; $i++)
	{
		# 表示文字列作成
		my $style = "";
		if (($i < $start) || ($i >= $start + $last))
		{
			$val = "・";
		}
		else
		{
			$val = $i - $start + 1;
			my $hash;
			my $imgfname = "";
			my $popup_text = "";
			foreach $hash (@hashlist)
			{
				if ($hash->{'day'} == $val)
				{
					if ($imgfname eq "")
					{
						if ($hash->{'division'} == 1)	# おはなし会 (あかちゃん向け)
						{
							$imgfname = "./img/ca-aka.gif";
						}
						elsif ($hash->{'division'} == 2)	# おはなし会 (小さい子向け)
						{
							$imgfname = "./img/ca-o-t.gif";
						}
						elsif ($hash->{'division'} == 3)	# おはなし会 (大きい子向け)
						{
							$imgfname = "./img/ca-o-o.gif";
						}
						elsif ($hash->{'division'} == 10)	# 子どもえいが会

						{
							$imgfname = "./img/ca-eiga.gif";
						}
						elsif ($hash->{'division'} == 20)	# その他のイベント
						{
							$imgfname = "./img/ca-ibent.gif";
						}
					}
					if ($popup_text ne "")
					{
						$popup_text .= "<br />";
					}
					$popup_text .= "・" . $hash->{'contents'};
				}
			}

			if ($imgfname ne "")
			{
				$style = " style=\"background-image: url($imgfname); background-repeat: no-repeat; background-position: center;\"";
				$val = "<a href=\"./ohanashiibento/ohanashiibento.cgi\" onmouseover=\"show_popup(this, '$popup_text');\" onmouseout=\"hide_popup();\" style=\"display: block; width: 100%; height: 100%; text-decoration: none;\">$val</a>";
			}
		}

		if ($i % 7 == 0)
		{
			$cal .= "\t\t\t\t\t\t\t<tr>\n";
		}

		# 曜日判定

		if ($i % 7 == 0)	# 日曜日

		{
			$cal .= "\t\t\t\t\t\t\t\t<td class=\"sun\"$style>$val</td>\n";
		}
		elsif ($i % 7 == 2)	# 火曜日

		{
			$cal .= "\t\t\t\t\t\t\t\t<td class=\"tue\"$style>$val</td>\n";
		}
		elsif ($i % 7 == 6)	# 土曜日

		{
			$cal .= "\t\t\t\t\t\t\t\t<td class=\"sat\"$style>$val</td>\n";
		}
		else
		{
			$cal .= "\t\t\t\t\t\t\t\t<td$style>$val</td>\n";
		}

		if ($i % 7 == 6)
		{
			$cal .= "\t\t\t\t\t\t\t</tr>\n";
		}
	}

	$cal .= "\t\t\t\t\t\t</table>\n";

	return $cal;
}

sub wday
{
	my ($year, $mon, $day)=@_;
	if ($mon < 3)
	{
		$mon += 12;
		$year--;
	}
	return ($year + int($year / 4) - int($year / 100) + int($year / 400) + int((13 * $mon + 8) / 5) + $day) % 7;
}

# HTML出力 ($cal, $bodyleft, $bodyrightが埋め込まれている)
print<<HTML_EOF;
Content-type:text/html

<?xml version="1.0" encoding="EUC-JP"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=EUC-JP" />
<title>長崎市立図書館 こどもとしょかん</title>
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta name="Description" content="まちと一体化した建物には、新刊や、暮らし、趣味、ビジネス書に地域資料などが満載。県内外の図書館とネットワークを組み、充実のサポートで皆さんの知的欲求に応えます。" /><meta name="Keywords" content="長崎,長崎県,長崎市,長崎市立図書館,図書館" />
<link rel="stylesheet" href="css/import.css" type="text/css" media="all" />
<link rel="stylesheet" href="css/top.css" type="text/css" media="all" />
<!-- <link rel="stylesheet" href="../../css/import.css" type="text/css" media="all" /> -->
<!-- <link rel="stylesheet" href="../../css/sub-base.css" type="text/css" media="all" /> -->
<!-- <link rel="stylesheet" href="../../css/tosyokan-sisetu.css" type="text/css" media="all" /> -->
<link rel="start" href="index.html" title="最初のページ" />
<link rel="contents" href="haitizu/haitizu.html" title="こどもとしょかんちず" />
<link rel="copyright" href="annai/annai.html" title="りようあんない" />
<link rel="contents" href="sagasu/sagasu.html" title="本をさがそう" />
<link rel="contents" href="ohanashiibento/ohanashiibento.html" title="おはなし会・イベント" />
<link rel="contents" href="#" title="かりている本・よやくしている本" />
<link rel="contents" href="#" title="パスワード・メールアドレスのとうろく・へんこう" />
<link rel="contents" href="#" title="ランキング" />
<link rel="contents" href="link/link.html" title="リンク集" />
<script src="../js/external.js" type="text/javascript"></script>
<script language="javascript">
<!--
	function offset_popup(obj)
	{
		var offsettop = 0, offsetleft = 0;
		do
		{
			offsettop += obj.offsetTop  || 0;
			offsetleft += obj.offsetLeft || 0;
			obj = obj.offsetParent;
		} while (obj);
		return [offsetleft, offsettop];
	}

  	// ポップアップメッセージ表示
	function show_popup(obj, msg)
	{
		var ary = offset_popup(obj);
		var ele = document.getElementById('popup');
		var ele_text = document.getElementById('popup_text');
		ele_text.innerHTML = msg;
		ele.style.display = 'block';
		ele.style.position = 'absolute';
		ele.style.left = (ary[0] - ele.clientWidth / 2 + obj.clientWidth / 2) + "px";
		ele.style.top = (ary[1] - ele.clientHeight) + "px";
		return;
	}

	// ポップアップメッセージ非表示
	function hide_popup()
	{
		var ele = document.getElementById('popup');
		ele.style.display = 'none';
		return;
	}
// -->
</script>
</head>
<body onunload="javascript:hide_popup();">
<div id="container">
	<div id="container-wrapper">
	
		<!-- ▼head ここから-->
		<div id="header-kodomo">
			<h1>こどもとしょかん</h1>
			<div id="topmenu">
				<ul>
					<li class="menu-top1"><a href="../index.html" title="長崎市立図書館のページへ">長崎市立図書館のページへ</a></li>
				</ul>
		  </div>
		</div>
		<!-- ▲head ここまで-->

		<!-- ▼menu ここから-->
		<div id="menu-kodomo">
			<div id="menu-kodomo-up">
				<ul>
					<li class="menu1"><a href="haitizu/haitizu.html" title="こどもとしょかんちず">こどもとしょかんちず</a></li>
					<li class="menu2"><a href="annai/annai.html" title="りようあんない">りようあんない</a></li>
					<li class="menu3"><a href="sagasu/sagasu.html" title="本をさがそう">本をさがそう</a></li>
					<li class="menu4"><a href="ohanashiibento/ohanashiibento.cgi" title="おはなし会・イベント">おはなし会・イベント</a></li>
				</ul>
			</div>
			<div id="menu-kodomo-down">
				<ul>
					<li class="menu5"><a href="../../winj/opac/login.do?lang=jh&dispatch=/opac/lend-list.do" title="かりている本・よやくしている本">かりている本・よやくしている本</a></li>
					<li class="menu6"><a href="../../winj/opac/login.do?lang=jh&dispatch=/opac/user-update.do&every=1" title="パスワード・メールアドレスのとうろく・へんこう">パスワード・メールアドレスのとうろく・へんこう</a></li>
					<li class="menu7"><a href="../../winj/opac/best-reader-list.do?lang=jh" title="ランキング">ランキング</a></li>
					<li class="menu8"><a href="link/link.html" title="リンク集">リンク集</a></li>
				</ul>
			</div>
		</div>
		<!-- ▲menu ここまで-->
		
		<!-- ▼カレンダー ここから-->
		<div id="carender">
			<div id="carender-wrapper">
			
				<div id="carender-top"><h3>こどもとしょかんイベントカレンダー</h3></div>
				
				<div id="carender-inner">
				
					<div id="ca-left">
						<div id="ca-left-inner">
										    
                            <div class="cabox">
								<div class="title"><h3>おはなし会</h3></div>
								<div class="cabox-inner">
                                  <ul>
                                    <li class="aka">あかちゃん向け</li>
                                    <li class="tisai">小さい子向け</li>
                                    <li class="oukii">大きい子向け</li>
                                  </ul>
							  	</div>
								<div class="bottom">&nbsp;</div>
							</div>
							
                            <div class="cabox2">
								<ul>
									<li class="eiga">こどもえいが会</li>
									<li class="ibent">その他イベント</li>
								</ul>
							</div>
							
						</div>
					</div>
					
					<div id="ca-right">
						<div id="kongetu-left">
$bodyleft
						</div>
						<div id="raigetu-right">
$bodyright
						</div>
						
						<div class="clr">&nbsp;</div>
					</div>
					
					<div class="clr">&nbsp;</div>
				</div>
				
			</div>
		</div>
		<!-- ▲カレンダー ここまで-->
		
		<div id="footer">
			<div id="footer-inner">
				<address>長崎市立図書館　〒850-0032　長崎市興善町1-1　TEL:095-829-4946　FAX:095-829-4948</address>
				<div id="copy">COPYRIGHT&copy;2008.NAGASAKI CITY.ALL RIGHTS RESERVED.</div>
			</div>
		</div>
		
		
	</div>
</div>
<script type="text/javascript">
<!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
// -->
</script>
<script type="text/javascript">
<!--
var pageTracker = _gat._getTracker("UA-4924941-1");
pageTracker._initData();
pageTracker._trackPageview();
// -->
</script>
$popup
</body>
</html>
HTML_EOF
