function profile_load() {
  jar = new CookieJar({expires: '', path: '/'});
  var profile = jar.get('profile');
  if (profile == null) {
  	profile = {
  		poemIDs: [],
  		rate: [],
  	};
  }
  return profile;
}
function profile_save(profile) {
  jar.put('profile', profile);
}

function toggleClipboard(value) {
  div = 'clipboard' + value;
  $(div).innerHTML = 'Saving...';

  var profile = profile_load();
  if (profile.poemIDs.include(value)) {
  	profile.poemIDs = profile.poemIDs.without(value);
    $(div).innerHTML = '<div style="margin-bottom: 6px;"><a href="javascript:toggleClipboard(\''+ value +'\')"><img src="/i/buttons/add_poem.gif"/></a></div><div style="margin-bottom: 6px;">Removed from poem book</div>';
  } else {
  	profile.poemIDs.push(value);
    $(div).innerHTML = '<div style="margin-bottom: 6px;"><a href="javascript:toggleClipboard(\''+ value +'\')"><img src="/i/buttons/remove_poem.gif"/></a></div><div style="margin-bottom: 6px;">Added to your poem book!</div>';
  }
  profile_save(profile);
}
function renderClipboard(value) {
  var profile = profile_load();
  var ui = '<div style="margin-bottom: 6px;"><a href="/profile/clipboard"><img width="126" height="19" alt="My poem book" src="/i/type/my_poem_book.gif"/></a></div>';
  ui += '<div id="clipboard'+ value +'" style="margin-bottom: 20px;">';
  if (profile.poemIDs.include(value)) {
    ui += '<div style="margin-bottom: 6px;"><a href="javascript:toggleClipboard(\''+ value +'\')"><img src="/i/buttons/remove_poem.gif"/></a></div>';
  } else {
    ui += '<div style="margin-bottom: 6px;"><a href="javascript:toggleClipboard(\''+ value +'\')"><img src="/i/buttons/add_poem.gif"/></a></div>';
  }
  ui += '</div>';
  document.write(ui);
}

