
var Cities = Class.create();
Cities.prototype = {
    loaded_: false,
    loading_: false,
    element_: null,
    city_list_: null,
    pref_name_euc: null,
    pref_code: null,
    value_by_code: true,
    city_code_: null,
    prefs_: null,
    
    initialize: function(prefs, opts) {
        this.prefs_ = prefs; 
        opts = Object.extend({
            value_by_code: true
        }, opts || {});

        this.value_by_code = opts.value_by_code;
    },
    
    load: function(pref_code, element, city_code) {
        if (this.loaded_) {
            this.setCitiesToElement(element);
            this.prefs_.onCitiesLoaded_(this);
        } else {
            if (this.loading_) return;
            this.loading_ = true;
            this.element_ = element;
            this.city_code_ = city_code;
            var req = '/remote/get_cities';
            if (this.value_by_code) { 
                req += '_by_code'
            }
            new Ajax.Request(req, {
                method: 'get',
                parameters: 'pref=' + pref_code,
                onComplete: this.saveElements.bind(this)
            });
        }
    },
    
    saveElements: function(transport) {
        var objects_ = eval('(' + transport.responseText + ')');
        this.city_list_ = objects_.items;
        this.pref_name_euc = objects_.name_euc;
        this.pref_code = objects_.pref_code;
        this.loading_ = false;
        this.loaded_ = true;
        this.setCitiesToElement(this.element_, this.city_code_);
        this.element_ = null;
        this.city_code_ = null;
        this.prefs_.onCitiesLoaded_(this);
    },
    
    setCitiesToElement: function(element, city_code) {
        var opts = element.options;
        var index = 0;
        var selIndex = 0;
        opts[0] = new Option("指定しない", '', false, false);
        index++;
        this.city_list_.each(function(value) {
            opts[index] = new Option(value.name, value.value, false, false);
            if (value.value == city_code) {
                selIndex = index;
            }
            index++;
        });
        element.options.length = index;
        element.selectedIndex = selIndex;
    }
};

//
var Prefs = Class.create();
Prefs.prototype = {
    on_pref_selected_handlers_: null,
    on_city_selected_handlers_: null,
    prefs_selector_: null,
    cities_selector_: null,
    cities_cache_: new Object(),
    initialize: function(prefs_selector, cities_selector, opts) {
        this.opts_ = Object.extend({
            value_by_code: true,
            pref_code: 0,
            city_code: 0
        }, opts || {});
        
        this.prefs_selector_ = $(prefs_selector);
        this.cities_selector_ = $(cities_selector);
        this.prefs_selector_.onchange = function() {
            this.loadCities(this.prefs_selector_.selectedIndex);
        }.bind(this);
        
        this.cities_selector_.onchange = function() {
            var cities = this.cities_cache_[this.prefs_selector_.selectedIndex];
            var city = null;
            if (cities != null) {
                var index = this.cities_selector_.selectedIndex;
                if (0 < index) {
                    index = index - 1;
                    city = cities.city_list_[index];
                }
            }
            this.onCitiesSelected_(cities, city);
        }.bind(this);

    },
    
    loadCities: function(n, city_code) {
    	if (n<0 || n>47) {
    		alert('loadCities: illeagal parameter('+cities_selector.name+','+n+')');
    		return;
    	}
    	if (n == 0) {
    	   var elm = $(this.cities_selector_);
    	   elm.options.length = 0;
    	   elm.options[0] = new Option("指定しない", '');
    	   this.onCitiesLoaded_(null);
    	   return;
    	}
    	var cities = this.cities_cache_[n];
    	if (cities == null) {
    	    cities = new Cities(this, this.opts_);
    	    this.cities_cache_[n] = cities;
    	}
    	cities.load(n, $(this.cities_selector_), city_code);
    },
	
	select_pref_by_value: function(pref_code, city_code) {
	   var selector = this.prefs_selector_;
	   var index = -1;
	   var me = this;
	   $A(selector.options).each(function(option) {
	       if (option.value == pref_code) {
	           index = option.index;
	           selector.selectedIndex = option.index;
	           this.loadCities(pref_code, city_code);
	           throw $break;
	       }
	   }.bind(me));
	},

    add_on_pref_selected_handler: function(handler_object) {
        if (this.on_pref_selected_handlers_ == null) {
            this.on_pref_selected_handlers_ = new Array();
        }
        this.on_pref_selected_handlers_.push(handler_object);
    },
    
	onCitiesLoaded_: function(cities) {
	   if (this.on_pref_selected_handlers_ != null) {
	       this.on_pref_selected_handlers_.each(function(handler) {
	           handler(cities);
	       });
	   }
	},
	
	add_on_city_selected_handler: function(handler_object) {
        if (this.on_city_selected_handlers_ == null) {
            this.on_city_selected_handlers_ = new Array();
        }
        this.on_city_selected_handlers_.push(handler_object);
	},

	onCitiesSelected_: function(cities, city) {
	   if (this.on_city_selected_handlers_ != null) {
	       this.on_city_selected_handlers_.each(function(handler) {
	           handler(cities, city);
	       });
	   }
	}
	
}
