// grab the language
var tinymce_language = trans.Language.match(/(\w{2})_.*/)[1];

if(!$.isMobile() && !$.testingActive){
    $(function(){
        $(".mceEditor").filter(function(){
            // exclude the threaded comments and edit existing comment
            return $(this).parents(".HL_Form_Create_Comment_Threaded, .HL_Form_Edit_Comment").length === 0;
            })
        .tinymce({
            // Location of TinyMCE script
            script_url : '/common/js/tiny_mce/tiny_mce_gzip.php',
            theme: "advanced",
            language: tinymce_language,
            plugins: "spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
            relative_urls: false,
            extended_valid_elements: "style[]",
            cleanup_callback: "myCustomCleanup",
            theme_advanced_toolbar_location: "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location: "bottom",
            theme_advanced_path : false,
            theme_advanced_resizing: true,
            theme_advanced_resizing_use_cookie: false,
            theme_advanced_resize_horizontal: false,
            theme_advanced_blockformats: "p,h1,h2,h3,pre",
            theme_advanced_buttons1: "link,unlink,image,table,separator,"
            + "formatselect,forecolor,separator,"
            + "bold,italic,underline,strikethrough,separator,"
            + "bullist,numlist,outdent,indent,separator,"
            + "justifyleft,justifycenter,justifyright,justifyfull,separator,"
            + "spellchecker,code,fullscreen,pasteword",
            theme_advanced_buttons2: "",
            width: "100%",
            spellchecker_rpc_url : "/common/js/tiny_mce/plugins/spellchecker/rpc.php"
        });
    });
}

function myCustomCleanup(type, value)
{
    switch (type) {
        case "insert_to_editor":
            break;
        case "insert_to_editor_dom":
            var scriptTags = value.getElementsByTagName('script');
            if (scriptTags && scriptTags.length) {
                for (var i = (scriptTags.length - 1); i >= 0; i--) {
                    scriptTags[i].parentNode.removeChild(scriptTags[i]);
                }
            }
            break;
        case "get_from_editor":
            value = value.replace(/<p>\s*<\/p>/gi, '');
            break;
        case "get_from_editor_dom":
            break;
    }
    return value;
}