// like the poem
function addLike(poemId)
{
    // if the user is not logged in show a message
    // make the Ajax request
    url = baseUrl + '/ajax/poem/like/format/json';
    params = {poemId: poemId};
    
    new Ajax.Request(url, {
   parameters: params,
   onCreate: function(){

       // disable the submit button
       $('like-text').update('Sending...');    
   },
   onSuccess: function(transport) {
   
       var res = transport.responseJSON;
       
       if(res.status == 'success')
       {
           // the user is added to add them to the DOM
           $('addLikes').insert({bottom: '<li id="likeId-'+res.likeId+'"><a href=""><div class="roundbox"><div class="ltop"></div><div class="rtop"></div><a href="#"><img src="'+baseUrl + '/pic/'+res.username+'/s" ></a><div class="lbtm"></div><div class="rbtm"></div></div><!--End .roundbox--> </a> <span class="username"><a href="#">'+res.username+'</a></span></li>'});
       
           $('likeText').update(res.likeText);
           
           // change the button
           $('button-like').update('<span class="unlike-it-text" onclick="javascript:unlike('+ poemId +')" id="like-text">Unlike this!</span>');
           
       }
       else
       {
           // tell them to log in
           $('likeText').update(pleaseLogInMessage());
           
           // change the button back
           $('button-like').update('<span onclick="javascript:addLike('+poemId+')" class="like-it-text" id="like-text">Like this!</span>');          
       }
          },
          method: 'post'
        });
    
}

function unlike(poemId)
{
 // if the user is not logged in show a message
    // make the Ajax request
    url = baseUrl + '/ajax/poem/unlike/format/json';
    params = {poemId: poemId};
    
    new Ajax.Request(url, {
   parameters: params,
   onCreate: function(){

       // disable the submit button
       $('like-text').update('Sending...');    
   },
   onSuccess: function(transport) {
   
       var res = transport.responseJSON;
       
       if(res.status == 'success')
       {
          // remove the user from the DOM
          $('likeId-'+res.likeId).remove();
          
          // update the likeTex thingy
          $('likeText').update(res.likeText);
          
          // morf the button back to its like state
          $('button-like').update('<span onclick="javascript:addLike('+poemId+')" class="like-it-text" id="like-text">Like this!</span>');
       }
          },
          method: 'post'
        });
}

// show more liknds
var currentOffset = 0;

function showMoreLikes(poemId, likeCount)
{
    // if the user is not logged in show a message
    // make the Ajax request
    currentOffset = currentOffset + 3;
    
    url = baseUrl + '/ajax/poem/get.likes/format/html';
    params = {poemId: poemId, offset: currentOffset};
   
    new Ajax.Request(url, {
   parameters: params,
   onCreate: function(){

   // hide the button if the offset is greater then the amount of likes
   if(currentOffset >= (likeCount - 3))
   {
       // hide the lke count button
       $('show-more-likes').hide();
   }

   },
   onSuccess: function(transport) {
   
       var res = transport.responseText;
       $('addLikes').insert({bottom: res});
       $('see-more-text').update('See more'); 
 

          },
          method: 'post'
        });
}