
var address = Class.create();
address.prototype = {
    
  cats: null,
  cat1: null,
  cat2: null,
  custom_field: null,
  holder: null,
  id1: null,
  id2: null,
  url: null,
  
  initialize: function(cat1, cat2, url, custom){
    if($(cat1) && $(cat2)){
        this.cat1 = $(cat1);
        this.cat2 = $(cat2);
        this.custom_field = $(custom);
        this.holder = $(custom + "_holder")
        this.cat2.disabled = true;
        this.url = url;

        this.cat1.observe('change', function(){
            this.select(this.cat1.options[this.cat1.selectedIndex].value, null);
        }.bind(this));

        this.cat2.observe('change', function(){
            if(this.cat2.options[this.cat2.selectedIndex].value < 0){
                this.custom(true);
            }else{
                this.custom(false);
            }
        }.bind(this));
    }
  },
  
  activate: function(id1, id2){
    if(this.cat1 && this.cat2){
        var id1 = (parseInt(id1)!=0 ? parseInt(id1) : null);
        var id2 = (parseInt(id2)!=0 ? parseInt(id2) : null);
        if (id1!=null){
            this.select(id1, id2);
        }
    }
  },
  
  select: function (id1, id2){
      while(this.cat2.options.length>1){
          this.cat2.options[1].remove();
      }
      this.cat2.disabled = true;
      if(id2!=null){
        this.id2 = id2;
      }else{
          this.id2 = "";
      }
      if(id1){
          this.id1 = id1;
          this.cat2.disabled = true;
          this.cat2.options[0].selected = true;
          this.cat2.options[0].innerHTML = "Palaukite...";
          new Ajax.Request(this.url + ",type.addres_lvl_2,parent." + id1, {
          onSuccess: function(response) {
              var list = response.responseJSON;
              this.relistJSON(list);
          }.bind(this)
        });
      }
   },
   relistJSON: function (list){
        var i = 0;
        var open = true;
        
        if (list.length > 1) {
            for(i = 0; i < list.length; i++){
                this.cat2.options[i+1] = new Element('option');
                this.cat2.options[i+1].innerHTML = list[i].title;
                this.cat2.options[i+1].value = list[i].id;
                this.cat2.options[i+1].selected = (list[i].def ? true : false);
                if(this.id2 == list[i].id){
                    this.cat2.options[i+1].selected = true;
                    open = false;
                }
            }
            this.cat2.options[0].innerHTML = "Pasirinkite...";
            this.cat2.options[0].disabled = false;
        }else if(list.length == 1){
            this.cat2.options[1] = new Element('option');
            this.cat2.options[1].innerHTML = list[0].title;
            this.cat2.options[1].value = list[0].id;
            this.cat2.options[1].selected = true;

            this.cat2.options[0].innerHTML = "Pasirinkite...";
            this.cat2.options[0].disabled = true;
            i = 1;
        }

        this.cat2.options[i+1] = new Element('option');
        this.cat2.options[i+1].innerHTML = " -Nerandu savo miesto- ";
        if(this.id2 < 0 && open){
            this.cat2.options[i+1].selected = true;
            this.custom(true);
        }else{
            this.custom(false);
        }
        this.cat2.options[i+1].value = (-this.id1);
        
        this.cat2.disabled = false;
        this.cat2.focus();
   },
   custom: function (show){
       if(show){
           this.holder.setStyle({display:"block"});
           this.custom_field.focus();
       }else{
           this.holder.setStyle({display:"none"});
           this.custom_field.value = "";
       }
   }
};