var lightboxImages = [];
var editAlbumID = 0;
var editImageID = 0;
var currentAlbum = 0;

$(document).ready(function() {
    $("#edit_album").dialog({
        autoOpen: false,
        height: 250,
        width: 350,
        closeOnEscape: true,
        draggable: false,
        close: function() {
            editAlbumID = 0;
            $("#edit_album_title").val("");
            $("#edit_album_description").val("");
        },
        buttons: [
            {
                text: "Save",
                click: function() { saveAlbumInfo(); }
            },
            {
                text: "Cancel",
                click: function() { $(this).dialog("close"); }
            }
        ]
    });
    $("#edit_image").dialog({
        autoOpen: false,
        height: 250,
        width: 350,
        closeOnEscape: true,
        draggable: false,
        close: function() {
            editImageID = 0;
            $("#edit_image_title").val("");
            $("#edit_image_description").val("");
        },
        buttons: [
            {
                text: "Save",
                click: function() { saveImageInfo(); }
            },
            {
                text: "Cancel",
                click: function() { $(this).dialog("close"); }
            }
        ]
    });
    $("#btn_refresh").click(function() { refreshAlbums() });
    $("#btn_album_0").click(function() { showAlbum($(this)) });
    $(".albuminfo_label").click(function() { editAlbum(currentAlbum); });
    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");
            getAlbumInfo();
        },
        error: function(jqp, status, error) {
            toastr.error("Error refreshing album list!\n" + error, "Server Error");
        }
    });
}

function updateLightboxImages() {
    $.ajax({
        type: 'GET',
        url: 'ajax/getalbuminfo.php',
        dataType: 'json',
        success: function(data, stat, jqo) {
            lightboxImages = data.images;
            lightboxImages.forEach(function(image, index) {
                $("#title_"+image.id).text(image.title);
            });
        },
    });
}

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));
                }
            });
            currentAlbum = data.currentalbum;
            $(".griditem_title").click(function() {
                if ( data.currentalbum == 0 ) {
                    editAlbum($(this).attr("id").substring($(this).attr("id").lastIndexOf("_") + 1));
                } else {
                    editImage($(this).attr("id").substring($(this).attr("id").lastIndexOf("_") + 1));
                }
            });
        },
        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);
    });
    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");
        }
    });
}

function saveImageInfo() {
    if ( editImageID == 0 ) return;
    if ( $("#edit_image_title").val() == "" ) {
        toastr.error("Image title cannot be blank!", "Image Title Error");
        return;
    }
    $.ajax({
        type: 'POST',
        url: 'ajax/saveimage.php',
        dataType: 'json',
        data: {
            imageid: editImageID,
            title: $("#edit_image_title").val(),
            description: $("#edit_image_description").val()
        },
        success: function(data, stat, jqo) {
            if ( data.success ) {
                $("#edit_image").dialog("close");
                $("#title_"+data.image.id).text(data.image.title);
                updateLightboxImages();
            } else {
                toastr.error("Could not get image information!", "Image Data Error");
            }
        },
        error: function(jqp, status, error) {
            toastr.error("Error saving image!\n" + error, "Server Error");
        }
    });
}

function saveAlbumInfo() {
    if ( editAlbumID == 0 ) return;
    if ( $("#edit_album_title").val() == "" ) {
        toastr.error("Album title cannot be blank!", "Album Title Error");
        return;
    }
    $.ajax({
        type: 'POST',
        url: 'ajax/savealbum.php',
        dataType: 'json',
        data: {
            albumid: editAlbumID,
            title: $("#edit_album_title").val(),
            description: $("#edit_album_description").val()
        },
        success: function(data, stat, jqo) {
            if ( data.success ) {
                $("#edit_album").dialog("close");
                $("#title_"+data.album.id).text(data.album.title);
                if ( currentAlbum != 0 ) {
                    $("#albumtitle").text(data.album.title);
                    $("#albumdescription").text(data.album.description);
                }
            } else {
                toastr.error("Could not get album information!", "Album Data Error");
            }
        },
        error: function(jqp, status, error) {
            toastr.error("Error saving album!\n" + error, "Server Error");
        }
    });
}

function editAlbum(albumid) {
    $.ajax({
        type: 'GET',
        url: 'ajax/getalbum.php',
        dataType: 'json',
        data: {
            albumid: albumid
        },
        success: function(data, stat, jqo) {
            if ( data.success ) {
                editAlbumID = data.album.id;
                $("#edit_album_title").val(data.album.title);
                $("#edit_album_description").val(data.album.description);
                $("#edit_album").dialog("open");
            } else {
                toastr.error("Could not get album information!", "Album Data Error");
            }
        },
        error: function(jqp, status, error) {
            toastr.error("Error getting album!\n" + error, "Server Error");
        }
    });
}

function editImage(imageid) {
    $.ajax({
        type: 'GET',
        url: 'ajax/getimage.php',
        dataType: 'json',
        data: {
            imageid: imageid
        },
        success: function(data, stat, jqo) {
            if ( data.success ) {
                editImageID = data.image.id;
                $("#edit_image_title").val(data.image.title);
                $("#edit_image_description").val(data.image.description);
                $("#edit_image").dialog("open");
            } else {
                toastr.error("Could not get image information!", "Image Data Error");
            }
        },
        error: function(jqp, status, error) {
            toastr.error("Error getting image!\n" + error, "Server Error");
        }
    });
}

// vim: ts=4:sw=4
