/* 	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 EditorSubmitButton Nathan Townsend
 	
 	
 	
*/




function EditorSubmitButton(){
  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("EditorSubmitButton.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function(ctrlName)
    {
      try{
    
          name = ctrlName;

          control = $("<div condition='' function='CLICK' definition='Communication.SerialRequest($(\"#rightColumn\"))' />");

          control.html("<input type='button' value='Submit' id='" + name + "' name='" + name + "'></input>");

          control.addClass("component EditorSubmitButton");

          control.attr("ref", "EditorSubmitButton");

          input = $($(control).find("input"));

          //Log.Add("EditorSubmitButton.Create", "Created: " + input.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorSubmitButton.Create", err, LogType.Error); }
    };

    this.DefaultMode = function()
    {
      try{
      
        RemoveFeatures();
				
				this.AttachFunctions();
				
				if((control.attr("converted") == "true") && (PropertiesEditor.Saving == true))
				{
				  alert("The button upgrade process has been completed");
				  control.removeAttr("converted");
				}
				
				
				/*
				input.unbind("click.default").bind("click.default", function(){
					
					$(this).attr("disabled", "true");

				  var id = $(this).attr("id");
										
					setTimeout(function(){ $("#" + id).removeAttr("disabled"); }, 3000);
					
				});
				*/
				
        
        Helper.RemoveComponentID(control);
        
      } catch(err){ Log.Add("EditorSubmitButton.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("EditorSubmitButton.EditMode", err, LogType.Error); }
    };
    


    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);
					
          input = $(control.find("input"));
					
					this.BaseLoad(control, input);
					
					// the following code is used to remove the hard coded onclick
					var ret = input.attr("onclick");
        
        	if(Utilities.IsFunction(ret))
        	{
            ret = ret.toString();
						
						if(ret.indexOf("Communication.")>-1)					
    					input.removeAttr("onclick");
					}
					
					if(control.attr("converted") == "true")
					{
					  alert("The button has been converted. \n\nYou now need to update the scripts on the buttons.\n\n     ***Note: 'function onclick()' needs to be removed from the scripts\n\n     Communication.Back no longer works, use 'Communication.SerialRequest($(\"#rightColumn\"))' to call the post process and set the template to be loaded (this is the default script for new EditorSubmitButtons)");
					}

          //Log.Add("EditorSubmitButton.Load", "Loaded: " + input.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorSubmitButton.Load", err, LogType.Error); }
    };

    this.Refresh = function()
    {
      try{
      
          ResizeControl();
          
          //Log.Add("EditorSubmitButton.Refresh", "Refreshed: " + input.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorSubmitButton.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("Validate"), this.GetCauseValidation, this.SetCauseValidation);					
          
          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);
						
            properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("DefaultButton"), this.GetDefaultButton, this.SetDefaultButton);						
          }
          
          return properties;
          
       } catch(err){ Log.Add("EditorSubmitButton.GetProperties", err, LogType.Error); }
    };

		
    this.GetCauseValidation = function()
    { 
      try{
       
       var cv = input.attr("CauseValidation");
			 
			 return (cv != null);
      
      } catch(err){ Log.Add("EditorSubmitButton.CauseValidation", err, LogType.Error); }
    };		
		
		
    this.GetDefaultButton = function()
    { 
      try{
       
        return input.attr("DefaultButton");

      } catch(err){ Log.Add("EditorSubmitButton.CauseValidation", err, LogType.Error); }
    };				
		

    this.GetControl = function()
    { 
      try{
       
       return control; 
      
      } catch(err){ Log.Add("EditorSubmitButton.GetControl", err, LogType.Error); }
    };

    this.GetCaption = function()
    { 
      try{
        return input.val(); 
      } catch(err){ Log.Add("EditorSubmitButton.GetCaption", err, LogType.Error); }
    };

    this.GetHeight = function()
    { 
      try{
        return input.height(); 
      } catch(err){ Log.Add("EditorSubmitButton.GetHeight", err, LogType.Error); }
    };

    this.GetName = function()
    { 
      try{ 
        return input.attr("name"); 
      } catch(err){ Log.Add("EditorSubmitButton.GetName", err, LogType.Error); }
    };

    
    this.GetWidth = function()
    { 
      try{
        return input.width(); 
      } catch(err){ Log.Add("EditorSubmitButton.GetWidth", err, LogType.Error); }
    };
    

    this.SetCaption = function(newCaption){
      try{
        input.val(newCaption);
      } catch(err){ Log.Add("EditorSubmitButton.SetCaption", err, LogType.Error); }
    };
		
		
    this.SetDefaultButton = function(newCaption){
      try{
			
        input.attr("DefaultButton", newCaption);
				
      } catch(err){ Log.Add("EditorSubmitButton.SetCaption", err, LogType.Error); }
    };		
		
    this.SetCauseValidation = function(boolVal)
    { 
      try{
       
       if(boolVal)
			   input.attr("CauseValidation", "true");
			 else
			   input.removeAttr("CauseValidation");
			 

      } catch(err){ Log.Add("EditorSubmitButton.SetCauseValidation", err, LogType.Error); }
    };			

    this.SetName = function(newName){
      try{
        
      } catch(err){ Log.Add("EditorText.SetName", err, LogType.Error); }
    };

		/*
    this.SetOnClick = function(Script){
      try{

          input.attr("onclick", Utilities.FilterScript(Script));
          
      } catch(err){ Log.Add("EditorSubmitButton.SetOnClick", err, LogType.Error); }
    };
		*/
    
    this.SetWidth = function(newWidth){
      try{
          var w = Utilities.ToNumber(newWidth) + 2;
          input.css("width", w);
          ResizeControl();
      } catch(err){ Log.Add("EditorSubmitButton.SetWidth", err, LogType.Error); }
    };

    this.SetHeight = function(newHeight){
      try{
          var h = Utilities.ToNumber(newHeight) + 2;
          input.css("height", h);
          ResizeControl();
      } catch(err){ Log.Add("EditorSubmitButton.SetHeight", err, LogType.Error); }
    };


    /* PUBLIC PROPERTIES */    
    this.NameRequired = true;
    
    this.Ref = "EditorSubmitButton";




  /* 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("EditorSubmitButton.ResizeControl", err, LogType.Error); }
  }


  // 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){ 
              input.width(ui.size.width);
              input.height(ui.size.height);
              ResizeControl();
            }
          });
        }
        
        var ctrl = Helper.GetEditorComponent(control);
        
        control.unbind("dblclick.EditorSubmitButton").bind("dblclick.EditorSubmitButton", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("editing");
        
        featuresAdded = true;
        
        //Log.Add("EditorSubmitButton - AddFeatures", "Features Added", LogType.Info);
				
        
    } catch(err){ Log.Add("EditorSubmitButton.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.EditorSubmitButton");
        
        var ctrl = Helper.GetEditorComponent(control);
        
        //control.click(new Function(ctrl.GetOnClick()));
        
        control.removeClass("editing");
        
        featuresAdded = false;
        
        //Log.Add("EditorSubmitButton - RemoveFeatures", "Features Removed", LogType.Info);
				
        
    } catch(err){ Log.Add("EditorSubmitButton.RemoveFeatures", err, LogType.Error); }
  }

 // Catches every error in Editor Text
 } catch(err){ Log.Add("EditorSubmitButton", err, LogType.Error); }
}


/* +++++++++++++++++++ END EditorSubmitButton CONTROL +++++++++++++++++++*/






