/* ***************************************
 * グローバル変数の設定
 */

// サイトのドメインを抽出してsiteDomainに格納
var siteDomain = location.host.replace(/:[0-9]*/, '');


/* ***************************************
 * 汎用関数１：コンテンツカテゴリーを取得（サイトトップの場合は'home'が返ります
 */

function getCategory(){
	var d, n;
	d = document.URL.replace(/http.*\-job\.com:?[0-9]*\//, '');
	d = d.replace(/;.*/, '');
	
	if(!d | d == 'index.html') return 'home';
	n = d.split('/');
	return ( n[0] == 'develop' ) ? n[1] : n[0];
	
}




/* ***************************************
 * 全文検索
 */

this.setTextSearch = function(){
	var setSearchWord, myUrl = document.URL;
	var st = $("#searchText");
	// 検索ボックスに検索キーワードをセットする
	if( myUrl.match(/joblist\/(.*)/) ){
		setSearchWord = RegExp.$1;
		st.val(decodeURI(setSearchWord));
	}
	
	// 検索ボタンを押下したときに検索を実行
	$("#textSearchExe").click(function(){ exeSearch(); });
	
	// 検索ボックス内でEnterを押下したときに検索を実行
	st.keydown(function(e){
		// エンターキーが押され、なおかつ中身が空でない時、検索が実行される。
		if( e.keyCode == 13 && st.val() != "" ) exeSearch();
	});

	function exeSearch(){
		// 変数定義
		var str, myhost = location.host;
		
		// ローカル環境用の書き分け
		if( myUrl.match(/http:\/\/127\.0\.0\.1:[0-9]*\/([a-z]*)\/.*/) ) myhost = myhost + "/" + RegExp.$1;
		location.href = "http://" + myhost + "/joblist/" + encodeURI(st.val());
		return false;
	}
};


/* ******************************************
 * URLの日本語パラメーターをURLエンコードする
 */

this.encodeURLParamToUTF8 = function(){
	$("#wrap a").each(function(){
		var tarUrl = $(this).attr("href");
		if(tarUrl.indexOf('searchWord') != -1){
			tarUrl = encodeURI(tarUrl);
			$(this).attr("href", tarUrl);
		}
	});
}




/* ************************************
 * 検索ボックスの透かし
 */
this.setWatermark = function(){
	if( $("#searchText").val() == "" ){
		$("#watermark label").fadeIn(80);	
	}

	$("#searchText").click(function(){
		$("#watermark label").fadeOut(80);
	});
	
	$("#searchText").blur(function(){
		if( $("#searchText").val() == "" ){
			$("#watermark label").fadeIn(80);	
		}
	});
};

this.setEnterKey = function(){
	
	$("#searchText").keydown(function(e){
		var myUrl = document.URL;
		//エンターキーが押され、なおかつ中身が空でない時、検索が実行される。
		if( e.keyCode == 13 && $("#searchText").val() != "" ){
			// 変数定義
			var str, myhost;
			myhost = location.host;
			
			// ローカル環境用の書き分け
			if( myUrl.match(/http:\/\/127\.0\.0\.1:[0-9]*\/([a-z]*)\/.*/) ) myhost = myhost + "/" + RegExp.$1;
			location.href = "http://" + myhost + "/joblist/" + encodeURI($("#searchText").val());

		}
	})
}



/* ************************************
 * お問い合わせフォームのチェックボックス後の表示内容
 */

this.contactForm = function(){
	show();
	$(".contact").change(function() { show(); });
	
	function show(){
		var tid = "#" + $("input.contact:checked").val();
		if( $(tid).css("display") == "none" ){
			$(".response").css("display", "none");
			$(tid).fadeIn("100");
		}
	}
}

		
/* ************************************
 * gnavi現在見ているページに対応して画像が変わる
 */
 
this.setGnaviClass = function(){
	var str = getCategory();
	if( str != null ){
		$("li." + str + " a").addClass("gnaviOn");
	}
}

/* ************************************
 * メインパネルを設定する
 */
 
this.setMainPanel = function(){
	var cat, str;
	cat = getCategory();
	if( cat == "support" ){
		str = document.URL.match(/flow/);
		if( str == "flow" ){
			$("#title img").attr("src","/images/flowPanel.png").attr("height","100");
		}else{
			$("#title img").attr("src","/images/supportPanel.png").attr("height","100");
		}
	}else if( cat == "refer" ){
		$("#title").css("display","none");
	}else if( cat.match(/jobitem|joblist|search/) ){ 
		$("#title img").attr("src","/images/jobofferPanel.png").attr("height","200");
	}else if( cat == 'style') {
		$("#title img").attr("src","/images/stylePanel.png").attr("height","100");
	}else if( cat == 'success' ){
				$("#title img").attr("src","/images/successPanel.png").attr("height","100");
	}else if( cat == 'private' ){
				$("#title img").attr("src","/images/privatePanel.png").attr("height","100");
	}else if( cat == 'home' ){
		$("#title").css("display","none");
	}else{
		$("#title").css("display","none");
	};
	
}


/* ************************************
 * フッターのサイト内リンクを設定
 */
 
this.setFooterLink = function(){
	var cat = getCategory();
	if( cat.match(/home|joblist|search|jobdetail/)){
		$("#footerLink .pt1").css('display', 'block');
	}else if( cat.match(/private|support|success|style/)){
		$("#footerLink .pt2").css('display', 'block');
	}
	
	var obj = $("#fl" + cat);
	var str = $("a", obj).text();
	obj.empty();
	obj.text(str);
};

/* ************************************
 * パンくずリストの場所を変更する
 */
 
this.translateTopicPath = function(){
	var dammy =  $("#dammyTopicPath");
	var h = dammy.html();
	dammy.remove();
	
	$("#topicPath").html(h);
};

/* ************************************
 * regist
 */

this.highlightTd = function(){
	$("form input, form select, form textarea").focus(function(){
		$(this).parent("td").css("background-color", "#FFFFD5");
		$(this).parent("td").prev().css({ 	"background-color"	: "#E9E9EF",
											"color"				: "#000000" } );
	});
	
	$("form input, form select, form textarea").blur(function(){
		$(this).parent("td").css("background-color", "#fff");
		$(this).parent("td").prev().css({ 	"background-color"	: "#F5F5F5",
											"font-weight"		: "normal",
											"color"				: "#333333" });
	});
};


function jobResumeInputShow(){
	var al = $("#fJobCount option").length;
	var v = $("#fJobCount").val();
	displayJob(v);
	$("#fJobCount").blur(function(){
		var num = $(this).val();
		displayJob(num)
	});
	
	function displayJob(n){
		// 一旦非表示
		$(".jobResumeInput").css("display", "none");
		
		// 表示
		for( var i = 1; i <= n; i++ ){
			$("#jobResumeInput" + i).css("display", "block");
			tarId = "BCSelector" + i;
			var cl = $("#" + tarId + " option:selected").attr("class");
				if( cl != undefined || cl != null ){
					cl = ".Cat1_" + cl.replace("BCat", "");
					$(cl, "#SCat" + i).css("display", "block");
				}
			}
		return false;
	}
}

this.wishLocationChane = function(){
	var region = new Array("wishHokkaido", "wishTohoku", "wishKanto", "wishChubu", "wishKinki", 
								"wishHokuriku", "wishChugoku", "wishShikoku", "wishKyusyu");
	
	function checkMiddle(){
		var cnt = 0;
		for( var i = 0; i < region.length; i++ ){
			var tid = region[i];
			if($("#" + tid + " th input").is(":checked") == true ) cnt = cnt + 1;
		}
		
		if( cnt == region.length ){
			$("#wishLocationAll").attr("checked", "checked");
		}else{
			$("#wishLocationAll").attr("checked", false);
			$("#wishLocationAll").removeAttr("checked");
			$("#wishLocationAll").removeAttr("CHECKED");
		}
	}
	
	for( var i = 0; i < region.length; i++ ){
		// 中カテゴリークリック
		$("#" + region[i] + " th input").click(function(){
			var chk = $(this).is(":checked");
			var trid = $(this).parents("tr").attr("id");
			
			if( chk == true ){
				$(this).attr("checked", true);
				$("#" + trid + " td input").attr("checked", "checked");
			}else{
				$(this).removeAttr("checked");
				$("#" + trid + " td input").attr("checked", false);
				$("#" + trid + " td input").removeAttr("checked");
				$("#" + trid + " td input").removeAttr("CHECKED");				
			}
			checkMiddle();
		});
		
		// 小カテゴリークリック
		$("#" + region[i] + " td input").click(function(){
			var trid = $(this).parents("tr").attr("id");
			var num = $("#" + trid + " td input").length;
			var chknum = $("#" + trid + " td input:checked").length;
			
			if( num == chknum ){
				$("#" + trid + " th input").attr("checked", "checked");
			}else{
				$("#" + trid + " th input").attr("checked", false);
				$("#" + trid + " th input").removeAttr("ckecked");
				$("#" + trid + " th input").removeAttr("CHECKED");
			}
			
			checkMiddle();
		});
	}
	
	// 全国をクリック
	$("#wishLocationAll").click(function(){
		var chka = $(this).is(":checked");
		if( chka == true ){
			$(this).attr("checked", "checked");
			for( var i = 0; i < region.length; i++ ){
				$("#" + region[i] + " input").attr("checked", "checked");
			}
		}else{
			$(this).removeAttr("checked");
			for( var i = 0; i < region.length; i++ ){
				$("#" + region[i] + " input").attr("checked", false);
				$("#" + region[i] + " input").removeAttr("checked");
				$("#" + region[i] + " input").removeAttr("CHECKED");
				
			}
		}
	});
};

this.hideJoblistTbl = function(){
	$(".joblistTbl").each(function(){
		$("tr:gt(4)", this).css("display", "none");
	});
};


function relationTypeOfIndustry(obj){
	var bcNum = obj.id;
	    bcNum = bcNum.replace("BCSelector","");
	    
	$("#SCat" + bcNum + " select").each(function(){
		$(this).css("display", "none");
		$("option:eq(0)", this).attr("selected", "selected");
	});
	
	var cl = $("#" + obj.id + " option:selected").attr("class");
		cl = ".Cat1_" + cl.replace("BCat", "");
		$(cl, "#SCat" + bcNum).css("display", "block");
};


/* ************************************
 * 追加コンテンツサイドバースクロールフォロー
 */
 
this.setScrollBox = function(){
 	$sidebar = $("#followContent"),
 	$window = $(window),
 	offset = $sidebar.offset();
 	
 	if( offset != null ){
	 	topPadding = 10;
	 	$window.scroll(function(){
	 		if ($window.scrollTop() > offset.top){
	 			$sidebar.stop().animate({
	 				marginTop:$window.scrollTop() - offset.top + topPadding }, 2600, 'quart');
 			} else {
 				$sidebar.stop().animate({
 					marginTop:0
 				});
 			}
 		});
	}
};


$(document).ready(function(){
	encodeURLParamToUTF8();
	setTextSearch();
	setWatermark();
	setEnterKey();
	setFooterLink();
	hideJoblistTbl();
	translateTopicPath();
	contactForm();
	setGnaviClass();
	setMainPanel();
	highlightTd();
	jobResumeInputShow();
	wishLocationChane();
	setScrollBox();
	jQuery.easing.quart = function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	};
});

