var UserTab = klass.create();
Object.extend(UserTab.prototype, {
  initialize: function(parent, tabName) {
    this.parent = parent;
    this.tabName = tabName;
    this.tab = $(this.tabName);
    this.tabView = $(this.tabName + 'view');

    Event.observe(this.tab, 'click', this.changeTab.bindAsEventListener(this));
  },

  changeTab: function() {
    this.parent.changeTab(this.tab, this.tabView);
  }
});

var User = klass.create();
Object.extend(User.prototype, {
  initialize: function(instance, owner, showPromoted, showDemoted) {
    this.id = instance;
    this.owner = (owner == "True") ? true : false;
    this.showPromoted = (showPromoted == "True") ? true : false;
    this.showDemoted = (showDemoted == "True") ? true : false;

    this.lastTab = null;
    this.lastTabView = null;

    if (this.owner) {
      this.tabs = ['accountinfo', 
                    'relationships', 
                    'submissions', 
                    'comments', 
                    'promoted', 
                    'demoted', 
                    'themes'];
    } else if (this.showPromoted && this.showDemoted) {
      this.tabs = [ 'relationships', 
                    'submissions', 
                    'comments', 
                    'promoted', 
                    'demoted']; 
    } else if (this.showPromoted) {
      this.tabs = [ 'relationships', 
                    'submissions', 
                    'comments', 
                    'promoted']; 
    } else if (this.showDemoted) {
      this.tabs = [ 'relationships', 
                    'submissions', 
                    'comments', 
                    'demoted']; 
    } else {
      this.tabs = [ 'relationships', 
                    'submissions', 
                    'comments']; 
    }
    this.bindControls();

    if (this.owner) {
      this.changeTab($('accountinfo'), $('accountinfoview'));
    } else {
      this.changeTab($('relationships'), $('relationshipsview'));
    }
  },

  bindControls: function() {
    var self = this;
    this.tabs.each(function(item) {
      new UserTab(self, item);
    });
  },

  changeTab: function(tab, tabView) {
    if (this.lastTab != null) {
      this.lastTab.className = '';
    }

    if (this.lastTabView != null) {
      this.lastTabView.style.display = 'none';
    }

    tab.className = 'selected';
    this.lastTab = tab;
    tabView.style.display = 'inline';
    this.lastTabView = tabView;
  }
});

var g_userComponents = [];

function g_userInit() {
  g_userComponents.each(function(inst, i) {
    new User(inst.id, inst.owner, inst.showPromoted, inst.showDemoted);
  });
}

g_components.push(g_userInit);

// vim:set sts=2 sw=2 expandtab:
