<script type='text/javascript'>
// This file is part of the Neo template. As I spent quite some time developing
// this code, please give proper credits to Hackosphere if you are going to
// re-use the code.
// - Ramani@hackosphere

var postTitle = new Array();
var postContent = new Array();
var postId = new Array();
var postDate = new Array();
var postPermalink = new Array();
var commentAuthor = new Array();
var commentContent = new Array();
var commentDate = new Array();
var blogid, blogurl, totalComments;
var monthnames = new Array();
var startIndex = 1;
var oldestFirstOrder = 0;

function dateString(rawdate, needYear) {
   monthnames[1] = "January";
   monthnames[2] = "February";
   monthnames[3] = "March";
   monthnames[4] = "April";
   monthnames[5] = "May";
   monthnames[6] = "June";
   monthnames[7] = "July";
   monthnames[8] = "August";
   monthnames[9] = "September";
   monthnames[10] = "October";
   monthnames[11] = "November";
   monthnames[12] = "December";
   var year = rawdate.substring(0,4);
   var month = rawdate.substring(5,7);
   var day = rawdate.substring(8,10);
   str = monthnames[parseInt(month,10)] + ' ' + day;
   if (needYear) str = str + ', ' + year;
   return (str);
}

function updateLabelPosts(json) {
  feedid = json.feed.id.$t;
  start = feedid.indexOf('blog-');
  blogid = feedid.substr(start+5);

  for (var k = 0; k &lt; json.feed.link.length; k++) {
      if (json.feed.link[k].rel == 'alternate') {
        alturl = json.feed.link[k].href;
        break;
      }
  }
  index = alturl.lastIndexOf('/');
  var label = alturl.substr(index+1);
  label = decodeURIComponent(label);

  var labelposts = document.getElementById("LabelPosts");
  var labeltitle = document.getElementById("LabelTitle");

  if (label == "") {
    labeltitle.innerHTML = '&lt;center&gt;&lt;h2&gt;Recent Posts&lt;/h2&gt;&lt;/center&gt;&lt;hr/&gt;';
  } else {
    labeltitle.innerHTML = '&lt;h2&gt;Posts labelled "' + label + '"&lt;/h2&gt;';
    labeltitle.innerHTML = labeltitle.innerHTML + '&lt;img style="border: 0pt none;" src="http://www.feedburner.com/fb/images/pub/feed-icon16x16.png"/&gt;&amp;nbsp;';
    labeltitle.innerHTML = labeltitle.innerHTML + '&lt;a href="' + blogurl + 'feeds/posts/default/-/' + label + '"&gt;Subscribe&lt;/a&gt;&lt;hr/&gt;';
  }
  var disp = '';

  for (var i = 0; i &lt; json.feed.entry.length; i++) {
    var entry = json.feed.entry[i];
    var selfurl;
    postTitle[i] = entry.title.$t;
    postContent[i] = entry.content.$t;
    postDate[i] = entry.published.$t.substring(0,10);
    entryid = entry.id.$t;
    start = entryid.indexOf('post-');
    postId[i] = entryid.substr(start+5);

    for (var k = 0; k &lt; entry.link.length; k++) {
      if (entry.link[k].rel == 'alternate') {
        postPermalink[i] = entry.link[k].href;
        break;
      }
    }

    disp = disp + '&lt;a href="javascript:updatePost(' + i + ');"&gt;' + entry.title.$t + '&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;';
  }
  if (label == "index.html") label = "";
  disp = disp + '&lt;p class="navLinks"&gt;';
  if (startIndex != 1) {
    disp = disp + '&lt;a href="javascript:fetchNewerPosts(\'' + label + '\');"&gt;&lt;&lt;&amp;nbsp;Newer&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;';
  }

  if (json.feed.entry.length == numPosts) {
    disp = disp + '&lt;a href="javascript:fetchOlderPosts(\'' + label + '\');"&gt;Older&amp;nbsp;&gt;&gt;&lt;/a&gt;';
  }
  disp = disp + '&lt;/p&gt;';
  labelposts.innerHTML = disp;
}

