var optionalItems = [
    {'key': 'allowImages', 'item': {'name': 'InsertImage', 'title': 'Image', 'css': 'wym_tools_image'}},
    {'key': 'allowTables', 'item': {'name': 'InsertTable', 'title': 'Table', 'css': 'wym_tools_table'}},
];

function useTool(toolItems, toolItem, use) {
    found = false;
    for (i=0; i<toolItems.length; i++)
        if (toolItems[i].name == toolItem.name) {
            found = true;
            break;
        }
    if (!use && found)
        toolItems.splice(i,1);
    else if (use && !found)
        toolItems[toolItems.length] = toolItem;
}

$(document).ready(function () {
    jQuery('.html_editor').each(function() {

        var toolItems = [
            {'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'}, 
            {'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
            {'name': 'InsertOrderedList', 'title': 'Ordered_List', 'css': 'wym_tools_ordered_list'},
            {'name': 'InsertUnorderedList', 'title': 'Unordered_List', 'css': 'wym_tools_unordered_list'},
            {'name': 'Paste', 'title': 'Paste_From_Word', 'css': 'wym_tools_paste'},
            {'name': 'Undo', 'title': 'Undo', 'css': 'wym_tools_undo'},
            {'name': 'Redo', 'title': 'Redo', 'css': 'wym_tools_redo'},
            /*{'name': 'ToggleHtml', 'title': 'HTML', 'css': 'wym_tools_html'},*/
            {'name': 'Preview', 'title': 'Preview', 'css': 'wym_tools_preview'},
        ];

        for (i=0; i<optionalItems.length; i++)
            useTool(toolItems, optionalItems[i].item, get_option($(this), optionalItems[i].key));

        $(this).wymeditor({
            updateSelector: "input:submit",
            updateEvent:    "click",

            basePath: "/media/common/rsrc/js/wymeditor/",
            skin: "rptlab",

            postInit: function(wym) {
                wym.hovertools(); // other plugins...
                wym._box.find('.wym_tools_image > a').unbind("click").click(function () {
                    loadPicker('picker_img', function (id) {
                        wym.insert('<img src="/media/imadj/data/' + id+ '" />');
                    });
                    return false;
                });
            },
            toolsItems: toolItems,
            logoHtml: '',
        })
    })
});
