/* ----------------------------------------------------------------------------
 * search.js
 * 検索フォーム制御 javascript (クイック検索フォーム含む)
 * 
 * CREATED:			2009.12.11
 * LAST MODIFIED:	2010.01.05
 * 
 * Copyright (C) 2009 ACTOR,co,ltd. Allrights Reserved.
 * ------------------------------------------------------------------------- */
/*
	(*) jQuery を使用
*/

function sf_init()
{
	redraw_condition();
}

function sf_category_checkbox(input_name, category_no, check, display_id)
{
	var class_name = input_name + category_no;
	var c = $A($$("input." + class_name));
	
	c.each(function(v, i)
	{
		v.checked = check;
	});
	
	sf_checkbox(input_name, display_id);
}

function sf_checkbox(input_name, display_id)
{
	var str = sf_string_checkbox(input_name);
	redraw_condition_row(display_id, str, true);
}

function sf_string_checkbox(input_name)
{
	var nodes = $A($$('input[name="' + input_name + '[]"]'));
	var data = [];
	nodes.each(function(node){
		if (node.checked)
		{
			data.push(node.getAttribute('rel'));
		}
	});
	
	if (data.length > 0)
	{
		return data.join("、");
	}
	else
	{
		return SF_DEFAULT_STRING;
	}
}

function redraw_condition_row(display_id, str, check_all_row)
{
	if (str !== "")
	{
		$("tr_" + display_id).show();
	}
	else
	{
		$("tr_" + display_id).hide();
	}
	
	$("td_" + display_id).innerHTML = escapeHTML(str);
	
	if (check_all_row)
	{
		redraw_condition();
	}
}

function redraw_condition()
{
	var all_hide = true;
	
	var nodes = $A($$('.condition_display tr'));
	nodes.each(function(node) {
		if (node.visible())
		{
			all_hide = false;
		}
	});
	
	if (all_hide)
	{
		$('sf_display_table').hide();
		$('sf_display_none').show()
	}
	else
	{
		$('sf_display_table').show();
		$('sf_display_none').hide()
	}
}

function change_per_page(pp)
{
	var node;
	
	node = $$('input[name="' + SF_FORM_NAME_PER_PAGE + '"]');
	node[0].value = pp;
	
	node = $$('input[name="' + SF_FORM_NAME_PAGE + '"]');
	node[0].value = 1;
	
	$('sf_result_hidden__').action = "result.html";
	$('sf_result_hidden__').submit();
}

function change_page(p)
{
	var node = $$('input[name="' + SF_FORM_NAME_PAGE + '"]');
	node[0].value = p;
	$('sf_result_hidden__').action = "result.html";
	$('sf_result_hidden__').submit();
}

function sf_clear()
{
//	alert("クリアボタン作成中");
}

function sf_toggle_form(input_name)
{
	var form = $j('#form_' + input_name);
	var icon = $j('#sf_icon_' + input_name);
	
	var src = APP_URL + "img/search_box/" + (form.css("display") == "none" ? "icon_minus.gif" : "icon_plus.gif");
	
	icon.attr("src", src);
	form.slideToggle("fast");
}

function sf_change_salary(f1, f2)
{
//	alert(sf_salary_string(f1, f2));
}

/* --------------------------------------------------------- */
/* 文字列生成関数 */
/* --------------------------------------------------------- */
function sf_spot_string()
{
	return SF_DEFAULT_STRING;
}

function sf_salary_string(f1, f2)
{
	var s1 = $(f1);
	var l1 = "";
	if (s1.selectedIndex > 0)
	{
		l1 = s1.options[s1.selectedIndex].text;
	}
	
	var s2 = $(f2);
	var l2 = "";
	
	if (s2 && (s2.selectedIndex > 0))
	{
		l2 = s2.options[s2.selectedIndex].text;
	}
	
	if (l1 !== "" && l2 !== "")
	{
		return l1 + " " + l2;
	}
	else
	{
		return SF_DEFAULT_STRING;
	}
}

function sf_kodawari_string()
{
	return SF_DEFAULT_STRING;
}

