var lightboxImages = [];

$(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) {
            lightboxImages = [];
            if ( data.currentalbum == 0 ) {
                $("#albumdetails").addClass("hidden");
            } else {
                $("#albumdetails").removeClass("hidden");
                lightboxImages = data.images;
            }
            $("#albumtitle").html(data.albumtitle);
            $("#albumdescription").html(data.albumdescription);
            $("#albumcontents").html(data.albumcontents);
            $(".albumthumbnail").click(function() {
                if ( data.currentalbum == 0 ) {
                    showAlbum($(this));
                } else {
                    showImage($(this));
                }
            });
            console.log(lightboxImages);
        },
        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);
    var startIndex = 0;
    var images = [];
    var captions = [];
    lightboxImages.forEach(function(image, index) {
        if ( image.id == imageid ) startIndex = index;
        images.push(image.imageurl);
        captions.push(image.description);
    });
    /*
    images.forEach(function(image, index) {
        if ( basename(image, "/") == basename(clickedElement.attr("src"), "/") ) startIndex = index;
    });
    */
    SimpleLightbox.open({
        items: images,
        captions: captions,
        startAt: startIndex
    });
}

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
