
$(document).ready(function(){

	// ** ヘッダー
	if ($("body").attr("class")) {
		var cl = $("body").attr("class");
		var cls = cl.split("_");
		if (cls[0] == "page" || cls[0] == "") {
			cls[0] = "news";
			cls[1] = "";
		}
		$("#gn_"+cls[0]).addClass("current");
		if (cls[1]) {
			$("#gn_"+cls[0]+"_"+cls[1]).addClass("current_page_item");
		} else {
			$("#gn_"+cls[0]+" li:first-child").addClass("current_page_item");
		}
	} else {
			$("#gn_news").addClass("current");
			$("#gn_news_gyoseiDantai").addClass("current_page_item");
	}


	// ** 会員窓
	if ($("#remain").html() == "") {
		$("#remain").hide();
	}

	// ** 写真の枠
	$(".picF").each(function(){
		var w = "";
		var w_p = "";
		if ($(this).css("width")) {
			w = $(this).css("width");
		} else {
			w = $(this).width();
		}
		w_p = parseInt(w) - 5;
		if ($(this).css("float") == "left") {
			$(this).wrap("<div class='picFrame floatL marginR5' style='width:"+w+"'></div>");
			$(this).css("float", "none");
		}
		else if ($(this).css("float") == "right") {
			$(this).wrap("<div class='picFrame floatR marginL5' style='width:"+w+"'></div>");
			$(this).css("float", "none");
		} else {
			$(this).wrap("<div class='picFrame' style='width:"+w+"'></div>");
		}
		if ($(this).attr("alt")) {
			$(this).after("<p style='width:"+ w_p +"px'>"+$(this).attr("alt")+"</p>");
		}
	});
	$(".article img").each(function(){
		var articleImg = $(this).css("float");
		if (articleImg) {
			$(this).addClass(articleImg);
		}
	});
	

	// ** フォントサイズ
	$("p").css("font-size",$.cookie('fsize'));

	// ** 予測テキスト
	$(".inputGuide").each(function(){
		if ($(this).val() == "") {
			$(this).val($(this).attr("title"));
			$(this).css("color", "#888888");
		}
	});
	$(".inputGuide").focus(function(){
		if ($(this).val() == $(this).attr("title")) {
			$(this).val("");
			$(this).css("color", "#000000");
		}
	});
	$(".inputGuide").blur(function(){
		if ($(this).val() == "" || $(this).val() == $(this).attr("title")) {
			$(this).val($(this).attr("title"));
			$(this).css("color", "#888888");
		}
	});

	// リード
	if (!$(".lead").html()) {
		$(".lead").hide();
	}

	// お気に入り
	loadFavorite();

	// タグ追加
	loadAddKeyword();

});

// タグ追加のロード
function loadAddKeyword () {
	$(".tagList li").hover(
		function(){
			var param = "sts_mykeyword="+$(this).find(".sts_keyword_a").html();
			$this = $(this);
			$.ajax({
				type: 'POST',
				url: "/lib.ajax.php",
				data: param,
				success:function(text){
					$this.find(".sts_keyword p").html(text);

					// タグ追加のロード
					$(".addMyKeyword").click(function(){
						var param = "add_tag="+$(this).parents().prev().html();
						$thiss = $(this);
						$.ajax({
							type: 'POST',
							url: "/lib.ajax.php",
							data: param,
							success:function(text){
								$this.find(".sts_keyword p").html(text);
								loadAddKeyword();
							}
						});
					});

					// タグ削除のロード
					$(".rmMyKeyword").click(function(){
						var param = "rm_tag="+$(this).parents().prev().html();
						$thiss = $(this);
						$.ajax({
							type: 'POST',
							url: "/lib.ajax.php",
							data: param,
							success:function(text){
								$this.find(".sts_keyword p").html(text);
								loadAddKeyword();
							}
						});
					});

				}
			});
		},
		function(){
			$(this).find(".sts_keyword p").html('<img src="/images/ajax-loader-sts.gif">');
		}
	);
}
 
 

