(function($) {
  
  /* Page Likes */
  $(document).ready(function() {
    
    var pageLikesCookieName = 'pageLikes',
        cookieSeparator = ',',
        pageLikes = biooncIAC.helpers.getCookie(pageLikesCookieName) || "",
        $likePage = $('.thumbs-up');
    
    function addLike($el, increment) {
      
      if (increment) {
        var $likesEl = $el.next(),
            likes = parseInt($likesEl.html(), 10) || 0;
        $likesEl.html(likes + 1);
      }
      
      $el.unbind().click(function() {
        return false;
      }).find('.link-icon').addClass('selected');
    }
    
    // Page rating handler
    $likePage.click(function(e) {
      
      if (pageLikes.indexOf(pageLikeId) > -1) {
        return false;
      }
      
      e.preventDefault();
      e.stopImmediatePropagation();
      
      var $this = $(this);

      if (pageLikes.length > 0) pageLikes += cookieSeparator;
      pageLikes += pageLikeId;
      
      biooncIAC.helpers.setCookie(pageLikesCookieName, pageLikes, 365, '/');
    
      $.post( "/data/iac/like", { 
          page: pageLikeId,
          action: "increment"
        },
        function() {
          addLike($this, true);
        }
      );
    });
    
    if (pageLikes) {
      var eachPageLike = pageLikes.split(cookieSeparator),
      pl = eachPageLike.length; while(pl--) {
        if (pageLikeId === eachPageLike[pl]) {
          addLike($likePage, false);
          break;
        }
      }
    }
    
  });
  
  
  /* Both link and page like could be refactored to be much more DRY but don't have the time right now */
  
  /* Link likes */
  $(document).ready(function() {
    
    var linkLikesCookieName = 'linkLikes',
        cookieSeparator = ',',
        linkLikes = biooncIAC.helpers.getCookie(linkLikesCookieName) || cookieSeparator,
        $likeLink = $('.link-thumb');
    
    function getLinkId(href) {
      return href.replace(/[^a-zA-Z 0-9]+/g,'').split(' ')[0];
    }
    
    function addLike($el, increment) {
      if (increment) {
        var $likesEl = $el.next(),
            likes = parseInt($likesEl.html(), 10) || 0;
        $likesEl.html(likes + 1);
      }
      
      $el.unbind().click(function() {
        return false;
      }).find('span').addClass('selected');
    }
  
    // External links rating handler
    $likeLink.click(function(e) {
      
      e.preventDefault();
      e.stopImmediatePropagation();
      
      var $this = $(this),
          linkId = getLinkId($this.prev('a').attr('href'));
      
      if (linkLikes.indexOf(cookieSeparator+linkId+cookieSeparator) > -1) {
        return false;
      }
      
      linkLikes += linkId + cookieSeparator;
      
      biooncIAC.helpers.setCookie(linkLikesCookieName, linkLikes, 365, '/');
      
      $.ajax({
        type: "POST",
        url: "/data/iac/like",
        data: { 
          page: linkId,
          action: "increment"
        },
        success: function() {
          addLike($this, true);
        },
        error: function(jqXHR, textStatus, errorThrown) {
          if (errorThrown === 'Unable_to_send_duplicate_request') {
            addLike($this, false);
          }
        }
      });
      
    }).each(function() {
      
      var $this = $(this),
          linkId = getLinkId($this.parent().find('.third-party').attr('href'));
          
      if (linkLikes.indexOf(cookieSeparator+linkId+cookieSeparator) > -1) {
        addLike($this, false);
      }
      
    });
  
  });

})(jQuery);
