function onLinkedInLoad() {
    IN.Event.on(IN, "auth", onLinkedInAuth);
}
function onLinkedInAuth() {

    if ($('#linkedIn-latest-update-middle').length != 0) {
        var posts = [];

        IN.API.Raw("/groups/2253610/posts:(site-group-post-url,relation-to-viewer:(is-liked),creation-timestamp,id,title,summary,creator:(id,first-name,last-name,picture-url,headline))?category=discussion&order=recency&count=4")
      .result(function peopleSearchCallback(result) {
          $.each(result.values, function () {
              var post = {
                  firstName: '',
                  lastName: '',
                  creatorId: '',
                  picture: '',
                  postId: '',
                  title: '',
                  timeSinceAdded: '',
                  postLink: '',
                  canLiked: true,
                  likeLabel: 'Like'
              };

              post.firstName = this.creator.firstName;
              post.lastName = this.creator.lastName;
              post.creatorId = this.creator.id;
              if (this.creator.pictureUrl) {
                  post.picture = this.creator.pictureUrl;
              }
              post.postId = this.id;
              post.title = this.title;
              post.timeSinceAdded = relativeTime(this.creationTimestamp);
              post.postLink = this.siteGroupPostUrl;
              if (this.relationToViewer.isLiked) {
                  post.canLiked = false;
                  post.likeLabel = 'Unlike';
              }

              posts.push(post);
          });

          $('#linkedIn-latest-update-middle').html('');
          $('#LinkedInPostTpl').tmpl(posts).appendTo('#linkedIn-latest-update-middle');
          $('#linkedIn-latest-update-middle').append('<div class="seeAllLink"><a href="http://www.linkedin.com/groups?updates=&gid=2253610&updatesFrom=all&goback=%2Eanp_2253610_1320246476508_1" target="_blank">See All Updates<span class="linkIcon"></span></a></div>');
      });

    }
}

function LikePost(postId,needToLike) {
    IN.API.Raw("/posts/" + postId + "/relation-to-viewer/is-liked")
       .method("PUT")
    .body(JSON.stringify(needToLike))
    .result(function () {
        onLinkedInAuth();
    })
    .error(function error(e) {
        
    });
}

function onLinkedInSignout() {
}

function relativeTime(time) {

    // Adapted from James Herdman's http://bit.ly/e5Jnxe

    var period = new Date(time);
    var delta = new Date() - period;

    if (delta <= 10000) {	// Less than 10 seconds ago
        return 'Just now';
    }

    var units = null;

    var conversions = {
        millisecond: 1, 	// ms -> ms
        second: 1000, 	// ms -> sec
        minute: 60, 		// sec -> min
        hour: 60, 		// min -> hour
        day: 24, 		// hour -> day
        month: 30, 		// day -> month (roughly)
        year: 12			// month -> year
    };

    for (var key in conversions) {
        if (delta < conversions[key]) {
            break;
        }
        else {
            units = key;
            delta = delta / conversions[key];
        }
    }

    // Pluralize if necessary:

    delta = Math.floor(delta);
    if (delta !== 1) { units += 's'; }
    return [delta, units, "ago"].join(' ');

}
  


