$(document).ready(function() {
    $("#btn_refresh").click(function() { refreshAlbums() });
    $("#btn_album_0").click(function() { showAlbum($(this)) });
    getAlbumInfo();
});

function refreshAlbums() {
    toastr.info("Starting refresh of albums.\nThis may take a while if\nthere are many new images.", "Starting Refresh");
    $.ajax({
        type: 'GET',
        url: 'ajax/refreshalbums.php',
        dataType: 'json',
        success: function(data, stat, jqo) {
            toastr.success("Album list refreshed", "Refresh");
        },
        error: function(jqp, status, error) {
            toastr.error("Error refreshing album list!\n" + error, "Server Error");
        }
    });
}

function getAlbumInfo() {
    $.ajax({
        type: 'GET',
        url: 'ajax/getalbuminfo.php',
        dataType: 'json',
        success: function(data, stat, jqo) {
            if ( data.currentalbum == 0 ) {
                $("#albumdetails").addClass("hidden");
            } else {
                $("#albumdetails").removeClass("hidden");
            }
            $("#albumtitle").html(data.albumtitle);
            $("#albumdescription").html(data.albumdescription);
            $("#albumcontents").html(data.albumcontents);
            $(".albumthumbnail").click(function() {
                if ( data.currentalbum == 0 ) {
                    showAlbum($(this));
                } else {
                    showImage($(this));
                }
            });
        },
        error: function(jqp, status, error) {
            toastr.error("Error retrieving album info!\n" + error, "Server Error");
        }
    });
}

function showImage(clickedElement) {
    var imageid = clickedElement.attr("id").substring(clickedElement.attr("id").lastIndexOf("_") + 1);
    console.log(imageid);
}

function showAlbum(clickedElement) {
    var albumid = clickedElement.attr("id").substring(clickedElement.attr("id").lastIndexOf("_") + 1);
    $.ajax({
        type: 'GET',
        url: 'ajax/setcurrentalbum.php',
        dataType: 'json',
        data: {
            albumid: albumid
        },
        success: function(data, stat, jqo) {
            $("#albumcontents").html("");
            getAlbumInfo();
        },
        error: function(jqp, status, error) {
            toastr.error("Error setting current album!\n" + error, "Server Error");
        }
    });
}

// vim: ts=4:sw=4