$(document).ready(function() {
    $("#loading").css("opacity", 0.0).corner();
});

function getIndex(index) {
    var re = new RegExp(/#\d/);
    if (window.location.hash.match(re))
        index = window.location.hash.substring(1);
    return index;
}

function loadAlbum(album, index, max) {
    $("#loading").fadeTo("slow", 1.0, function() {
        $("#album").fadeTo("slow", 0.0, function() {
            $(this).empty().load("album.ashx?album=" + album + "&index=" + getIndex(index) + "&max=" + max, null, function() {
                registerEvents();
                registerStyles();

                $(this).fadeTo("slow", 1.0, function() {
                    $("#loading").fadeTo("slow", 0.0);
                });
            });
        });
    });
}

function registerEvents() {
    $("#pager a").corner("5px").click(function() {
        $("#pager a").removeClass("selected");
        $(this).addClass("selected");
        loadAlbum($(this).attr("rel"), $(this).attr("id").slice(5), 50);
    });
    
    $(".thumb a.preview").fancybox({
        "easingIn": "easeOutBack",
        "easingOut": "easeInBack",
        "overlayShow": false,
        "titlePosition": "inside",
        "zoomSpeedIn": 600,
        "zoomSpeedOut": 500
    });

    $(".thumb a").hover(function() {
        $(this).find("img").stop().animate({
            marginTop: "-" + ($(this).find("span").height() > 0 ? $(this).find("span").height() + 10 : 0) + "px"
        }, 200).parent("a").find("span").stop().fadeTo(1250, 1.0);
    }, function() {
        $(this).find("img").stop().animate({
            marginTop: "0px"
        }, 200).parent("a").find("span").stop().fadeTo("fast", 0.0);
    });
}

function registerStyles() {
    $.each(jQuery.browser, function(i) {
        if ($.browser.msie && $("#pager a:first").height() > 0)
            $("#pager").css("height", ($("#pager a:first").height() + 15) + "px");
    });
}
