(function () {
"use strict";

function request(querytext, func, errorfunc) {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function() {
                if (req.readyState !== 4) {
                        return;
                }
                if (req.status !== 200) {
                        errorfunc();
                        return;
                }
                func(req.responseText);
        };
        req.open("GET",querytext);
        req.send();
}

gotonewpost.onclick = function(e) {
        window.location="/newpost.html"+window.location.search
};

document.getElementById("posts").innerHTML='Loading posts...';

var postsarray=[];
var functionsarray=[];
var lastpost=-1;
var toppost=-1;
var postsperpage=5;

function changepost(postnum,data) {
        var p=JSON.parse(data);
        var postdate=new Date(0);
        postdate.setUTCSeconds(parseInt(p.date));
        var header=[];
        var body=[];
        var comments=[];
        header.push('<div class="postheader">');
        header.push('<p class="title"><span class="title">');
        header.push(decodeURIComponent(p.title));
        header.push('</span><span class="postnum">#');
        header.push(postnum);
        header.push('</span></p>');
        header.push('<p class="authorpostnum">Por <span class="author">');
        header.push(p.author);
        header.push('</span> el <span class="date">');
        header.push(postdate.toString());
        header.push('</span></p>');
        header.push('</div>')
        body.push('<div class="postbody">');
        body.push(decodeURIComponent(p.text));
        body.push('</div>');
        comments.push('<div class="comments">');
        for(var i in p.comments) {
                var c=p.comments[i];
                comments.push('<div class="comment">');
                comments.push('<p class="commentheader">');
                var commentdate=new Date(0);
                commentdate.setUTCSeconds(parseInt(c.date));
                comments.push(decodeURIComponent(c.author));
                comments.push(" ");
                comments.push(commentdate.toString());
                comments.push('</p><p>');
                comments.push(decodeURIComponent(c.text));
                comments.push('</p>');
                comments.push('</div>');
        }
        comments.push('<div class="commentbox"><p class="commentbox">');
        comments.push('<input type="text" class="inputcomment" id="comment'+(toppost-postnum)+'" placeholder="Escribir comentario..."/>');
        comments.push('<input type="button" class="inputsendcomment" id="commentbutton'+(toppost-postnum)+'" value="Enviar"/>');
        comments.push('</p></div></div>');
        postsarray[toppost-postnum]='<div class="post">'+header.join("")+body.join("")+'</div>'+comments.join("") /*+JSON.stringify(p) */;
        functionsarray[toppost-postnum]=function() {
                var requri=[];
                requri.push("/newcomment");
                requri.push(window.location.search);
                requri.push("&n="+postnum);
                requri.push("&t=");
                requri.push(encodeURIComponent(document.getElementById("comment"+(toppost-postnum)).value));
                request(requri.join(""),function(r) {
                        refreshpost(postnum);
                },function() {});
        }
}

function clearpost(postnum) {
        postsarray[toppost-postnum]=undefined;
        functionsarray[toppost-postnum]=undefined;
}

function displayposts() {
        var contents=[]
        for(var i=0;i<postsperpage;i++) {
                if(postsarray[i]==undefined)
                        continue;
                contents.push(postsarray[i]);
        }
        if(contents.length==0)
                contents[0]='No posts loaded';
        document.getElementById("posts").innerHTML=contents.join("\n");
        for(var i=0;i<postsperpage;i++) {
                if(functionsarray[i]==undefined)
                        continue;
                document.getElementById("commentbutton"+i).onclick=functionsarray[i];
        }
}

function refreshpost(postnum) {
        request("/getpost"+window.location.search+"&n="+postnum, function(r1) {
                changepost(postnum,r1);
                displayposts();
        },function() {
                clearpost(postnum);
        });
}


request("/lastpost"+window.location.search,function(r) {
        lastpost=r;
        toppost=r;
        for(var i=0;i<postsperpage;i++)
                refreshpost(r-i);
},function() {
        document.getElementById("posts").innerHTML='There are no posts';
});

}());