/* 	Copyright 2008 Maximum Processing Inc
	This script provides editor capabilities

	ABOUT THIS SCRIPT

	DEPENDENCIES
	  1. logging.js

	
	Change Log
 	Created 11/19/2008 Nathan Townsend
 	11/22/2008 - added logging NT
*/



Editor = new function()
{
  try{

  
  /* PRIVATE PROPERTIES */
  var _editorEnabled = null;
  
  var _currentComponent = null;


  
  return{
    /* PUBLIC METHODS */
    
    // Called when a template component is dropped onto a container
    BuildComponent: function(Ref, Container)
    {
      try{
      
        if(Ref == null)
            Log.Add("Editor.BuildComponent", "The Template control passed in did not specify a ref attribute", LogType.Error);

        var ctrl = Helper.CreateEditorComponent(Ref);

        ctrl.AppendTo(Container);
        
      } catch(err){ Log.Add("Editor.BuildComponent", err, LogType.Error); }
        
    },
    
    // Deletes a component from the editor
    DeleteComponent: function()
    {
      try{
      
          if(_currentComponent == null)
            return;
          
          Helper.DeleteEditorComponent(_currentComponent);
          
          Editor.HideProperties();
					
      } catch(err){ Log.Add("Editor.DeleteComponent", err, LogType.Error); }      
    },
    
    
    // Disables the editor and sets each component to its default state
    DisableEditor: function()
    {
      try{

          if(_editorEnabled == false)
            return;
					
			    _editorEnabled = false;
          
          $(".component").each(function(){
            try
            {
                var ctrl = Helper.GetEditorComponent(this);

                ctrl.DefaultMode();
            } catch(err) { Log.Add("Editor.DisableEditor", err, LogType.Error); }
          });
          
          Helper.ClearStoredComponents();
         
          //if($("#ComponentDefinitions").dialog("isOpen"))
              $("#ComponentDefinitions").dialog("close");
      
       } catch(err){ Log.Add("Editor.DisableEditor", err, LogType.Error); }
      
    },
    
    EditorProperty: function(Item, Get, Set, Args)
    {
       try{
          
          this.Item = Item;
          this.Get = Get;
          this.Set = Set;
          this.Args = Args;
          
       } catch(err){ Log.Add("Editor.EditorProperty", err, LogType.Error); }
    },
    
    
    // Enables the editor and sets each component to its editable state
    EnableEditor: function()
    {
      try{

          if(_editorEnabled)
            return;
          
          _editorEnabled = true;
            
          $(".component").each(function(){
            try 
            {
                var ctrl = Helper.GetEditorComponent(this);
                
                ctrl.EditMode();
                
            } catch(err) {Log.Add("Editor.EnableEditor", err, LogType.Error); }
          });
          
          Log.Add("Editor.EnableEditor", "Editor Enabled", LogType.Info);

      } catch(err){ Log.Add("Editor.EnableEditor", err, LogType.Error); }
                    
    },
    
		
    HideProperties: function()
    {
       try{
      
		 		 PropertiesEditor.Hide();
            
       } catch(err){ Log.Add("Editor.HideProperties", err, LogType.Error); }
    },
    
    
    // Is called when an ajax post inserts new html into the document
    Refresh: function(){
      try{
        
        if(_editorEnabled == false)
            return;
        
        
        $(".component").each(function(){
          try 
          {
              var ctrl = Helper.GetEditorComponent(this);
              ctrl.EditMode();
          } catch(err) {Log.Add("Editor.Refresh", err, LogType.Error); }
        });
        
        
        Log.Add("Editor.Refresh", "Refresh Called", LogType.Info);
        
      } catch(err){ Log.Add("Editor.Refresh", err, LogType.Error); }
    },
    
    

    
    
    ShowAcceptedComponents: function(Container)
    {
      try{
			 
          if(_editorEnabled == false)
            return;
      
          var ctrl = Helper.GetEditorComponent(Container);
          
          if(ctrl.Accept == null)
          {
            if($("#ComponentDefinitions").dialog("isOpen"))
              $("#ComponentDefinitions").dialog("close");
            
            return;
          }
          
          var tree = GenerateComponentTree(ctrl.Accept);
					
          $("#ComponentDefinitions").find("#components").html(tree);
          
					$("#ComponentDefinitions").find("#components").treeview();

          $("#ComponentDefinitions").dialog("open");  
      
      } catch(err){ Log.Add("Editor.ShowAcceptedComponents", err, LogType.Error); }    
    },
    
    
    // Shows a table to update component properties
    ShowProperties: function(Component)
    {
       try{
			 
           var ctrl = Helper.GetEditorComponent(Component);
           
           var properties = ctrl.GetProperties();
					 
           PropertiesEditor.Show(properties, this.DeleteComponent, ctrl.Ref);
					 
           _currentComponent = $(Component);
					 
					 Global.EditingTemplate = true;
					 
           //Log.Add("Editor.ShowProperties", "Show Properties Succeeded", LogType.Info);
           
        } catch(err){ Log.Add("Editor.ShowProperties", err, LogType.Error); }
    },
    
    // returns the properties of the current component
    GetProperties: function()
    {
      try{
       
         var ctrl = Helper.GetEditorComponent(_currentComponent);
       
         return ctrl.GetProperties();
      
      } catch(err){ Log.Add("Editor.SetProperties", err, LogType.Error); }
    },
    
    IsEnabled: function()
    { 
      try{
        return _editorEnabled;
      } catch(err){ Log.Add("Editor.IsEnabled", err, LogType.Error); }    
    },
        
    

    /* PUBLIC PROPERTIES */
    
    // the tag name of the component templates that will be dropped onto containers
    ComponentTemplateTagName: "SPAN"
    
   
  }
  
  
  
  function GenerateComponentTree(Accept)
  {
    try{
  
        var outer = $("<li/>");
        
        var caption = $("<span class='folder'>Components</span>");
        
        var ul = $("<ul/>");
        
        outer.append(caption);
        
        outer.append(ul);
        
        for(var i = 0; i<Accept.length; i++)
        {
          var li = $("<li/>");
          
          ul.append(li);
        
          var span = $("<span class='file'/>");
          
          li.append(span);
          
          var component = $("<span class='componentdef'/>");
        
          component.text(Accept[i]);
          
          component.attr("ref", Accept[i]);
          
          component.draggable({ helper: 'clone' });
          
          span.append(component);
        }
        
        return outer;
        
    } catch(err){ Log.Add("Editor.GenerateComponentTree", err, LogType.Error); }
  }
  
	
  
 
 
 // Catches errors that occur anywhere in the editor
 } catch(err){ Log.Add("Editor", err, LogType.Error); } 
}




