/* 	Copyright 2008 Maximum Processing Inc
	A lable that can be styled and positioned

	ABOUT THIS SCRIPT
	Required public methods
	  . AppendTo
          . DefaultMode
          .  EditMode
          . GetControl
          . Load
          
        Required Public Properties
          . Ref
          . NameRequired
	
        Dependencies:
          . logging.js
          . container.rules.js
          . utilities.js

	Change Log
 	Created 11/21/2008 Nathan Townsend
 	11/22/2008 - added logging NT
 	
*/








function EditorLabel(){
  try{
	

  this.inheritFrom = TemplateBase;

	this.inheritFrom();	


	
  /* PRIVATE PROPERTIES */
  var control = null;

  var featuresAdded = false;
  
  //var properties = null;
  
  var label = null;
  
 
  /* PUBLIC METHODS */

    // Appends the control to a container element
    this.AppendTo = function(ContainerElement)
    {
      try{
          $(ContainerElement).append(control);
					
					this.BaseLoad(control, label);
          
          if(Editor.IsEnabled())
            this.EditMode();
            
      } catch(err){ Log.Add("EditorLabel.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function()
    {
      try{
          control = $("<div condition='' />");

          control.html("<label class='handle Default'>Label</label>");

          control.addClass("component EditorLabel");

          control.attr("ref", "EditorLabel");

          label = $($(control).find("label"));

          //Log.Add("EditorLabel.Create", "Created new label", LogType.Info);
					
        
      } catch(err){ Log.Add("EditorLabel.Create", err, LogType.Error); }
    };
    

    this.DefaultMode = function()
    {
      try{
        
				featuresAdded = true;
				
        RemoveFeatures();
        
				this.AttachFunctions();
				
        Helper.RemoveComponentID(control);
        
      } catch(err){ Log.Add("EditorLabel.DefaultMode", err, LogType.Error); }
    };

    this.EditMode = function()
    { 
      try{
			
        AddFeatures();
				
				this.DetachFunctions();		
						
      } catch(err){ Log.Add("EditorLabel.EditMode", err, LogType.Error); }
    };
    
    this.GetProperties = function()
    {
      try {

          var properties = this.GetBaseProperties();

          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Caption"), this.GetCaption, this.SetCaption);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Class"), this.GetClass, this.SetClass, this.Classes);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Style"), this.GetStyle, this.SetStyle);

	        return properties;
          
       } catch(err){ Log.Add("EditorLabel.GetProperties", err, LogType.Error); }
    };

    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);
					
          label = $(control.find("label"));
					
					this.BaseLoad(control, label);

      } catch(err){ Log.Add("EditorLabel.Load", err, LogType.Error); };
    };

    this.GetControl = function(){try{ return control; } catch(err){ Log.Add("EditorLabel.GetControl", err, LogType.Error); } };

    this.GetCaption = function(){try{ return label.html(); } catch(err){ Log.Add("EditorLabel.GetCaption", err, LogType.Error); } };
    
    this.GetClass = function(){try{ return label.attr("class"); } catch(err){ Log.Add("EditorLabel.GetClass", err, LogType.Error); } };
    
    this.GetStyle = function(){try{ return label.attr("style"); } catch(err){ Log.Add("EditorLabel.GetStyle", err, LogType.Error); }};

    this.SetCaption = function(newCaption){try{ label.html(newCaption); } catch(err){ Log.Add("EditorLabel.SetCaption", err, LogType.Error); }};
    
    this.SetClass = function(newClass){try{ label.attr("class", newClass); } catch(err){ Log.Add("EditorLabel.SetClass", err, LogType.Error); }};
    
    this.SetStyle = function(newStyle){try{ label.attr("style", newStyle); } catch(err){ Log.Add("EditorLabel.SetStyle", err, LogType.Error); }};


    /* PUBLIC PROPERTIES */
    this.Classes = ["Default", "PageTitle", "DefaultBold", "Required", "RequiredBold", "Extended", "ExtendedBold", "RequiredExtended", "RequiredExtendedBold", "InfoColor", "AttentionColor"];
    
    this.NameRequired = false;
    
    this.Ref = "EditorLabel";




  /* PRIVATE METHODS */

  // 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});
        }

        if(pref == "StaticContainer") 
        {
          control.resizable({
            autoHide: true, 
            containment: "parent", 
            distance: 25,
            knobHandles: true, 
            maxHeight: control.height(),
            minHeight: control.height(),
            transparent: true
          });
        }
        
        control.bind("dblclick.EditorLabel", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("editing");
        
        featuresAdded = true;
        
     } catch(err){ Log.Add("EditorLabel.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.EditorLabel");
        
        control.removeClass("editing");
				
        featuresAdded = false;
        
     } catch(err){ Log.Add("EditorLabel.RemoveFeatures", err, LogType.Error); }
  }

	
	
 } catch(err){ Log.Add("EditorLabel", err, LogType.Error); }
}
