Blahger.Comments = ActiveElement.Collection.spawn('comment');

Blahger.Comment = ActiveElement.Base.spawn('comment', {
  getLinkElement: function(){
    return this.element.down('.permalink');
  },
  getLinkValue: function(){
    var e = this.getLinkElement()
    return e && e.readAttribute('href');
  },
  setLinkValue: function(href){
    var e = this.getLinkElement();
    e && e.writeAttribute('href', href);
    return this.getLink();
  },
  getUrlElement: function(){
    return this.element.down('.author');
  },
  getUrlValue: function(){
    var e = this.getUrlElement();
    return e && e.readAttribute('href');
  },
  setUrlValue: function(url){
    var e = this.getUrlElement();
    e && e.writeAttribute('href', url);
  }
});




Blahger.CommentForm = ActiveElement.Form.spawn('comment', {

  afterInitialize: function(){
    this.observeFieldsets();
  },
  
  getOpenidUrlElement: function(){
    return this.element.down('#openid_url');
  },
  
  getOpenidFieldset: function(){
    return this.element.down('fieldset.openid');
  },
  getNormalFieldset: function(){
    return this.element.down('fieldset.normal');
  },
  getSwitcher: function(){
    return this.element.down('.openid-switcher');
  },
  
  activateOpenid: function(){
    if (typeof this._openidUrl == 'string') { this.set('openid_url', this._openidUrl); }
    this.getNormalFieldset().addClassName('inactive');
    this.getOpenidFieldset().removeClassName('inactive');
    this.openidSwitcherLink.update("I don't want to use OpenID");
  },
  inactivateOpenid: function(){
    this._openidUrl = this.get('openid_url');
    this.set('openid_url', '');
    this.getNormalFieldset().removeClassName('inactive');
    this.getOpenidFieldset().addClassName('inactive');
    this.openidSwitcherLink.update("I want to use OpenID");
  },
  openidActivated: function(){
    return !this.getOpenidFieldset().hasClassName('inactive');
  },
  toggleOpenid: function(){
    this.openidActivated() ? this.inactivateOpenid() : this.activateOpenid();
  },
  
  observeFieldsets: function(){
    var form = this;

    var switcher = this.getSwitcher();
    this.openidSwitcherLink = (new Element('a', {href:'#'})).update("Don't use OpenID");
    switcher.update(this.openidSwitcherLink);
    this.openidSwitcherLink.observe('click', function(e){
      e.stop();
      form.toggleOpenid();
      form.element.focus(form.openidActivated() ? 'openid_url' : 'name');
    });

    this.activateOpenid();
  }

});
