/* 	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/25/2008 Nathan Townsend
 	
 	
 	
*/







//  Editor Text Control [NT 11/24/2008]
function EditorRadio(){
  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("EditorRadio.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function(ctrlName)
    {
      try{
    
          name = ctrlName;

          control = $("<div condition='' />");

          control.html("<input type='radio' id='" + name + "' name='" + name + "'><span>Radio Button</span>");

          control.addClass("component EditorRadio");

          control.attr("ref", "EditorRadio");

          input = $($(control).find("input"));

          span = $($(control).find("span"));

          span.css("cursor", "text");

          span.addClass("notRequired");

          span.click(function(){input.focus()});
          
          //Log.Add("EditorRadio.Create", "Created: " + input.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorRadio.Create", err, LogType.Error); }
    };

    this.DefaultMode = function()
    {
      try{
			
			  featuresAdded = true;
      
        RemoveFeatures();
				
				this.AttachFunctions();
        
        Helper.RemoveComponentID(control);
        
      } catch(err){ Log.Add("EditorRadio.DefaultMode", err, LogType.Error); }
    };

    this.EditMode = function()
    {
      try{
      
        AddFeatures();
				
  			this.DetachFunctions();				
        
      } catch(err){ Log.Add("EditorRadio.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("Checked"), this.GetChecked, this.SetChecked);
          
          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("EditorRadio.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("EditorRadio.Load", "Loaded: " + input.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorRadio.Load", err, LogType.Error); }
    };

    this.Refresh = function()
    {
      try{
      
          //Log.Add("EditorRadio.Refresh", "Refreshed: " + input.attr("name"), LogType.Info);
					
          
      } catch(err){ Log.Add("EditorRadio.Refresh", err, LogType.Error); }
    };
		
		
		this.GetErrorMessage = function(ser)
		{
      try{ 
			
			  if(this.GetRequired())
				{
					var name = input.attr("name");
					
					if(ser.indexOf("&" + name + "=") == -1)
					  return "You must choose one of the radios '" + span.text() + "'";
					
					return null;
				}
      } catch(err){ Log.Add("EditorText.GetErrorMessage", err, LogType.Error); }		  
		}		


		this.ErrorStatus = function(boolStatus)
		{
      try{
			
			  if(boolStatus)
				{
				  var name = control.find("input").attr("name");
					
					$("div[Ref='EditorRadio']").each(function(){
					
						if($(this).find("input[name='" + name + "']").length > 0)
						{
						  $(this).addClass("error");
						}
					});
				}
				else
				{
				  var name = control.find("input").attr("name");
					
					$("div[Ref='EditorRadio']").each(function(){
					
						if($(this).find("input[name='" + name + "']").length > 0)
						{
						  $(this).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.GetControl = function()
    { 
      try{
       
       return control; 
      
      } catch(err){ Log.Add("EditorRadio.GetControl", err, LogType.Error); }
    };

    this.GetCaption = function()
    { 
      try{
        return span.text(); 
      } catch(err){ Log.Add("EditorRadio.GetCaption", err, LogType.Error); }
    };

    this.GetName = function()
    { 
      try{ 
        return input.attr("name"); 
      } catch(err){ Log.Add("EditorRadio.GetName", err, LogType.Error); }
    };

    this.GetRequired = function()
    {
      try{ 
        return span.hasClass("required");
      } catch(err){ Log.Add("EditorRadio.GetRequired", err, LogType.Error); }
    };
    
    this.GetValue = function()
    {
      try{ 
        return input.val();
      } catch(err){ Log.Add("EditorRadio.GetValue", err, LogType.Error); }
    };

    this.SetCaption = function(newCaption){
      try{
        span.text(newCaption);
      } catch(err){ Log.Add("EditorRadio.SetCaption", 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("EditorRadio.SetRequired", err, LogType.Error); }
    };
    
    this.SetValue = function(newValue){try{ input.val(newValue); } catch(err){ Log.Add(this.Ref + "SetValue", err, LogType.Error); }};




    /* PUBLIC PROPERTIES */    
    this.NameRequired = true;
    
    this.Ref = "EditorRadio";

  





  // 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.EditorRadioProp", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.bind("dblclick.EditorRadio", function(event){ event.stopPropagation(); input.removeAttr("checked"); });
        
        control.addClass("editing");
        
        featuresAdded = true;
        
    } catch(err){ Log.Add("EditorRadio.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.EditorRadioProp");
				
        control.unbind("dblclick.EditorRadio");				
        
        control.removeClass("editing");
        
        featuresAdded = false;
        
    } catch(err){ Log.Add("EditorRadio.RemoveFeatures", err, LogType.Error); }
  }

 // Catches every error in Editor Text
 } catch(err){ Log.Add("EditorRadio", err, LogType.Error); }
}


/* +++++++++++++++++++ END EditorRadio CONTROL +++++++++++++++++++*/






