$(document).ready(function() {
    $.clipboardReady(function() {
        $saveToClipboardButton = $('button.copyURLToClipboard');

        // This commented use of attr() should work, but has problems
        // on IE in jQuery 1.2: inner html is returned instead of
        // value of attr 'value'. See http://dev.jquery.com/ticket/3259
        //
        // For now, the solution that seems to work accross platforms
        // is to use a custom attribute.
        //$saveToClipboardButton.click(function() {
        //    $.clipboard($(this).attr("value"));
        //});
        $saveToClipboardButton.click(function() {
            $.clipboard($(this).attr("fileURL"));
        });

        $saveToClipboardButton.hover(
            function() {
                Tip(getCopyPublicAccessURLTip());
            },
            function(){}
            );

    }, {
        swfpath: "data/script/jquery.clipboard.swf",
        debug: true
    });

    $(".createPublicAccessTip").hover(
        function() {
            Tip(getCreatePublicAccessTip());
        },
        function() {}
        );

    // Stripe the table for downloadable files:
    $("table#downloadFiles tbody tr:nth-child(odd)").addClass("striped");
    $("table#userTable tbody tr:nth-child(odd)").addClass("striped");


    var fileDeleteOptions = {
        url: 'data/handleAjaxDeleteFile.php',
        dataType: 'json',
        beforeSubmit: verifyDelete,
        success: deleteFileCallback
    }
    $('.deleteFileForm').ajaxForm(fileDeleteOptions);


    var projectDeleteOptions = {
        url: 'data/fileexchange/handleAjaxDeleteProject.php',
        dataType: 'json',
        beforeSubmit: verifyDelete,
        success: deleteProjectCallback,
        error: deleteProjectError
    }
    $('.deleteProjectForm').ajaxForm(projectDeleteOptions);


    var userDeleteOptions = {
        url: 'data/fileexchange/handleAjaxDeleteUser.php',
        dataType: 'json',
        beforeSubmit: verifyDelete,
        success: deleteUserCallback,
        error: deleteUserError
    }
    $('.deleteUserForm').ajaxForm(userDeleteOptions);
    

    $('.updatePublicAccess').click(function(event) {
        var fields = $(this).serialize();
        $.post("data/updatePublicAccess.php", fields, updateCopyURLFormCallback, "json");
    });

});


$.fn.toggleCheckbox = function() {
    return this.each(function() {
        this.checked = !this.checked;
    });
};


function updateCopyURLFormCallback(data) {
    var idAttr = "I" + data.id;
    if (!(data.success > 0)) {
        $("#" + idAttr + " .updatePublicAccess input:checkbox").toggleCheckbox();
        alert("Problem updating public status of file.");
        return;
    }

    var target = $('tr#' + idAttr + ' .copyURLForm');
    if (data.isPublic) {
        target.show("normal");
    } else {
        target.hide("normal");
    }
}


function verifyDelete() {
    return confirm("Really delete?");
}


function deleteFileCallback(data, status) {
    var idAttr = "I" + data.id;
    if (-1 == data.success) {
        alert("Problem deleting file from database!\nPlease <a href='mailto:hillb@uci.edu'>contact Brian.</a>")
        return;
    } else {
        $('tr#' + idAttr).css("background", "#ffc8c8").fadeOut(1000, function() {
            $(this).remove();
            $("table#downloadFiles tbody tr").removeClass("striped");
            $("table#downloadFiles tbody tr:nth-child(odd)").addClass("striped");
        });
    }
}


function deleteProjectCallback(data, status) {
    // Only for debugging:
    //alert("in deleteProjectCallback, data: " + data + "  , status: " + status);
    
    if (0 == data.success) {
        alert(data.msg + "\nPlease <a href='mailto:hillb@uci.edu'>contact Brian.</a>")
    } else {
        //alert(data.msg + "data.id: " + data.id);
        $('tr#' + data.id).css("background", "#ffc8c8").fadeOut(1000, function() {
            $(this).remove();
            $("table#userTable tbody tr").removeClass("striped");
            $("table#userTable tbody tr:nth-child(odd)").addClass("striped");
        });
    }
}


function deleteUserCallback(data, status) {
    if (0 == data.success) {
        alert(data.msg + "\nPlease <a href='mailto:hillb@uci.edu'>contact Brian.</a>");
    } else {
        $('tr#' + data.id).css("background", "#ffc8c8").fadeOut(1000, function() {
            $(this).remove();
             $("table#userTable tbody tr").removeClass("striped");
            $("table#userTable tbody tr:nth-child(odd)").addClass("striped");
        });
    }
}


function deleteUserError(xhrInstance, statusText) {
    alert("deleteUserError: " + statusText + "\n xhrInstance: " + xhrInstance);
}


function deleteProjectError(xhrInstance, statusText) {
    alert("deleteProjectError: " + statusText + "\n xhrInstance: " + xhrInstance);
}


function getCreatePublicAccessTip() {
    return "<p>"
    + "Marking a file as having 'Public Access' allows<br />"
    + "copying and sending a link to the file to individuals<br />"
    + "who don't have an appropriate account to<br />"
    + "download the file otherwise."
    + "</p>";
}


function getCopyPublicAccessURLTip() {
    return "<p>"
    + "This is a public access file.<br />"
    + "You can copy and paste this link to allow<br />"
    + "individuals that don't have an appropriate<br />"
    + "account to access this file directly."
    + "</p>";
}
