﻿
// Text Editor Initialization
tinyMCE.init({ 
	mode : "none",
	init_instance_callback : "GetEditorID",
	theme : "advanced",
	plugins : "style,layer,table,save,advhr,advimage,uploader,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,visualchars,nonbreaking,xhtmlxtras,template",
	theme_advanced_buttons1_add : "|,fontsizeselect, mceUploader",
	theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview",
	theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace",
	theme_advanced_buttons3_add_before : "tablecontrols,separator",
	theme_advanced_toolbar_location : "bottom",
	theme_advanced_toolbar_align : "left",
	theme_advanced_path_location : "bottom",
	content_css : "App_Themes/kennedy_generic/font_styles.css",
    plugin_insertdate_dateFormat : "%Y-%m-%d",
    plugin_insertdate_timeFormat : "%H:%M:%S",
	extended_valid_elements : "hr[class|width|size|noshade],font[face|size|style],span[class|align|style]",
	file_browser_callback : "openBrowser",
	theme_advanced_resize_horizontal : false,
	nonbreaking_force_tab : true,
	apply_source_formatting : true,
	relat2ive_urls : false,
	rem2ove_script_host : false,
	encoding : "xml"
});

var FileWindow;
function openBrowser(field_name, url, type, win) {
    // capture reference to the window containing the image insertion UI
	FileWindow = win;
	
	// open the ImageLoader
	window.open("../../../../BCEditor/ImageLoader.aspx?field_name=" + field_name, "ImageLoader", "scrollbars=1");
}

function returnImageUrl(field_name, imageUrl)
{
    // callback function used by ImageLoader
    FileWindow.document.forms[0].elements[field_name].value = imageUrl;
}

function fileBrowserCallBack(field_name, url, type, win) {
	// This is where you insert your custom filebrowser logic
	alert("Example of filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

	// Insert new URL, this would normaly be done in a popup
	win.document.forms[0].elements[field_name].value = "someurl.htm";
}

function focusEditor(ClientID)
{
    document.getElementById(ClientID).parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.zIndex = "99";
}
function unfocusEditor(ClientID)
{
    document.getElementById(ClientID).parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.zIndex = "0";
}


// Editor initialization storage variables for multiple editor instances
var HiddenID = new Array();
var EditorControlCount = 0;
var EditorInstanceCount = 0;

// XMLHttpRequest object
var XHReq = CreateXMLHttpRequest();
var AppRoot = "";
var MCEUpdateContentURI = "";

// Function creates a new instance of the editor
function AddEditor(textareaID, hiddenInstID, hiddenContentID, contentID, appRoot, updateContentPage)
{
    // Store hidden field id in array
    HiddenID[EditorControlCount] = hiddenInstID;
    EditorControlCount++;
    
    // Add the specified textarea to the editor object
    tinyMCE.execCommand('mceAddControl', true, textareaID);
    
    // Store the editor's content id in hidden field
    document.getElementById(hiddenContentID).value = contentID;
    
    // Store URI for update page
    AppRoot = appRoot;
    MCEUpdateContentURI = appRoot + updateContentPage;
}

// Function records the instance's editor id
// (This is the 'init_instance_callback' function;
//  it is called automatically after 'OpenEditor'.)
function GetEditorID(inst)
{
    // Store the instance's editor id in hidden field
    document.getElementById(HiddenID[EditorInstanceCount]).value = inst.editorId;
    
    // Increment instance counter
    EditorInstanceCount++;
}

// Function loads the editor object with its content text
function LoadEditorContent(textareaID, hiddenID, containerID)
{
    // Get the instance's editor id from hidden field
    var EditorID = document.getElementById(hiddenID).value;
    
    // Get the content text
    var ContentText = document.getElementById(containerID).innerHTML;
    
    // Set the editor's content and focus
    tinyMCE.execInstanceCommand(EditorID, 'mceSetContent', false, ContentText, true);
}

// Function updates the content container from the editor
function SaveEditorContent(hiddenID, containerID, hiddenContentID)
{
    // Get the instance's editor id from hidden field
    var EditorID = document.getElementById(hiddenID).value;
    
    // Get the content text
    var ContentText = tinyMCE.getContent(EditorID);
    
    // Update the content container
    document.getElementById(containerID).innerHTML = ContentText;
    
    // Get the content id
    var ContentID = document.getElementById(hiddenContentID).value;
    
    // Update content in database via asynchronous POST request
    UpdateContentData(ContentID, ContentText);
}

// Function creates XMLHttpRequest object if possible
function CreateXMLHttpRequest() {
    try { return new XMLHttpRequest(); } catch(e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
    
    // Browse does not support JavaScript XMLHttpRequest
    alert("Content was not updated on server; browser must support JavaScript XMLHttpRequest.");
    return null;
}

// Function handles response from XMLHttpRequest object
function XHReqStateChanged()
{
    // if request readystate is complete and response status is successful load
    if(XHReq.readyState == 4 && XHReq.status == 200)
    {
        // Output response text
        if(XHReq.responseText.length < 70)
        {
            alert(XHReq.responseText);
        }
        else
        {
            // Authentication cookie has expired
            if(confirm("Content changes were not saved on the server.\nSession has expired and the page will be redirected to login form."))
            {
                // Redirect to login form
                window.location = AppRoot + "Login.aspx?ReturnUrl=" + escape(window.location);
            }
        }
    }
}

// Function makes an asynchronous POST to update content data on server
function UpdateContentData(ContentID, ContentText)
{
    if(XHReq != null)
    {
        var xml = "<?xml version='1.0'?><Content id='" + ContentID + "'><![CDATA[" + ContentText + "]]></Content>";
        XHReq.open("POST", MCEUpdateContentURI, true);
        XHReq.onreadystatechange = XHReqStateChanged;
        XHReq.setRequestHeader('Content-Type','text/xml');
        XHReq.send(xml);
    }
}