if (typeof(KampoQ) == 'undefined') {
    KampoQ = {};
}
KampoQ.Parts = {
    ID_SUBJECT : 'kensaku_subj',
    ID_PREF : 'kensaku_pref',
    ID_CITY : 'kensaku_city',

    idSubj : '',
    idPref : '',
    idCity : '',
    idSubmit : '',

    _existsJq : false,
    _existsPt : false,
    /**
     * 検索プルダウン初期化
     */
    init : function(idSubj, idPref, idCity, idSubmit)
    {
        this._existsJq = (typeof(jQuery) != 'undefined');
        this._existsPt = (typeof(Prototype) != 'undefined');
        
        
        // load時(クラス読み込み完了後)に処理実行
        if (this._existsPt) {
            Event.observe(window, 'load', function(){
                KampoQ.Parts._initOnLoad(idSubj, idPref, idCity, idSubmit);
            });
        } else if (this._existsJq) {
            $(document).ready(function()
            {
                KampoQ.Parts._initOnLoad(idSubj, idPref, idCity, idSubmit);
            });
        } else {
            alert('No library.');
        }
    },
    /**
     * 検索プルダウン初期化 (load完了時に実行)
     */
    _initOnLoad : function(idSubj, idPref, idCity, idSubmit)
    {
        this.idSubj = idSubj;
        this.idPref = idPref;
        this.idCity = idCity;
        this.idSubmit = idSubmit;

        var func = function(event)
        {
            
            if (KampoQ.Parts.check()) {
                return true;
            } else {
                if (KampoQ.Parts._existsPt) {
                    Event.stop(event);
                } else if (KampoQ.Parts._existsJq) {
                    event.preventDefault();
                }
                alert('都道府県、市区町村を選択してください');
                return false;
            }
        };
        if (this._existsPt) {
            Event.observe($(idSubmit), 'click', func);
            Event.observe($(idSubj), 'change', function(){ KampoQ.Parts.kensakuUpdated(); });
            Event.observe($(idPref), 'change', function(){ KampoQ.Parts.kensakuUpdated(); });
        } else if (this._existsJq) {
            $('#' + idSubmit).bind('click', func);
            $('#' + idSubj).bind('change', function(){ KampoQ.Parts.kensakuUpdated(); });
            $('#' + idPref).bind('change', function(){ KampoQ.Parts.kensakuUpdated(); });
        }
        this.kensakuUpdated();
    },
    log : function(e)
    {
        if (console && console.log) {
            console.log(e);
        }
    },
    /**
     * 科目、都道府県プルダウンを変更したとき
     */
    kensakuUpdated : function()
    {
        try {
            var subject, pref;
            if (this._existsPt) {
                subject = $F(this.idSubj);
                pref    = $F(this.idPref);
            } else if (this._existsJq) {
                subject = $('#' + this.idSubj).val();
                pref    = $('#' + this.idPref).val();
            }
            // 都道府県が未入力: 何もしない
            if (pref.length == 0) {
                return;
            }
            //
            this._updateCities(subject, pref);
        } catch (e) {
            this.log(e);
        }
    },
    /**
     * 市区町村プルダウンを更新
     */
   _updateCities : function(subject, pref)
   {
       try {
           // 片方が未入力: 何もしない
           if (pref.length == 0) {
               return;
           }
           if (this._existsPt) {
               new Ajax.Request('/clinic-search/json_city', {
                   method : 'get',
                   parameters : {sbj : subject, pref : pref},
                   onSuccess : function(transport) {
                       // 市区町村selectを更新
                       var json = transport.responseText.evalJSON();
                       KampoQ.Parts._updateOptions(json);
                   }
               });
           } else if (this._existsJq) {
               $.ajax({
                   type: 'GET',
                   url: '/clinic-search/json_city',
                   data: {sbj : subject, pref : pref},
                   dataType : 'json',
                   success: function(msg){
                       KampoQ.Parts._updateOptions(msg);
                   }
               });
           }
       } catch (e) {
           this.log(e);
       }
   },
   /**
    * 市区町村プルダウンを更新
    */
   _updateOptions : function(json)
   {
       // optionsクリア
       this.removeOptions(this.idCity);
       var select = document.getElementById(this.idCity);
       for (var i = 0, maxlen = json.length ; i < maxlen ; i++) {
           var caption, val;
           if (this._existsPt) {
               caption = String(json[i].name + ' (' + json[i].num + ')').escapeHTML();
               val     = String(json[i].name).escapeHTML();
           } else if (this._existsJq) {
               caption = String(json[i].name + ' (' + json[i].num + ')');
               val     = String(json[i].name);
           }
           select.options[i + 1] = new Option(caption, val);
       }
       
   },
   /**
    * selectのoptionsをクリア
    */
   removeOptions : function(idSelect)
   {
       // 既存selectから先頭項目以外のoptionを除去
       var select = document.getElementById(idSelect);
       for (var i = 0, maxlen = select.options.length ; i < maxlen ; i++) {
           select.options[1] = null;
       }
   },
   /**
    * 
    */
   check : function()
   {
       var pref, city;
       if (this._existsPt) {
           pref = $F(this.idPref);
           city = $F(this.idCity);
       } else if (this._existsJq) {
           pref = $('#' + this.idPref).val();
           city = $('#' + this.idCity).val();
       }
       return (pref.length > 0 && city.length > 0);
   }
};