function updatePost(index) {
   var datediv = document.getElementById("PostDate");
   var titlediv = document.getElementById("PostTitle");
   var contentdiv = document.getElementById("PostContent");
   var editdiv = document.getElementById("PostEditLink");
   var commentdiv = document.getElementById("PostCommentLink");

   datediv.innerHTML = dateString(postDate[index], 1);
   titlediv.innerHTML = '&lt;a href="' + postPermalink[index] + '"&gt;' + postTitle[index] + '&lt;/a&gt;';
   contentdiv.innerHTML = '&lt;p&gt;' + postContent[index] + '&lt;/p&gt;&lt;div style="clear: both;"&gt;&lt;/div&gt;';
   if (editdiv)
     editdiv.innerHTML = '&lt;a href="http://www2.blogger.com/post-edit.g?blogID=' + blogid + '&amp;postID=' + postId[index] + '"&gt;&lt;span class="quick-edit-icon"&gt;&amp;#160;&lt;/span&gt;&lt;/a&gt;';
   commentdiv.innerHTML = '&lt;a target="_blank" href="http://www2.blogger.com/comment.g?blogID=' + blogid +'&amp;postID=' + postId[index] + '"&gt;Post a Comment&lt;/a&gt;';

   var backlinksdiv = document.getElementById("backlinks-container");
   backlinksdiv.innerHTML = '';

   //var postdiv = document.getElementById("post");
   //postdiv.scrollIntoView(true);
   checkFullNeo();
   fetchComments(postId[index]);
}

function updateComments(json) {
  if (json.feed.entry) {   
    for (var i = 0; i &lt; json.feed.entry.length; i++) {
      var entry = json.feed.entry[i];
      commentAuthor[i] = entry.author[0].name.$t;
      commentContent[i] = entry.content.$t;
      commentDate[i] = entry.published.$t.substring(0,10);
    }
    totalComments = json.feed.entry.length;
  } else totalComments = 0;

  updateCommentsPage(0);
}

function showNextPage(page) {
  updateCommentsPage(page);
  var commentdiv = document.getElementById('comments');
  commentdiv.scrollIntoView(true);
}

function updateCommentsPage(page) {
  var commentdiv = document.getElementById('comments');

  if (totalComments == 0) {
      commentdiv.innerHTML = "&lt;center&gt;&lt;h2&gt;No comments yet&lt;/h2&gt;&lt;/center&gt;";
      return;
  }
  disp = "&lt;center&gt;&lt;h2&gt;" + totalComments + " comments";

  var numpages = (totalComments / 10);
  if (numpages &gt; 1) {
     disp = disp + ",&amp;nbsp;Pages:&amp;nbsp;"
     for (i = 0; i &lt; numpages; i++) {
       if (i == page)
          disp = disp + (i+1) + "&amp;nbsp;&amp;nbsp;";
       else
          disp = disp + '&lt;a href="javascript:updateCommentsPage(' + i + ');"&gt;' + (i+1) + '&lt;/a&gt;&amp;nbsp;&amp;nbsp;';
     }
  }

  disp = disp + "&lt;/h2&gt;&lt;/center&gt;&lt;dl class='comments-block' id='comments-block'&gt;";

if (oldestFirstOrder == 1) {
  max = totalComments - 1 - (page * 10);
  min = totalComments - 1 - ((page+1) * 10);
  if (min &lt; 0)
      min = 0;
  for (var i = max; i &gt;= min; i--) {
      disp = disp + "&lt;dt class='comment-author'&gt;" + "On " + dateString(commentDate[i], 0) + ", " + commentAuthor[i] + " said...&lt;/dt&gt;";
      if (commentAuthor[i] == blogAuthor) {
          disp = disp + "&lt;dd class='comment-body-author'&gt;" + commentContent[i] + "&lt;/dd&gt;&lt;br/&gt;";
      } else {
          disp = disp + "&lt;dd class='comment-body'&gt;" + commentContent[i] + "&lt;/dd&gt;&lt;br/&gt;";
      }
  }
} else { 
  max = (page + 1) * 10;
  if (max &gt; totalComments)
      max = totalComments;
  for (var i = page * 10; i &lt; max; i++) {
      disp = disp + "&lt;dt class='comment-author'&gt;" + "On " + dateString(commentDate[i], 0) + ", " + commentAuthor[i] + " said...&lt;/dt&gt;";
      if (commentAuthor[i] == blogAuthor) {
          disp = disp + "&lt;dd class='comment-body-author'&gt;" + commentContent[i] + "&lt;/dd&gt;&lt;br/&gt;";
      } else {
          disp = disp + "&lt;dd class='comment-body'&gt;" + commentContent[i] + "&lt;/dd&gt;&lt;br/&gt;";
      }
  }
}
  disp = disp + "&lt;/dl&gt;";

  if (page &lt; (numpages-1)) {
    disp = disp + '&lt;center&gt;&lt;h3&gt;&lt;a href="javascript:showNextPage(' + (page+1) + ');"&gt;Next comments page&gt;&gt;&lt;/a&gt;&lt;/h3&gt;&lt;/center&gt;&lt;br/&gt;';
  }

  commentdiv.innerHTML = disp;
}
function fetchOlderPosts(label) {
   startIndex = startIndex + numPosts;
   fetchPosts(label);
   document.getElementById('LabelDisplay').scrollIntoView(true);
}

