/* 	Copyright 2008 Maximum Processing Inc
	This script provides standard functions used by Javascript Control Objects found in the controls.js

	ABOUT THIS SCRIPT
        
	DEPENDENCIES
	  1. logging.js
          2. utilities.js
	
	EXAMPLES
	1. Get the Javascript Control Object for an existing HTML Component and calls one of its methods
           var div = $("component div selector");
           var text = Helper.GetEditorComponent(div)
           text.SetCaption("This is new");

	2. Create a new Component
	   var ctrl = Helper.CreateEditorComponent("EditorText");
           ctrl.AppendTo( $("#container") );


	Change Log
 	Created 11/17/2008 Nathan Townsend
 	11/22/2008 - added logging NT
*/



eStore = new Storage();

// The Control Helper object
Helper = new function(){
  try{

  // an incrimental value used to relate stored html components to their javascript objects
  var _componentId = 0;


  // an array of ObjectPairs
  var EditorControls = new Array();
    
  
  
  /* PUBLIC METHODS */

  return{
   
   ClearStoredComponents: function(){
     try{
		 
		   eStore.Reset();
      
     } catch(err){ Log.Add("Helper.ClearStoredComponents", err, LogType.Error); }
   },
   
   ClearStoredComponent: function(ComponentElement)
   {
	   try
		 {
       var id = $(ComponentElement).attr("EditorID");
		 
	  	 eStore.Remove(id);
			 
     } catch(err){ Log.Add("Helper.ClearStoredComponent", err, LogType.Error); }			 
   },
	 
   CreateEditorComponent: function(strRef, componentName)
   {
      try{
      
         if(strRef == null)
           Log.Add("Helper.CreateEditorComponent", "CreateEditorControl: The ref passed in was null", LogType.Error);

         var ctrl = CreateComponent(strRef, componentName);
				 
				 Helper.SetComponentID(ctrl.GetControl());
     
		 		 eStore.AddComponent(ctrl, _componentId);

         return ctrl;
         
      } catch(err){ Log.Add("Helper.CreateEditorComponent", err, LogType.Error); }
    },
    
    // Deletes an element from the screen and removes it from storage
    DeleteEditorComponent: function(ComponentElement)
    {
      try{
     
        var id = Helper.GetComponentID(ComponentElement);
				
				eStore.Remove(id);
      
        $(ComponentElement).remove();
        
      } catch(err){ Log.Add("Helper.DeleteEditorComponent", err, LogType.Error); }
    },
    
    
   // returns the javascript object for the control [NT 11/17/2008]
   GetEditorComponent: function(ComponentElement)
   {
     try{
   
         var ref = $(ComponentElement).attr("ref");
         
         if(ref == null)
           Log.Add("Helper.GetEditorComponent", "The element passed in did not contain a ref attribute", LogType.Error);

				 var ctrl = null;
				 
			   var temp = $(ComponentElement)[0];

				 try{

					 ctrl = temp[ref];

					 if(ctrl != null)
					 {
             //Log.Add("Helper.GetEditorComponent", "return existing " + ref, LogType.Info);					 
					   return ctrl;
					 }

					 else throw "not found";
						 
				 }catch(err){
				 
           var fn = new Function("return new " + ref + "();");
            
           ctrl = fn();
          
           if(ctrl == null)
             Log.Add("Helper.GetEditorComponent", "The control type " + ref + " could not be initialized.", LogType.Error)
  
           ctrl.Load(ComponentElement);
					 
					 temp[ref] = ctrl;
					 
           //Log.Add("Helper.GetEditorComponent", "return new " + ref, LogType.Info);					 
					 
					 return ctrl;
				 
				 }
				 

				 
				 
				 
					 

				 /*
         var id = Helper.GetComponentID(ComponentElement);
				 
				 var ctrl = eStore.GetComponent(id);
         
         if(ctrl != null)
         {
           Log.Add("Helper.GetEditorComponent", "return existing " + ref + " EditorID = " + Helper.GetComponentID(ComponentElement), LogType.Warning);
           
           return ctrl;
         }
        
         var fn = new Function("return new " + ref + "();");
          
         ctrl = fn();
        
         if(ctrl == null)
           Log.Add("Helper.GetEditorComponent", "The control type " + ref + " could not be initialized.", LogType.Error)

         ctrl.Load(ComponentElement);
     
         Helper.SetComponentID(ctrl.GetControl());
				 
				 var id = Helper.GetComponentID(ctrl.GetControl());				 
				 
				 eStore.AddComponent(ctrl, id);

         Log.Add("Helper.GetEditorComponent", "return new " + ref, LogType.Warning);
         
         return ctrl;
				 */
         
     
     } catch(err){ Log.Add("Helper.GetEditorComponent", err, LogType.Error); }
   },
   
   GetParentRef: function(Component){
     try{
   
       var p = $(Component)[0].parentNode;
       return $(p).attr("ref");
       
     } catch(err){ Log.Add("Helper.GetParentRef", err, LogType.Error); }
   },
   
   // Get a previously assigned id for the component
   GetComponentID: function(Component)
   {
     try{
      return $(Component).attr("EditorID");
     } catch(err){ Log.Add("Helper.GetComponentID", err, LogType.Error); }
   },
   
   RemoveComponentID: function(Component)
   {
     try{
      $(Component).removeAttr("EditorID");
     } catch(err){ Log.Add("Helper.RemoveComponentID", err, LogType.Error); }
   },
   
   SetComponentID: function(Component)
   {
     try{

      _componentId += 1;
       
      $(Component).attr("EditorID", _componentId);
      
     } catch(err){ Log.Add("Helper.SetComponentID", err, LogType.Error); }
   }

 }
 


  function CreateComponent(strRef, componentName)
  {
    try{
  
         var ctrl = null;
           
         var fn = new Function("return new " + strRef + "();");
          
         ctrl = fn();
          
         if(ctrl == null)
           Log.Add("Helper - CreateComponent", "A component could not be created for ref = '" + strRef + "'", LogType.Error);
         
         if(ctrl.NameRequired)
         {
           if(strRef == "EditorRadio" || strRef=="EditorSubmitButton" )
             var name = (componentName==null ? Utilities.PromptForName(false) : componentName);
           else
             var name = (componentName==null ? Utilities.PromptForName(true) : componentName);
           
           ctrl.Create(name);
           
         } else {
            ctrl.Create();
         }
         
         return ctrl;
     
    } catch(err){ Log.Add("Helper.CreateComponent", err, LogType.Error); }  
  }
  
  
  // catches errors that occur anywhere in the Helper
  } catch(err){ Log.Add("Helper", err, LogType.Error); }
} 

/* +++++++++++++++++++ END ControlHelper CONTROL +++++++++++++++++++*/





