/* 	Copyright 2008 Maximum Processing Inc
	This script provides definitions for Javascript Container Components

	ABOUT THIS SCRIPT

        DEPENDENCIES:
          . logging.js
          . controls.helper.js
          . editor.js
          . utilities.js
          

	Change Log
 	Created 11/19/2008 Nathan Townsend
 	11/22/2008 - added logging NT
*/




//  StaticContainer [NT 11/19/2008]
function StaticContainer(){
  try{
	
  this.inheritFrom = TemplateBase;

	this.inheritFrom();		

  /* PRIVATE PROPERTIES */
  var control = null;
  
  var h3 = null;

  var featuresAdded = false;
  
  var properties = null;
  


  /* PUBLIC METHODS */

	
    // Appends the control to a container element
    this.AppendTo = function(ContainerElement)
    {
      try{
          $(ContainerElement).append(control);
					
					this.BaseLoad(control);

          if(Editor.IsEnabled())      
            this.EditMode();
      } catch(err){ Log.Add("StaticContainer.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function()
    {
      try{
          control = $("<div condition='' class='component StaticContainer' ref='StaticContainer'><h3 class='handle'>Caption</h3></div>");
          
          h3 = $(control.find("h3"));
          
          //Log.Add("StaticContainer.Create", "Created", LogType.Info);
          
      } catch(err){ Log.Add("StaticContainer.Create", err, LogType.Error); }
    };
    
    this.DefaultMode = function()
    {
      try{
        
        RemoveFeatures();
				
        Helper.RemoveComponentID(control);
        
      } catch(err){ Log.Add("StaticContainer.DefaultMode", err, LogType.Error); }
    };

    this.EditMode = function()
    {
      try{
        AddFeatures();
      } catch(err){ Log.Add("StaticContainer.EditMode", err, LogType.Error); }
    };

    this.GetProperties = function()
    {
      try{
          var properties = this.GetBaseProperties();
          
          var pref = Helper.GetParentRef(control);

          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Borderless"), this.GetBorderless, this.SetBorderless);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("ID"), this.GetID, this.SetID);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Caption"), this.GetCaption, this.SetCaption);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Height"), this.GetHeight, this.SetHeight);
          
          if(pref == "StaticContainer")
            properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Width"), this.GetWidth, this.SetWidth);
          
          //Log.Add("StaticContainer.GetProperties", "Properties Loaded", LogType.Info);
        
          return properties;
          
       } catch(err){ Log.Add("StaticContainer.GetProperties", err, LogType.Error); }
    };
    
    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);
					
					this.BaseLoad(control);
          
          h3 = $(control.find(">h3"));
          
      } catch(err){ Log.Add("StaticContainer.Load", err, LogType.Error); }      
    };
    
    this.GetBorderless = function(){try{ return control.hasClass("borderless");  } catch(err){ Log.Add("StaticContainer.GetBorder", err, LogType.Error); }};

    this.GetCaption = function(){try{  return h3.text();} catch(err){ Log.Add("StaticContainer.GetCaption", err, LogType.Error); }};
    
    this.GetControl = function(){try{  return control; } catch(err){ Log.Add("StaticContainer.GetControl", err, LogType.Error); }};
    
    this.GetHeight = function(){try{  return  control.height(); } catch(err){ Log.Add("StaticContainer.GetHeight", err, LogType.Error); }};
    
    this.GetID = function(){try{  return  control.attr("id"); } catch(err){ Log.Add("StaticContainer.GetID", err, LogType.Error); }};
    
    this.GetWidth = function(){try{  return  control.width(); } catch(err){ Log.Add("StaticContainer.GetWidth", err, LogType.Error); }};
    
    this.SetBorderless = function(boolValue){
      try{
        
        if(boolValue)
          control.addClass("borderless");
        else
          control.removeClass("borderless");
          
      } catch(err){ Log.Add(this.Ref + "SetBorderless", err, LogType.Error); }
    };
    
    this.SetCaption = function(newCaption){
      try{
        h3.text(newCaption);
        if(h3.text()=="")
           h3.css("visibility", "hidden");
        else
           h3.css("visibility", "visible");
         
      } catch(err){ Log.Add("StaticContainer.SetCaption", err, LogType.Error); }
    };
    
    this.SetHeight = function(newHeight){
      try{
        var h = Utilities.ToNumber(newHeight) + 1;
        control.css("height", h);
      } catch(err){ Log.Add("StaticContainer.SetHeight", err, LogType.Error); }
    };
    
    this.SetID = function(newID){try{ control.attr("id", newID);} catch(err){ Log.Add("StaticContainer.SetID", err, LogType.Error); }};
    
    this.SetWidth = function(newWidth){
      try{
          var w = Utilities.ToNumber(newWidth); // + 12;
          control.css("width", w);
      } catch(err){ Log.Add("StaticContainer.SetWidth", err, LogType.Error); }      
    };
    
    /* PUBLIC PROPERTIES */    
    this.NameRequired = false;
    
    this.Ref = "StaticContainer";
    
    this.Accept = ["StaticContainer", "EditorText", "EditorLabel", "EditorDropDown", "EditorMemo", "EditorCheckBox", "EditorSubmitButton", "EditorRadio", "EditorLink", "TransferList"]; 

  
  



  /* PRIVATE METHODS */

  // adds features to the control based on the rules specified for this type of control
  function AddFeatures()
  {
    try{
        if(featuresAdded)
          return;
        
        $(control).droppable({
            accept: Utilities.GetAcceptedComponents(["StaticContainer", "EditorText", "EditorLabel", "EditorDropDown", "EditorMemo", "EditorCheckBox", "EditorSubmitButton", "EditorRadio", "EditorLink", "TransferList"]),
            greedy: true,
            activeClass: 'droppable-active',
            hoverClass: 'droppable-hover',
            drop: function(ev, ui) {
              var ref = $(ui.draggable).attr("ref");
              Editor.BuildComponent(ref, $(this));
            }
        });
        
        var pref = Helper.GetParentRef(control);
          
        if((pref == "StaticContainer")) 
        {
          h3.attr("unselectable", "on");
          h3.css("cursor", "move");
          control.draggable({containment: "parent", delay:500, handle:h3});
        }
       
        if((pref == "StaticContainer")) 
        {
          control.resizable({
            autoHide: true, 
            containment: "parent", 
            distance: 25,
            knobHandles: true, 
            transparent: false,
						stop: function(){
						  var t = Utilities.ToNumber($(this).css("top"));
							$(this).css("top", t+1);
						  var l = Utilities.ToNumber($(this).css("left"));
							$(this).css("left", l+1);							
						}
          });
        }
        
        control.bind("dblclick.staticcontainer", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        h3.attr("unselectable", "on");
				

        
        featuresAdded = true;
        
     } catch(err){ Log.Add("StaticContainer.AddFeatures", err, LogType.Error); }
  }

  
  // removes features from the control that were previously added 
  function RemoveFeatures()
  {
    try{
        if(!featuresAdded)
          return;
        
        $(control).droppable("destroy");
        
        var pref = Helper.GetParentRef(control);
          
        if(pref == "StaticContainer") 
        {
          h3.removeAttr("unselectable");
          h3.css("cursor", "default");
          control.draggable("destroy");
        }

        if(pref == "StaticContainer") 
          control.resizable("destroy");
          
        control.unbind("dblclick.staticcontainer");
        
        h3.removeAttr("unselectable");

          
        featuresAdded = false;
        
     } catch(err){ Log.Add("StaticContainer.RemoveFeatures", err, LogType.Error); }
  }

} catch(err){ Log.Add("StaticContainer", err, LogType.Error); }
}
