// We only need one instance of DLComments
if(!this.DLComments) {



/**
 * DLComments
 *
 * @author      Duc Tri Le
 * @copyright   Copyright (c) 2008, Duc Tri Le
 * @version     0.1 - May 16, 2008: Initial release.
 *              0.0 - April 10, 2008: Started.
 */
var DLComments = {
    /**
     * Copy the inner HTML of the original element to a textarea (creating it
     * if it doesn't already exist. This will hide the original element and
     * restore it when the textarea loses focus.
     *
     * @param string    field       The type of field to create.
     * @param object    original    The DOM element in which the inner HTML will
     *      be retrieved from.
     * @param string    type        The type of setting: approval|modify.
     * @param string    name        The name of the input field.
     * @param string    id          The id.
     */
    copyData: function(field, original, type, name, id) {
        // Check the checkbox
        var check_box = document.getElementById('DLComments_checkbox_'+type+'_'+id);
        check_box.checked = true;

        var field_span = document.getElementById('DLComments_'+type+'_'+name+'_edit_'+id);

        // Create the field element if it doesn't already exist
        if(field_span.innerHTML == '') {
            // Create the element
            var element = document.createElement(field);

            // Set the element properties
            element.name = 'DLComments_'+name+'_'+id;
            if(type == 'input') { element.type = 'text'; }

            // Add the element to document
            field_span.appendChild(element);
        }

        // Get the text area
        var field_element = field_span.firstChild;

        // Copy the content of the given element over to the textarea and style
        field_element.value = original.innerHTML;
        field_element.style.width = '95%';
        if(field == 'textarea') { field_element.style.height = '150px'; }

        // Set the onblur event
        field_element.onblur = DLSupport.curry(DLComments.restoreData, this, original, field_span, field_element);

        // Hide the original element and show the textarea span
        original.style.display = 'none';
        field_span.style.display = '';
        field_element.focus();
    },

    /**
     * Copy the value of the field back to the original element while keeping
     * the field in the document.
     *
     * @param object    original        The original DOM element.
     * @param object    field_span      The span that contains the field.
     * @param object    field           The field from which the value is to be
     *      retrieved.
     */
    restoreData: function(original, field_span, field) {
        // Copy the value of the textarea to the original element
        original.innerHTML = field.value;

        // Hide the textarea span and show the original element
        field_span.style.display = 'none';
        original.style.display = '';
    },

    /**
     * Process the server response.
     *
     * @param object    form            The form that began the request.
     * @param object    message_area    The DOM element that will hold the
     *      message returned by the request.
     * @param object    html_area       The DOM element that will hold the HTML
     *      returned by the request.
     * @param object    transport       The AJAX transport.
     */
    serverResponse: function(form, message_area, html_area, transport) {
        // Decode the response
        var response = JSON.parse(transport.responseText);

        // Only continue if it is a valid object
        if(response) {
            // Display the server message unless we didn't have one
            if((response.message.length > 0) && message_area) {
                message_area.style.display = '';
                message_area.style.color = response.success ? '#009F3C' : '#DF0024';
                message_area.innerHTML = response.message;

                window.location.hash = message_area.id;
            } else if(message_area) {
                message_area.style.display = 'none';
            }

            // See if we need to hide the HTML area
            if(response.hide_html_area && html_area) {
                html_area.style.display = 'none';
            } else if((response.html.length > 0) && html_area) {
                html_area.style.display = '';
                html_area.innerHTML = response.html;
            }
        } else {
            window.alert('An error occurred during transmission. Terminating.');
        }

        // Enable all of the submit buttons
        DLSupport.Form.enableSubmitFields(form);
        DLSupport.hideLoadingImage();
    },

    /**
     * Toggle the display of the given data and update the given link to reflect
     * the new action.
     *
     * @param object    link    The link that will toggle the display of the
     *      data.
     * @param object    data    An object containing various information about
     *      what to toggle. It includes the id of the element to hide or show,
     *      and the text of the link when that element is hidden and when it is
     *      visible.
     */
    showHide: function(link, data) {
        var element = document.getElementById(data.id);

        // Depending on the status, continue appropriately
        if(link.innerHTML == data.show) {
            element.style.display = '';
            link.innerHTML = data.hide;
        } else {
            element.style.display = 'none';
            link.innerHTML = data.show;
        }
    },

    /**
     * Submit a form to the server.
     *
     * @param object    form        The form to submit.
     * @param string    type        The type of request.
     */
    submitForm: function(form, type) {
        // Get some data
        var message_area = document.getElementById('DLComments_message_area_'+type);
        var html_area = document.getElementById('DLComments_html_area_'+type);

        // Convert the form data to a query string
        var query_string = DLSupport.Form.toQueryString(form);

        // Disable all of the submit buttons
        DLSupport.Form.disableSubmitFields(form);
        DLSupport.showLoadingImage('red');

        // Submit the form
        DLSupport.AJAX.request(form.action, {
            method: form.method,
            query_string: query_string,
            onSuccess: DLSupport.curry(DLComments.serverResponse, this, form, message_area, html_area)
        });

        return false;
    }
};



}