//----------------------------------------------------------------------//
//- onload
//----------------------------------------------------------------------//
$(document).ready(function(){
//	get_query();
});


//----------------------------------------------------------------------//
//- キーワードで検索
//----------------------------------------------------------------------//
var searchApp = {
	area_changeflag : false,

	params : {
		"area"			: "",
		"prefecture"	: [],
		"work"			: [],
		"ct"			: []
	},

	_remove_array:function(val,arr){
		var r = [];
		for(var i = 0; i < arr.length; i++){
			if(arr[i] != val){
				r.push(arr[i]);
			}
		}
		return r;
	},

	//クエリ配列作成
	getParam:function(){
		var q = {};
		if(this.params["prefecture"].length > 0){
			q["prefecture"] = this.params["prefecture"];
		}
		else if(this.params["area"]){
			q["area_code"] = this.params["area"];
		}

		q["work"] = this.params["work"];
		q["ct"] = this.params["ct"];

		q["type"] = "area2pref";

		return q;
	},

	//クエリ配列作成（ページ遷移用）
	getParam_gosearch:function(){
		var q = {};
		if(this.params["prefecture"].length > 0){
			q["prf[]"] = this.params["prefecture"];
			q["ar"] = this.params["area"];
		}
		else if(this.params["area"]){
			q["ar"] = this.params["area"];
		}

		q["wk[]"] = this.params["work"];
		q["jt[]"] = this.params["ct"];

		q["type"] = "area2pref";

		return q;
	},

	search:function(){
		$.ajax({
			url			: "/carereq.php",
			data		: this.getParam(),
			type		: 'GET',
			dataType	: 'json',
			timeout		: 50000,
			cache		: false,
			error		: function(){ alert("error") },
			success		: function(json){
				//総件数
				$(".kyuujin-kensuu .count").html(json["cnt"]);

				//県
				var str_prefecture = "";
				if(searchApp.area_changeflag && json["prefecture"].length > 0){
					for(var i = 0; i < json["prefecture"].length; i++){
						str_prefecture +=	'<li class="area'+json["prefecture"][i]["id"]+'">';
						str_prefecture +=		'<a href="javascript:searchApp.replace_prefecture(\''+json["prefecture"][i]["id"]+'\')">';
						str_prefecture +=			'<div>' + json["prefecture"][i]["name"] + '</div>';
//						str_prefecture +=			'<div>【<span>' + json["prefecture"][i]["cnt"] + '】</span></div>';
						str_prefecture +=		'</a>';
						str_prefecture +=	'</li>';
					}
					$(".ken").html('<ul class="ken-base">' + str_prefecture +'</ul><div class="c">.</div>');
				}

				//職種
/*
				if(json["job_category"].length > 0){
					for(var i = 0; i < json["job_category"].length; i++){
						$(".ct"+json["job_category"][i]["name"] + " A").html('【'+json["job_category"][i]["cnt"]+'】');
					}
				}
*/

				searchApp.area_changeflag = false;
			}
		});
	},

	//「求人情報を表示」
	go_kyuujin:function(page){
		if (this.params["prefecture"].length == 0 &&
			this.params["area"].length == 0 &&
			this.params["work"].length == 0 &&
			this.params["ct"].length == 0) {
				window.alert("検索条件を選択してください。");
				return false;
		}
		window.location.href = "/"+page+".php?" + jQuery.param(this.getParam_gosearch());
	},
	//「更に絞り込む」
	go_filter:function(page){
		window.location.href = "/"+page+".php?" + jQuery.param(this.getParam_gosearch());
	},
	// 「マイ検索条件で検索」add 090204 22:40
	go_mysearch:function(page){
		window.location.href = "/"+page+".php?act=mysearch&" + jQuery.param(this.getParam_gosearch());
	},

	replace_area:function(id){
		searchApp.area_changeflag = true;

		$(".prefecture").val(id);

		this.params["area"] 		= id;
		this.params["prefecture"]	= [];

		this.search();
	},

	replace_prefecture:function(id){
		if($(".ken .area"+id).hasClass("ken-on")){
			$(".ken .area"+id).removeClass("ken-on");
			this.params["prefecture"] = this._remove_array(id,this.params["prefecture"]);
		}
		else{
			$(".ken .area"+id).addClass("ken-on");
			this.params["prefecture"].push(id);
		}

//		this.search();
	},

	replace_work:function(id){
		if($(".hatarakikata .work"+id).hasClass("work"+id+"-on")){
			$(".hatarakikata .work"+id).removeClass("work"+id+"-on");
			this.params["work"] = this._remove_array(id,this.params["work"]);
		}
		else{
			$(".hatarakikata .work"+id).addClass("work"+id+"-on");
			this.params["work"].push(id);
		}

//		this.search();
	},

	replace_ct:function(id){
		if($(".syokusyu .ct"+id).hasClass("ct"+id+"-on")){
			$(".syokusyu .ct"+id).removeClass("ct"+id+"-on");
			this.params["ct"] = this._remove_array(id,this.params["ct"]);
		}
		else{
			$(".syokusyu .ct"+id).addClass("ct"+id+"-on");
			this.params["ct"].push(id);
		}

//		this.search();
	}
};
