// Object to create a separate name space for GForum javascript functions
function GForumAdmin() {}

// Initialize the GForum object
$(document).ready(function() {
    GForumAdmin = new GForumAdmin();
});

GForumAdmin.prototype.ajax_url = 'admin.cgi';

/**
 * Initializes the drag and drop functionality for the dashboard
 */
GForumAdmin.prototype.initDashboard = function () {
    // Init the column sortables
    $('.widget_column').sortable(
        {
            items: '.widget',
            handle: '.widget_heading',
            opacity: 0.3,
            delay: 100,
            placeholder: 'widget_hover',
            connectWith: ['#dashboard_column0', '#dashboard_column1'],
            stop: function () { GForumAdmin.saveWidgetPositions(); }
        }
    );

    // Init the row sortables
    $('.widget_row').sortable(
        {
            items: '.widget',
            handle: '.widget_heading',
            opacity: 0.3,
            delay: 100,
            placeholder: 'widget_hover',
            connectWith: ['#dashboard_row0', '#dashboard_row1'],
            stop: function () { GForumAdmin.saveWidgetPositions(); }
        }
    );

    GForumAdmin.initToolbarButtons();

    // Init the widget add buttons
    $('.widget_add').click(function () {
        GForumAdmin.addWidget($(this).attr('id').substr(4));
        return false;
    });

    // Init the widget configuration page buttons
    $('.widget_list_toggle').click(function () {
        $('#widget_list').slideToggle('slow', function () {
            var old_text = $('.widget_toggle_text').text();
            var new_text = $('.widget_toggle_text_exchange').text();
            $('.widget_toggle_text').text(new_text);
            $('.widget_toggle_text_exchange').text(old_text);
        });

        return false;
    });
};

/**
 * Binds the click functions to the widget toolbar buttons
 */
GForumAdmin.prototype.initToolbarButtons = function () {
    $('.widget_close').unbind();
    $('.widget_minimize').unbind();

    // Init the widget close buttons
    $('.widget_close').click(function () {
        $(this).parents('.widget').fadeOut('slow', function () {
            $(this).remove();
            GForumAdmin.saveWidgetPositions();
        });
        return false;
    });

    // Init the widget minimize buttons
    $('.widget_minimize').click(function () {
        $(this).parents('.widget').children('.box_content').slideToggle();
        $(this).parents('.widget').find('.widget_minimize').toggleClass('hidden');
        GForumAdmin.toggleWidgetMaximizationState($(this).parents('.widget').attr('id'));
        return false;
    });
};


/**
 * Ajax function to save the current widget positions
 */
GForumAdmin.prototype.saveWidgetPositions = function () {
    $.post(GForumAdmin.ajax_url, {
        'do': 'json_save_widget_positions',
        'column0': $('#dashboard_column0').sortable('serialize'),
        'column1': $('#dashboard_column1').sortable('serialize'),
        'row0': $('#dashboard_row0').sortable('serialize'),
        'row1': $('#dashboard_row1').sortable('serialize')
    });
};

/**
 * Ajax function to toggle the maximization state of a widget
 */
GForumAdmin.prototype.toggleWidgetMaximizationState = function (widget_id) {
    $.post(GForumAdmin.ajax_url, {
        'do': 'json_toggle_widget_maximization_state',
        'widget_id': widget_id
    });
};

/**
 * Ajax function to add a widget to the dashboard
 */
GForumAdmin.prototype.addWidget = function (widget_id) {
    $.post( GForumAdmin.ajax_url, { 'do': 'json_add_widget', 'widget_id': widget_id }, function(data) {
        if (data.success) {

            var container;
            if (data.data.widget_config.container == 'column') {
                container = '#dashboard_column' + data.data.widget_config.column;
            }
            else {
                container = '#dashboard_row0';
            }

            $(container).prepend(data.data.widget_html);
            GForumAdmin.initToolbarButtons();
        }
    }, "json");
};

/**
 * Ajax function to fetch the update information and reset the numbers in the DOM
 */
GForumAdmin.prototype.fetchUpdates = function (widget_id) {
    $.post( GForumAdmin.ajax_url, { 'do': 'json_fetch_updates' }, function(data) {
        if (data.success) {
            var updates = data.data.updates;

            // Loop trough the update types
            for (var type in updates) {
                if (typeof type == 'string') {
                    eval('var current =  updates.' + type);

                    // Reset the number of updates
                    $('#updates_list').find('.' + type).text(current);

                    // Highlight critical updates
                    if (current > 0 && type == 'critical') {
                        $('#updates_list').find('.' + type).parent().addClass('important');
                    }

                    // Singular/plural replacements
                    if (current != 1) {
                        $('#updates_list').find('.' + type + '_plural').show();
                        $('#updates_list').find('.' + type + '_singular').hide();
                    }
                }
            }

            // Show the link to the updates page in case we got some updates
            if (updates.total) {
                $('#updates_link').show();
            }

            // Show the list
            $('#updates_list').slideDown("slow");
        } else {
            $('#updates_error').text(data.message);
            $('#updates_error').show();
        }

        $('#updates_spinner').remove();
    }, "json");
};


