var Search = Class.create();
Search.prototype = {
  //
  // コンストラクタ
  // submitElm: "",
  // 
  isSetLimit: false,

  initialize: function(search_submit_id, search_form_id) {
	this.submitElm = $(search_submit_id);
	this.formElm = $(search_form_id);

	Event.observe(this.submitElm , 'click', this.execute.bindAsEventListener(this));
  },

  init: function() {
	this.isSetLimit = false;
	$A($$(".CategorySearchSets")).each(function(element) { if (element) { element.remove(); } });
	if ($("set1_type")) { $("set1_type").remove(); }
	if ($("set2_type")) { $("set2_type").remove(); }
	if ($("set3_type")) { $("set3_type").remove(); }
	if ($("set4_type")) { $("set4_type").remove(); }
	$A($$(".set1")).each(function(element) { if (element) { element.remove(); } });
	$A($$(".set2")).each(function(element) { if (element) { element.remove(); } });
	$A($$(".set3")).each(function(element) { if (element) { element.remove(); } });
	$A($$(".set4")).each(function(element) { if (element) { element.remove(); } });
  },

  getChecked: function(checkbox_class) {
	rest = $A($$("." + checkbox_class)).findAll(function(element) { return element.checked; }).invoke('getValue');
	this.generateSearchHidden(rest, checkbox_class);
	return rest;
  },

  generateSearchHidden: function(checkedAry, kind) {
	var num = 0;
	if (kind == "area_category") {
	  num = 1;
	} else if (kind == "job_category") {
	  num = 2;
	} else if (kind == "employ_model_category") {
	  num = 3;
	} else if (kind == "worktime_category") {
	  num = 4;
	}
	if (num > 0) {
	  checkedAry.each(function (value, index) {
		if (index == 0) {
		  hidden = new Element("input", { type : "hidden", name : "CategorySearchSets", value : "set" + num});
		  Element.addClassName(hidden, "CategorySearchSets");  
		  this.formElm.insert(hidden);
		  hidden = new Element("input", { type : "hidden", name : "set" + num + "_type", value : "or", id : "set" + num + "_type"});
		  this.formElm.insert(hidden);
	    }
	    hidden = new Element("input", { type : "hidden", name : "set" + num, value : value});
		Element.addClassName(hidden, "set" + num);  
	    this.formElm.insert(hidden);
	  }.bind(this));
	  if (this.isSetLimit == false) {
		this.isSetLimit = true;
	  }
	}
  },

  execute: function(e) {
	this.init();
	e.stop();
	var area_categories= this.getChecked("area_category");
	var job_categories = this.getChecked("job_category");
	var employ_model_categories= this.getChecked("employ_model_category");
	var worktime_categories = this.getChecked("worktime_category");

	if (this.isSetLimit == true) {
	  hidden = new Element("input", { type : "hidden", name : "limit" , value : 5});
	  this.formElm.insert(hidden);
	}
	this.formElm.submit();
  }
}

//---------------------------------------------------------------------------
// ページロード完了時のイベント設定処理
//---------------------------------------------------------------------------
Event.observe (window, 'load', function() { oSearch = new Search("search_submit_id", "search_form_id"); }, false);

