var ProfilePage = Class.create({

  initialize: function(){
    var about_me_edit = $('about_me_edit');
    
//    var broker_category_dd = $('dir_categories');
//    if(broker_category_dd){
//      broker_category_dd.observe('change', this.updateSubCategory.bindAsEventListener(this));
//    }
    var broker_category_dd = $('dir_subcategories');
    if(broker_category_dd){
      broker_category_dd.observe('change', function(event) {
      	window.location.href='/brokers/directory/broker_sub_category_id:' + event.target.value;
      });
    }
  },
  
  updateSubCategory: function(event){
    var category = Event.element(event);
    var subcategory = $('dir_subcategories');
    
    //clear the options.
    this.clearOptions(subcategory);
    
    //Populate the subcategory form.
    var url = '/broker_categories/getsubcats/' + category.value;
    new Ajax.Request(url, {
      method: 'get',
      onSuccess: function(transport) {
        var subcats = transport.responseText.evalJSON();
        subcats.each(function(cat){
            var val = cat.BrokerSubCategory.id;
            var name = cat.BrokerSubCategory.name;
            var opt = new Element('option', {value: val}).update(name);
            subcategory.options.add(opt);
        });
      }
    });

  },
  
  clearOptions: function(selectBox, def){
    selectBox.options.length = 0;
    var option = new Element('option', {value: ''}).update('Sub-Category');
    selectBox.options.add(option);
  },
  
  showEdit: function(id){
    //show an edit field
    var showId = id + "_edit";
    var hideId = id;
    this.hideIt(hideId);
    this.showIt(showId);
  },
  
  hideEdit: function(id){
    //Hide only the id element
    var hideId = id + "_edit";
    var showId = id;
    this.hideIt(hideId);
    this.showIt(showId);
  },
  
  hideAllEdit: function(){
    //Hide all the elements
  },
  
  hideIt: function(id){
    if($(id)!=null){
      $(id).hide();
    }
  },
  
  showIt: function(id){
    if($(id)!=null) $(id).show();
  },
  
  setValue: function(id, val){
    if($(id)!=null) $(id).value = val;
  },
  
  showLoader: function(){
    this.showIt('loader');
  },
  
  hideLoader: function(){
    this.hideIt('loader');
  },
  
  selectText: function(id){
    $(id).select();
  },
  
  accountTypes: {
    1: "Bronze",
    2: "Silver",
    3: "Platinum",
    4: "Directory"
  },
  
  changeAccount: function(elem){
    var account = this.accountTypes[elem.value];
    if(account == "Bronze"){
      this.hideIt('title');
      this.hideIt('internet-address');
    }
    else if(account == "Silver"){
      this.showIt('title');
      this.hideIt('internet-address');
    }
    else if(account == "Platinum"){
      this.showIt('title');
      this.showIt('internet-address');
    }
    else {
      this.hideIt('title');
      this.hideIt('internet-address');
    }
  },
  
  toggleCheckAll: function(elem){
    if(elem.checked){
      //check all available checkboxes
      $$('input[type=checkbox]').each(function(el){
          el.checked = true;
      });
    }
    else {
      //uncheck all available checkboxes
      $$('input[type=checkbox]').each(function(el){
          el.checked = false;
      });
    }
  }
});

