// Copyright (c) 2007 Stephen Paul Weber
// http://jscripts.ning.com/ids/2941927
//
// Date format modified and source code reformatted by Arthaey Angosii, 2007.

if (typeof(commentPhotoIds) == 'undefined') {
    var commentPhotoIds = [];
}

function toggleitem(postid, linkid, newtxt, displaytype) {
    if (!displaytype) {
        displaytype = 'block';
    }

    var whichpost = document.getElementById(postid);
    if (whichpost.style.display != "none") {
        whichpost.style.display = "none";
    } else {
        whichpost.style.display = displaytype;
    }

    if(linkid) {
        var lnk = document.getElementById(linkid);
        lnk.href = "javascript:toggleitem('" + postid + "','" +
            linkid + "','" + lnk.innerHTML + "');";
        lnk.innerHTML = newtxt;
    }
} // end function toggleitem

var peekaboo_comments_last_author = '';

function peekaboo_comments_author_display(id, authorurl) {
    if (id && authorurl.url) {
        var block = document.getElementById('author-' + id);
        if (block) {
            var name = block.innerHTML;
            block.innerHTML = '<a href="' + authorurl.url + '">' +
                name + '<\/a>';
        }
    }

    commentPhotoIds.push({'id':'commentphoto'+id, 'url':authorurl.url});

    if (peekaboo_comments_last_author == id &&
        typeof(commentPhotoDo) == 'function')
    {
        commentPhotoDo();
    }
    if (peekaboo_comments_last_author == id &&
        typeof(peekaboo_comments_callback) == 'function')
    {
        peekaboo_comments_callback();
    }
} // end function peekaboo_comments_author_display

var peekaboo_comments_blockid = '';
var peekaboo_comments_callback = '';
var peekaboo_comments_maxurl = '';
var peekaboo_comments_max = false;

function peekaboo_comments_display_callback(data) {
  document.getElementById(peekaboo_comments_blockid).innerHTML = '';

  data = data.feed;
  data.items = data.entry;

  if(!data.items) { return; }

  var template = '';

  function zeropad(n) { return n > 9 ? n : '0' + n; }

  data.items.reverse();
  var count = 0;

  for (i in data.items) {
      if (!data.items[i].link) { continue; }

      /*
      // debug statement
      var itemStr = '';
      for (j in data.items[i]) {
          itemStr += "\n - obj[" + j + "]: " + data.items[i][j];
      }
      console.log("data.items[" + i + "]: " + itemStr);
      */

      data.items[i].link = data.items[i].link[0].href;
      data.items[i].updated = data.items[i].updated['$t'];
      data.items[i].content = data.items[i].content['$t'];
      data.items[i].author.name = data.items[i].author[0].name['$t'];

      var tmp = data.items[i].link.split('#');
      data.items[i].link = tmp.join('#c');

      var dt = data.items[i].updated.match(/^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)\.(\d+)([+-]\d+):(\d+)$/);

      if (!dt) {
         dt = data.items[i].updated.match(/^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)\.(\d+)(Z)$/);
      }

      var time = 0;
      if (dt) {
         time = new Date(
            parseInt(dt[1],10),
            parseInt(dt[2],10)-1 /*since January==0 in Date lingo*/,
            parseInt(dt[3],10),
            parseInt(dt[4],10),
            parseInt(dt[5],10),
            parseInt(dt[6],10),
            parseInt(dt[7],10)
         ).getTime();

         if (dt[8] == 'Z') { dt[8] = 0; }
         if (!dt[9])       { dt[9] = 0; }

         // adjust to GMT
         time = time + -(60*60*1000*parseInt(dt[8],10)) +
            -(60*1000*parseInt(dt[9],10));
      }

      time = time - ((new Date()).getTimezoneOffset()*60*1000);

      var MONTH_NAMES = [
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
      ];
      var ftime = new Date(time);

      var timeStr;
      var hours = ftime.getHours();
      if (hours == 12) {
         timeStr = hours + ":" + zeropad(ftime.getMinutes()) + " PM";
      } else if (hours < 12) {
         timeStr = hours + ":" + zeropad(ftime.getMinutes()) + " AM";
      } else {
         timeStr = (hours - 12) + ":" + zeropad(ftime.getMinutes()) + " PM";
      }

      ftime = MONTH_NAMES[ftime.getMonth()] + " " + ftime.getDate() +
         ", " + ftime.getFullYear() + " " + timeStr;

      var out = comment_form_template;
      out = out.replace(/\[\[PERMALINK\]\]/, data.items[i].link);
      out = out.replace(/\[\[DATE\]\]/, ftime);
      out = out.replace(/\[\[UTIME\]\]/, parseInt(time/1000));
      out = out.replace(/\[\[AUTHORNAME\]\]/,
          data.items[i].author.name.replace(/\s+/g, '-'));
      out = out.replace(/\[\[AUTHOR\]\]/,
        '<span id="author-' + tmp[1] + '">' + data.items[i].author.name +
        '<\/span>');
      out = out.replace(/\[\[BODY\]\]/, data.items[i].content);
      out = out.replace(/\[\[CID\]\]/, tmp[1]);
      document.getElementById(peekaboo_comments_blockid).innerHTML += out;

      var theScript = document.createElement("script");
      theScript.type = "text/javascript";
      theScript.src =
        'http://singpolymaplay.ning.com/blogger/url4name.php?xn_auth=no' +
        '&url=' + encodeURIComponent(tmp[0]) +
        '&name=' + encodeURIComponent(data.items[i].author.name) +
        '&callback=peekaboo_comments_author_display' +
        '&parameter=' + encodeURIComponent('"' + tmp[1] + '"');

      document.body.appendChild(theScript);
      peekaboo_comments_last_author = tmp[1];
      count++;

      if (peekaboo_comments_max && count >= peekaboo_comments_max) {
         document.getElementById(peekaboo_comments_blockid).innerHTML +=
            '<br /><a href="' + peekaboo_comments_maxurl +
            '">More &raquo;</a>';
         break;
      }
  } // end for i in data
} // end function peekaboo_comments_display_callback

function peekaboo_comments_display(url, blockid, callback, maxurl, max) {
   if (!url || !blockid) { return; }

   peekaboo_comments_blockid = blockid;
   peekaboo_comments_callback = callback;
   peekaboo_comments_max = max;
   peekaboo_comments_maxurl = maxurl;

   document.getElementById(peekaboo_comments_blockid).innerHTML =
    '<i>Loading comments...</i>';

   var theScript = document.createElement("script");
   theScript.type = "text/javascript";
   theScript.src = url +
    "?alt=json-in-script&callback=peekaboo_comments_display_callback";

   document.body.appendChild(theScript);
} // end function peekaboo_comments_display
