var feeds = new Array();
var currentFeedTimestamp;
var noPush = false;
var skipRemove = false;
var feedUrl = "http://vitochin.blogspot.com/feeds/posts/default";

function putFeedInto(a) {
    var c = "<br><div id='attention'>☃<br><div class = 'back'>~ <span id = 'latestlink'>latest</span> ~</span></div>";
    if (a == "leftbar") {
        currentFeedTimestamp = Date.parse(currentFeedTimestamp).addHours(-1).toString("yyyy-MM-ddTHH:mm:ss");
        $.getJSON(feedUrl + "?max-results=1&orderby=published&published-max=" + currentFeedTimestamp + "&alt=json-in-script&jsoncallback=?");
        if (feeds.length == 0) {
            navigationBar("#rightbar");
            skipRemove = true
        }
    } else {
        if (a == "rightbar") {
            feeds.pop();
            var d = feeds.pop();
            var b = d;
            currentFeedTimestamp = Date.parse(b).addHours(1).toString("yyyy-MM-ddTHH:mm:ss");
            if (feeds.length == 0) {
                c = ""
            }
            $.getJSON(feedUrl + "?max-results=1&orderby=published&published-max=" + currentFeedTimestamp + "&alt=json-in-script&jsoncallback=?")
        } else {
            if (a == "back") {
                noPush = true;
                if (currentFeedTimestamp != null) {
                    if (currentFeedTimestamp.substring(0, 1) == "#") {
                        currentFeedTimestamp = currentFeedTimestamp.substring(1)
                    }
                    if (feeds.length == 1) {
                        c = ""
                    }
                    currentFeedTimestamp = Date.parse(currentFeedTimestamp).addHours(1).toString("yyyy-MM-ddTHH:mm:ss");
                    $.getJSON(feedUrl + "?max-results=1&orderby=published&published-max=" + currentFeedTimestamp + "&alt=json-in-script&jsoncallback=?")
                } else {
                    $.getJSON(feedUrl + "?max-results=1&alt=json-in-script&jsoncallback=?")
                }
            } else {
                feeds = [];
                c = "";
                $.getJSON(feedUrl + "?max-results=1&alt=json-in-script&jsoncallback=?")
            }
        }
    }
    gdata = {
        io: {
            handleScriptLoaded: function (h) {
                var e = "<h2>" + h.feed.entry[0].title.$t + "</h2>";
                publishedDate = Date.parse(h.feed.entry[0].published.$t.substr(0, 19)).toString("dd MMMM yyyy");
                $(this).css("color", "black");
                publishedDate = "<span class='publishedDate'>Published: " + publishedDate + "</span>";
                var f = h.feed.entry[0].content.$t;
                var g = "";
                if (h.feed.entry[0].link[1].title != "0 Comments") {
                    g = "<br><span id='comm'>COMMENTS</span>";
                    var caction = "display";
                } else {
                    g = "<br><span id='comm'>COMMENT</span>";
                    var caction = "create";
                }
                $("#main").html("<div id='feed'>" + e + publishedDate + f + g + c + "</div>");
		        linkize("#comm");
		        //linkize("#acomm");
                /*
                $("#comm").click(function(){
                    alert("boko");
                    //post_comment(title);
                });
                */
		        comment(e,"#comm",h.feed.entry[0].id.$t.substr(h.feed.entry[0].id.$t.search(/post-/i)+5), caction);
                refreshHeight();
                backButton("#latestlink");
                if (noPush == false) {
                    feeds.push(h.feed.entry[0].published.$t.substr(0, h.feed.entry[0].published.$t.search(/\./)))
                } else {
                    noPush = false
                }
                currentFeedTimestamp = h.feed.entry[0].published.$t.substr(0, h.feed.entry[0].published.$t.search(/\./));
                window.location.hash = currentFeedTimestamp;
                navigationBar("#leftbar");
                navigationBar("#rightbar");
                if (currentFeedTimestamp == "2007-03-03T10:26:00") {
                    removeNavigationBar("#leftbar")
                }
                if (feeds.length > 1) {
                    navigationBar("#rightbar")
                } else {
                    if (skipRemove == true) {
                        skipRemove = false
                    } else {
                        removeNavigationBar("#rightbar")
                    }
                }
            }
        }
    }
}
function loadContent(a) {
    $.post("contents/" + a + ".html", {}, function (c) {
        var b = "<div id='attention'>⟽<br><div class = 'back'>~ <span id = 'backlink'>blog</span> ~</span></div>";
        $("#main").html(c + b);
        refreshHeight();
        backButton("#backlink");
        removeNavigationBar("#leftbar");
        removeNavigationBar("#rightbar");
        window.location.hash = a
    })
}
$(document).ready(function () {
    navigationBar("#leftbar");
    navigationBar("#rightbar");
    if (location.hash == "") {
        putFeedInto()
    } else {
        if (location.hash.match(/[a-z]/) != null) {
            loadContent(location.hash.substring(1))
        } else {
            currentFeedTimestamp = location.hash;
            putFeedInto("back")
        }
    }
    menuBar()
});

