// Overview

// An API has been created for the Scenekunst website in order to get the most commented articles and the latest comments from Facebook. Data from Facebook is fetched every 30 minutes. The API is located at http://scenekunst.livesite.no/api/comments.php

// The API format has been set to JSON and JSONP. JSON can be used when the request is made on the server side, while JSONP is used in client side for cross-domain requests using Javascript. For JSONP, we need to add the jsoncallback param: http://scenekunst.livesite.no/api/comments.php?jsoncallback=?

// The API always return a "response" param which can take the following values:
// * FAIL - When an error has occurred. For example, requesting the API without passing any parameters will return:
// {"response":"FAIL","error":"Param filter is required"}
// OK - When the the API call was successful; it will return an array with the data:
// {"respose":"OK","data":[array]}


// Parameters
// filter - The type of results to show. Can take 2 values:
// top - For most commented articles
// latest - For latest comments
// limit - The number of results to return in the data array. Defaults to 5.
// type - The type of articles to return. Values can be: all, main, review and news. Defaults to all. This can be used to return results depending on the website's section if needed.
// text - Wether to show or not the article's description and message for the comments. Can be true or false. Defaults to false.


// Return data

// top
// id
// type (main, review and news)
// title
// link
// pub_date
// commentsbox_count
// description (when text param is true)

// latest
// id
// type
// title
// link
// pub_date
// commentsbox_count
// user_id
// user_name
// created_time
// description (when text param is true)
// message (when text param is true)


// Examples

// Since the Scenekunst website uses jQuery, $.getJSON can be used to fetch data from the API.

// $.getJSON('http://dev.scenekunst.livesite.no/api/comments.php?jsoncallback=?', { filter: "top" }, function(response) {
 // $.each(response.data, function(i, item) {
   // console.log(item);
 // });
// });

// The above code will get the top 5 commented articles. Item's data can be accessed using item.id, item.title, etc and this can be rendered in HTML in the way they need it. Firebug can be used to inspect item data.

// All dates are returned in Unix timestamp and can be formatted in Javascript using the Date() object, for example:

// var date = new Date(unix_timestamp*1000);
// var hours = date.getHours();
// var minutes = date.getMinutes();
// var seconds = date.getSeconds();
// ...

// Don't forget to multiply by 1000 since dates in Javascript are handled in milliseconds.



$.getJSON('http://scenekunst.livesite.no/api/comments.php?jsoncallback=?', { filter: "latest", limit: 5 }, function(response) {
 $.each(response.data, function(i, item) {
   var title = item.title;
   var link = item.link;
   var navn = item.user_name
   var antall = item.commentsbox_count;
   var html ='<li class="article"><a href="'+link+'" title="'+title+'">'+title+' ('+antall+' innlegg)</a></li>';
   $("#latest .menu-list").append($(html));
 });
});

