
var i = 1;
var last_str;
var flg = 0;
var def_msg = "左のテキストボックスに入力すると候補が表示されます。";

$(function(){
	$($("#menu a.active").attr("href")).siblings("div").hide();
	$("#menu a").mouseover(function(){
		if ($("#menu a.active").closest("li").attr("id")!=$(this).closest("li").attr("id")) {
			$("#menu a.active").removeClass("active");
			$(this).addClass("active");
			$($(this).attr("href")).siblings("div").hide();
			$($(this).attr("href")).fadeIn("slow");
		}
	}).click(function(){
		return false;
	});
	
	$("#accordion div:not(:first)").hide();
	$("#accordion h3 a").click(function(){
		if ($("#accordion div:visible").index()!=$(this).closest("h3").next("div").index()) {
			$("#accordion div:visible").slideUp("fast");
			$(this).closest("h3").next("div").slideDown("fast");
		}
		return false;
	});
	
	$("#bbs_button").click(function(){
		$.post("/board/load/",{n:i},function(dat){$("#articles").append(dat);});
		i++;
	});
	
	$("#name_list, #play_list").sortable().css("cursor", "move");

	$("input[name=email_check]").keydown(function(){
		// 貼りつけ防止
		if (event.ctrlKey==true && event.keyCode==86) {
			return false;
		}
	}).bind("contextmenu",function(){
		// コンテクストメニュー防止
		return false;
	});
	$("input,textarea").keydown(function(){
		$(this).parent().removeClass("error");
		$(this).next().remove("span");
	});
	$("#check").submit(function(){
		//エラーの初期化
		$("span.error").remove();
		$("dl dd").removeClass("error");
		$(":text,textarea").filter(".validate").each(function(){
			//必須項目のチェック
			$(this).filter(".required").each(function(){
				if($(this).val()==""){
					$(this).parent().append("<span class=\"error\">未入力です。</span>")
				}
			})
			//メールアドレスのチェック
			$(this).filter(".mail").each(function(){
				if($(this).val() && !$(this).val().match(/^[A-Za-z0-9\.\-_]+[\w-]+@[\w\.-]+\.\w{2,}$/)){
					$(this).parent().append("<span class=\"error\">書式が正しくないアドレスです。</span>")
				}
			})
			//メールアドレス確認のチェック
			$(this).filter(".mail_check").each(function(){
				if($(this).val() && $(this).val()!=$("input[name="+$(this).attr("name").replace(/^(.+)_check$/, "$1")+"]").val()){
					$(this).parent().append("<span class=\"error\">アドレスが一致していません。</span>")
				}
			})
		})
		//エラーの際の処理
		if($("span.error").size()>0){
			$("span.error").parent().addClass("error");
			return false;
		}
	})
	start_suggest();
	
})

jQuery.extend(jQuery.easing,{
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});

function pagetop() {
	var position = $("#footer").position();
	$("html,body").animate({scrollTop:0},position.top,"easeInOutCubic");
}

function twitter() {
	new TWTR.Widget({
		version: 2,
		type: 'profile',
		rpp: 5,
		interval: 6000,
		width: 'auto',
		height: 150,
		theme: {
			shell: {
				background: '#fff',
				color: '#333'
			},
			tweets: {
				background: '#fff',
				color: '#333',
				links: '#339'
			}
		},
		features: {
			scrollbar: true,
			loop: false,
			live: true,
			hashtags: true,
			timestamp: true,
			avatars: false,
			behavior: 'all'
		}
	}).render().setUser('UTpkai').start();
}

function listup(target) {
	var str = $("#"+target).val();
	if (str.length>=1&&str!=last_str||target=="music") {
		var param = (target!="music") ? {t:target,s:str} : {t:"music",s:str,c:$("#composer").val()};
		$.post("/member/suggest/",param,function(dat){
			if (dat) $("#entry_suggest").html(dat);
		});
		last_str = str;
	} else if (!str) {
		$("#entry_suggest").html(def_msg);
	}
}

function clearmusic() {
	$("#composer").val("");
	$("#music").val("");
}

function addplayer(val) {
	if (!val) val = $("#player").val();
	data = "\t\t\t\t<li>"+val+"";
	data += "<input type=\"hidden\" name=\"player[]\" value=\""+val+"\"> ";
	data += "<input type=\"button\" onclick=\"javascript:$(this).parent().remove()\" value=\"削除\" class=\"button\"></li>\n";
	$("#name_list").append(data);
}

function addcomposer(val) {
	if (!val) val = "";
	$("#composer").val(val);
	$("#music").focus();
}

function addmusic(composer,music) {
	if (!music) {
		composer = $("#composer").val();
		music = $("#music").val();
	}
	if ($("#composer").val()==composer && $("#music").val()==music) {
		data = "\t\t\t\t<li>作曲： "+composer+"<br>曲名： "+music;
		data += "<input type=\"hidden\" name=\"composer[]\" value=\""+composer+"\">";
		data += "<input type=\"hidden\" name=\"music[]\" value=\""+music+"\">";
		data += "<input type=\"button\" onclick=\"javascript:$(this).parent().remove()\" value=\"削除\" class=\"button\"></li>\n";
		$("#play_list").append(data);
	} else {
		$("#composer").val(composer);
		$("#music").val(music);
	}
	$("#list").append(data);
}

function start_suggest() {
	if (location.pathname=="/member/entry/form/") {
		$("#player, #composer, #music").focus(function(){
			if (!flg) {
				var target = $(this).attr('id');
				last_str = $(this).val();
				suggest = setInterval("listup('"+target+"')",500);
				flg = 1;
			}
		}).blur(function(){
			if (flg) {
				clearInterval(suggest);
				$("#entry_suggest").val(def_msg);
				flg = 0;
			}
		});
		$("#entry_suggest").html(def_msg);
	}
}

