var Timer = 30 * 1000;

$(document).ready(function(){

	$(".toggle_container").hide(); 
	$(".active").show(); 
	$("h2.trigger").toggle(
		function(){ $(this).addClass("active") },
		function(){ $(this).removeClass("active") }
	);
	$("h2.trigger").click(function(){
		$(this).next(".toggle_container").slideToggle("slow");
	});

	$(function(){ Reload(); });
	$(function(){ externalLinks(); });
});

// 外部リンクの場合は別ウインドウを開く
function externalLinks() {
	$("a[rel='external']").each(function(){ this.target = "_blank"; });
}

// thicboxを開く
function thickbox(link) {
	var t = link.title || link.name || null;
	var a = link.href || link.alt;
	var g = link.rel || false;
	var i = $("#id").val();
	var p = $("#pass").val();
	a += '&amp;id=' + i + '&amp;pass=' + p + '&amp;TB_iframe=true';
	tb_show(t, a, g);
	link.blur();
	return false;
}

// 指定時間で再読み込み
function Reload() {
	if ($('table').is("#rooms") == true) {
		$.post(
			"lobby.cgi",
			{ mode:"roomtable", id:$('#id').val(), pass:$('#pass').val() }
		);
	}
	else if ($('div').is("#sanka") == true) {
		$("#sanka").load(
			"home.cgi",
			{ mode:"sanka", id:$('#id').val(), pass:$('#pass').val() }
		);
	}
	var loadTime = 30;
	clearTimeout(Timer);
	Timer = setTimeout('Reload()', loadTime * 1000);
}

function sendform(link, flag, status, usr) {
	document.home.action = link.href || link.alt;	// フォーム送信先CGI
	$("#mode").val(flag);
	$("#status").val(status);
	if (flag == "duel" && ( 1 < $("#limit").val() || status.match('pass') )) {
		var s = $("#limit").val() == 2 ? "入室を許可するユーザーのIDを入力してください" : "入室パスワードを入力してください"
		status = status.replace('pass','');
		$("#status").val(status);
		if (prompt(s)) {
			$("#l_pass").val(prompt(s));
			document.home.submit()
		} else {
			s = $("#limit").val() == 2 ? "IDが入力されていません" : "入室パスワードが入力されていません"
			alert(s);
		}
	} else if (flag == "logout") {
		if ( window.confirm('ログアウトしてもよろしいですか？') ) {
			alert('ご利用ありがとうございました');
			document.home.submit()
		} else {
			return false
		}
	} else if (flag == "reload") {
		$(function(){ Reload(); });
	} else if ( flag == "duel" || flag == "audience" || flag == "deck" || flag == "lobby" || flag == "login" ) {
		document.home.submit();
	} else if ( flag == "message_form" || flag == "add_list_form" || flag == "del_list_form" ) {
		$("#column_2_field").load(
			"home.cgi",
			{ mode:flag, status:$("#status").val(), id:$('#id').val(), pass:$('#pass').val(), user:usr }
		);
	} else if (flag == "write_bbs") {
		var options = {
			target: '#bbs_list', // サーバの戻りを出力する場所を指定 
			type: 'post', // postまたはget 
			timeout: 3000 //タイムアウトを設定
		};
		$('#home').ajaxSubmit(options);
		$("#comment").val("").focus();
	} else if (flag == "del_message") {
		if ( window.confirm('このメッセージを削除してもよろしいですか？') ) {
			alert('メッセージを削除しました');
			var t = document.home.action == 'home.cgi' ? '#message_list' : '#bbs_list'; // サーバの戻りを出力する場所を指定
			var options = { target: t, type: 'post', timeout: 3000 };
			$('#home').ajaxSubmit(options);
		} else {
			return false;
		}
	} else if (flag == "del_mess_form") {
		$("#column_2_field").load(
			"index.cgi",
			{ mode:flag, status:status, pass:$('#pass').val() }
		);		
	} else {
		var options = { target: '#column_2_field', type: 'post', timeout: 3000 }; 
		$('#home').ajaxSubmit(options);
	}
	$("#mode").val();
	return false;
}