tinyMCE.init({
    setup : function(ed) {
        ed.onPostProcess.add(function(ed, o) {
            // this_site_regex matches href or src attribute where the value is this url or any thehcn.net url, eg an absolute url pointing to the same site. 
            // the replacement just removes the absolute string so src="http://example.thehcn.net/helloworld.php" will return src="/helloworld.php"
            var this_site_regex = new RegExp("(\\b(?:href|src)=[\"']?)\\s*https?://(?:" + window.location.hostname.replace(/\./g, '\\.') + "|[\\w-.]+\\.thehcn\\.net|[\\w-.]+\\.hcn\\.(?:nocdev|nocbeta|networkofcare)\\.org)([/\"'>\\s])", "igm");
            o.content = o.content.replace(this_site_regex, "$1$2");

            // newline_regex matches the empty divs that TinyMCE creates when a user hits enter, and replaces them with a div containing a nbsp
            // to create a full line break.
            var newline_regex = new RegExp("(<div[^>]*>(&nbsp;)?</div>)", "igm");
            o.content = o.content.replace(newline_regex, "<div>&nbsp;</div>");
        });
    },
    width: '100%',
    height: '400px',
    forced_root_block: 'div',
    theme : "advanced",
    mode : "specific_textareas",
    editor_selector : "mceEditor",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_source_editor_width : '500',
    theme_advanced_source_editor_height : '600',
    plugins : "fullscreen,table,autosave,paste,imagemanager,preview,hcnlink,advlist",
    plugin_preview_width : "500",
    plugin_preview_height : "600",
    content_css : "/themes/Standard/style/style.css, /themes/Standard/style_custom/style_custom.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,forecolor,backcolor,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,indent,outdent,|,link,unlink,image,|,cleanup,code,pasteword,pastetext,paste",
    theme_advanced_buttons2 : "styleselect,formatselect,fontselect,fontsizeselect,|,undo,redo,removeformat,|,fullscreen,|,preview",
    theme_advanced_buttons3 : "tablecontrols,|,hr,sub,sup,charmap",
    valid_elements : "a[*],abbr[*],acronym[*],address[*],area[*],b[*],bdo[*],big[*],blockquote[*],br[*],button[*],caption[*],center[*],cite[*],code[*],col[*],colgroup[*],del[*],dfn[*],dir[*],div[*],dl[*],dd[*],dt[*],em[*],embed[*],fieldset[*],font[*],form[*],h1[*],h2[*],h3[*],h4[*],h5[*],h6[*],hr[*],i[*],iframe[*],img[*],input[*],ins[*],kbd[*],label[*],legend[*],li[*],map[*],menu[*],nobr[*],object[*],ol[*],optgroup[*],option[*],p[*],param[*],pre[*],q[*],s[*],samp[*],script[*],select[*],small[*],span[*],strike[*],strong[*],sub[*],sup[*],table[*],tbody[*],td[*],textarea[*],tfoot[*],th[*],thead[*],tr[*],tt[*],u[*],ul[*],var[*]"
});

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");
            }
        }
    }
}

