/* 	Copyright 2008 Maximum Processing Inc
	This script provides javascript Control Objects that control the behavior of the editor

	ABOUT THIS SCRIPT [* = not required on EditableContent Container]
	Required public methods
	  0. AppendTo
          1. DefaultMode
          2  EditMode
          3. GetControl
          4. Load
          
        Required Public Properties
          1. Ref
          2. NameRequired
	
        Dependencies:
        1. logging.js
        2. container.rules.js
        3. utilities.js

	Change Log
 	Created 11/24/2008, copied from EditorMemo Nathan Townsend
 	
 	
 	
*/







//  Editor Text Control [NT 11/24/2008]
function EditorMemo(){
  try{

  this.inheritFrom = TemplateBase;

	this.inheritFrom();
	
	
  /* PRIVATE PROPERTIES */
  var control = null;

  var featuresAdded = false;
  
  var textarea = null;

  var properties = null;
  
  var span = null;

  
  /* PUBLIC METHODS */

	
    // Appends the control to a container element
    this.AppendTo = function(ContainerElement)
    {
      try{
    
          $(ContainerElement).append(control);
					
					this.BaseLoad(control, textarea);
          
          if(Editor.IsEnabled())
            this.EditMode();
            
          this.Refresh();  
					
					control.attr("origT", control.css("top"));

					control.attr("origL", control.css("left"));				      
          
      } catch(err){ Log.Add("EditorMemo.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function(ctrlName)
    {
      try{
    
          name = ctrlName;

          control = $("<div condition='' />");

          control.html("<span>Memo</span><textarea id='" + name + "' name='" + name + "'></textarea>");

          control.addClass("component EditorMemo");

          control.attr("ref", "EditorMemo");

          textarea = $($(control).find("textarea"));

          span = $($(control).find("span"));

          span.css("cursor", "text");

          span.addClass("notRequired");

          span.click(function(){textarea.focus()});
          
          //Log.Add("EditorMemo.Create", "Created: " + textarea.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorMemo.Create", err, LogType.Error); }
    };

    this.DefaultMode = function()
    {
      try{
        
        RemoveFeatures();
				
				this.AttachFunctions();
				
        Helper.RemoveComponentID(control);
				
	      if(control.attr("EditHTML") == "true" && PropertiesEditor.Saving == false)
			  {
				  var height = textarea.height();
				  span.css("display", "none");
				  oFCKeditor = new FCKeditor(textarea.attr('id'), '100%', height);
    		  oFCKeditor.BasePath = "../../fckeditor/" ;
	  		  oFCKeditor.ReplaceTextarea();

				}
        
      } catch(err){ Log.Add("EditorMemo.DefaultMode", err, LogType.Error); }
    };

    this.EditMode = function()
    {
      try{
      
			  control.attr("origT", control.css("top"));
				
				control.attr("origL", control.css("left"));
			
        AddFeatures();
				
				this.DetachFunctions();
        
      } catch(err){ Log.Add("EditorMemo.EditMode", err, LogType.Error); }
    };
    
    this.GetProperties = function()
    {
      try{
          var pref = Helper.GetParentRef(control);
             
          var properties = this.GetBaseProperties();
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Name"), this.GetName, null);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Caption"), this.GetCaption, this.SetCaption);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Required"), this.GetRequired, this.SetRequired);
					
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("EditHTML"), this.GetEditHTML, this.SetEditHTML);					

          if(pref == "StaticContainer")
          {
            properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Width"), this.GetWidth, this.SetWidth);
            
            properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Height"), this.GetHeight, this.SetHeight);
          }
          
          return properties;
          
       } catch(err){ Log.Add("EditorMemo.GetProperties", err, LogType.Error); }
    };

    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);
					
          span = $(control.find("span"));

          span.css("cursor", "text");
            
          textarea = $(control.find("textarea"));

          span.click(function(){textarea.focus()});
					
					this.BaseLoad(control, textarea);
					
					textarea.css("margin-left", "");
					
					ResizeControl();				

          //Log.Add("EditorMemo.Load", "Loaded: " + textarea.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorMemo.Load", err, LogType.Error); }
    };

    this.Refresh = function()
    {
      try{
      
          ResizeControl();
          
          //Log.Add("EditorMemo.Refresh", "Refreshed: " + textarea.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorMemo.Refresh", err, LogType.Error); }
    };

		
		this.GetErrorMessage = function()
		{
      try{ 
			  if(this.GetRequired())
				{
				  if( Utilities.Trim(textarea.val()).length == 0 )
					{
					  return "You must enter the field marked '" + span.text() + "'";
					}
					
					return null;
				}
      } catch(err){ Log.Add("EditorText.GetErrorMessage", err, LogType.Error); }		  
		}		

		this.ErrorStatus = function(boolStatus)
		{
      try{
			
			  if(boolStatus)
				  control.addClass("error");
				else
				  control.removeClass("error");			  
			  
			           
      } catch(err){ Log.Add("EditorText.ErrorStatus", err, LogType.Error); }		
		}
		
		this.SetFocus = function()
		{
      try{

			  textarea.focus();
				
      } catch(err){ Log.Add("EditorText.SetFocus", err, LogType.Error); }
		}		
		

    this.GetControl = function()
    { 
      try{
       
       return control; 
      
      } catch(err){ Log.Add("EditorMemo.GetControl", err, LogType.Error); }
    };

    this.GetCaption = function()
    { 
      try{
        return span.text(); 
      } catch(err){ Log.Add("EditorMemo.GetCaption", err, LogType.Error); }
    };
		
    this.GetEditHTML = function()
    { 
      try{
       
       return control.attr("EditHTML"); 
      
      } catch(err){ Log.Add("EditorMemo.GetControl", err, LogType.Error); }
    };		

    this.GetName = function()
    { 
      try{ 
        return textarea.attr("name"); 
      } catch(err){ Log.Add("EditorMemo.GetName", err, LogType.Error); }
    };

    this.GetRequired = function()
    {
      try{ 
        return span.hasClass("required");
      } catch(err){ Log.Add("EditorMemo.GetRequired", err, LogType.Error); }
    };

    this.GetWidth = function()
    { 
      try{
        return textarea.width(); 
      } catch(err){ Log.Add("EditorMemo.GetWidth", err, LogType.Error); }
    };
    
    this.GetHeight = function()
    { 
      try{
        return textarea.height(); 
      } catch(err){ Log.Add("EditorMemo.GetHeight", err, LogType.Error); }
    };

    this.SetCaption = function(newCaption){
      try{
        span.text(newCaption);
      } catch(err){ Log.Add("EditorMemo.SetCaption", err, LogType.Error); }
    };
		
    this.SetEditHTML = function(value)
    { 
      try{
       
        control.attr("EditHTML", value);
				
      } catch(err){ Log.Add("EditorMemo.GetControl", err, LogType.Error); }
    };


		/*
    this.SetName = function(newName){
      try{
        
        var ctrl = Helper.GetEditorComponent(control);

        var width = ctrl.GetWidth();
        
        var height = ctrl.GetHeight();
        
        var value = textarea.val();
        
        var newTA = $("<textarea id='" + newName + "' name='" + newName + "'></textarea>");
        
        newTA.val(value);

        newTA.css("height", height);
        
        newTA.css("width", width);
        
        textarea.replaceWith(newTA);
        
        textarea = newTA;
        
      } catch(err){ Log.Add("EditorText.SetName", err, LogType.Error); }
    };      
		*/
		
    this.SetRequired = function(required){
      try{
          span.removeClass("required").removeClass("notRequired");

          if(required == "true")
            span.addClass("required");
          else
            span.addClass("notRequired");
            
      } catch(err){ Log.Add("EditorMemo.SetRequired", err, LogType.Error); }
    };

    this.SetWidth = function(newWidth){
      try{
          var w = Utilities.ToNumber(newWidth);
          textarea.css("width", w);
          ResizeControl();
      } catch(err){ Log.Add("EditorMemo.SetWidth", err, LogType.Error); }
    };

    this.SetHeight = function(newHeight){
      try{
          var h = Utilities.ToNumber(newHeight);
          textarea.css("height", h);
          ResizeControl();
      } catch(err){ Log.Add("EditorMemo.SetHeight", err, LogType.Error); }
    };


    /* PUBLIC PROPERTIES */    
    this.NameRequired = true;
    
    this.Ref = "EditorMemo";

  



  /* PRIVATE METHODS */
  // sizes the control around the textarea element
  function ResizeControl(){
    try{
 
        var width = textarea.outerWidth();

        var height = textarea.outerHeight();
				
				var mr = Utilities.ToNumber(span.css("margin-right"));
				
				var pt = Utilities.ToNumber(span.css("padding-top"));

				span.css("width", width - mr).css("height", height - pt);

        width = width + Utilities.ToNumber(control.css("padding-left")) + Utilities.ToNumber(control.css("padding-right")) + Utilities.ToNumber(control.css("border-left-width")) + Utilities.ToNumber(control.css("border-right-width"));
        
        height = height + Utilities.ToNumber(control.css("padding-top")) + Utilities.ToNumber(control.css("padding-bottom")) + Utilities.ToNumber(control.css("border-top-width")) + Utilities.ToNumber(control.css("border-bottom-width"));
        
        control.css("width", width);
        
        control.css("height", height);
				
				control.css("top", control.attr("origT"));
				
				control.css("left", control.attr("origL"));								
        
    } catch(err){ Log.Add("EditorMemo.ResizeControl", err, LogType.Warning); }
  }


  // adds features to the control based on the rules specified for this type of control
  function AddFeatures()
  {
    try{
        if(featuresAdded)
          return;
        
        var pref = Helper.GetParentRef(control);

        if(pref == "StaticContainer")
        {
          control.css("cursor", "move");
          control.draggable({
					 containment: "parent", 
					 delay:500,
					 stop: function(){
					 	control.attr("origT", control.css("top"));
						control.attr("origL", control.css("left"));
					 }
					});
        }

        if(pref == "StaticContainer")
        {
          control.resizable({
            autoHide: true, 
            distance: 25,
            resize: function(e, ui){ 
              textarea.width(ui.size.width);
              textarea.height(ui.size.height);
              ResizeControl();
            },
						stop: function(){
						  var t = Utilities.ToNumber($(this).css("top"));
							$(this).css("top", t+1);
						  var l = Utilities.ToNumber($(this).css("left"));
							$(this).css("left", l+1);							
						}						


          });
        }
        
        control.bind("dblclick.EditorMemo", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("editing");
        
        span.attr("unselectable", "on");
        
        featuresAdded = true;
        
        //Log.Add("EditorMemo - AddFeatures", "Features Added", LogType.Info);
				
        
    } catch(err){ Log.Add("EditorMemo.AddFeatures", err, LogType.Error); }
  }

  // removes features from the control that were previously added 
  function RemoveFeatures()
  {
    try{
        if(!featuresAdded)
          return;
        
        control.css("cursor", "default");

        control.draggable("destroy");

        control.resizable("destroy");
          
        control.unbind("dblclick.EditorMemo");
        
        control.removeClass("editing");
        
        span.removeAttr("unselectable");
        
        featuresAdded = false;
        
        //Log.Add("EditorMemo - RemoveFeatures", "Features Removed", LogType.Info);
				
        
    } catch(err){ Log.Add("EditorMemo.RemoveFeatures", err, LogType.Error); }
  }

 // Catches every error in Editor Text
 } catch(err){ Log.Add("EditorMemo", err, LogType.Error); }
}


/* +++++++++++++++++++ END EditorMemo CONTROL +++++++++++++++++++*/






