//Browser Version
// Scripts use to customize the editor control.

var ie = (navigator.appName == "Microsoft Internet Explorer");

function ResizeContentArea(editor)     //Resize Editor to Fit Content    
{            
    if (editor.InitialHeight == -1)         
    {         
        editor.InitialHeight = editor.Document.body.clientHeight;      
    }           
    var theIFrame = document.getElementById("RadEContentIframe" + editor.Id);     
    var oDoc = editor.Document;
    if (!ie)
    {
        theIFrame.style.height = editor.InitialHeight + "px";
        editor.SetSize(editor.GetWidth(), editor.InitialHeight);
    }
    var targetHeight = oDoc.body.scrollHeight;
    if (targetHeight > editor.InitialHeight)         
    {           
        theIFrame.style.height = parseInt(targetHeight) + "px";
        if (!ie) editor.SetSize(editor.GetWidth(), targetHeight);//FireFox bug, TD does not decrease height      
    }         
}         
function AllowTransparency(editor)
{
    var iframe = document.getElementById("RadEContentIframe" + editor.Id);
    iframe.allowTransparency = true;
    iframe.style.background = "Transparent";
    editor.Document.body.style.margins = "0px";
}
function MoveToolbars(editor)  
{        
    // This function is wired to an editor by adding  OnClientLoad="MoveFixedToolbar"  to the editor  
    // Should set editor ToolbarMode="Default"
    var originalToolbarBaseID = "Top"+editor.Id;  
    var divToolBarContainer = document.createElement('div'); // This is a wrapper DIV to hold all of the toolbars for a given editor.  
    
    // The wrapper is what we will manipulate when we show/hide an editors tools  
    divToolBarContainer.id = originalToolbarBaseID;  
    divToolBarContainer.style.display = "none"; // Wrapper is initially hidden
    
    var originalToolbarBase = document.getElementById(originalToolbarBaseID);
    while(originalToolbarBase.childNodes.length > 0)// Loop through all the tool bars and move them to the DIV.
    {
        var currentToolbar = originalToolbarBase.firstChild;
        //originalToolbarBase.removeChild(currentToolbar);
        var newDiv = document.createElement('div'); //Wrap the toolbar in a DIV.     
        newDiv.id = originalToolbarBaseID;  
        newDiv.appendChild(currentToolbar); // Add the toolbar to the DIV.      
        newDiv.style.display = "block";
        newDiv.style.clear = "both";
        newDiv.style.float = "none";
        divToolBarContainer.appendChild(newDiv); // Add the toolbar (and its wrapper) to the destination wrapper DIV.   
    }
    //Call AddToolbar function on the Page.  This is a function offered by the page for registering toolbars.
    EPEditingAddToolbar(divToolBarContainer); //Add an editors wrapper to the main DIV that holds toolbars      
    // add an onfocus event to each editor  
    editor.AttachEventHandler ("onfocus", function (e)  
    {  
        EPEditingShowToolbar(divToolBarContainer);  
    });
}

//Copy CSS Styles to Editor from Parent HTML Tag    
function CopyStylesToEditorHelper(element)
{
    if (element.currentStyle) // Handle IE
        return element.currentStyle;
    else // Handle Others
        return document.defaultView.getComputedStyle(element,null);
    return null;
}

function CopyStylesToEditor(editor)
{
    var theIFrame = document.getElementById("RadEContentIframe" + editor.Id); 
    var theDocBody = editor.Document.body;
    var IFrameCompStyle = CopyStylesToEditorHelper(theIFrame);
    if (IFrameCompStyle != null) {
        theDocBody.style.color = IFrameCompStyle.color;
        theDocBody.style.fontFamily = IFrameCompStyle.fontFamily;
        theDocBody.style.fontSize = IFrameCompStyle.fontSize;
        theDocBody.style.fontWeight = IFrameCompStyle.fontWeight;
        theDocBody.style.lineHeight = IFrameCompStyle.lineHeight;
        theDocBody.style.textAlign = IFrameCompStyle.textAlign;
        theDocBody.style.letterSpacing = IFrameCompStyle.letterSpacing;
   }
}

function AttachResizeEvents(editor)  //Attach resize events to handle text changes    
{         
    editor.InitialHeight = -1;         
        
    editor.GetContentArea().style.overflow = "hidden";         
    editor.Document.body.scroll = "no";       
            
    var resizeFnRef = function anon(){ResizeContentArea(editor)};         
    editor.AttachEventHandler("RADEVENT_SEL_CHANGED", resizeFnRef);        
    editor.AttachEventHandler("keyup", resizeFnRef);
} 

function OnClientLoad(editor)
{
    CopyStylesToEditor(editor);
    AllowTransparency(editor);
    ResizeContentArea(editor);
    MoveToolbars(editor);
    
    //Declare events to resize editor to fit page.
    var resizeFnRef = function anon(){ResizeContentArea(editor)};
    window.setTimeout(resizeFnRef,1000);
    AttachResizeEvents(editor);
}