﻿
function setupTinyMCE(file, id, theme, valid, invalid) {
    if (!tinyMCE.get(id)) {
        simple = "";
        if (theme == "simple") {
            simple = { mode: "none",
                theme: "advanced",
                plugins: "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
                theme_advanced_buttons1: "bold, italic, underline, strikethrough, separator, undo, cleanup, redo, separator,  link, unlink, separator, bullist, numlist",
                theme_advanced_buttons2: "",
                theme_advanced_buttons3: "",
                theme_advanced_toolbar_location: "bottom",
                theme_advanced_toolbar_align: "center",
                paste_remove_styles: true,
                external_image_list_url: "tinymce_images.js",
                document_base_url: "/",
                setup: function(ed) { createTinyMCE(ed); },
                content_css: "/appgenie.css"
            }
            tinyMCE.settings = simple;
        }
        if (theme == "external") {
            simple = { mode: "none",
                theme: "advanced",
                plugins: "safari,autoresize,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
                theme_advanced_buttons1: "bold, italic, underline, strikethrough, separator, undo, cleanup, redo, separator,  mylink, unlink, separator, bullist, numlist",
                theme_advanced_buttons2: "",
                theme_advanced_buttons3: "",
                theme_advanced_toolbar_location: "external",
                theme_advanced_resizing_min_height: "10px",
                theme_advanced_resizing: true,
                paste_remove_styles: true,
                convert_urls: false,
                height: "10px",
                theme_advanced_toolbar_align: "center",
                external_image_list_url: "tinymce_images.js",
                document_base_url: "/",
                setup: function(ed) {
                    createTinyMCE(ed); ed.onClick.add(function(ed) { foc(ed); });
                    ed.addButton('mylink', { title: 'Add a link',
                        image: '/images/link.gif',
                        onclick: function() { showObject(id + "_dv"); }
                    });
                },
                content_css: "/appgenie.css"
            }
            tinyMCE.settings = simple;
        }
        if (theme == "advanced") {
            simple = { mode: "none",
                theme: "advanced",
                plugins: "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
                theme_advanced_buttons1_add: "fontselect,fontsizeselect",
                theme_advanced_buttons2_add: "separator,insertdate,inserttime,preview,zoom,separator,forecolor,backcolor",
                theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
                theme_advanced_buttons3_add_before: "tablecontrols,separator",
                theme_advanced_buttons3_add: "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
                theme_advanced_toolbar_location: "top",
                theme_advanced_toolbar_align: "left",
                convert_urls: false,
                external_image_list_url: "tinymce_images.js",
                document_base_url: "/",
                setup: function(ed) { createTinyMCE(ed); },
                content_css: "/appgenie.css"
            }
            tinyMCE.settings = simple;
        }
        if (document.getElementById(id)) {
            document.getElementById(id)._file = file;
            document.getElementById(id)._theme = "advanced";
            document.getElementById(id)._valid = valid;
            document.getElementById(id)._invalid = invalid;
            tinyMCE.execCommand('mceAddControl', false, id);
        }
    }
}

function paramsTinyMCE(path, id) {
    if (tinyMCE.get(id)) {
    }
}

function showTinyMCE(id) {
    if (!tinyMCE.get(id)) {
        tinyMCE.execCommand('mceAddControl', false, id);
    }
}

function hideTinyMCE(id) {
    if (tinyMCE.get(id)) {
        //tinyMCE.execCommand('mceFocus', false, id);                    
        tinyMCE.execCommand('mceRemoveControl', false, id);
    }
}

function initTinyMCE() {
    tinyMCE.init({ mode: "none",
        theme: "advanced",
        //plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
        theme_advanced_buttons1: "",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        external_image_list_url: "tinymce_images.js",
        document_base_url: "/",
        setup: function(ed) { createTinyMCE(ed); },
        content_css: "/appgenie.css"
    });
}

function killMCE(obj) {
    if (obj) {
        for (i = 0; i < obj.elements.length; i++) {
            hideTinyMCE(obj.elements[i].id);
        }
    }
}

function createTinyMCE(ed) {
    obj = document.getElementById(ed.id);
    ed.settings['external_image_list_url'] = obj._file + ".js";
    ed.settings['content_css'] = obj._file + ".css";
    ed.settings['theme'] = obj._theme;
    ed.settings['invalid_elements'] = obj._invalid;
    ed.settings['extended_valid_elements'] = obj._valid;
}

function foc(ed) {
    tmp = ed.getContent();
    if (tmp.indexOf("What's on your mind?") >= 0) { ed.setContent(""); }
    if (tmp.indexOf("Give us your feedback!") >= 0) { ed.setContent(""); }
}

function AddLink(id, text) {
    obj = document.getElementById(text);
    if (obj) {
        text = obj.value;
        if (text.substring(0, 1) != "?") {
            if (text.toLowerCase().indexOf("http://") != 0) {
                text = "http://" + text;
            }
            i = text.toLowerCase().indexOf(document.domain.toLowerCase());
            if (i >= 0) {
                text = "<a href=\"" + text.substring(i + document.domain.length) + "\">" + obj.value + "</a>";
            } else {
                text = "<a href=\"" + text + "\" target=\"new\">" + obj.value + "</a>";
            }
        } else {
            text = "<a href=\"/Portal.aspx" + text + "\">" + obj.value + "</a>";
        }
        tinyMCE.execCommand('mceInsertContent', false, text);
        hideObject(id);
        obj.value = "http://";
    }
}

