﻿function EditableTextLoadEditor(editorId, onLoad)
{
    var settings = document.getElementById(editorId);
    var editor = new FCKeditor(editorId) ;
    editor.Config['CustomConfigurationsPath'] = '/BuiltIn/Wavenet.Web.UI.WebControls.EditableText.Fck.js.ashx' ;
    editor.BasePath = settings.BasePath || '/wvnportal/fckeditor2/';
    if (settings.StylesXmlPath)
    {
        editor.Config.StylesXmlPath = settings.StylesXmlPath;
    }
    if (settings.EditorAreaCss)
    {
        editor.Config.EditorAreaCSS = settings.EditorAreaCss;
    }
    if (settings.ImageBrowserURL)
    {
        editor.Config.ImageBrowserURL = settings.ImageBrowserURL;
    }
    if (settings.FlashBrowserURL)
    {
        editor.Config.FlashBrowserURL = settings.FlashBrowserURL;
    }
    if (settings.LinkBrowserURL)
    {
        editor.Config.LinkBrowserURL = settings.LinkBrowserURL;
    }
    if (settings.ImageUploadURL)
    {
        editor.Config.ImageUploadURL = settings.ImageUploadURL;
    }
    if (settings.FlashUploadURL)
    {
        editor.Config.FlashUploadURL = settings.FlashUploadURL;
    }    
    
    if (settings.EditPage)
    {
        editor.Config.EditPage = settings.EditPage;
    }
    if (settings.linkSelect)
    {
        editor.Config.linkSelect = settings.linkSelect;
    }
    if (settings.CustomLg)
    {
        editor.Config.CustomLg = settings.CustomLg;
    }
    if (settings.PortalAlias)
    {
        editor.Config.PortalAlias = settings.PortalAlias;
    }
    if (settings.linkUrl)
    {
        editor.Config.linkUrl = settings.linkUrl;
    }
    if (settings.LinkCustom)
    {
        editor.Config.LinkCustom = settings.LinkCustom;
    }
    editor.Config.LinkInternalWindowWidth = 400;
    editor.Config.LinkInternalWindowHeight = 400;
    
    
    if (settings.ToolBar)
    {
        editor.ToolbarSet = settings.ToolBar;
    }
    if (settings.EditorHeight)
    {
        editor.Height = parseInt(settings.EditorHeight, 10);
    }
    editor.ReplaceTextarea();
    function WaitEndInit()
    {
        if (window.FCKeditorAPI && FCKeditorAPI.GetInstance(editorId) && FCKeditorAPI.GetInstance(editorId).Status == FCK_STATUS_COMPLETE)
        {
            onLoad(FCKeditorAPI.GetInstance(editorId));
        }
        else
        {
            setTimeout(WaitEndInit, 10);
        }
    }
    WaitEndInit();
}
function EditableTextEdit(control, ajax)
{
    control = document.getElementById(control);
    var editorArea = document.getElementById(control.id + "_editor");
    var editLink = document.getElementById(control.id + "_Commands").getElementsByTagName("a")[0];
    
    var editorControl = control.id + "_editor_control";
    if (editorArea.loaded)
    {
        if (editorArea.style.display == "none")
        {
            control.style.display = "none";
            editorArea.style.display = "block";
            editLink.innerHTML = "Close";
        }
        else
        {
            control.style.display = "block";
            editorArea.style.display = "none";
            editLink.innerHTML = "Edit";
        }
        return;
    }
    EditableTextLoadEditor(editorControl, function(editor)
    {
        var settings = document.getElementById(editorControl);
        editorArea.loaded = true;
        function SaveComplete(message)
        {
            alert("Saved successfully.");
            settings.Value = editor.GetXHTML();
            control.innerHTML = message;
            control.style.display = "block";
            editorArea.style.display = "none";
            editLink.innerHTML = "Edit";
        }
        function SaveError(message)
        {
            alert(message.substring(0, message.length - 1));
        }
        editor.JSSave = function()
        {
            ajax(editor.GetXHTML(), SaveComplete, SaveError);
        };
        editor.SetHTML(settings.Value);
        control.style.display = "none";
        editorArea.style.display = "block";
        editLink.innerHTML = "Close";
    });
}