/* 	Copyright 2008 Maximum Processing Inc
	This script provides javascript Control Objects that control the behavior of the editor

	ABOUT THIS SCRIPT 
	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
 	
 	
 	
*/







//  Editor Text Control [NT 11/24/2008]
function EditorCheckBox(){
  try{
	
  this.inheritFrom = TemplateBase;

	this.inheritFrom();		
	

  /* PRIVATE PROPERTIES */
  var control = null;

  var featuresAdded = false;
  
  var input = 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, input);
					
          if(Editor.IsEnabled())
            this.EditMode();
            
          this.Refresh();        
          
      } catch(err){ Log.Add("EditorCheckBox.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function(ctrlName)
    {
      try{
    
          name = ctrlName;

          control = $("<div condition='' />");

          control.html("<input type='checkbox' id='" + name + "' name='" + name + "'><span>Check Box</span>");

          control.addClass("component EditorCheckBox");

          control.attr("ref", "EditorCheckBox");

          input = $($(control).find("input"));

          span = $($(control).find("span"));

          span.css("cursor", "text");

          span.addClass("notRequired");

          span.click(function(){input.focus()});
          
          //Log.Add("EditorCheckBox.Create", "Created: " + input.attr("name"), LogType.Info);
          
      } catch(err){ Log.Add("EditorCheckBox.Create", err, LogType.Error); }
    };

    this.DefaultMode = function()
    {
      try{
			
        RemoveFeatures();
				
				this.AttachFunctions();
        
        Helper.RemoveComponentID(control);
				
      } catch(err){ Log.Add("EditorCheckBox.DefaultMode", err, LogType.Error); }
    };

    this.EditMode = function()
    {
      try{
      
        AddFeatures();
				
				this.DetachFunctions();
        
      } catch(err){ Log.Add("EditorCheckBox.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, this.SetName);
          
          //properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Checked"), this.GetChecked, this.SetChecked);
          
          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("Value"), this.GetValue, this.SetValue);
          
          return properties;
          
       } catch(err){ Log.Add("EditorCheckBox.GetProperties", err, LogType.Error); }
    };

    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);

          span = $(control.find("span"));

          span.css("cursor", "text");
            
          input = $(control.find("input"));

          span.click(function(){input.focus()});
					
					this.BaseLoad(control, input);
					
          //Log.Add("EditorCheckBox.Load", "Loaded: " + input.attr("name"), LogType.Info);
          
      } catch(err){ Log.Add("EditorCheckBox.Load", err, LogType.Error); }
    };

    this.Refresh = function()
    {
      try{
      
          //Log.Add("EditorCheckBox.Refresh", "Refreshed: " + input.attr("name"), LogType.Info);

      } catch(err){ Log.Add("EditorCheckBox.Refresh", err, LogType.Error); }
    };
		
		
		this.GetErrorMessage = function()
		{
      try{ 
			  if(this.GetRequired())
				{
				  if( !input[0].checked )
					{
					  return "You must check the box named '" + 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{

			  input.focus();
				
      } catch(err){ Log.Add("EditorText.SetFocus", err, LogType.Error); }
		}
		
		
		
		
    
    this.GetChecked = function()
    {
      try{
        
        var s = input[0].outerHTML;
        
        var start = s.indexOf("#");
        
        var stop = s.indexOf("#", start + 1) + 1;
        
        if((start > -1) && (stop > start))
          return s.substring(start, stop);
        else
          return "";
      
      } catch(err){ Log.Add( this.Ref + ".GetChecked", err, LogType.Error); }
    };

    this.GetControl = function()
    { 
      try{
       
       return control; 
      
      } catch(err){ Log.Add("EditorCheckBox.GetControl", err, LogType.Error); }
    };

    this.GetCaption = function()
    { 
      try{
        return span.text(); 
      } catch(err){ Log.Add("EditorCheckBox.GetCaption", err, LogType.Error); }
    };

    this.GetName = function()
    { 
      try{ 
        return input.attr("name"); 
      } catch(err){ Log.Add("EditorCheckBox.GetName", err, LogType.Error); }
    };

    this.GetRequired = function()
    {
      try{ 
        return span.hasClass("required");
      } catch(err){ Log.Add("EditorCheckBox.GetRequired", err, LogType.Error); }
    };
    
    this.GetValue = function(){try{  return input.attr("value"); } catch(err){ Log.Add( this.Ref + ".GetValue", err, LogType.Error); }};

    this.SetCaption = function(newCaption){
      try{
        span.text(newCaption);
      } catch(err){ Log.Add("EditorCheckBox.SetCaption", err, LogType.Error); }
    };
    
		/*
    this.SetChecked = function(newValue)
    {
      try{
        
        var ctrl = Helper.GetEditorComponent(control);
        
        var name = ctrl.GetName();
        
        var newInput = $("<input type='checkbox' id='" + name + "' name='" + name + "' " + newValue + " >");
        
        input.replaceWith(newInput);
        
        input = newInput;
        
      } catch(err){ Log.Add(this.Ref + "SetChecked", err, LogType.Error); }
    };
    
    this.SetName = function(newValue)
    {
      try{
        
        var ctrl = Helper.GetEditorComponent(control);
        
        var checked = ctrl.GetChecked();
       
        var newInput = $("<input type='checkbox' id='" + newValue + "' name='" + newValue + "' " + checked + " >");
        
        input.replaceWith(newInput);
        
        input = newInput;
        
      } catch(err){ Log.Add(this.Ref + "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("EditorCheckBox.SetRequired", err, LogType.Error); }
    };

    this.SetValue = function(newValue){try{ input.attr("value", newValue); } catch(err){ Log.Add(this.Ref + "SetValue", err, LogType.Error); }};


    /* PUBLIC PROPERTIES */    
    this.NameRequired = true;
    
    this.Ref = "EditorCheckBox";





  // 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")
        {
          span.attr("unselectable", "on");
          span.css("cursor", "move");
          control.draggable({containment: "parent", delay:500, handle:span});
        }

        control.bind("dblclick.EditorCheckBox", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("editing");
        
        featuresAdded = true;
        
        //Log.Add("EditorCheckBox - AddFeatures", "Features Added", LogType.Info);
        
    } catch(err){ Log.Add("EditorCheckBox.AddFeatures", err, LogType.Error); }
  }

  // removes features from the control that were previously added 
  function RemoveFeatures()
  {
    try{
        if(!featuresAdded)
          return;
        
        span.css("cursor", "default");

        span.removeAttr("unselectable");
        
        control.draggable("destroy");

        control.unbind("dblclick.EditorCheckBox");
        
        control.removeClass("editing");
        
        featuresAdded = false;
        
        //Log.Add("EditorCheckBox - RemoveFeatures", "Features Removed", LogType.Info);
				
        
    } catch(err){ Log.Add("EditorCheckBox.RemoveFeatures", err, LogType.Error); }
  }

 // Catches every error in Editor Text
 } catch(err){ Log.Add("EditorCheckBox", err, LogType.Error); }
}


/* +++++++++++++++++++ END EditorCheckBox CONTROL +++++++++++++++++++*/