// お気に入りのロード
function loadFavorite() {
	$(".favorite").click(function(){
		if ($(this).hasClass("cant")) {
			$(this).qtip({
				content:'<div>この機能はプレミアム会員専用です。会員登録を行なってからご利用ください。<br />または<a href="/login">ログインします</a>。 </div>',
				position: {corner:{tooltip:'topMiddle'}},
				show: {ready: true},
				hide: { when: { event: 'mouseout'}, delay: 2000 },
				style: { 
					color:'#689999',
					padding:10,
					background:'#ebfaf8',
					border: {
					width: 0,
					radius: 3,
					color: '#deedeb'
					 }
				}
			});
			// location.href = "/login?alert=permission_denied";
		} else {
			var param = "favorite=ON";
			if (!$(this).hasClass("rm")) {
				param += "&flg=add";
			}
			if (location.href == 'http://www.jutaku-s.com/favorite' || location.href == 'http://www.jutaku-s.com/favorite#scrp') {
				var t = $(this);
				param += "&id_type="+$(this).parent().parent().find(".h_type").val();
				param += "&id="+$(this).parent().parent().find(".h_id").val();
			} else {
				var t = $(".favorite");
				param += "&url="+location.href;
			}
			$.ajax({
				type: 'POST',
				url: "/lib.ajax.php",
				data: param,
				success:function(text){
					if (text == 9) {
						alert("error");
					} else {
						if (t.hasClass("rm")) {
							t.removeClass("rm");
							t.parent().parent().removeClass("scraped");
						} else {
							t.addClass("rm");
							t.parent().parent().addClass("scraped");
						}
					}
				}
			});
		}
	});
}

// ** フォントサイズ
function font(size){
	$("p").css("font-size",size);
	$("a").css("font-size",size);
	$.cookie("fsize",size,{expires:30,path:'/'});
}


// -- フォームチェック
function chkRegist() {
	
	var b = true;
	
	if (checking() == false) {
		b = false;
	}
	//alert(b);
	if (b == false) {
		alert('【入力内容に誤りがあります】\n\n赤字の部分をご確認下さい。\n\n');
		var targetOffset = $('.inputError:first').offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 500);
		$("#subimitForm input").each(function(){
			if ($(this).attr("required") == true) {
				$(this).keypress(function(){checking();});
				$(this).blur(function(){checking();});
				$(this).click(function(){checking();});
			}
		});
	}
	return b;
}


function checking() {
	
	var c = true;
	
	// 必須項目
	$("#subimitForm input").each(function(){
		if ($(this).hasClass("required") == true) {
		
			var tp = $(this).attr("type");
			var val = $(this).val();
			var el = $(this).get(0);
			
			switch (tp) {
				case 'text':
				if (val == "") {
					$(this).next(".err").html("必須項目です");
					$(this).addClass("inputError");
					c = false;
				} else {
					$(this).next(".err").html("");
					$(this).removeClass("inputError");
				}
				break;
				case 'radio':
				if ($('input[name='+$(this).attr("name")+']:checked').val() == undefined) {
					$(this).parent().next(".err").html("必須項目です");
					$(this).addClass("inputError");
					c = false;
				} else {
					$(this).parent().next(".err").html("");
					$(this).removeClass("inputError");
				}
				break;
			}
			if (el.tagName == 'SELECT'){
				if ($(this).val() == '') {
					$(this).next(".err").html("必須項目です");
					$(this).addClass("inputError");
					c = false;
				} else {
					$(this).next(".err").html("");
					$(this).removeClass("inputError");
				}
			}
		}
	});

	// メールアドレス
	$(".email").each(function(){
		if ($(this).val() != '') {
			if (!$(this).val().match(/.+@.+\..+/)) {
				$(this).next(".err").html("メールアドレスを入力して下さい");
				$(this).addClass("inputError");
				c = false;
			} else {
				$(this).next(".err").html("");
				$(this).removeClass("inputError");
			}
		}
	});

	return c;
}


// -- エンターキーsubmitの解除
function submitStop(e){
	if (!e) var e = window.event;
	if(e.keyCode == 13)
		return false;
}


// -- 確認画面から戻る際のactionの変更
function formBack(){
	$("form").attr("action", "?state=back#formSection");
}
function formBack_p(){
	window.back();
	return false;
}


// -- タグの追加
function addTag(name) {
	$.ajax({
		type: 'POST',
		url: "/lib.ajax.php",
		data: "tagType="+name+"&addTag="+$("#"+name).val(),
		success:function(text){
			if (text == 9) {
				alert("検索結果のプルダウンより選択して下さい");
			} else {
				$("#"+name).append(text);
				deleteTag();
				$("#"+name).val("");
			}
		}
	});
}

// -- タグの削除
function deleteTag() {
	$(".deleteTag").click(function(){
		$(this).parent().remove();
	});
}



