/* 	Copyright 2008 Maximum Processing Inc
	This script provides definitions for Javascript Container Components

	ABOUT THIS SCRIPT [* = not required on EditableContent Container]
	Required public methods
          . DefaultMode
          . EditMode
          . GetControl
          . Load
          
        Required Public Properties
          . Ref
          . NameRequired
          . Accept
	
        DEPENDENCIES:
        1. container.rules.js
        2. logging.js

	Change Log
 	Created 11/18/2008 Nathan Townsend
 	Added logging 11/22/2008 NT
 	11/22/2008 - added logging NT
*/


//  EditableContent Container [NT 11/19/2008]
function EditableContent(){
  try{

  /* PRIVATE PROPERTIES */
  var control = null;

  var featuresAdded = false;
  


  /* PUBLIC METHODS */

  return {

    DefaultMode: function()
    {
        try{
        
            RemoveFeatures();
						
    				control.bind("keydown", function(event){
    				  if(event.keyCode == 13)
    					{
							  var btn = $(this).find(".EditorSubmitButton input[DefaultButton='true']");
							  var e = new jQuery.Event("click");
   							btn.trigger(e);
    					}
    				});							
						
            
            Helper.RemoveComponentID(control);
          
        } catch(err){ Log.Add("EditableContent.DefaultMode", err, LogType.Error)};
    },

    EditMode: function()
    {
      try{
      
        AddFeatures();    
      
      } catch(err){ Log.Add("EditableContent.EditMode", err, LogType.Error)};
    },



    Load: function(ControlElement)
    {
        try{
        
          control = $(ControlElement);

          //Log.Add("EditableContent.Load", "Loaded", LogType.Info);
          
        } catch(err){ Log.Add("EditableContent.EditMode", err, LogType.Error)};
    },



    GetControl: function()
    { 
        try{
        
            return control;   
        
        } catch(err){ Log.Add("EditableContent.GetControl", err, LogType.Error)};
    },
    
    /* CONSTANTS */    
    NameRequired: false,
    
    Ref: "EditableContent",
    
    Accept: ["StaticContainer", "DynamicContainer", "EditorLabel", "TransferList", "ValidationContainer", "ScriptingContainer"]
    
  }
  

  


  /* 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(EditableContent().Accept),
            greedy: true,
            activeClass: 'droppable-active',
            hoverClass: 'droppable-hover',
            drop: function(ev, ui) {
              var ref = $(ui.draggable).attr("ref");
              Editor.BuildComponent(ref, $(this));
            }
        });
        
        control.bind("dblclick.editablecontent", function(event){event.stopPropagation(); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("ui-sortable");
        
				var acc = new EditableContent().Accept;
				
				var str = "";
				
				for(var i = 0; i<acc.length; i++)
				  str += "." + acc[i] + ",";
					
				str = str.removeLastChar();

        $(control).sortable({ items: ">.component", handle:">.handle", delay:500});

        featuresAdded = true;
          
    } catch(err){ Log.Add("EditableContent.AddFeatures", err, LogType.Error)};
    
  }

  // removes features from the control that were previously added 
  function RemoveFeatures()
  {
    try{
    
        if(!featuresAdded)
          return;
        
        $(control).droppable("destroy");
        
        control.unbind("dblclick.editablecontent");
        
        $(control).sortable("destroy");
        
        featuresAdded = false;
          
    } catch(err){ Log.Add("EditableContent.RemoveFeatures", err, LogType.Error)};
    
  }

} catch(err){ Log.Add("EditableContent", err, LogType.Error); }
}

