/* 	Copyright 2008 Maximum Processing Inc
	This script provides definitions for Dynamically sized Javascript Container Components

	ABOUT THIS SCRIPT
	Required public methods
	  . AppendTo
          . DefaultMode
          . EditMode
          . GetControl
          . GetProperties
          . Load
          
        Required Public Properties
          1. Ref
          2. NameRequired
          3. Accept
	
        DEPENDENCIES:
        1. container.rules.js
        2. logging.js

	Change Log
 	Created 11/19/2008 Nathan Townsend
 	11/22/2008 - added logging NT
*/




//  DynamicContainer [NT 11/19/2008]
function DynamicContainer(){
  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("DynamicContainer.AppendTo", err, LogType.Error);}
    };
    
    this.Create = function()
    {
      try{
        
          control = $("<div condition='' class='component DynamicContainer' ref='DynamicContainer'><h3 class='handle'>Caption</h3></div>");
          
          h3 = $(control.find(">h3"));
      
      } catch(err){ Log.Add("DynamicContainer.Create", err, LogType.Error);}

    };
    
    this.DefaultMode = function()
    {
      try{
      
        RemoveFeatures();
				
				FormatTableMaster();		
        
        Helper.RemoveComponentID(control);
        
      } catch(err){ Log.Add("DynamicContainer.DefaultMode", err, LogType.Error);}
    };


    this.EditMode = function()
    {
      try{
      
        AddFeatures();
        
      } catch(err){ Log.Add("DynamicContainer.EditMode", err, LogType.Error);}
    };


    this.GetProperties = function()
    {
       try{

          var properties = this.GetBaseProperties();
          
          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);
          
          return properties;
          
       } catch(err){ Log.Add("DynamicContainer.GetProperties", err, LogType.Error);}
    };
    

    this.Load = function(ControlElement)
    {
       try{
       
          control = $(ControlElement);
					
					this.BaseLoad(control);
          
          h3 = $(control.find(">h3"));

       } catch(err){ Log.Add("DynamicContainer.Load", err, LogType.Error);}
    };


    this.GetCaption = function(){try{ return h3.text(); } catch(err){ Log.Add("DynamicContainer.GetCaption", err, LogType.Error);}};

    this.GetControl = function(){ try{ return control; } catch(err){ Log.Add("DynamicContainer.GetControl", err, LogType.Error);} };
    
    this.GetID = function(){try{  return  control.attr("id"); } catch(err){ Log.Add("StaticContainer.GetID", err, LogType.Error); }};
    
    this.SetCaption = function(newCaption){ try{ h3.text(newCaption);} catch(err){ Log.Add("DynamicContainer.SetCaption", err, LogType.Error);}};
    
    this.SetID = function(newID){try{ control.attr("id", newID);} catch(err){ Log.Add("StaticContainer.SetID", err, LogType.Error); }};
    
    
    /* PUBLIC PROPERTIES */    
    this.NameRequired = false;
    
    this.Ref = "DynamicContainer";
    
    this.Accept = ["StaticContainer", "EditorLabel", "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", "EditorLabel", "TransferList"]),
            greedy: true,
            activeClass: 'droppable-active',
            hoverClass: 'droppable-hover',
            drop: function(ev, ui) {
              var ref = $(ui.draggable).attr("ref");
              Editor.BuildComponent(ref, $(this));
            }
        });
        
        $(control).sortable({ items: '> .component' });
         
        control.bind("dblclick.DynamicContainer", function(event){event.stopPropagation(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        h3.attr("unselectable", "on");
        
        featuresAdded = true;
        
     } catch(err){ Log.Add("DynamicContainer.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.DynamicContainer");
        
        $(control).sortable("disable");
        
        h3.removeAttr("unselectable");
          
        featuresAdded = false;
        
     } catch(err){ Log.Add("DynamicContainer.RemoveFeatures", err, LogType.Error);}
  }
	
	
	function FormatTableMaster(){
	  try{
		
				var tm = control.find(".TableMaster");
				
				if(tm.length == 0)
				  return;
					
				var t = new Date().getTime();
				
				tm.each(function(){
				
				  var table = $(this);
					
					if(table.attr("wrapped") != "true")
					{
					
  					table.find("tr:odd").addClass("even");

						table.find("tr").hover(
						  function(){
							  $(this).addClass("hover");
							},
							function(){
							  $(this).removeClass("hover");							
							}
  					);
						
  					var id = table.attr("id");
  					
  					if(id == null || id.length == 0)
  					{
  					  table.attr("id", t);
  						id = t.toString();
  					}
  						
  					table.wrap("<div class='TableMasterWrapper'></div>");
  					table.attr("wrapped", "true");
						
  					
  					var wrapper = table.parents(".TableMasterWrapper");
  					
  					try{
  
  						// get the actual height of the table		
    					var height = table.css("height");
  						
  						// the table should not have a height set
  					  table.css("height", "auto");
  
  						// the default height set in the style sheet
  						var defaultHeight = parseInt(wrapper.css("height").replace("px"));
  
  						// format the height if specified
  						if(height.indexOf("px") > -1)
  						  height = parseInt(height.replace("px", ""));
  						else
  						  height = defaultHeight;
  
  						// set the height of the table, or the default height if not specified
  					  wrapper.css("height", height);
  						
  						// shrink to fit smaller than default table heights
  						var th = parseInt(table.outerHeight());
  						
  						if(th <  defaultHeight)
  						  wrapper.css("height", th + 25); 
  						
  						
  					}catch(err){}	
  					
  					superTable(id);
						
						var tables = control.find("table");
						
						$(tables[0]).find("td").each(function(i){
						  
							// set sort order to ascending by default
						  $(this).attr("sortDir", "asc");
														
						  // call sort when clicked
							$(this).click(function(){
							
							  var so = $(this).attr("sortDir");
								
								var id = $(this).parents("table").attr("id");
								
							  $(tables[1]).find("tbody>tr").tsort("td:eq(" + i + ")", {order: so});
								
								$(tables[1]).find(".even").removeClass("even");
								
								$(tables[1]).find("tr:odd").addClass("even");
								
								$(this).attr("sortDir", so=="asc" ? "desc" : "asc");
								
								$(this).parents("table").find(".asc").removeClass("asc");
								$(this).parents("table").find(".desc").removeClass("desc");								
								$(this).addClass(so=="asc" ? "asc" : "desc");
	
							});
						});
						
  					t++;
					}			
					
				});			
				
     } catch(err){ Log.Add("DynamicContainer.FormatTableMaster", err, LogType.Error);}	
	}


} catch(err){ Log.Add("DynamicContainer", err, LogType.Error); }
}




