var optionalItems = [
    {'key': 'allowImages', 'item' : 'image'},
    {'key': 'allowTables', 'item' : 'table'},
];

// This is needed to somehow initialize tinyMCE before "$(document).ready(...)"
tinyMCE.init({
    mode : "none",
    theme : "advanced",
    plugins : 'inlinepopups,table'
});

var editor_id=0;
$(document).ready(function () {
    jQuery('.html_editor').each(function() {
        $(this).attr('id', 'tinymce_editor_' + editor_id);
        
        optionalItemsStr = '';
        for (i=0; i<optionalItems.length; i++)
            if (get_option($(this), optionalItems[i].key))
                optionalItemsStr += optionalItems[i].item + ',';
        if (optionalItemsStr.length > 0)
            optionalItemsStr += "seperator,";
    
        tinyMCE.init({
            mode : "exact",
            elements : 'tinymce_editor_' + editor_id,
            theme : "advanced",
            theme_advanced_buttons1 : "formatselect,bold,italic,underline,separator," + optionalItemsStr + "bullist,numlist,separator,undo,redo,separator,link,unlink,separator,help",
            theme_advanced_buttons2 : "",
            theme_advanced_buttons3 : "",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_blockformats : "p,h1,h2,h3,h4",
            plugins : 'inlinepopups,table',
            setup : function(ed) {
                ed.addButton('image', {
                    title : 'Select image',
                    image : '/media/common/rsrc/edit_image.gif',
                    onclick : function() {
                        loadPicker('picker_img', function (id) {
                            ed.focus();
                            ed.selection.setContent('<img src="/media/imadj/data/' + id+ '" />');
                        });
                    }
                });
            }
        });
        editor_id++;
    })
});
