var _editor;

function getHtml()
{
    return _editor.get_html(true);
}

function setHtml(html)
{
    var editor = $find("<%=RadEditor1.ClientID%>");

    if (editor)
        editor.set_html(html);
    else
        document.getElementById("tempContent").value = html;
		   		   
}

function OnClientLoad(editor, args)
{
    _editor = editor;

    var setEditorFocus = function ()
    {
        editor.setFocus();
    }
    window.setTimeout(setEditorFocus,500); 
    
    var tempContent = document.getElementById("tempContent");
    if (tempContent.value != '' && tempContent.value != null && editor.get_html(true) != tempContent.value) 
        editor.set_html(tempContent.value);
    
    var foreColorTool = editor.getToolByName("ForeColor");   
    var backColorTool = editor.getToolByName("BackColor");                                                    
     
    //Add the custom content button to the foreground color picker             
    foreColorTool.old_renderFooter = foreColorTool.renderFooter;
    foreColorTool.renderFooter = function()
    {
        this.old_renderFooter();                
        //Create a button and add it to the tool
        var button = document.createElement("button");
        button.innerHTML = "Add custom color..";
        button.onclick = showForeColorDialog;
        this.get_popupElement().appendChild(button);   
    }
    
    //Add the custom content button to the background color picker             
    backColorTool.old_renderFooter = backColorTool.renderFooter;
    backColorTool.renderFooter = function()
    {
        this.old_renderFooter();                
        //Create a button and add it to the tool
        var button = document.createElement("button");
        button.innerHTML = "Add custom color..";
        button.onclick = showBackColorDialog;
        this.get_popupElement().appendChild(button);   
    }
             
    function showForeColorDialog()
    {
          var foreCallbackFunction = function(sender, color)
          {  
              if(color.charAt(0) != '#')
                color = '#' + color;
                                   
              var colors = foreColorTool.get_items();              
              
              //If no colors are set get the colors from the editor
              if (colors.length == 0)
              {
                colors = editor.get_colors();
              }
       
              colors[colors.length] = color;                     
              foreColorTool.set_items(colors);                   
              //Set the color to the current selection
              editor.get_document().execCommand("ForeColor", false, color);                   
         }
         
         //Hide the color picker
         foreColorTool.hide();
     
         //Show the Custom Color dialog
         editor.showExternalDialog(
              'applets/htmleditor/customColorDialog.aspx',
              null,
              600,
              400,
              foreCallbackFunction,
              null,
              'ForeColor',
              true,
              Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
              false,
              false);
    }
           
    function showBackColorDialog()
    {
        var backCallbackFunction = function(sender, color)
        {  
            if(color.charAt(0) != '#')
            color = '#' + color;
                           
            var colors = foreColorTool.get_items();              

            //If no colors are set get the colors from the editor
            if (colors.length == 0)
            {
                colors = editor.get_colors();
            }

            colors[colors.length] = color;      
            backColorTool.set_items(colors);                  
            //Set the color to the current selection
            editor.get_document().execCommand("BackColor", false, color);                   
        }
     
        //Hide the color picker
        backColorTool.hide();
     
     //Show the Custom Color dialog
        editor.showExternalDialog(
        'applets/htmleditor/customColorDialog.aspx',
        null,
        600,
        400,
        backCallbackFunction,
        null,
        'BackColor',
        true,
        Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move,
        false,
        false);
    }
}
