function htmlEncode(s)
{
    if (s) {
        s = s.replace(/&/g,"&amp;");
        s = s.replace(/</g,"&lt;");
        s = s.replace(/>/g,"&gt;");
        s = s.replace(/'/g, "&#39;");
        s = s.replace(/"/g, "&quot;");
    }
    return s;
}

$(function() {
    var res = document.location.pathname.match(/\/(\d{6})(\d{2})(\w+)\//);
    var initIndex = 0;
    if (res) {
        $("a[href*=" + res[0] + "]").addClass("current");
        $.each($("ul.monthlist"), function(i, v) {
            if ($(v).attr("id") == "monthlist-" + res[1]) initIndex = i;
        });
    } else if (document.location.pathname == "/photos/") {
        $("a.entrylink:first").addClass("current");
    }
    $("ul.monthlist").hide();
    $("#entrylist").tabs("ul.monthlist", {
        tabs: "h2.month",
        effect: "slide",
        initialIndex: initIndex
    });

    var h = [];
    var as = $("#pictures a");
    var size = as.size();
    $.each(as, function(i, v) {
        var a = $(v);
        var name = a.children().attr("src").match(/^(.+).thumb.jpg$/)[1];
        var mt = a.attr("mediatype");
        a.attr("prev", (i == 0 ? size : i) - 1);
        a.attr("next", (i + 1) % size);

        h.push("<div class='overlay' id='overlay-" + i + "'><!--");
        if (mt == "image") {
            h.push("<img width='500' height='500' src='" + name + ".jpg'>");
        } else {
            if (Modernizr.video && Modernizr.video.h264) {
                h.push("<video width='500' controls autoplay src='" + name + ".mp4'></video>");
            } else {
                var flowplayer = "http://static.snoyman.com/photos/flowplayer.swf"; // FIXME
                h.push('<object width="500" height="375" type="application/x-shockwave-flash" data="' + flowplayer + '">');
                h.push('<param name="movie" value="' + flowplayer + '">');
                h.push('<param name="flashvars" value=\'config={"clip":"' + name + '.mp4"}\'>');
                h.push('</object>');
            }
        }
        h.push("--></div>");
        a.attr("rel", "#overlay-" + i);
        a.attr("href", "#");
    });
    $("body").append(h.join(''));
    var overlaySettings = {
        expose: {
            color: '#fff',
            opacity: 0.97
        },
        top: 10,
        onBeforeLoad: function() {
            var trig = this.getTrigger();
            var alt = htmlEncode(trig.children().attr("alt"));
            var prev = trig.attr("prev");
            var next = trig.attr("next");

            var node = this.getOverlay().get(0).firstChild;
            while (node) {
                if (node.nodeType === 8) {
                    var h = [];
                    h.push("<div class='killme'>");
                    h.push("<h1>" + alt + "</h1>");
                    h.push("<table><tr>");
                    h.push("<td><a class='prev' rel='#overlay-" + prev + "'> </a></td><td class='center'>");
                    h.push(node.nodeValue);
                    h.push("</td><td><a class='next' rel='#overlay-" + next + "'> </a></td></tr></table>");
                    h.push("</div>");
                    this.getOverlay().append(h.join(''));
                    break;
                }
                node = node.nextSibling;
            }
            $(".killme a[rel]").click(function(){
                var rel = $(this).attr("rel");
                var trig = $("a[rel=" + rel + "]");
                var prev = trig.attr("prev");
                var next = trig.attr("next");
                var alt = htmlEncode(trig.children().attr("alt"));
                $("div.killme .prev").attr("rel", "#overlay-" + prev);
                $("div.killme .next").attr("rel", "#overlay-" + next);
                $("div.killme .center").html(getComment($(rel)));
                $("div.killme h1").html(alt);
            });
        },
        onClose: function() {
            this.getOverlay().children("div.killme").remove();
        }
    };
    $("a[rel]").overlay(overlaySettings);
});

function getComment(x) {
    var node = x.get(0).firstChild;
    while (node) {
        if (node.nodeType === 8) return node.nodeValue;
        node = node.nextSibling;
    }
    return "";
}
