Realius.Profile = {
  RecentLocationsPresenter: PresenterClass.create(),
  MoveListPresenter: PresenterClass.create(),
  MoveListControlsPresenter: PresenterClass.create(),
  FormModel: ModelClass.create(),
  
  initialize: function() {
    var model = new Realius.Profile.FormModel();
    
    new Realius.Profile.RecentLocationsPresenter({
      messageBus: MessageBus,
      model: model
    });
    
    new Realius.Profile.MoveListPresenter({
      messageBus: MessageBus,
      model: model
    });
    
    new Realius.Profile.MoveListControlsPresenter({
      messageBus: MessageBus,
      model: model
    });
    
    $$("#status-filter-options li").each(function(li) {
      li.observe("click", 
        MessageBus.notify.bind(MessageBus, "statusChanged", li.readAttribute("value")))
    });

    var showRecentlySoldHomes = $("recently-sold-homes-show-all")
    if (showRecentlySoldHomes) {
      showRecentlySoldHomes.observe("click", MessageBus.notify.bind(MessageBus, "showAllSoldsButtonClicked"))
    }
  }
};

Object.extend(Realius.Profile.FormModel.prototype, {
  setLocationName: function(locationName){
    $('location_name').value = locationName;
    this.notify('locationNameUpdated');
  },
  
  getLocationName: function() {
    return $('location_name').value
  },
  
  setPageNumber: function(number){
    $('page_number').value = number;
  },
  
  setStatus: function(newStatus) {
    $('status').value = newStatus;
    this.notify('statusChanged', newStatus)
  },
  
  resetFilters: function() {
    this.setLocationName("");
    this.setPageNumber(1);
    this.refreshMoves();
  },
  
  refreshMoves: function(){
		var self = this;
		self.notify('refreshMovesStarted');
    Form.request('remote-form', {
      onSuccess: function(response){
        self.notify('refreshMovesSucceeded', response.responseText);
      }
    });
  }
});

Object.extend(Realius.Profile.RecentLocationsPresenter.prototype, {
  initialize: constructor('messageBus', 'model'),

  messageBus_newPageRequested: function(number){
    this.model.setPageNumber(number);
    this.model.refreshMoves();
  },
  
  messageBus_recentlyPlayedLocationClicked: function(location){
    Element.extend(location);
    this.model.setLocationName(location.readAttribute("title"));
    this.model.setPageNumber(1);
    this.model.refreshMoves();
  },
  
  model_locationNameUpdated: function() {
    $$('#recently-played-locations .selected').invoke('removeClassName', 'selected')
    var name = this.model.getLocationName();
    if(!name.blank()) {
      $$('#recently-played-locations a').
        detect(function(location) {return location.readAttribute('title') == name}).
        addClassName('selected');      
    }
  }
});

Object.extend(Realius.Profile.MoveListPresenter.prototype, {
  initialize: constructor('messageBus', 'model'),

  model_refreshMovesSucceeded: function(response) {
    $("houses-played").innerHTML = response;
  }, 
  
  model_refreshMovesStarted: function() {
    var spinner = $("spinner");
    if (spinner) {
      $("houses-played").innerHTML = spinner.innerHTML
    }
  },
  
  model_locationNameUpdated: function() {
    name = this.model.getLocationName();
    if(!name.blank()) {
       name = "near " + name;
       $$(".reset-location-filter").invoke('show')
    } else {
      $$(".reset-location-filter").invoke('hide');  
    }
    
    $("location-title").innerHTML = name;
  },
  
  messageBus_resetClicked: function() {
    this.model.resetFilters();
  }
});

Object.extend(Realius.Profile.MoveListControlsPresenter.prototype, {
  initialize: constructor('messageBus', 'model'),

  model_statusChanged: function(newStatus) {
    $$("#status-filter-options li.selected").invoke("removeClassName", "selected")
    $$("#status-filter-options li[value="+newStatus+"]").invoke("addClassName", "selected")
  },
  
  messageBus_statusChanged: function(newStatus) {
    this.model.setStatus(newStatus);
    this.model.refreshMoves(newStatus);
  },
  
  messageBus_showAllSoldsButtonClicked: function() {
    this.model.setStatus('sold');
    this.model.setLocationName("");
    this.model.setPageNumber(1);
    this.model.refreshMoves();
  }
});
