/*
 * This function is used to display an alert in the message center
 * first param is the message
 * second param is type, meaning message or error
 */
function displayAlert(message, type)
{
    // remove the class that is on there
    $('#messagecenter_fill').removeClass('error');
    // show the message center
    $('#messagecenter').show();
    // stop all animations on the message center
    $('#messagecenter').stop(true);
    // if it's an error message, add the error class
    if (type == "error")
        $('#messagecenter_fill').addClass('error');

    // fill the information
    $('#messagecenter_fill').html(message);

    // flashing box effect
    $('#messagecenter').fadeTo(500,0.0).fadeTo(500,1.0).fadeTo(500,0.0).fadeTo(500,1.0).fadeTo(5000,1.0).fadeOut(1000);
}

/*
 * This function will check a given extension against
 * extensions that are acceptable
 * return true if accepted, false if not
 */
function extensionCheck(ext)
{
    ext.toString().toLowerCase();
    var exts = ['jpg', 'gif', 'jpeg'];
    for (i = 0; i < exts.length; i++)
        if (ext == exts[i])
            return true;

    return false;
}

/*
 * This is called when a user tries to create a gallery
 * The gallery will be created via a POST request to the server
 * and then added to the list of available galleries
 */
function createGallery()
{
    // AJAX request
    $.post(
        'functions.php',
        {'action': "createGallery",
         'galleryname': $('#galleryname').val()},
        function(data) {
            // evaluate the JSON data that was returned from the request
            var JSON = eval("("+data+")");

            // if there were errors, report them
            if (JSON.errors) {
                displayAlert(JSON.errors[0], "error");
            // if there were messages, report them
            } else {
                displayAlert(JSON.messages[0], "message");

                // add that gallery to the manage gallery selection now
                $('#galleryid').append('<option value="'+ JSON.galleryid + '">' + JSON.galleryname + '</option>');
            }
            // clear the field
            $('#galleryname').val('');

        }
    ); // end ajax
}

/*
 * Called when a user attempts to delete an image
 * The first param is the element (DOM element of the image cell)
 * A request to the server is made to delete the image
 */
function deletePicture(element)
{
    // prompt the user to be sure to remove it
    var check = confirm("Are you sure you want to delete this picture?");
    if (check) {
        // get the name of the image
        var imagename = $(element).parent().parent().parent().parent().find(".gallerypic").attr("name");
        var galleryid = $(element).parent().parent().parent().parent().find(".gallerypic").attr("galleryid");

        // remove the image from the database using POST request
        $.post(
             'functions.php',
            {'action': "deletePicture",
             'galleryid': galleryid,
             'filename': imagename},
             function(data) {

                // parse JSON data received from server
                var JSON = eval("("+data+")");

                if (JSON.errors) {
                    displayAlert(JSON.errors[0], "error");
                } else {
                    displayAlert(JSON.messages[0], "message");

                    // now that the image is deleted, we want to remove it from the DOM tree
                    $(element).parent().parent().parent().parent().parent().parent().remove();

                    // are there any pics left?
                    if (JSON.picsleft[0] == 0)
                        $('#sortable').append("<p id=\"none\">No pictures uploaded yet</p>");
                } // error/messasges if
            } // ajax callback
        ); // end ajax

    } // check
}

function editCaption(pictureID, newCaption, oldCaption)
{
    if (newCaption != null) {
        $.post(
            'functions.php',
            {'action': "editCaption",
             'pictureID': pictureID,
             'newCaption': newCaption},
             function(data) {
                 var JSON = eval("("+data+")");

                 if (JSON.errors) {
                     displayAlert(JSON.errors[0], "error");
                 } else {
                     displayAlert(JSON.messages[0], "message");
                 }
             }
        );
    }
}

function updateImagePositions() {
    $(".gallerypic").each(function (index) {
        var filename = $(this).attr("name");

        $.post(
            "functions.php",
            {"filename": filename,
             "position": index,
             "action": "updateImagePosition"},
            function (data) {

            }
        );
    });
}

function deleteGallery(galleryid)
{
    var check = confirm("Are you POSITIVE you want to delete this gallery? ALL PICTURES WILL BE REMOVED!");
    if (check) {
        $.post (
            'functions.php',
            {'action': "deleteGallery",
             'galleryid': galleryid},
            function (data) {

                var JSON = eval("("+data+")");

                if (JSON.errors) {
                    displayAlert(JSON.errors[0], "error");
                } else {
                    displayAlert(JSON.messages[0], "message");
                }

                // remove the gallery option from the select list
                $('#galleryid :selected').remove();
            }
        );
    } // check
}


