<?php
	require("__include-common.php");
	require("../account/__logincheck.php");
	require("../calendar/_my_calendar.php");
?>
<?php
	$_SESSION[SESSION_PREFIX.'_SCRIPT_NAME'] = $_SERVER['SCRIPT_NAME'];	// index.php redirect用
	if (isset($_GET["t"])) {
		$target_type = $_GET["t"];
		setcookie("index_jump_targettype", $target_type, time() + LOGIN_COOKIE_EXPIRE, "/");
	} else {
		if (isset($_COOKIE["index_jump_targettype"])) {
			$target_type = $_COOKIE["index_jump_targettype"];
		} else {
			$target_type = "_blank";
			setcookie("index_jump_targettype", $target_type, time() + LOGIN_COOKIE_EXPIRE, "/");
		}
	}
	$onFocus = "document.search_form.".SEARCH_FORM_INPUT_TEXT_NAME.".focus()";
	if (GADGET_WEATHER_USE == 'YES') {
		html_header(HTML_TITLE, '', '', ' onload="weatherSet();'.$onFocus.'"');
		require("__include-weather.php");				// 	天気予報
	} else {
		html_header(HTML_TITLE, '', '', ' onload="'.$onFocus.'"');
	}
	page_header(False);		//ログイン表示なし
	contents_header();
?>
<table id="body_table">
<tr>
<td>
	<?php
		search_body($target_type);
	?>
	<?php
		if ($_SESSION['login_id'] <> '') {
			index_body($target_type);
		}
	?>
</td>
<td class="side_box">
	<?php
		if (GADGET_WEATHER_USE == 'YES') {
			echo '<div class="side_parts">';
			gadget_weather();
			echo '</div>';
		}
		if (BLOG_PARTS_SCRIPT_TOP1 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_TOP1);
			echo '</div>';
		}
		if (BLOG_PARTS_SCRIPT_TOP2 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_TOP2);
			echo '</div>';
		}
		if ($_SESSION['login_id'] <> '') {
			if (CHAT_LOG_VIEW_CNT_MIN > 0) {
				echo '<div class="side_parts">';
				chat_body();
				echo '</div>';
			}
			if (BBS_VIEW_THREAD > 0) {
				echo '<div class="side_parts">';
				bbs_body();
				echo '</div>';
			}
			if (TODO_VIEW_CNT_MIN > 0) {
				echo '<div class="side_parts">';
				todo_body();
				echo '</div>';
			}
			if (SCHEDULE_VIEW_DAY > 0 || CALENDAR_VIEW_MONTH > 0) {
				echo '<div class="side_parts">';
				calendar_body();
				echo '</div>';
			}
		}
		if (BLOG_PARTS_SCRIPT_BOTTOM1 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_BOTTOM1);
			echo '</div>';
		}
		if (BLOG_PARTS_SCRIPT_BOTTOM2 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_BOTTOM2);
			echo '</div>';
		}
	?>
</td>
<td class="side_box">
	<?php
		if (BLOG_PARTS_SCRIPT_RIGHT1 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_RIGHT1);
			echo '</div>';
		}
		if (BLOG_PARTS_SCRIPT_RIGHT2 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_RIGHT2);
			echo '</div>';
		}
		if (BLOG_PARTS_SCRIPT_RIGHT3 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_RIGHT3);
			echo '</div>';
		}
		if (BLOG_PARTS_SCRIPT_RIGHT4 <> '') {
			echo '<div class="side_parts">';
			require(BLOG_PARTS_FOLDER.BLOG_PARTS_SCRIPT_RIGHT4);
			echo '</div>';
		}
	?>
</td>
</tr>
</table>
<?php
	page_footer();
	html_footer();
	exit();
