var CommunitySelector = new Class({
    initialize: function(area_available){
        if($('id_area') && $('id_community') && $('community_choice_form')){
            this.area_list = $('id_area');
            this.community_list = $('id_community');
            this.form = $('community_choice_form');
            this.form.getElement('.submit').setStyles({'display': 'none'});
            this.set_events();
            if(area_available) this.area_list.setStyles({'display': 'inline', 'width':'auto'});
        }
    },

    set_events: function(){
        this.area_list.addEvent('change', function(){
            var select_request = new Json.Remote("/api/community_select/", {onComplete: function(data){
                this.community_list.empty();
                data.each(function(e){
                    var option = new Element('option', {'value':e[0]}).set('text', e[1]);
                    this.community_list.adopt(option);
                }.bind(this));
            }.bind(this)}).send({'restrict_area_id':this.area_list.value});
        }.bind(this));

        this.community_list.addEvent('change', function(){
            this.form.submit();
        }.bind(this));

    }



});