tinyMCE.init({
    theme : "advanced",
    mode : "specific_textareas",
    editor_selector : "mceEditor",
    theme_advanced_toolbar_location : "top",
    plugins : "fullscreen,table,autosave,paste,imagemanager",
    content_css : "/themes/WhatcomGreen/style/style.css",
    style_formats : [
        {title : "Page Title", block : "div", classes : "searchtitle"},
        {title : "Section Title", block : "div", classes : "storytitle"},
        {title : "Section Heading Box", block : "div", classes : "subheaderbox subheader subheadertitle"},
        {title : "Block Title", block : "div", classes : "blocktitle blocktitlebackground"},
        {title : "Block Background", block : "div", classes : "blockcontentbackground"},
        {title : "Small Text", inline : "span", classes : "tiny"},
        {title : "No-Underline Link", selector : "a", classes : "nounderline"},
    ],
    table_styles : "",
    table_row_styles : "Column Heading=sectionheaderbar;Row Heading=sectionsubheaderbar;",
    table_cell_styles : "",
    theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect",
    theme_advanced_buttons2 : "bullist,numlist,|,indent,outdent,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code",
    theme_advanced_buttons3 : "tablecontrols,|,fullscreen",
    theme_advanced_buttons4 : "hr,removeformat,visualaid,|,sub,sup,|,charmap",
    valid_elements : "@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
        + "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
        + "onkeydown|onkeyup|align],a[rel|rev|charset|hreflang|tabindex|accesskey|type|"
        + "name|href|target|title|class|onfocus|onblur],strong/b,em/i,strike,u,"
        + "#p,-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|"
        + "src|border|alt=|title|hspace|vspace|width|height|align],-sub,-sup,"
        + "-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|"
        + "height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|"
        + "height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,"
        + "#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor"
        + "|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,"
        + "-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face"
        + "|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],"
        + "object[classid|width|height|codebase|*],param[name|value|_value],embed[type|width"
        + "|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,"
        + "button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|"
        + "valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],"
        + "input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value],"
        + "kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],"
        + "q[cite],samp,select[disabled|multiple|name|size],small,"
        + "textarea[cols|rows|disabled|name|readonly],tt,var,big"
});

function toggleEditor(id) {
    var onlink = document.getElementById(id+"_visualeditoronlink");
    var offlink = document.getElementById(id+"_visualeditorofflink");
    if (!tinyMCE.get(id)) {
        tinyMCE.execCommand("mceAddControl", false, id);
        onlink.style.visibility = "hidden";
        offlink.style.visibility = "visible";
        editorOn = true;
    } else {
        tinyMCE.execCommand("mceRemoveControl", false, id);
        onlink.style.visibility = "visible";
        offlink.style.visibility = "hidden";
        editorOn = false;
    }
    return editorOn;
};

function toggleVisualMode(controlID, formatControlID) 
{
    var editorOn = toggleEditor(controlID);

    if (formatControlID) {
        // Change the content-type dropdown to HTML formatting
        var felem = document.getElementById(formatControlID);
        if (felem) {
            if (editorOn) {
                autoSelectDropDown(felem, "1");
            } else {
                autoSelectDropDown(felem, "0");
            }
        }
    }
}
