var id;

$(document).ready(function() {

    if ($("#photos").length > 0)
    {
        var hash = window.location.hash;

        $("#pagination a").each(function() {
            $(this).click(function() {
                hash = $(this).attr('href');
                id = parseInt(hash.substr(7, hash.length - 7));
                load_photo();
            });
        });

        $("#nav").show();
        $("#photo").prepend('<img src="" id="current" alt="" /><div id="loading"></div>');
        $("#current").hide();
        $("#photos").hide();

        if (hash == '')
            id = 1;
        else
            id = parseInt(hash.substr(7, hash.length - 7));

        load_photo();

        $("#prev a").click(function() {
            if (id == 1)
                id = count;
            else
                id = id - 1;

            load_photo();
            return false;
        });

        $("#next a").click(function() {
            if (id == count)
                id = 1;
            else
                id = id + 1;

            load_photo();
            return false;
        });
    }

});

function load_photo() {
    window.location.hash = "#photo-" + id;

    $("#pagination li").each(function() { $(this).removeClass('active'); })
    $("#link-" + id).addClass('active');

    $("#loading").fadeIn(500, function() {
        var s = $("#photo-" + id + " img").attr('src');
        image = new Image;
        image.onload = function() {
            image.onload = null;
            $("#current").attr('src', s);
            $("#current").show();
        }
        image.src = s;
    });

    $("#loading").fadeOut(500);
}
