/**
 * Zeigt in einem extra Fenster für Admins die Notizen zu Benutzer/Profilen an.
 */
(function($, jQuery) {
    jQuery.fn.hwsNotes = function(options) {
        options = jQuery.extend({}, jQuery.fn.hwsNotes.defaults, options);

        return $(this).each(function() {
            if (options.id == 0) {
                return;
            }

            $.ajax({
                url: options.url+'?type=' + options.type + '&id=' + options.id,
                type: 'get',
                dataType: 'json',
                success: function(data) {
                    var notesDialog = $('<div class="hwsnotes"><div class="hwsnotes-container"></div></div>');
                    $("body").append(notesDialog);
                    var notesContainer = notesDialog.find('.hwsnotes-container');

                    if (data.length == 0) {
                        notesContainer.append('<p>Keine Notizen gefunden.</p>');
                    } else {
                        var itemClass = 'first';
                        notesContainer.append('<p align="center"><a href="#" class="hwsnotes-show-all">Alle ' + data.length + ' Notizen anzeigen</a>| <a href="#" class="hwsnotes-close">Schließen</a></p><ul></ul>');
                        var nodeList = notesContainer.find('ul');

                        for (var noteId in data) {
                            var note = data[noteId];

                            if (note.text.length > 300) {
                                note.text = note.text.substring(0, 300) + '... [Gekürzt]';
                            }

                            var item = $('<li class="' + itemClass + '"></li>').append(
                                '<p>' + note.text + '</p><p><small>Von: ' + note.writer + ', ' +
                                'Datum: ' + note.date + '</strong></p>');
                            nodeList.append(item);
                            itemClass = '';
                        }

                        notesContainer.find('.hwsnotes-show-all').click(function() {
                            notesContainer.find('li').addClass('first');
                        });
                        notesContainer.find('.hwsnotes-close').click(function() {
                            notesDialog.remove();
                        });
                    }
                }
            })
        });
    }
    jQuery.fn.hwsNotes.defaults = {
        notesProviderUrl: '',
        type: 'profile',
        id: 0
    }
})(jQuery, jQuery);
