//---------------------------------------------------------------------------
// formの外側に存在するチェックボックス送信用クラス
//---------------------------------------------------------------------------
var Search = Class.create();
Search.prototype = {
  selectElm: "", // イベント対象のボタン要素
  form: "", // formタグの要素
  
  //
  // コンストラクタ
  // submitId：サブミットボタンのid
  // formId：フォームのid
  // dataClassName：送信対象のクラス名
  // checkAll：すべてチェックするためのチェックボックスのid
  //
  initialize: function(selectElm) {
	this.selectElm = $(selectElm);
//	this.dataClassElms = document.getElementsByClassName(dataClassName);
//	this.form = $(formId);
	// this.checkAllElm = $(checkAllId);
	Event.observe(this.selectElm, 'change', this.send.bindAsEventListener(this));
	// Event.observe(this.checkAllElm, 'click', this.checkAll.bindAsEventListener(this));
  },
/*
  // すべてチェック用のチェックボックスが押されたとき場合のすべてのチェックボックスのonもしくはoff
  checkAll: function(event) {
  	var checkAllElm = this.checkAllElm; // eachメソッド内では、this.checkAllElmが無効になるので、再指定
	var nodes = $A(this.dataClassElms);
	nodes.each(function (node) {
	  if (checkAllElm.checked == true) {
		node.checked = true;
	  } else {
		node.checked = false;
	  }
	})
  },
*/
  // 送信する。
  send: function(event) {
//	alert(this.selectElm.options[this.selectElm.selectedIndex].value);

    if (this.selectElm.options[this.selectElm.selectedIndex].value != "") {
	  if (location.pathname.search(/.*index\.php.*/i) != -1) {
	    location.href = "http://" + location.host + location.pathname + "?page=/product/search_result/" + this.selectElm.options[this.selectElm.selectedIndex].value;
	  } else {
	    location.href = "http://" + location.host + location.pathname + "index.php?page=/product/search_result/" + this.selectElm.options[this.selectElm.selectedIndex].value;
	  }
	}

	//if (location.href.search(/http:\/\/ml5\/tsurumi\/public_html\/.*/i) != -1) {
//	  location.href = 
	//}
	/*
	for (var i in location) {
	alert(i + "：" + location[i]);
//	  alert(location[i]);
	}
	*/
	/*
	var nodes = $A(this.dataClassElms);
	var checkCount = 0;
	if (nodes.length > 0) {
	  checkCount = this.createHidden(nodes);
	}
	
	this.form.submit();
	Event.stop(event);
	return false;
  */
  },

  // 選択したチェックボックス(公開指定)の数を数える
  checkOpenCount: function (nodes) {
	var checkCount = 0;
	nodes.each (function (node) {
	  if (node.checked	== true) {
		checkCount++;
	  }
	});

	if (checkCount != 3) {
	  alert(checkCount + "個の項目は選択しております。\n公開する項目は3個です。");
	  return false;
	}
  },
  
  // サブミットで渡すためにhiddenを生成する。
  createHidden: function (nodes) {
	var f = this.form; // eachメソッド内では、this.formが無効になるので、再指定
	var checkCount = 0;
	nodes.each(function (node) {
	  if (node.checked == true) {
		var input = document.createElement('input');		
		input.type = "hidden";
		input.name = "data[Product][id][]";
		input.value = node.value;
		f.appendChild(input);
//		checkCount++;
	  } else {
		var input = document.createElement('input');		
		input.type = "hidden";
		input.name = "data[Product][no_id][]";
		input.value = node.value;
		f.appendChild(input);
		checkCount++;
	  }
	});
//	return checkCount;
  }
}

//---------------------------------------------------------------------------
// ページロード完了時のイベント設定処理
//---------------------------------------------------------------------------
Event.observe (window, 'load', function() {
  if ($("search-item")) {
	oSearch = new Search("search-item");
  }
}, false);