function sf_week_string(input_name)
{
	return checkbox_string(input_name);
}

function sf_kinmu_string(input_name)
{
	return checkbox_string(input_name);
}

function sf_word_string(input_name)
{
	var ret = "";
	
	var nodes = $A($$('input[name="' + input_name + '"]'));
	ret = nodes[0].value;
	
	if (ret == "" || ret == WordForm.EMPTY_STRING)
	{
		return SF_DEFAULT_STRING;
	}
	else
	{
		return ret;
	}
}

function rsf_init()
{
	redraw_condition();
}


/* --------------------------------------------------------- */
/* 以下、クイック検索用 */
/* --------------------------------------------------------- */
function qsf_init()
{
	sf_init();
	
	// IE8で、非常勤tab選択、更に詳細な条件で検索、ブラウザ戻るボタン、更に詳細な条件で検索、と遷移した時
	// 意図したページ（常勤検索ページ）に遷移しない → とりあえずsetTimeout()で呼び出して回避
	// 他のブラウザでは、問題ない。
//	qsf_tab(1);
	setTimeout(function () { qsf_tab(1); }, 10);
}

// 引数
// tab_no - 1:常勤 2:非常勤 3:スポット
function qsf_tab(tab_no)
{
	for (var i = 1; i <= 3; i++)
	{
		$('qs_tab' + i).className = 'search' + i + (i == tab_no ? 'a' : '');
		
		if (i == tab_no)
		{
			$('condition_form_' + i).show();
		}
		else
		{
			$('condition_form_' + i).hide();
		}
	}
	
	qsf_redraw_condition(tab_no);
	$('__sf_employ_type').value = tab_no;
}

// 「現在の設定条件」の表示項目を再描画する
function qsf_redraw_condition(tab_no)
{
	var nodes = $A($$('.condition_display tr'));
	nodes.each(function(node) {
		var r = node.className;
		
		if (SF_DISP_CONDITION[tab_no].indexOf(r) != -1 && $("td_" + r).innerHTML !== "")
		{
			node.show();
		}
		else
		{
			node.hide();
		}
	});
	
	redraw_condition();
}

function qsf_open_window(name)
{
	var animate = true;
	
	var opt = {
		opacity: 30,
		minHeight: 100,
		overlayClose: true,
		persist: true
	};
	
	if (animate)
	{
		opt["onOpen"] = function (dialog) {
			dialog.overlay.fadeIn('fast', function () {
				dialog.data.show();
				dialog.container.fadeIn('fast');
			});
		}
	}
	
	$j('#window_' + name).modal(opt);
	
	return false;
}

function qsf_close(input_name)
{
	// input_name: ウィンドウ閉じたときのみ、設定を反映させるときに使うつもり
	$j.modal.close();
	return false;
}

function qsf_more()
{
	var post_to = ["", "search_full.html", "search_part.html", "search_spot.html"];
	
	$('sf__').action = post_to[$F('__sf_employ_type')];
	$('sf__').submit();
}


var WordForm = Class.create(
{
	initialize: function(dom, guide, display_id)
	{
		this.dom = dom;
		this.guide = guide;
		this.display_id = display_id;
		
		if (this.dom.value == "")
		{
			this.guide.style.display = "";
		}
		
		Event.observe(this.dom, 'keyup', this.onkeyup.bind(this));
		Event.observe(this.dom, 'change', this.onkeyup.bind(this));
		Event.observe(this.dom, 'focus', this.onfocus.bind(this));
		Event.observe(this.dom, 'blur', this.onblur.bind(this));
		Event.observe(this.guide, 'focus', this.onfocus.bind(this));
	},
	
	onkeyup: function()
	{
		redraw_condition_row(this.display_id, this.value(), true);
	},
	
	onfocus: function()
	{
		this.guide.style.display = "none";
		this.dom.focus();
	},
	
	onblur: function()
	{
		if (this.dom.value == "")
		{
			this.guide.style.display = "";
		}
	},
	
	value: function()
	{
		return (this.dom.value == this.EMPTY_STRING ? "" : this.dom.value);
	}
	
});

