//  hitolog LivedoorProfile feed

(function($) {

  $.hitologLivedoorProfileFeed = function(options) {
    return new HitologLivedoorProfileFeed(options);
  };

  // constructor
  function HitologLivedoorProfileFeed(options) {
    var defaults = {
      apiUrl: "http://portal.profile.livedoor.com/api/item/feed",
      numItems: 5,
      retryCount: 10,
      encoding: "utf8",
      type: null
    };
    var options = $.extend(defaults, options);

    this.options = options;
    this.initialize();
  }

  // class implementation
  HitologLivedoorProfileFeed.prototype = {
    initialize: function() {
      var self = this;
      if (!this.options.apiUrl) {
        alert("apiUrl is required.");
        return;
      }
      if (!this.options.feedUrl) {
        alert("feedUrl is required.");
        return;
      }

      // vars
      this.nNumTry = 0;

      // elements
      this.oTitle = $("#" + this.options.titleId);
      this.oContent = $("#" + this.options.contentId);
      this.oEntryList = $("#" + this.options.entryListId);

      // get feed
      this.getFeed();
    }
    ,

    // methods
    getFeed: function(url) {
      var self = this;
      var url = this.options.apiUrl;
      var inData = {
        url: this.options.feedUrl,
        row: this.options.numItems,
        oe: this.options.encoding
      };
      $.ajax({
        url: url,
        data: inData,
        dataType: "jsonp",
        jsonp: "callback",
        success: function(outData, textStatus) { self.onGetFeedSuccess(url, inData, outData, textStatus); }
      });
    }
    ,
    onGetFeedSuccess: function(url, inData, outData, textStatus) {
      var self = this;
      this.nNumTry++;
      if (outData.status == "fail" || this.nNumTry > this.options.retryCount) {
        // error
        return;
      } else if (outData.status == "wait") {
        // retry
        window.setTimeout(function() { self.getFeed(); }, outData.waittime);
        return;
      }
      this.renderTitle(this.oTitle, outData.title, outData.link);
      this.renderEntries(this.oEntryList, outData.entries);
    }
    ,
    renderTitle: function(oTitle, title, link) {
      if (!oTitle) {
        return;
      }
      oTitle
      .text(title)
      .attr("href", link);
    }
    ,
    renderEntries: function(oEntryList, entries) {
      for (var i = 0; i < entries.length; i++) {
        var entry = entries[i];
        var title, link, date
        if (this.options.type == "twitterMain") {
          title = entry.title.split(": ")[1];
          link = entry.link;
          date = entry.date ? "20" + entry.date : "";
        } else {
          title = entry.title;
          link = entry.link;
          date = entry.date ? "20" + entry.date : "";
        }
        this.oEntryList.append(
          $("<li />")
          .append(
            $("<a />", {
              href: link,
              text: title
            })
          )
          .append(
            $("<span />", {
              text: date
            })
          )
        );
      }
    }
  }

})(jQuery);

