/*
 * Copyright 2012 Gerwin Sturm, FoldedSoft e.U., http://www.foldedsoft.at/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Date.prototype.nice_date = function () {
  "use strict";
  var y, m, d, h, min, sec;
  y = this.getFullYear().toString();
  m = (this.getMonth() + 1).toString();
  d  = this.getDate().toString();
  h = this.getHours().toString();
  min = this.getMinutes().toString();
  sec = this.getSeconds().toString();
  return y + "-" + (m[1] ? m : "0" + m[0]) + "-" + (d[1] ? d : "0" + d[0]) + " " + (h[1] ? h : "0" + h[0]) + ":" + (min[1] ? min : "0" + min[0]) + ":" + (sec[1] ? sec : "0" + sec[0]);
};

$(document).ready(function () {
  "use strict";
  if ($("#comments").length > 0) {
    var PAGE_ID, API_KEY, url, links, i, request;
    PAGE_ID = "105664891782412134818";
    API_KEY = "AIzaSyBsanfIBvMCrWXzLAOlC3XaQYfpVuL4sIA";

    url = "";
    links = document.getElementsByTagName("link");
    for (i = 0; i < links.length; i++) {
      if (links[i].getAttribute("rel") === "canonical") {
        url = links[i].getAttribute("href");
      }
    }
    if (url === "") {
      url = window.location.toString();
    }
    request = "https://www.googleapis.com/plus/v1/people/" + PAGE_ID + "/activities/public?maxResults=100&key=" + API_KEY;
    $.get(request, function (resp) {
      var activity_id, activity_url, a, i, chk_found, att, request;
      activity_id = "";
      activity_url = "";
      if (resp.items) {
        for (i = 0; i < resp.items.length; i++) {
          if (resp.items[i].object.attachments) {
            for (a = 0; a < resp.items[i].object.attachments.length; a++) {
              att = resp.items[i].object.attachments[a];
              if (att.objectType === "article") {
                if (att.url === url) {
                  activity_id = resp.items[i].id;
                  activity_url = resp.items[i].url;
                  break;
                }
              }
            }
          }
          if (activity_id !== "") {
            break;
          }
        }
      }
      if (activity_id !== "") {
        $("#comments").html("<div id=\"comments_link\"></div><div id=\"comments_comments\"></div>");
        $("#comments_link").html("You can leave comments on <a href=\"" + activity_url + "\">this Google+ post</a>.<hr>");
        request = "https://www.googleapis.com/plus/v1/activities/" + activity_id + "/comments?maxResults=100&key=" + API_KEY;
        $.get(request, function (resp) {
          var i, post_time, item;
          if (resp.items) {
            for (i = 0; i < resp.items.length; i++) {
              item = resp.items[i];
              post_time = new Date(item.published);
              $("#comments_comments").append(
                "<p class=\"smalll\">"
                  + "<img src=\"" + item.actor.image.url + "\" style=\"width: 20px; height:20px;\"> "
                  + "<a href=\"" + item.actor.url + "\">" + item.actor.displayName + "</a>"
                  + " - " + post_time.nice_date() + "</p>\n"
              );
              $("#comments_comments").append(resp.items[i].object.content + "<br><br>\n");
            }
          }
        }, "jsonp");
      }
    }, "jsonp");
  }
});
