﻿// JScript File

    function EditTextArea(Me, TextArea, Width, Height, ToolbarSet )
    {

      ShowEditorMessagePanel(TextArea);

      var ApplicationPath = getElementFromID("ApplicationPath").innerHTML;

      var oFCKeditor = new FCKeditor( TextArea ) ;
      oFCKeditor.BasePath = ApplicationPath +  "/FCKeditor/";
      
      
      //Custom Config file overrides the config in the basepath.
      oFCKeditor.Config["CustomConfigurationsPath"] = ApplicationPath + "/Editor/CustomEditorConfig.js";     
   
      //alert(document.location.pathname)
      //var ImagePath = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('_samples')) ;

       var HomeRelativeUrl = "";
       var WebSiteSection = "";
       
      if(getElementFromID("HomeRelativeUrl") != null)
        var HomeRelativeUrl = getElementFromID("HomeRelativeUrl").innerHTML;

      if(getElementFromID("WebSiteSection") != null)
        var WebSiteSection = "&Section=" +  getElementFromID("WebSiteSection").innerHTML;


      oFCKeditor.Config["ImageBrowserURL"] = HomeRelativeUrl + "ImagePicker.aspx?Source=Editor" + WebSiteSection;        
      oFCKeditor.Config["LinkBrowserURL"] = HomeRelativeUrl + "ImagePicker.aspx?Source=Editor" + WebSiteSection;     
           
    //  oFCKeditor.Config = HomeRelativeUrl;
    //  oFCKeditor.Config["BaseHref"] = HomeRelativeUrl
    //  alert(oFCKeditor.Config["BaseHref"])
     

  	    //############################ Editor Image Absolute Path HACK ########################
 	    //First Remove all absolute paths, then add absolute path - prevent double paths
 	    //in cases where some images may have absolute and other have relative paths
 	    //NEED this before the editor sets the content with the innerhtml of the text area 

 	    var Content = getElementFromID(TextArea).value;

 	    Content = RemoveImageAbsolutePath(Content);
 	    Content = AddImageAbsolutePath(Content) 
 	    
 	    getElementFromID(TextArea).value = Content;
 	    
 	    //############################################################

     
      oFCKeditor.Width = Width ;
      oFCKeditor.Height = Height ;
      oFCKeditor.ToolbarSet = ToolbarSet ;
      oFCKeditor.ReplaceTextarea() ;


    }  




    function GetEditorContents(Me, EditorInstance )
    {

	    // Get the editor instance that we want to interact with.
	    var oEditor = FCKeditorAPI.GetInstance(EditorInstance) ;

	   // Get the editor contents in XHTML.
	   var Content =  oEditor.GetXHTML( true )  ;		// "true" means you want it formatted.
    
    
        //################### Editor Path HACK #############
        
        Content = RemoveImageAbsolutePath(Content)
        //##################################################

    
        return Content;
    
    }



    function SetEditorContents(Me, EditorInstance,Content)
    { 

        //Need to check if editor exist first to prevent script error if editor doesnt exist

	    if(getElementFromID(EditorInstance + 'Bit').innerHTML == "1")
	    {


	    // Get the editor instance that we want to interact with.
	    var oEditor = FCKeditorAPI.GetInstance(EditorInstance) ;

	    // Set the editor contents (replace the actual one).
	    //oEditor.SetHTML( 'This is the <b>new content</b> I want in the editor.' ) ;
 	  

 	    //############################ Editor Image Absolute Path HACK ########################
 	    //First Remove all absolute paths, then add absolute path - prevent double paths
 	    //in cases where some images may have absolute and other have relative paths
 	    
 	    Content = RemoveImageAbsolutePath(Content);
 	    Content = AddImageAbsolutePath(Content) 
 	    
 	    //############################################################
 	    
 	    
 	    oEditor.SetHTML( Content ) ;
 
  
        }
  
  
    }


function FCKeditor_OnComplete(editorInstance)
{ 

    var Editor = editorInstance.Name; 
  
      
    //Set Bit to '1' after loading
    getElementFromID(Editor + 'Bit').innerHTML = "1";
  
    //Use this only to hide "loading editor" message

    HideEditorMessagePanel();
       
}






//This is a work arround since the editor have issues with displaying relative path images
function AddImageAbsolutePath(Content)
{

    var ImageRelativePath = getElementFromID("ImageRelativePath").value; //"../image/"
    var ImageAbsolutePath = getElementFromID("ImageAbsolutePath").value
  

    //var Regex = /src=\"..\/image\//gi
    var Regex = new RegExp("src=\"" + ImageRelativePath, "gi");

    var RepString = "src=\"" + ImageAbsolutePath;

    var NewContent = Content.replace(Regex, RepString);
 
    return NewContent;

}



function RemoveImageAbsolutePath(Content)
{

    var ImageRelativePath = getElementFromID("ImageRelativePath").value
    var ImageAbsoluteUrl = getElementFromID("ImageAbsoluteUrl").value
    var ImageAbsolutePath = getElementFromID("ImageAbsolutePath").value
 
 
    //MS Explorer converts a path to an absoluteurl path whenever the value 
    //is read from a 'div' element. So it will for example convert
    // "../image" to "http://localhost:61756/usersites/personal/eric/image/" 
    //So also need to check for that. Firefox doesnt have this issue
 
    //FIRST replace "http://localhost:61756/usersites/personal/eric/image/"
    var Regex1 = new RegExp("src=\"" + ImageAbsoluteUrl, "gi");
   
    //THEN replace "/usersites/personal/eric/image/"
    var Regex2= new RegExp("src=\"" + ImageAbsolutePath, "gi");
   
    var RepString = "src=\"" + ImageRelativePath;

    var NewContent = Content.replace(Regex1, RepString).replace(Regex2, RepString);


    return NewContent;

}


/*
//This is a work arround since the editor have issues with displaying relative path images
function AddImageAbsolutePath(Content)
{

    var HomeRelativeUrl = getElementFromID("HomeRelativeUrl").innerHTML;

    //var Regex = /src=\"..\/image\//gi
    var Regex = new RegExp("src=\"../image/", "gi");


    var RepString = "src=\"" + HomeRelativeUrl + "image/";

    var NewContent = Content.replace(Regex, RepString);
 
    return NewContent;

}



function RemoveImageAbsolutePath(Content)
{

    var HomeRelativeUrl = getElementFromID("HomeRelativeUrl").innerHTML;
    
    var Regex = new RegExp("src=\"" + HomeRelativeUrl + "image/", "gi");
    
    
    var RepString = "src=\"../image/";

    var NewContent = Content.replace(Regex, RepString);


    return NewContent;

}


*/
