// Requires:
//  - cookie.js
//  - prototype.js

MemMember = Class.create({

  initialize: function (pageSlug, cookieName) {
    this.pageSlug = pageSlug;
    var memberCookie = new Cookie(cookieName);
    this.name = memberCookie.get();
    if (this.name) {
      this.name = this.ensafenString(this.name)
    }
  },


  ensafenString: function (str) {
    return unescape(str).replace(/\+/g, ' ').stripScripts().stripTags();
  },


  signinStatus: function (withinDiv) {
    withinDiv = $(withinDiv);
    if (this.name) {
      withinDiv.update(
        'Signed in as ' + this.linkHTML(this.name, 'account') + '. ' +
        this.linkHTML("Sign out", 'signout')
      );
    } else {
      withinDiv.update(this.linkHTML('Sign in', 'signin'));
    }
  },


  linkHTML: function(text, action) {
    return '<a href="/'+this.pageSlug+'/'+action+'">'+text+'</a>';
  },


  requireSignin: function (withinDiv, loginForm, authedContent) {
    withinDiv = $(withinDiv);
    if (this.name) {
      withinDiv.update(authedContent);
      var me = this;
      withinDiv.select('.mem_insert_name').each(function (elem) {
        elem.update(me.linkHTML(me.name, 'account'));
      });
    } else {
      withinDiv.update(loginForm);
    }
  }

});