?>
<?php
function search_body($target_type) {
?>
<div id="search_body">
<?php
if (defined("_DEFINE_INDEX_SEARCH_MY") && _DEFINE_INDEX_SEARCH_MY <> '') {
	require(_DEFINE_INDEX_SEARCH_MY);
} else {
	require(_DEFINE_INDEX_SEARCH);
}
?>
</div><!-- id="search_body" -->
<?php
}
?>
<?php
function index_body($target_type) {
	$con = my_mysql_connect(_DB_SCHEMA);
	//カテゴリのみでホームページ登録なしを抽出
	$sql_no_hp = "SELECT m_category.id_category, m_category.c_categoryName, m_category.c_categoryDisplayColumn,";
	$sql_no_hp .= " m_category.c_categoryDisplayOrder, m_category.c_categoryDisplayColor";
	$sql_no_hp .= " FROM m_category";
	$sql_no_hp .= " LEFT JOIN (SELECT id_category FROM m_homepage WHERE c_delete = 0) T1";
	$sql_no_hp .= " ON m_category.id_category = T1.id_category";
	$sql_no_hp .= " WHERE T1.id_category is NULL";
	$sql_no_hp .= " AND m_category.id_account = '".$_SESSION['current_id']."'";
	$sql_no_hp .= " AND m_category.c_delete = 0";
?>
	<div id="index_body">
		<table id="index_main">
		<tr>
	<?php
		$sqlsel = "select * from v_homepage where id_account = '" . $_SESSION['current_id'] . "'";
		for ($ix=1; $ix<=4; $ix++) {
			$sql = $sqlsel . " and c_categoryDisplayColumn = " . $ix;
			if ($_SESSION['current_id'] != $_SESSION['login_id']) {
				$sql .= " and c_privacy = 0";
			}
			$sql .= " order by c_categoryDisplayOrder, c_displayOrder, id_category, c_title";
			$rs = my_mysql_query($sql);
			$sql = $sql_no_hp . " and c_categoryDisplayColumn = " . $ix;
			$rs_no_hp = my_mysql_query($sql);
			list_my_index($rs, $rs_no_hp, $target_type);
		}
	?>
		</tr>
		</table>
	</div><!-- id="index_body" -->
	<div id="index_target_change">
		リンク先を
		[ 
		<?php
		if ($target_type == "_self") {
			echo '<span class="target_select">●</span>';
		} else {
			echo '○';
		}
		?>
		<a href="<?= $_SERVER['SCRIPT_NAME'] ?>?t=_self" target="_self">同じウインドウ</a> ]
		[ 
		<?php
		if ($target_type == "_blank") {
			echo '<span class="target_select">●</span>';
		} else {
			echo '○';
		}
		?>
		<a href="<?= $_SERVER['SCRIPT_NAME'] ?>?t=_blank" target="_self">新しいウインドウ</a> ]
		で開く。
	</div><!-- id="index_target_change" -->
<?php
	$con = mysql_close($con);
}
?>
<?php
function chat_body() {
?>
	<script type="text/javascript">
	$(function(){
		chat_view();
	});
	function chat_view(){
		var d = new Date();	// キャッシュを利用されないため
		$("#chat_log").load("../chat/read-min.php?time="+d.getTime().toString());
		setTimeout('chat_view()', <?= CHAT_READ_CHECK_INTERVAL ?>);
	}
	function popup_chat() {
		w01 = window.open("../chat/index.php","","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=440,height=500");
	}
	</script>
	<table id="index_chat_table" style="border-color: <?= CHAT_VIEW_FRAME_COLOR ?>; background-color: <?= CHAT_VIEW_FRAME_COLOR ?>;">
	<tr><th id="index_chat_title"><a href="javascript:popup_chat()">チャット</a></th></tr>
	<tr><td style="border-color: <?= CHAT_VIEW_FRAME_COLOR ?>;">
		<div id="chat_log"></div>
	</td></tr>
	</table>
<?php
	return;
}
?>
<?php
function todo_body() {
	$con = my_mysql_connect(_DB_SCHEMA_calendar);
	$sql = 'select * from m_todo where id_account = ' . $_SESSION['current_id'];
	$sql .= ' and c_delete = 0 order by c_priority desc, id_todo asc';
	$rs = my_mysql_query($sql);
	$rowcnt = mysql_num_rows($rs);
?>
	<script type="text/javascript">;
	$(function(){
		todo_view()
	});
	function todo_view(){
		var d = new Date();	// キャッシュを利用されないため
		$("#todo_list").load("../todo/read-min.php?cn=<?= TODO_VIEW_CNT_MIN ?>&time="+d.getTime().toString());
	}
	function todo_view_all(){	// ToDo追加子ウインドウからの戻り用。ToDo全件表示。
		var d = new Date();	// キャッシュを利用されないため
		$("#todo_list").load("../todo/read-min.php?cn=<?= TODO_VIEW_CNT_MIN ?>&v=all&time="+d.getTime().toString());
	}
	function todo_display(){
		if ($(".todo_onoff").css("display") == "none") {
			$(".todo_onoff").css("display","");
		} else {
			$(".todo_onoff").css("display","none");
		}
	}
	function popup_todo() {
		w01 = window.open("../todo/index.php","","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=440,height=500");
	}
	</script>
	<table id="index_todo_table" style="border-color: <?= TODO_VIEW_FRAME_COLOR ?>; background-color: <?= TODO_VIEW_FRAME_COLOR ?>;">
	<tr><th id="index_todo_title">
	<a href="javascript:popup_todo()">ToDo</a>
	<input type="button" onclick="javascript:todo_display()" id="todo_onoff_button" value="△▽">
	</th></tr>
	<tr><td id="index_todo_thread" style="border-color: <?= TODO_VIEW_FRAME_COLOR ?>;">
		<div id="todo_list"></div>
	</td></tr>
	</table>
<?php
	$con = mysql_close($con);
	return;
}
?>
<?php
function bbs_body() {
?>
	<table id="index_bbs_table" style="border-color: <?= BBS_VIEW_FRAME_COLOR ?>; background-color: <?= BBS_VIEW_FRAME_COLOR ?>;">
	<tr><th id="index_bbs_title"><a href="../bbs/">掲示板</a></th></tr>
	<tr><td id="index_bbs_thread" style="border-color: <?= BBS_VIEW_FRAME_COLOR ?>;">
	<ul id="index_bbs_thread_item">
<?php
	$con = my_mysql_connect(_DB_SCHEMA_bbs);
	$sql = 'select * from m_bbs where p_id_bbs = 0 and c_delete = 0';
	$sql .= ' and ADDDATE(c_reply_lasttime, INTERVAL ' . BBS_VIEW_INTERVAL_DAY .' DAY) > NOW()';
	$sql .= ' order by c_reply_lasttime desc';
	$_SESSION['bbs_list_sql'] = $sql;
	$rs = my_mysql_query($sql);
	$rowcnt = mysql_num_rows($rs);
	if ($rowcnt > 0) {
		$ix=0;
		while ($rec=mysql_fetch_array($rs) and $ix<BBS_VIEW_THREAD) {
?>
		<li>
		<a class="" href="../bbs/view.php?id=<?= $rec['id_bbs'] ?>&move=<?= $ix ?>&row=<?= $rowcnt ?>&del=off&key=__reset__&pl=10&page=1"><?= my_htmlspecialchars($rec['c_subject']) ?></a><?= $rec['c_reply_cnt'] <> 0 ? '<span class="index_bbs_repcnt">&nbsp;('.$rec['c_reply_cnt'].')</span>' : '' ?>
		</li>
<?php
			$ix++;
		}
?>
		</ul>
<?php
	}
	$con = mysql_close($con);
?>
	</td></tr>
	</table>
<?php
	return;
}
?>
<?php
function calendar_body() {
	$year = date("Y");
	$month = date("n");
	$day = date("j");
	$con = my_mysql_connect(_DB_SCHEMA_calendar);
	if (SCHEDULE_VIEW_DAY > 0) {
?>
	<table id="index_today_table" style="border-color: <?= SCHEDULE_VIEW_FRAME_COLOR ?>; background-color: <?= SCHEDULE_VIEW_FRAME_COLOR ?>;">
		<tr><th id="index_today_title"><a href="../calendar/">カレンダー</a></th></tr>
<?php
		for ($ix=1; $ix<=SCHEDULE_VIEW_DAY; $ix++) {
			$date = str_for_mysql($year.'/'.$month.'/'.$day);
			$week = date_from_mysql("w", $date);
			if ($week == 0) {
				$day_class = "day_sunday";
			} elseif ($week == 6) {
				$day_class = "day_saturday";
			} else {
				$day_class = "day_weekday";
			}
			$holiday = holiday_check($year, $month, $day);
			if ($holiday) {
				$day_class = "day_holiday";
			}
			$day_class = $day_class . " index_today_schedule";
?>
		<tr><td class="<?= $day_class ?>" style="border-color: <?= SCHEDULE_VIEW_FRAME_COLOR ?>;">
		<p><a href='../calendar/input.php?uid=<?= $_SESSION['current_id'] ?>&y=<?= $year ?>&m=<?= $month ?>&d=<?= $day ?>' class='calendar_day'><?= $day ?></a>&nbsp;<?= day_week_view($date) ?>&nbsp;<span class='holiday_name'><?= $holiday ?></span></p>
<?php
			schedule_print($year, $month, $day, $_SESSION['current_id'], "", "", True, True, IMAGES_FOLDER, IMAGES_FOLDER_ORIGINAL);
			$nextday = mktime(0, 0, 0, $month, ++$day, $year);
			$year = date('Y', $nextday);
			$month = date('n', $nextday);
			$day = date('j', $nextday);
?>
		</td></tr>
<?php
		}
?>
	</table>
<?php
	}
	if (CALENDAR_VIEW_MONTH > 0) {
		if (CALENDAR_VIEW_FIRST == -1) {
			if ($month == 1) {
				$year = date("Y") - 1;
				$month = 12;
			} else {
				$year = date("Y");
				$month = date("n") - 1;
			}
		}
		for ($ix=1; $ix<=CALENDAR_VIEW_MONTH; $ix++) {
			calendar_month($year, $month, $_SESSION['current_id'], "", "", "calendar_tbl_mini", "schedule");
			if ($month == 12) {
				$year++;
				$month = 1;
			} else {
				$month++;
			}
		}
	}
?>
<?php
	$con = mysql_close($con);
	return;
}
?>
<?php
function list_my_index($rs, $rs_no_hp, $target_type) {
	$cate = 0;
	$cnt = 0;
?>
	<td>
<?php
	while ($rec = mysql_fetch_array($rs)) {
		if ($rec['id_category'] <> $cate) {
			if ($cnt <> 0) {
?>
		</td></tr>
		</table>
<?php
			}
?>
		<table class="index_category_box" style="background-color: <?= $rec['c_categoryDisplayColor'] ?>">
		<tr><th class="index_category">
			<a href="list.php?cat=<?= $rec['id_category'] ?>" target="_self"><?= my_htmlspecialchars($rec['c_categoryName']) ?></a>
<?php
			$cate = $rec['id_category'];
?>
		</th></tr>
		<tr><td class="index_mylink"><ul>
<?php
		}
		if ($rec['c_url'].'' <> '') {
?>
		<li>
			<a href="<?= $rec['c_url'] ?>" target="<?= $target_type ?>"><?= mb_substr(my_htmlspecialchars($rec['c_title']),0,20) ?></a>
		</li>
<?php
		}
		if ($rec['c_html'].'' <> '') {
?>
		<li class="blog_parts">
			<?= str_replace("＼", chr(0x5c), $rec['c_html']) ?>
		</li>
<?php
		}
		$cnt++;
	}
	if ($cnt <> 0) {
?>
		</ul></td></tr>
		</table>
<?php
	}
?>
<?php
	while ($rec = mysql_fetch_array($rs_no_hp)) {	//カテゴリのみでホームページ登録なし
?>
		<table class="index_category_box" style="background-color: <?= $rec['c_categoryDisplayColor'] ?>">
		<tr><th class="index_category">
			<a href="list.php?cat=<?= $rec['id_category'] ?>" target="_self"><?= my_htmlspecialchars($rec['c_categoryName']) ?></a>
		</th></tr>
		</table>
<?php
	}
?>
	</td>
<?php
}
?>
