/**
 * @author hooriza (ajaxUI team)
 */

(function(className) {

	SPS[className] = $Class({

		_o : null,
		_oOptions : null,

		_aRange : null,

		$init : function(sEl, oArg) {

			var oEl = $(sEl);

			var o = this._o = {
				wrap : oEl,
				fromStrokes : $$('.from_strokes', oEl)[0],
				toStrokes : $$('.to_strokes', oEl)[0],
				resultTableArea : $$('.result_table_area',oEl)[0]
			}

			this._oOptions = oArg || {};
			SPS.selectbox.touchAll('select_d', oEl);

			this._applyStrokesRange();

		},

		_applyStrokesRange : function() {

			var o = this._o;
			var aRange = [
				parseInt(SPS.getFormValue(o.fromStrokes)),
				parseInt(SPS.getFormValue(o.toStrokes))
			];

			this._aRange = aRange;
		},

		_changeStrokes : function(oEl, bTo) {

			var nTo = bTo ? 1 : 0;
			var o = this._o;

			this._aRange[nTo] = parseInt(SPS.getFormValue(oEl));
			if (this._aRange[1 - nTo] != -1) {

				if (this._aRange[0] > this._aRange[1]) {

					if (nTo) o.fromStrokes.selectedIndex = o.toStrokes.selectedIndex;
					else o.toStrokes.selectedIndex = o.fromStrokes.selectedIndex;

					SPS.selectbox.touchAll();
				}

			}

			this._applyStrokesRange();
			this._requestStrokes();

		},

		_changeFromStrokes : function(oEl) {
			this._changeStrokes(oEl, false);
		},

		_changeToStrokes : function(oEl) {
			this._changeStrokes(oEl, true);
		},

		_expTrID : null,

		_requestStrokes : function() {

			var self = this;
			var o = this._o;

			var aRange = [ this._aRange[0], this._aRange[1] ];

			if (aRange[0] == -1) aRange[0] = 0;
			if (aRange[1] == -1) aRange[1] = 999;

			var oAjax = $Ajax('/search/ajax/', {
				onload : function(oRes) {

					var sHtml = oRes.text();
					var sTrID = sHtml.match(/<!--\s+\[trID:([0-9]+)\]\s+-->/);

					if (!sTrID || sTrID[1] != self._expTrID) return;
					o.resultTableArea.innerHTML = sHtml;

					return;

				}
			});

			this._expTrID = SPS.uniqid();

			oAjax.request({
				'trID' : this._expTrID,
				'fromStrokes' : aRange[0],
				'toStrokes' : aRange[1],
				'bushouId' : self._oOptions.bushouId
			});

		}

	});

})('ct_17_01');
