$(document).ready(function(){
//	alert("HEL");
	$(".disable").remove();
	Appender();
	init();
});
function Appender()
{
	$('body').append('<div id="searchbar">'+
		'<a href="../sitemap.html">サイトマップ</a><br/>'+
		'<form action="http://lab.aica.ac.jp/search/" method="get">'+
				'<div id="searchBox">'+
					'<input type="text" name="q" id="queryBox"/>'+
					'<input type="submit" value="検索" />'+
				'</div>'+
		'</form>'+
		'</div>'+
		'<div id="all">'+
			'<div id="left">'+
				'<div id="title">'+
					'<a href="../index.html"><img src="../img/blogtitle.png" alt="Blog" /></a>'+
				'</div>'+
				'<div id="links">'+
					'<a id="menublog" href="../blog/index.php" ></a>'+
					'<a id="menutips" href="../tips/index.php" ></a>'+
					'<a id="menudl" href="../download/index.html" ></a>'+
					'<a id="menucon" href="../entrusted/index.html" ></a>'+
					'<a id="menulink" href="../link/index.html" ></a>'+
					'<a id="menumail" href="../mail/index.html" ></a>'+
					'<a id="menuout" href="../outline/index.html" ></a>'+
					'<a id="menuaisa" href="../greeting/index.html" ></a>'+
				'</div>'+
			'</div>'+
			'<div id="content">'+
			'</div>'+
			'<div id="right">'+
				'<div id="calendar">'+
				'</div>'+
				'<div id="category">'+
				'</div>'+
				'<div id="monthKind">'+
				'</div>'+
				'<div id="widget">'+
				'<h4>Blog Parts</h4>'+
				'<embed src="img/yagi.swf"type="application/x-shockwave-flash"width="180" height="180" bgcolor="#ffffff"pluginspage="http://www.adobe.com/go/getflashplayer_jp" />'+
				/*'<embed src="img/twittgadget.swf"type="application/x-shockwave-flash"width="180" height="180" bgcolor="#ffffff"pluginspage="http://www.adobe.com/go/getflashplayer_jp" />'+*/
				'</div>'+
			'</div>'+
		'</div>'+
		'<div id="footer">'+
			'<div id="footerimg">'+
			'</div>'+
			'<address>Copyright &copy; 2010 AICA研究室 All Rights Reserved.</address>'+
			'<br />'+
			'<p>このWEBサイトはAICA研究室所属の学生が自主的に制作運用するものです<br />※秋田経理情報専門学校の正式なコンテンツではありません。</p>'+
		'</div>'
	);
	$('#content').append($('#entries'));
	$('#content').append(
				'<div id="coments">'+
				'</div>'+
				'<div id="comentWriter">'+
					'コメントフォーム<br />'+
					'<div>'+
						'お名前:<br /><input type="text" id="writerName" value=""/><br />'+
					'</div>'+
					'<div>'+
						'本文:<br />'+
						'<textarea id="comentBody" rows="4" cols="40"></textarea>'+
					'</div>'+
					'<div>'+
						'<input type="button" id="writeComent" value="送信" onclick="writeComent()" />'+
					'</div>'+
				'</div>'
	);
}
function init()
{
	var splited = location.pathname.split("/");
	var fileName = splited[splited.length-1].replace(".html","");
	month = fileName.substring(2,4);
	year = fileName.substring(0,2);
//	alert(month + "*" + year);
	init_calendar();

	init_categorys();

	init_Months();
	
	init_Coments();
	
	//setNavi("../");
}

function init_Coments()
{
	var splited = location.pathname.split("/");
	var fileName = splited[splited.length-1].replace(".html","");
	
	$.ajax({
		type: "POST",
		url	: "getcoments.php",
		data: "date="+fileName,
		success: function(msg)
		{
			$("#coments").html(msg);
		}
	});
}

function writeComent()
{
	var isFailed = true;
	if($("#writerName").val() == "")	{
		$("#writerName").css("backgroundColor","#f99");
		isFailed = false;

	}else	{
		$("#writerName").css("backgroundColor","#fff");
	}

	if($("#comentBody").val() == "")	{
		$("#comentBody").css("backgroundColor","#f99");
		isFailed = false;

	}else	{
		$("#comentBody").css("backgroundColor","#fff");
	}

	if( !isFailed ) return;

	var writer = $("#writerName").val();
	var coment = $("#comentBody").val();
	var splited = location.pathname.split("/");
	var fileName = splited[splited.length-1].replace(".html","");
	
	$.ajax({
		type: "POST",
		url	: "writecoment.php",
		data: "date="+fileName+"&writer="+writer+"&coment="+coment,
		success: function(msg)
		{
			$("#writerName").val("");
			$("#comentBody").val("");
			init_Coments();
		}
	});
}

function init_Months()
{
	$.ajax({
		type: "POST",
		url	: "month.php",
		success: function(msg)
		{
			var title = "<h4>Archives</h4>";
			$("#monthKind").html(title+msg);
			$(".MonInsets").hide();
			$(".MonButtons").click(
				function()
				{
//					alert("HEL");
					$("#"+$(this).attr("id")+"Inset").toggle();
				});
//			alert("k");
		}
	});
}

function init_categorys()
{
	$.ajax({
		   type: "POST",
		   url: "category.php",
		   success: function(msg)
		   {
				var title = "<h4>Categories</h4>";
				$("#category").html(title+msg);
				$(".insets").hide();
				$(".buttons").click(
								function()
								{
									$("#"+$(this).attr("id")+"Inset").toggle();
								});
		   	}
				});
}

var month;
var year;
function init_calendar()
{
	var splited = location.pathname.split("/");
	var fileName = splited[splited.length-1];
	$.ajax({
		   type: "POST",
		   url: "calendar.php",
		   data: "getMonth="+(month)+"&getYear="+(year),
		   success: function(msg)
		   {
				var title = "<h4>Calendar</h4>";
				var insert = "";
				insert += "<input type='button' id='preMon' value='前月' />";
				insert += "<input type='button' id='nextMon' value='次月' />";
				$("#calendar").html( title+insert+msg );
	
				$("#preMon").click(
						function()
						{
							month--;
							init_calendar();
						});
	
				$("#nextMon").click(
						function()
						{
							month++;
							init_calendar();
						});
		   }
		 });
}