﻿function createStyleRule(selector, declaration) {
    if (!document.getElementsByTagName ||
      !(document.createElement || document.createElementNS)) return;
    var agt = navigator.userAgent.toLowerCase();
    var is_ie = ((agt.indexOf("msie") != -1) &&  (agt.indexOf("opera") == -1));
    var is_iewin = (is_ie &&  (agt.indexOf("win") != -1));
    var is_iemac = (is_ie &&  (agt.indexOf("mac") != -1));
    if (is_iemac) return; // script doesn't work properly in IE/Mac
    var head = document.getElementsByTagName("head")[0]; 
    var style = (typeof document.createElementNS != "undefined") ?
      document.createElementNS("http://www.w3.org/1999/xhtml", "style") :
      document.createElement("style");
    if (!is_iewin) {
        var styleRule = document.createTextNode(selector + " {" + declaration + "}");
            style.appendChild(styleRule); // bugs in IE/Win
    }
        style.setAttribute("type", "text/css");
    style.setAttribute("media", "screen"); 
    head.appendChild(style);
    if (is_iewin &&  document.styleSheets &&  document.styleSheets.length > 0) {
        var lastStyle = document.styleSheets[document.styleSheets.length - 1];
        if (typeof lastStyle.addRule == "object") {
            lastStyle.addRule(selector, declaration);
        }
    }
}

function openInfoWindow(container)
{
    var content = document.getElementById(container);
    var x = window.open('', 'Info', "scrollbars=1,width=600,height=600,top=150,left=400");
    x.document.write(content.innerHTML);
    x.focus();
}

function tinyMceInit(textAreaClientId, blogEntryId) 
{
        tinyMCE.init({
            // General options
            mode: "none",
            language: "nb",
            theme: "advanced",
            plugins: "advlink,vnimage,paste,linkautodetect",
 
            // Auto clean on paste
            paste_auto_cleanup_on_paste: true,
            paste_use_dialog : false,
            paste_convert_headers_to_strong : false,
            paste_strip_class_attributes : "all",
            paste_remove_spans : true,
            paste_remove_styles: true,
            
            // Theme options
            theme_advanced_buttons1: "formatselect,bold,italic,underline,link,unlink,vnimage",
            theme_advanced_buttons2: "",
            theme_advanced_toolbar_location: "top",
            theme_advanced_toolbar_align: "left",
            theme_advanced_statusbar_location: "",
            theme_advanced_resizing: false,
            theme_advanced_blockformats: "p,h3",
            remove_script_host: true,
            relative_urls: false,
            setup: function(ed) {
                ed.onChange.add(function(ed, l) {
                    var hidden = document.getElementById(textAreaClientId);
                    hidden.value = l.content;
                });

                // Force Paste-as-Plain-Text
                ed.onPaste.add(function(ed, e, o) {
                    ed.execCommand('mcePasteText', true);
                    return tinymce.dom.Event.cancel(e);
                });
            },

            // set entry id
            vnimage_entry_id: blogEntryId
        });   
        
        tinyMCE.idCounter=0;
        
        tinyMCE.execCommand('mceAddControl', false, textAreaClientId);
}