/* 	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 Nathan Townsend
 	
 	Depricated 02/20/2009 - EditorSubmitButton can be modified to work as a regular button now that custom scripts can be added
 	
*/







//  Editor Text Control [NT 11/24/2008]
function EditorButton(){
  try{
	
  this.inheritFrom = TemplateBase;

	this.inheritFrom();		
	

  /* PRIVATE PROPERTIES */
  var control = null;

  var featuresAdded = false;
  
  var input = null;
  
  var properties = null;
  
 
  /* PUBLIC METHODS */

    // Appends the control to a container element
    this.AppendTo = function(ContainerElement)
    {
      try{
    
          $(ContainerElement).append(control);
					
					this.BaseLoad(control, input);
					
          if(Editor.IsEnabled())
            this.EditMode();
            
          this.Refresh();
					
					control.attr("origT", control.css("top"));

					control.attr("origL", control.css("left"));					        
          
      } catch(err){ Log.Add("EditorButton.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function(ctrlName)
    {
      try{
    
          name = ctrlName;

          control = $("<div condition='' />");

          control.html("<input type='button' btnType='Custom' value='Button' id='" + name + "' name='" + name + "'></input>");

          control.addClass("component EditorButton");

          control.attr("ref", "EditorButton");

          input = $($(control).find("input"));

          //Log.Add("EditorButton.Create", "Created: " + input.attr("name"), LogType.Info);
          
      } catch(err){ Log.Add("EditorButton.Create", err, LogType.Error); }
    };

    this.DefaultMode = function()
    {
      try{
			
        RemoveFeatures();
				
				this.AttachFunctions();
        
        Helper.RemoveComponentID(control);
        
      } catch(err){ Log.Add("EditorButton.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("EditorButton.EditMode", err, LogType.Error); }
    };
    


    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);
					
          input = $(control.find("input"));
					
					this.BaseLoad(control, input);
					
					if(control.attr("converted") != "true")
					{
  					control.attr("ref", "EditorSubmitButton");
	  				control.removeClass("EditorButton");
		  			control.addClass("EditorSubmitButton");
			  		control.attr("converted", "true");
				  	var c = input.attr("onclick");
  					var obj = new Object();
	  				obj.Name = "click";
		  			obj.Body = c;
			  		this.SetFunctions([obj]);
				  	input.removeAttr("onclick");
					  alert("This screen contains an EditorButton and needs to be updated.\n\nFollow the instructions below:\n\n    1) Open this template in edit mode and save the template (don't make any changes - the template will be updated automatically)\n\n    2) Reopen the template in edit mode to complete this operation");
					}   
					
					// the following code is used to remove the hard coded onclick
					// TODO: figure out a way to move the onclick event to the template base functions
					return;
					var ret = input.attr("onclick");
        
        	if(Utilities.IsFunction(ret))
        	{
            ret = ret.toString();
						
						if(ret.indexOf("Communication.")>-1)					
    					input.removeAttr("onclick");
					}									

          //Log.Add("EditorButton.Load", "Loaded: " + input.attr("name"), LogType.Info);
          
      } catch(err){ Log.Add("EditorButton.Load", err, LogType.Error); }
    };

    this.Refresh = function()
    {
      try{
      
          ResizeControl();
          
          //Log.Add("EditorButton.Refresh", "Refreshed: " + input.attr("name"), LogType.Info);
          
      } catch(err){ Log.Add("EditorButton.Refresh", 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("OnClick"), this.GetOnClick, this.SetOnClick);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Type"), this.GetType, this.SetType, this.Types);
          
          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("EditorButton.GetProperties", err, LogType.Error); }
    };
    
    this.GetControl = function()
    { 
      try{
       
       return control; 
      
      } catch(err){ Log.Add("EditorButton.GetControl", err, LogType.Error); }
    };

    this.GetCaption = function()
    { 
      try{
        return input.val(); 
      } catch(err){ Log.Add("EditorButton.GetCaption", err, LogType.Error); }
    };

    this.GetHeight = function()
    { 
      try{
        return input.height(); 
      } catch(err){ Log.Add("EditorButton.GetHeight", err, LogType.Error); }
    };

    this.GetName = function()
    { 
      try{ 
        return input.attr("name"); 
      } catch(err){ Log.Add("EditorButton.GetName", err, LogType.Error); }
    };

    this.GetOnClick = function()
    { 
      try{
        var ret = input.attr("onclick");
        
        if(Utilities.IsFunction(ret))
        {
          ret = ret.toString();
          ret = ret.substring(22);
          ret = ret.substring(0, ret.length -1);  
        }
        
        return ret;
      
      } catch(err){ Log.Add("EditorButton.GetOnClick", err, LogType.Error); }
    };
    
    this.GetType = function()
    {
      try{
        
        return input.attr("btnType");
        
      } catch(err){ Log.Add("EditorButton.GetType", err, LogType.Error); }  
    };
    
    this.GetWidth = function()
    { 
      try{
        return input.width(); 
      } catch(err){ Log.Add("EditorButton.GetWidth", err, LogType.Error); }
    };
    
       

    this.SetCaption = function(newCaption){
      try{
        input.val(newCaption);
      } catch(err){ Log.Add("EditorButton.SetCaption", err, LogType.Error); }
    };

		this.SetLeft = function(val)
		{
		  try{
			  control.css("left", val);
			} catch(err){ Log.Add(this.Ref + ".SetLeft", err, LogType.Error); }
		};		
		
		
    this.SetOnClick = function(Script){
      try{
        
        input.attr("onclick", Utilities.FilterScript(Script));
          
      } catch(err){ Log.Add("EditorButton.SetOnClick", err, LogType.Error); }
    };
		
		this.SetTop = function(val)
		{
		  try{
			  control.css("top", val);
			} catch(err){ Log.Add(this.Ref + ".SetTop", err, LogType.Error); }
		};			
    
    this.SetType = function(newValue){
      try{
        
        input.attr("btnType", newValue);
        
        switch(newValue)
        {
          case "Back":
            input.attr("onclick", "Communication.Back()");
            break;
          
          case "Forward":
            input.attr("onclick", "Communication.Forward()");
            break;
        }
        
      } catch(err){ Log.Add("EditorButton.SetType", err, LogType.Error); }
    };
    
    this.SetWidth = function(newWidth){
      try{
          var w = Utilities.ToNumber(newWidth) + 4;
          input.css("width", w);
          ResizeControl();
      } catch(err){ Log.Add("EditorButton.SetWidth", err, LogType.Error); }
    };

    this.etHeight = function(newHeight){
      try{
          var h = Utilities.ToNumber(newHeight) + 4;
          input.css("height", h);
          ResizeControl();
      } catch(err){ Log.Add("EditorButton.SetHeight", err, LogType.Error); }
    };


    /* PUBLIC PROPERTIES */    
    this.NameRequired = true;
    
    this.Ref = "EditorButton";
    
    this.Types = ["Custom", "Back", "Forward"];

  



  /* PRIVATE METHODS */
  // sizes the control around the input element
  function ResizeControl(){
    try{
 
        var width = input.outerWidth();

        var height = input.outerHeight();

        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("EditorButton.ResizeControl", err, LogType.Error); }
  }


  // adds features to the control based on the rules specified for this type of control
  function AddFeatures()
  {
    try{
		
				return;
		  
		
        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){ 
              input.width(ui.size.width);
              input.height(ui.size.height);
              ResizeControl();
            }
          });
        }
        
        var ctrl = Helper.GetEditorComponent(control);
        
        ctrl.SetOnClick(ctrl.GetOnClick()); // prevents the script from executing in design mode
        
        control.bind("dblclick.EditorButton", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("editing");
        
        featuresAdded = true;
        
        //Log.Add("EditorButton - AddFeatures", "Features Added", LogType.Info);
        
    } catch(err){ Log.Add("EditorButton.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.EditorButton");
        
        var ctrl = Helper.GetEditorComponent(control);
        
        control.click(new Function(ctrl.GetOnClick()));
        
        control.removeClass("editing");
        
        featuresAdded = false;
        
        //Log.Add("EditorButton - RemoveFeatures", "Features Removed", LogType.Info);
        
    } catch(err){ Log.Add("EditorButton.RemoveFeatures", err, LogType.Error); }
  }

 // Catches every error in Editor Text
 } catch(err){ Log.Add("EditorButton", err, LogType.Error); }
}


/* +++++++++++++++++++ END EditorButton CONTROL +++++++++++++++++++*/






