/* Generates the membership box according to Member informations. 
Related snippet files live in project's root:
design/snippets/membership_box_js.html
design/snippets/membership_box.html */
var MembershipBox = {
  
  render : function() {
    if (Member.first_name() != null) {
      this.render_logged_in_box();
    }
    else {
      this.render_logged_out_box();
    }
    this.update_wishlist_link();
    $$('#membership-box ul')[0].show();
    $$('#membership-box p')[0].hide();
  },


  render_logged_in_box : function() {
    this.update_welcome_link();
    this.update_logout_link();  
  },

  update_welcome_link : function() {
    link = $$('#membership-box ul li a')[0];
    link.setAttribute('href', '/garden-club/my-membership');
    link.update(Member.first_name() + ' - My Profile'); 
  },
  update_logout_link : function() {
    link = $$('#membership-box ul li a')[1];
    link.setAttribute('href', '/garden-club/logout');
    link.update('logout');
  },


  render_logged_out_box : function() {
    this.update_sign_in_link();
    this.update_join_link();
  },

  update_sign_in_link : function() {
    link = $$('#membership-box ul li a')[0];
    link.setAttribute('href', '/garden-club/login');
    link.update('Sign in');
  },
  update_join_link : function() {
    link = $$('#membership-box ul li a')[1];
    link.setAttribute('href', '/garden-club/join');
    link.update('Join the Garden Club');
  },


  update_wishlist_link : function() {
    if (MyWishlist.exists()) {
      this.frontend_fix();
      $('wishlist-link').show();
      link = $$('#wishlist-link a')[0];
      url = '/wishlist?key=' + MyWishlist.key;
      link.setAttribute('href', url);
    }
  },
  // moving up to address mockup requirement 
  // (everywhere but not on homepage)
  frontend_fix : function() {
    if($('home') == null){
      $('membership-box').setStyle({ top: '5px' });
    }
    else{
      $('membership-box').setStyle({ top: '20px' });
    } 
  }

};

