//---------------------------------------------------------------------------
// 通常の手順以外のボタン送信用オブジェクト
//---------------------------------------------------------------------------
var SreachArea = Class.create();

SreachArea.prototype = {
  prefElms: "",
  region: ["kantou",  "kansai", "toukai", "hoxtukaidou_touhoku", "hokuriku_koushinnetu", "shikoku_chuugoku", "kyuushuu"],
  isDisplayRest: false, // 検索結果を表示している場合は、true
  currentResult: "default",
  sendPath: "",
  currentRegion: "",
  // コンストラクタ
  //
  initialize: function() {
	this.getCurrentRegion();
	// this.prefElms = $$(".prefs");
	Event.observe($("garea"), 'click', this.area_search_default_pref.bindAsEventListener(this)); // 勤務地で探すボタンが押された地域はデフォルトの都道府県で検索
	Event.observe($("gtrain"), 'click', this.train_search.bindAsEventListener(this)); // 沿線・駅から探すボタンが押された地域はデフォルトの都道府県で検索

	var m = "^.*" + document.location.host + "(.*)/advertisement/top.*$";
	if (document.location.href.match(m)) {
	} else {
	}
	this.sendPath = RegExp.$1;

	/*
    var pathAry=document.location.href.split("/");
	var loopSize = pathAry.size() - 4;
	for (i = 1; i < loopSize; i++) {
	  this.sendPath += "/" + pathAry[i];
	}
	*/
  },

  // 地方を調べる
  getCurrentRegion: function () {
	var currentRegion = "";
	this.region.each (function (value) {
	  if ($(value)) {
		currentRegion = value;
		return;
	  }
	});
	this.currentRegion = !currentRegion  ? "kantou" :currentRegion;
  },
  
  changeDisplay: function () {
	this.isDisplayRest = !this.isDisplayRest; // 表示状態を反転
  },
  // 送信する。
  area_search_default_pref: function(event) {
//	this.changeDisplay();
	if (this.currentResult == "area") {
	  $("search-result").update("<div id=\"msec01\"><div id=\"pachi-img\"><p><a href=\"#\">祝い金システムって？</a></p></div></div>");
	  this.currentResult = "default";
	  event.stop();
	  return;
	}
	this.currentResult = "area";
    // var prefElm = $(this.prefId);
	// var index = prefElm.selectedIndex;
	  // search_area($region_name = "kantou", $pref = 0) {
	var myAjax = new Ajax.Request(
			this.sendPath + '/advertisement/search_area/' + this.currentRegion + "/" , 
			{
				method: 'post', 
				onComplete: this.displayAreaDefaultPref.bindAsEventListener(this)
			});
	event.stop();
  },
  displayAreaDefaultPref: function(response) {
	resultText = response.responseText;

	resultObj = resultText.evalJSON();
	if (resultObj) {
	  var pref_html = "";
	  var area_html = ""
	  var currentRegion = this.currentRegion;
	  var sendPath = this.sendPath;
	  var domain = document.location.host;
	  var obj = this;
	  pref_html = "<div id=\"msec02\"><dl id=\"areaselbox01\"><dt>県を選ぶ</dt>";
	  resultObj[this.currentRegion].prefs.each(function (value) {
		// pref_html += "<li id='pref" + value.id + "'>" + value.name + "</li>";
		pref_html += "<dd><a href=\"#\" id='pref" + value.id + "'>" + value.name + "</a></dd>";
	  });
		pref_html += "</dl><dl id=\"areaselbox02\"><dt>勤務地を選ぶ</dt>";

	  resultObj[this.currentRegion].areas.each(function (value) {
		if (value.count > 0) {
		  pref_html += "<dd><a href='http://" + domain + sendPath + "/advertisement/search_result/" + currentRegion + "?area_master_id=" + value.id + "'>" + value.name + "(" + value.count + ")</a></dd>";
		} else {
		  pref_html += "<dd>" + value.name + "(" + value.count + ")</a></dd>";
		}
	  });
	  pref_html += "</dl></div>";
	  $("search-result").update(pref_html);
//	  $("area_result").update(area_html);
	  resultObj[this.currentRegion].prefs.each(function (value) {
		obj.observePref("pref" + value.id);
	  });
	  }
  },


  observePref : function (prefId) {
	Event.observe($(prefId), 'click', this.area_search.bindAsEventListener(this));
  },

  area_search: function(event) {
	var elm = event.element();
	var id = elm.id.replace("pref", "");
	id = parseInt (id);
	var myAjax = new Ajax.Request(
			this.sendPath + '/advertisement/search_area/' + this.currentRegion + "/" + id + "/", 
			{
				method: 'post', 
				onComplete: this.displayArea.bindAsEventListener(this)
			});
	event.stop();
  },
  displayArea: function(response) {
	resultText = response.responseText;
	resultObj = resultText.evalJSON();
	if (resultObj) {
	  var area_html = ""
	  var currentRegion = this.currentRegion;
	  var sendPath = this.sendPath;
	  var obj = this;
	  var domain = document.location.host;
	  area_html = "<dt>勤務地を選ぶ</dt>";
	  resultObj[this.currentRegion].areas.each(function (value) {
 		if (value.count > 0) {
		area_html += "<dd><a href='http://" + domain + sendPath + "/advertisement/search_result/" + currentRegion + "?area_master_id=" + value.id + "'>" + value.name + "(" + value.count + ")</a></dd>";
		} else {
		area_html += "<dd>" + value.name + "(" + value.count + ")</dd>";
		}
	  });
	  $("areaselbox02").update(area_html);
	}
  },
  
  
    // 送信する。
  train_search: function(event) {
	if (this.currentResult == "train") {
	  $("search-result").update("<div id=\"msec01\"><div id=\"pachi-img\"><p><a href=\"#\">祝い金システムって？</a></p></div></div>");
	  this.currentResult = "default";
	  event.stop();
	  return;	
	}
	this.currentResult = "train";

	var myAjax = new Ajax.Request(
			this.sendPath + '/advertisement/search_train/' + this.currentRegion + "/" , 
			{
				method: 'post', 
				onComplete: this.displayTrain.bindAsEventListener(this)
			});
	event.stop();
  },

 displayTrain: function(response) {
	resultText = response.responseText;
	resultObj = resultText.evalJSON();
	if (resultObj) {
	  var train_html = ""
	  var currentRegion = this.currentRegion;
	  var sendPath = this.sendPath;
	  var obj = this;
	  var domain = document.location.host;

	  train_html = "<div id=\"msec03\"><dl id=\"trainselbox\"><dt>路線を選ぶ</dt>";
	  resultObj[this.currentRegion].trains.each(function (value) {
 		if (value.count > 0) {
		  train_html += "<dd><a href=http://" + domain + sendPath + "/advertisement/search_result/" + currentRegion + "?train_master_id=" + value.id + ">" + value.name + "(" + value.count + ")</a></dd>";
		} else {
		  train_html += "<dd>" + value.name + "(" + value.count + ")</dd>";
		}
	  });
	  train_html += "</dl></div>";
	  $("search-result").update(train_html);
	}
  }
}

Event.observe (window, 'load', function() {
  if ($("garea") && $("gtrain"))
	oAddEvent = new SreachArea();
}, false);