function fetchNewerPosts(label) {
   startIndex = startIndex - numPosts;
   fetchPosts(label);
   document.getElementById('LabelDisplay').scrollIntoView(true);
}

function fetchLatestPosts(url, label) {
   blogurl = url;
   startIndex = 1;
   fetchPosts(label);
}

function fetchPosts(label) {
  var labelposts = document.getElementById("LabelPosts");
  var labeltitle = document.getElementById("LabelTitle");
  labeltitle.innerHTML = "&lt;h2&gt;&lt;img alt='Indicator' src='http://bloggerhacked.googlepages.com/indicator.gif'/&gt;&amp;nbsp;&amp;nbsp;Updating...&lt;/h2&gt;&lt;hr/&gt;";

  if (label == '') {
     feedurl = blogurl + 'feeds/posts/default?orderby=published&amp;start-index=' + startIndex + '&amp;max-results=' + numPosts + '&amp;alt=json-in-script&amp;callback=updateLabelPosts';
  } else {
     label = label.replace(" ", "%20");
     feedurl = blogurl + 'feeds/posts/default/-/' + label + '?orderby=published&amp;start-index=' + startIndex + '&amp;max-results=' + numPosts + '&amp;alt=json-in-script&amp;callback=updateLabelPosts';
  }

  var script = document.createElement('script');
  script.setAttribute('src', feedurl);
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

function fetchComments(postid) {
  var script = document.createElement('script');
  script.setAttribute('src', blogurl + 'feeds/' + postid + '/comments/default?max-results=100&amp;alt=json-in-script&amp;callback=updateComments');
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}


function showFullNeo() {
	var showspan = document.getElementById('showlink');
	var hidespan = document.getElementById('hidelink');
	var fullspan = document.getElementById('fullpost');
        showspan.style.display = 'none';
        hidespan.style.display = 'inline';
        fullspan.style.display = 'inline';
}
function hideFullNeo() {
	var showspan = document.getElementById('showlink');
	var hidespan = document.getElementById('hidelink');
	var fullspan = document.getElementById('fullpost');
        showspan.style.display = 'inline';
        hidespan.style.display = 'none';
        fullspan.style.display = 'none';
        var post = document.getElementById('post');
        post.scrollIntoView(true);
}
function checkFullNeo() {
	var showspan = document.getElementById('showlink');
	var hidespan = document.getElementById('hidelink');
        var fullpost = document.getElementById('fullpost');
        if (fullpost) {
            showspan.style.display = 'inline';
            hidespan.style.display = 'none';
        } else {
            showspan.style.display = 'none';
            hidespan.style.display = 'none';
        }
}
</script>