// Create the blogger service object
var bloggerService = new google.gdata.blogger.BloggerService('GoogleInc-jsguide-1.0');

// number of posts to be displayed per page
var postsPerPage = 5;

// The feed URI ued to retrieve a list of blogs for a particular logged-in user
//var feedUri = 'http://moofius.blogspot.com/feeds/posts/default';
var feedUri = 'http://valueu.blogspot.com/feeds/posts/default';

// The callback method invoked when getBlogFeed() returns the list of our blogs
var handleBlogFeed = function(blogFeedRoot) {
  var blogEntries = blogFeedRoot.feed.getEntries();
  
  bloggerService.getBlogPostFeed(feedUri, handleBlogPostFeed, handleError);
};

var posts;
var currentPost = 0;
var blogid;

// A callback method invoked getBlogPostFeed() returns data
var handleBlogPostFeed = function(postsFeedRoot) {
  posts = postsFeedRoot.feed.getEntries();
  blogid = postsFeedRoot.feed.getId().getValue();
  
  processCurrentPageView();
};

var handleError = function(error) {
  alert(error);
};

function getMyBlogFeed()
{
    bloggerService.getBlogFeed(feedUri, handleBlogFeed, handleError);
}

var handleCommentPosts = function(feedRoot) 
{
    var feed = feedRoot.feed;
    var entries = feed.entry;
    
    // count number of comments
    var count = entries.length;
    //alert (count);
    
    var postID = feed.getId().getValue();

    postID = extractPostId(postID);
    
    //alert ( postID );
    document.getElementById('post'+postID).innerHTML = count + ' Comments';

};

function extractPostId(inPostId)
{
    var t1 = inPostId.substring(inPostId.lastIndexOf('post')+4);
    var t2 = t1.substring(0,t1.indexOf('.'));
    
    return t2;
}

function extractId(inBlogId)
{
    return inBlogId.substring(inBlogId.lastIndexOf('-')+1);
}

function viewPost(i)
{
    var postTitle = posts[i].getTitle().getText();
    var postURL = posts[i].getHtmlLink().getHref();
    var postContent = posts[i].getContent().getText();
    var postID = posts[i].getId().getValue();
    
    blogid = extractId(blogid);
    postID = extractId(postID);
    
    //alert (postID);
    
    var commentFeedUri = 'http://www.blogger.com/feeds/'+blogid+'/'+postID+'/comments/default';
    
    var postCommentLink = 'http://www.blogger.com/comment.g?blogID='+blogid+'&postID='+postID+'&isPopup=true';
    
    bloggerService.getPostCommentFeed(commentFeedUri, handleCommentPosts, handleError);
    
    var comments = '<a class="comment-link" href="http://www.blogger.com/comment.g?blogID=' + blogid + '&postID=' + postID + 'isPopup=true"  onclick="window.open(\'http://www.blogger.com/comment.g?blogID=' + blogid+ '&postID=' + postID + '&isPopup=true\', \'bloggerPopup\', \'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=400,height=450\');return false;"><span id="post' + postID + '" style="">0 Comments</span></a> <a class="comment-link" href="http://www.blogger.com/comment.g?blogID=' + blogid + '&postID=' + postID + 'isPopup=true"  onclick="window.open(\'http://www.blogger.com/comment.g?blogID=' + blogid+ '&postID=' + postID + '&isPopup=true\', \'bloggerPopup\', \'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=400,height=450\');return false;"><span id="post2' + postID + '" style="">Post Comment</span></a><br /><br/>';
    
    var retval = "<b><a href=\"" + postURL + "\" target=\"_blank\">" + postTitle + "</a></b><br /><p>" + postContent + "</p><p><div class='comments'>"+comments+"</div></p>";
    
    return retval;
}

function processCurrentPageView()
{
    var feedcontent = "";
    
    var navigation = "";
    
    for( var i=currentPost*postsPerPage; i < (currentPost+1)*postsPerPage && i < posts.length; i++ )
    {
        feedcontent += viewPost(i);
    }
    
    if(currentPost == 0 && postsPerPage < posts.length) 
    {
        // first post
        navigation = "<a href='' onclick='viewOlder();return false;'>View Older</a>";
    }
    else if (currentPost == 0 && postsPerPage >= posts.length)
    {
        // only post
    }
    else if (currentPost != 0 && currentPost*postsPerPage >= posts.length-1-postsPerPage)
    {
        // last post
        navigation = "<a href='' onclick='viewNewer(); return false;'>View Newer</a>";
    }
    else
    {
        // everything else
        navigation = "<a href='' onclick='viewOlder(); return false;'>View Older</a> | <a href='' onclick='viewNewer();return false;'>View Newer</a>";
    }
    
    document.getElementById('FeedContent').innerHTML = feedcontent+navigation;
    getArchiveInfo();
}

function viewOlder()
{
    currentPost++;
    processCurrentPageView();
}

function viewNewer()
{
    currentPost--;
    processCurrentPageView();
}

function getArchiveInfo()
{
    var archivetext = "<div id='Topbar'><h3>Previous Posts</h3></div><div id='MiddleArea'>";
    
     for(var i=0; i < posts.length && i < 10; i++)
     {
        var postTitle = posts[i].getTitle().getText();
        var postURL = posts[i].getHtmlLink().getHref();
        
        archivetext += "<a href=\"" + postURL + "\" target=\"_blank\">" + postTitle + "</a><br />";
     }
     
     archivetext += "<br /><hr /><h3>Archive</h3><br />";
     
     // "http://www.asklilach.co.uk/2007_04_01_archive.html"
    
    var previousmonth = 0;
    var previousyear = 0;
    
    for(var i=0; i < posts.length; i++)
    {
        var postdate = posts[i].getPublished().getValue().getDate();
        
        if(previousmonth != postdate.getMonth() && previousyear != postdate.getFullYear())
        {
            previousmonth = postdate.getMonth();
            previousyear = postdate.getFullYear();
            
            archivetext += "<a href='http://valueu.blogspot.com/"+previousyear+"_"+(previousmonth+1)+"_01_archive.html'>" + getMonthName(previousmonth) + " " + previousyear + "</a><br/>";
        }        
        //alert (postdate.getDate() + "/" + (postdate.getMonth()+1) +"/" + postdate.getFullYear());
    }
    
    document.getElementById('ArchiveContent').innerHTML = archivetext + "</div><div id='Bottombar'></div>";
}

function getMonthName(month)
{
    if(month == 0)
    {
        return "January";
    }
    else if(month == 1)
    {
        return "February";
    }
    else if(month == 2)
    {
        return "March";
    }
    else if(month == 3)
    {
        return "April";
    }
    else if(month == 4)
    {
        return "May";
    }
    else if(month == 5)
    {
        return "June";
    }
    else if(month == 6)
    {
        return "July";
    }
    else if(month == 7)
    {
        return "August";
    }
    else if(month == 8)
    {
        return "September";
    }
    else if(month == 9)
    {
        return "October";
    }
    else if(month == 10)
    {
        return "November";
    }
    else if(month == 11)
    {
        return "December";
    }
    else
    {
        return "";
    }
}