/* 	Copyright 2008 Maximum Processing Inc
	A lable that can be styled and positioned

	ABOUT THIS SCRIPT
	Required public methods
	  . AppendTo
          . DefaultMode
          .  EditMode
          . GetControl
          . Load
          
        Required Public Properties
          . Ref
          . NameRequired
	
        Dependencies:
          . logging.js
          . container.rules.js
          . utilities.js

	Change Log
 	Created 12/01/2008 Nathan Townsend
 	
*/








function EditorLink(){
  try{
	
  this.inheritFrom = TemplateBase;

	this.inheritFrom();		


  /* PRIVATE PROPERTIES */
  var control = null;

  var featuresAdded = false;
  
  var properties = null;
  
  var link = null;
  
 
  /* PUBLIC METHODS */

    // Appends the control to a container element
    this.AppendTo = function(ContainerElement)
    {
      try{
          $(ContainerElement).append(control);
          
					this.BaseLoad(control, link);
					
          if(Editor.IsEnabled())
            this.EditMode();
            
      } catch(err){ Log.Add("EditorLink.AppendTo", err, LogType.Error); }
    };
    
    this.Create = function()
    {
      try{
          control = $("<div condition='' />");

          control.html("<a class='handle Default' onclick='Communication.LinkRequest(\"\");'>link</a>");

          control.addClass("component EditorLink");

          control.attr("ref", "EditorLink");

          link = $($(control).find("a"));

          //Log.Add("EditorLink.Create", "Created new link", LogType.Info);
					
        
      } catch(err){ Log.Add("EditorLink.Create", err, LogType.Error); }
    };
    

    this.DefaultMode = function()
    {
      try{
        
        RemoveFeatures();
				
				this.AttachFunctions();				
        
        Helper.RemoveComponentID(control);
        
      } catch(err){ Log.Add("EditorLink.DefaultMode", err, LogType.Error); }
    };

    this.EditMode = function()
    { 
      try{
			
        AddFeatures();
				
				this.DetachFunctions();
								
      } catch(err){ Log.Add("EditorLink.EditMode", err, LogType.Error); }
    };
    
    this.GetProperties = function()
    {
      try {
            
          var properties = this.GetBaseProperties();

          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Caption"), this.GetCaption, this.SetCaption);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Class"), this.GetClass, this.SetClass, this.Classes);
					
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Style"), this.GetStyle, this.SetStyle);
          
          properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Target"), this.GetTarget, this.SetTarget);

          //Log.Add("EditorLink.GetProperties", "Properties Loaded", LogType.Info);
					
        
          return properties;
          
       } catch(err){ Log.Add("EditorLink.GetProperties", err, LogType.Error); }
    };

    this.Load = function(ControlElement)
    {
      try{
          control = $(ControlElement);

          link = $(control.find("a"));
					
					this.BaseLoad(control, link);					

          //Log.Add("EditorLink.Load", "Loaded link", LogType.Info);
					
          
      } catch(err){ Log.Add("EditorLink.Load", err, LogType.Error); }
    };

    this.GetControl = function(){try{ return control; } catch(err){ Log.Add("EditorLink.GetControl", err, LogType.Error); } };

    this.GetCaption = function(){try{ return link.text(); } catch(err){ Log.Add("EditorLink.GetCaption", err, LogType.Error); } };
    
    this.GetClass = function(){try{ return link.attr("class"); } catch(err){ Log.Add("EditorLink.GetClass", err, LogType.Error); } };
		
    this.GetStyle = function(){try{ return link.attr("style"); } catch(err){ Log.Add("EditorLink.GetStyle", err, LogType.Error); }};
    
    this.GetTarget = function()
    {
      try{
        var ret = link.attr("onclick").toString();

        var start = ret.indexOf('"') + 1;
        
        var stop = ret.indexOf('"', start );

        ret = ret.substring(start, stop);
          
        return ret;

      } catch(err){ Log.Add("EditorLink.GetTarget", err, LogType.Error); }
    };

    this.SetCaption = function(newCaption){try{ link.text(newCaption); } catch(err){ Log.Add("EditorLink.SetCaption", err, LogType.Error); }};
    
    this.SetClass = function(newClass){try{ link.attr("class", newClass); } catch(err){ Log.Add("EditorLink.SetClass", err, LogType.Error); }};
		
    this.SetStyle = function(newStyle){try{ link.attr("style", newStyle); } catch(err){ Log.Add("EditorLink.SetStyle", err, LogType.Error); }};
    
    this.SetTarget = function(target){
      try{
        
        var s = 'Communication.LinkRequest("' +  target + '");';
        
        link.attr("onclick", s);
          
      } catch(err){ Log.Add("EditorLink.SetTarget", err, LogType.Error); }
    };    


    /* PUBLIC PROPERTIES */
    this.Classes = ["Default"];
    
    this.NameRequired = false;
    
    this.Ref = "EditorLink";



  /* PRIVATE METHODS */

  // 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");
          link.css("cursor", "move");
          link.attr("unselectable", "on");
          control.draggable({containment: "parent", delay:500, handle:link });
        }
				
        var ctrl = Helper.GetEditorComponent(control);
				
				ctrl.SetTarget(ctrl.GetTarget());

        control.bind("dblclick.EditorLink", function(event){event.stopPropagation(); event.preventDefault(); Editor.ShowProperties(control); Editor.ShowAcceptedComponents(this); });
        
        control.addClass("editing");
        
        featuresAdded = true;
        
        //Log.Add("EditorLink - AddFeatures", "Features Added", LogType.Info);
				
        
     } catch(err){ Log.Add("EditorLink.AddFeatures", err, LogType.Error); }
  }

  // removes features from the control that were previously added 
  function RemoveFeatures()
  {
    try{
    
        if(!featuresAdded)
          return;
        
        control.css("cursor", "default");
        
        link.css("cursor", "pointer");
        
        link.removeAttr("unselectable");

        control.draggable("destroy");
        
        control.resizable("destroy");
          
        control.unbind("dblclick.EditorLink");
        
        control.removeClass("editing");
				
        var ctrl = Helper.GetEditorComponent(control);

				var target = 'Communication.LinkRequest("' +  ctrl.GetTarget() + '");';
        
        control.click(new Function(target));
        
        featuresAdded = false;
        
        //Log.Add("EditorLink - RemoveFeatures", "Features Removed", LogType.Info);
				
        
     } catch(err){ Log.Add("EditorLink.RemoveFeatures", err, LogType.Error); }
  }

 } catch(err){ Log.Add("EditorLink", err, LogType.Error); }
}
