/* 	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/21/2008 Nathan Townsend
 	11/22/2008 - added logging NT
 	
 	
*/








function EditorDropDown(){
  try{
	
  this.TemplateBase = TemplateBase;

	this.TemplateBase();		
	
  //this.AjaxPostable = AjaxPostable;

	//this.AjaxPostable();		
	
		
	
  /* PRIVATE PROPERTIES */
  var control = null;

  var featuresAdded = false;
  
  var _select = 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, _select);
					
          if(Editor.IsEnabled())
            this.EditMode();
						
					control.attr("origT", control.css("top"));

					control.attr("origL", control.css("left"));						
    
      } catch(err){ Log.Add("EditorDropDown.AppendTo", err, LogType.Error); }
    };
    
    
    this.Create = function(ctrlName)
    {
      try{
          name = ctrlName;

          control = $("<div condition='' />");

          control.html("<span>Drop Down</span><br/><select id='" + name + "' name='" + name + "' size='1'>");

          control.addClass("component EditorDropDown");

          control.attr("ref", "EditorDropDown");

          _select = $($(control).find("select"));

          span = $($(control).find("span"));

          span.css("cursor", "text");

          span.addClass("notRequired");

          span.click(function(){_select.focus()});
					
          //Log.Add("EditorDropDown.Create", "Created: " + _select.attr("name"), LogType.Info);
					
      
      } catch(err){ Log.Add("EditorDropDown.Create", err, LogType.Error); }
    };

    this.DefaultMode = function()
    { 
      try{
        
        RemoveFeatures();
        
        Helper.RemoveComponentID(control);
				
				this.AttachFunctions();
        
      } catch(err){ Log.Add("EditorDropDown.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("EditorDropDown.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("MultiSelect"), this.GetMultiSelect, this.SetMultiSelect);					
					
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Options"), this.GetOptions, this.SetOptions);

          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Required"), this.GetRequired, this.SetRequired);
					
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Size"), this.GetSize, this.SetSize);					
          
          if(pref == "StaticContainer")
            properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Width"), this.GetWidth, this.SetWidth);
          
          return properties;
          
      } catch(err){ Log.Add("EditorDropDown.GetProperties", err, LogType.Error); }
    };

    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);
					
          span = $(control.find("span"));

          span.css("cursor", "text");
            
          _select = $(control).find("select");

          span.click(function(){_select.focus()});
					
					this.BaseLoad(control, _select);
					
          //Log.Add("EditorDropDown.Load", "Loaded: " + _select.attr("name"), LogType.Info);
					
      
      } catch(err){ Log.Add("EditorDropDown.Load", err, LogType.Error); }
    };

    this.Refresh = function()
    {
      try{
      
        ResizeControl();
      
        //Log.Add("EditorDropDown.Refresh", "Refreshed: " + _select.attr("name"), LogType.Info);
				
        
      } catch(err){ Log.Add("EditorDropDown.Refresh", err, LogType.Error); }
    };
		
		
		this.GetErrorMessage = function()
		{
      try{ 
			  if(this.GetRequired())
				{
				  if(_select.val() == "-1")
					{
					  return "You must select from the dropdown '" + 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{

			  _select.focus();
				
      } catch(err){ Log.Add("EditorText.SetFocus", err, LogType.Error); }
		}		
		
		

    this.GetControl = function(){try{ return control; } catch(err){ Log.Add("EditorDropDown.GetControl", err, LogType.Error); }};

    this.GetCaption = function(){try{  return span.text(); } catch(err){ Log.Add("EditorDropDown.GetCaption", err, LogType.Error); }};

    this.GetName = function(){try{  return _select.attr("name"); } catch(err){ Log.Add("EditorDropDown.GetName", err, LogType.Error); }};
    
    this.GetOptions = function(){try{  return _select.html(); } catch(err){ Log.Add("EditorDropDown.GetOptions", err, LogType.Error); }};
		
    this.GetMultiSelect = function(){try{  return _select.attr("multiple"); } catch(err){ Log.Add("EditorDropDown.GetMultiSelect", err, LogType.Error); }};		

    this.GetRequired = function(){try{  return span.hasClass("required");} catch(err){ Log.Add("EditorDropDown.GetRequired", err, LogType.Error); }};
		
    this.GetSize = function(){try{  return _select.attr("size"); } catch(err){ Log.Add("EditorDropDown.GetSize", err, LogType.Error); }};		

    this.GetWidth = function(){try{  return _select.width(); } catch(err){ Log.Add("EditorDropDown.GetWidth", err, LogType.Error); }};

    this.SetCaption = function(newCaption){try{  span.text(newCaption); } catch(err){ Log.Add("EditorDropDown.SetCaption", err, LogType.Error); }};
    

    this.SetName = function(newName){
      try{
			
			  return;
        
        var ctrl = Helper.GetEditorComponent(control);

        var width = ctrl.GetWidth();
        
        var html = _select.html();
        
        var newSel = $("<select id='" + newName + "' name='" + newName + "'>");
        
        newSel.html(html);
        
        newSel.css("width", width);
        
        _select.replaceWith(newSel);
        
        _select = newSel;
        
      } catch(err){ Log.Add("EditorText.SetName", err, LogType.Error); }
    };  

    this.SetMultiSelect = function(boolValue){try{ if(boolValue){_select.attr("multiple", " ")} else {_select.removeAttr("multiple")} } catch(err){ Log.Add("EditorDropDown.SetOptions", err, LogType.Error); } };		  
    
    this.SetOptions = function(newValue){try{ _select.html(newValue);  } catch(err){ Log.Add("EditorDropDown.SetOptions", 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("EditorDropDown.SetRequired", err, LogType.Error); }
    };

    this.SetSize = function(newValue){try{ _select.attr("size", newValue);  } catch(err){ Log.Add("EditorDropDown.SetSize", err, LogType.Error); } };
		
    this.SetWidth = function(newWidth){
      try{
          var w = Utilities.ToNumber(newWidth) + 4;
          _select.css("width", w);
          ResizeControl();
       } catch(err){ Log.Add("EditorDropDown.SetWidth", err, LogType.Error); }
    };


    /* PUBLIC PROPERTIES */    
    this.NameRequired = true;
    
    this.Ref = "EditorDropDown";

 



  /* PRIVATE METHODS */
  // sizes the control elements
  function ResizeControl(){
    try{

 				control.css("top", control.attr("origT"));
				
				control.css("left", control.attr("origL"));
		
    } catch(err){ Log.Add("EditorDropDown.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,
            maxHeight: control.height(),
            minHeight: control.height(),
            resize: function(e, ui){
						  Global.ShowEditModeMessage(ui.size.width);
              _select.width(ui.size.width);
              ResizeControl();
            }
					  
					});

        }
        
        control.bind("dblclick.EditorDropDown", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("editing");
        
        span.attr("unselectable", "on");
        
        featuresAdded = true;
        
        //Log.Add("EditorDropDown - AddFeatures", "Features Added", LogType.Info);
				
        
     } catch(err){ Log.Add("EditorDropDown.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.EditorDropDown");
        
        control.removeClass("editing");
        
        span.removeAttr("unselectable");
        
        featuresAdded = false;
        
        //Log.Add("EditorDropDown - RemoveFeatures", "Features Removed", LogType.Info);
				
        
     } catch(err){ Log.Add("EditorDropDown.RemoveFeatures", err, LogType.Error); }
  }


} catch(err){ Log.Add("EditorDropDown", err, LogType.Error); }
}
