/*
$Id: ticker-feed.js,v 1.6 2011/08/26 13:33:39 JanaR Exp $

--------------------------------------------------------------------------------
TickerFeed object represents
--------------------------------------------------------------------------------

<![CDATA[

*/

var tf = tickerfeed = {

  /*
    Array of posts. Each post is expected to be an object with properties: link, from, profilePageURL, text
   */
  posts : new Array(),

  account : "",

  setAccount : function(fbAccount) {
    tf.account = fbAccount;
  },

  /*
    Function parses posts from string (JSON format expected) with posts from Facebook and fills posts attribute.
  */
  parsePostsFromFacebookJson : function(fbFeedJsonString) {
    var fbFeedJson = (new Function("return " + fbFeedJsonString))();
    if (typeof fbFeedJson.data == "undefined") {
      console.log("Parsing Facebook posts failed: " + fbFeedJsonString);
      return;
    }
    jQuery.each(fbFeedJson.data, function(i, post) {
        if ((typeof post.id == "undefined") || (typeof post.from == "undefined") ||
          (typeof post.from.name == "undefined") || (typeof post.type == "undefined")) {
          console.log("Post " + i + " is not in expected format, skipping the post.");
          return true;
        }
        postid = post.id;
        //console.log("parsing post with id " + post.id);
        postidIdx = post.id.indexOf("_")
        if (postidIdx > -1) postid = post.id.substring(postidIdx + 1, postid.length);
        
        postText = "";
        if (typeof post.from != "undefined") { //shouldn't happen (if happenes, it is error in FB response)
          if (typeof post.message == "undefined") {
            if (typeof post.name != "undefined") postText = post.name;
            else if (typeof post.caption != "undefined") postText = post.caption;
            else if (typeof post.description != "undefined") postText = post.description;
          }
          else {
            postText = post.message;
          }
          //console.log("postText: " + postText);
          if (postText.length > 0) {
            postObject = {
              link: "http://www.facebook.com/" + tf.account + "?v=wall&story_fbid=" + postid,
              from: post.from.name,
              profilePageURL: "http://www.facebook.com/profile.php?id=" + post.from.id,
              text: postText
            }
            tf.posts.push(postObject);
          }
          else
            console.log("Skipping post " + i + " with id " + postid + " - no text to show returned in API reply.");
        }
    });

	},

	/*
    Function parses posts from string (JSON format expected) with posts from Twitter and fills posts attribute.
    Implement if Twitter should be loaded in the ticker.
  */
	parsePostsFromTwitterJson : function(twitterFeedJsonString) {
     //implement if needed
  },

  /*
    add posts rendered in ul element into div specified by divSel parameter
    @param divSel selector of the div into which posts should be rendered
  */
  renderFeedInDiv : function(divSel, ulId, ulClass) {
    var tickerDiv = jQuery(divSel);
    if (tf.posts.length == 0) return;
    tickerDiv.append("<ul id='" + ulId + "' class='" + ulClass + "'></ul>");
    tickerDiv = jQuery(divSel + " ul");
    jQuery.each(tf.posts, function(i, post) {
       tickerDiv.append("<li><a target='_blank' href='" + post.link + "'><span><span class=\"post-author\">" +
          post.from + ":</span>" + post.text + "</span></a></li>");
    });
  }

}
/* ]]> */
