// //////////////////////////////////
// jquery is this cool:
//

var you;
var ids;

// more(nl,t) {{{
function more(nl, t) {
    t.find("div.more").html("reloading...");

    $.getJSON("/comments/", { ts: (new Date()).getTime(), mode: 'more', nl: nl }, function(o) {
        draw(t, nl, o.comments, true, $(t.find("div.carea")).height());
    });
}
// }}}
// send(c,nl,t) {{{
function send(c, nl, t) {
    var cv = c.val();

    t.find("div.more").html(cv.length ? "posting..." : "reloading...");

    $.getJSON("/comments/", { ts: (new Date()).getTime(), mode: 'post', c:cv, nl: nl }, function(o) {
        if( o.e )   alert(o.e);
        if( o.ids ) ids = o.ids;

        draw(t, nl, o.comments, true);
    });
}
// }}}
// draw(t,nl,ca) {{{
function draw(t, nl, ca, focus, force_height) {
    t.html("");

    if( ca && ca.length ) {
        t.append('<span style="color: #999">&#8212; comments &#8212;&#8212;&#8212</span>');
        t.append('<div class="carea">');


        var x = $( t.find("div.carea") );

        if( force_height )
            x.css({height: force_height + "px", overflow: 'auto' });

        for(var i=0; i<ca.length; i++) {
            var c = ca[i];
            var s = (c.url === you ? " self" : "");

            x.append(
                '<div class="comment"><div class="auth'+s+'">&lt;<a href="' + c.url + '">' + c.nickname + '</a>&gt</div>'
                + '<div class="ctxt">' + c.comment + '<span class="time"> (' + c.bd + ' ago)</span></div></div>'
            );
        }

        if( force_height )
            x[0].scrollTop = x[0].scrollHeight;

            /*
            ** from #jquery!freenode
            **
            ** [22:19] JohnResig!!: jettero: $("#foo")[0].scrollTop = $("#foo")[0].scrollHeight;
            ** [22:25] jettero: JohnResig wow, thanks
            ** [22:26] _fil_: JohnResig: you could animate() it :p
            ** [22:26] _fil_: too easy lol
            ** [22:26] JohnResig: _fil_: with jQuery 1.2, you can: $("#foo").animate({ scrollTop: $("#foo")[0].scrollHeight }, "slow");
            ** [22:26] _fil_: hey :p
            ** [22:26] jettero: that's too much black magic
            ** [22:26] JohnResig: if you were to check jQuery out of SVN you could do that anow
            ** [22:26] JohnResig: *now
            */

        x.find("div.mbtn").click(function(){ more(nl, t); return false; });
    }

    var cid = "";
    var selfh = t.find("div.self").html();
    if( selfh && selfh.match("incognito") )
        cid = "<div>(change your identity by clicking \"<a href='/id/'>your identity</a>\" under site navagation on the right)</div>";

    t.append('<div class="more">' + ids + cid + '</div>');

    var c = t.find("input[type=text]");
    t.find("input").keypress(function(e) { if( e.keyCode != 13 ) return; send(c, nl, t); });
    t.find("input[type=submit]").click(function() { send(c, nl, t); });

    if( focus )
        t.find("input[type=text]")[0].focus();
}
// }}}
// scrollta  {{{
function scrollta(e) {
    var y = 0;

    for(var node = e; node; node=node.offsetParent)
        y += node.offsetTop;

    return y;
}
// }}}
// document ready {{{
$(document).ready(function() {
    $("div.commentstub").each(function() {
        $(this).html("loading...");
    });

    // we can find archive_of with $("archive_of").text(); // but we don't need it...

    $.getJSON("/comments/", { ts: (new Date()).getTime(), mode: 'init' }, function(o) {
        $("div.commentstub").each(function() {
            var t = $(this);
            var nl = t.attr("class").replace(/commentstub /,'');
            var ca = o.comments[nl];

            you = o.you;
            ids = o.ids;

            draw(t, nl, ca);

            // re-jump to named location if neccessary:
            var m = location.href.match("#.*");
            if( m ) {
                var t = $("a[name=" + m[0].replace("#","") + "]");
                if( t )
                    window.scrollTo(0, scrollta(t[0]));
            }
        })
    });

});
// }}}