function refreshHeight() {
    var a = ($("#main").height() + parseInt($("#thebody #main").css("padding-bottom"))) / 2;
    $("#leftbar").height(a).css("padding-top", a);
    $("#rightbar").height(a).css("padding-top", a)
}
function comment(title,a,b,caction) {
    $(a).click(function () {
        if (caction == "display") {
        $.getJSON("http://www.blogger.com/feeds/1549959395955666242/"+b+"/comments/default?alt=json-in-script&jsoncallback=?");
        gdata = {
            io: {
                handleScriptLoaded: function (h) {
                    var feed = title+"<h3>Comments</h3>";
                    for (i=h.feed.openSearch$totalResults.$t-1;i>-1;i--) {
                        var ca = "<a href='"+h.feed.entry[i].author[0].uri.$t+"'>"+h.feed.entry[i].author[0].name.$t+"</a>: <br>";
                        var cd = Date.parse(h.feed.entry[i].published.$t.substr(0, 19)).toString("dd MMMM yyyy");
                        cd = "<span class='publishedDate'> " + cd + "</span>";
                        var cc = "<p>"+h.feed.entry[i].content.$t+"</p>";
                        feed += "<div class='comment'>"+ca+cd+cc;
                        if (i == 0 ) {
                            feed += "</div>";
                        } else {
                            feed += "<hr></div>";
                        }
                    }
                    feed += "<br><span id='acomm'>ADD COMMENT</span>";
                    feed += "<div id='attention'>⟽<br><div class = 'back'>~ <span id = 'backlink'>back</span> ~</div></div>";
                    $("#main").html(feed);
                    refreshHeight();
                    backButton("#backlink");
    		        linkize("#acomm");
                    $("#acomm").click(function(){
                        post_comment(title);
                    });
                    }
                }
                }
        } else {
            post_comment(title);
        }
    })

}
function post_comment(title) {
    $.get("dynamic/comment.html", function(form){
    var subs = "<input type='hidden' class='comment_entry' value='"+title.replace(/(<([^>]+)>)/ig,"")+"'/>";
    var front = title+"<h3>Add Comment</h3>";
        $("#main").html(front+form+subs);
        refreshHeight();
        backButton("#backlink");
        $(".comment_entry").focus(function(){
            if ($(this).val() == $(this).attr("part")) {
                $(this).val("");
                $(this).css("color", "black");
            }
        });
        $(".comment_entry").blur(function(){
            if ($(this).val() == "") {
                $(this).val($(this).attr("part"));
                $(this).css("color", "gray");
            }
        });
        $("#post_button").click(function() {
            var commentString = "";
            var validSubmission = true;
            $(".comment_entry").each(function(key, value) {
                if (value.value.search(/\|\|/) == -1) {
                    commentString += value.value+"||";
                } else {
                    validSubmission = false;
                }
            });
            if (validSubmission) {
                $("#comment_console").html("Post triggered");
                $.post("dynamic/comment/submit", {"commentEntry": commentString}, function(data){
                    $("#comment_console").html(data);
                });
            } else {
                $("#comment_console").html("The | character is not allowed");
                    validSubmission = true;
            }
        });
    });
}
function navigationBar(a) {
    $(a).unbind("click").unbind("mouseover").unbind("mouseout");
    linkize(a);
    $(a).click(function () {
        putFeedInto($(this).attr("id"))
    });
    $(a).css({
        cursor: "pointer",
        opacity: 1
    })
}
function menuBar() {
    var a = new Array(".menuitem");
    for (key in a) {
        linkize(a[key]);
        $(a[key]).click(function () {
            loadContent($(this).attr("id"))
        })
    }
}
function backButton(a) {
    var b = new Array(a);
    for (key in b) {
        $(b[key]).unbind("click").unbind("mouseover").unbind("mouseout");
        linkize(b[key]);
        $(b[key]).click(function () {
            if (a == "#latestlink") {
                putFeedInto()
            } else {
                putFeedInto("back")
            }
        })
    }
}
function linkize(a) {
    $(a).mouseover(function () {
        $(this).css("color", "green")
    }).mouseout(function () {
        $(this).css("color", "black")
    })
}
function removeNavigationBar(a) {
    $(a).css({
        cursor: "default",
        opacity: 0
    });
    $(a).unbind("click")
}; 
