/* 	Copyright 2008 Maximum Processing Inc
	
	ABOUT THIS SCRIPT

	
	Change Log
 	Created 12/03/2008 Nathan Townsend
*/


function IF(){

  this.inheritFrom = BaseComponent;

	this.inheritFrom();

	var _xmlNode = null;
	
  this.Src = "../../images/RuleIF.png";

  this.Title = "IF Component";
	
	this.ToolTip = "Makes a binary decision to determine which of two components will be called next";
	
	
	

  this.Create = function()
  {
	  try{
		  var uo = new UndoObject();
			
			uo.AddUndo(RulesMaker.Load, RuleXML.GetVRM().xml);

      this.BaseCreate();
			
			this.SetType("IF");	

			_xmlNode = this.GetNode();

      var values = $(_xmlNode.find('>values')[0]);
      
      values.append(RuleXML.GetNewElement('v'));
			
      uo.AddRedo(RulesMaker.Load, RuleXML.GetVRM().xml);
      
      Undo.Add(uo);				

		} catch(err){Log.Add("IF.Create", err, LogType.Error);}    
  }
  
	this.Load = function(xmlNode)
  {
	  try{
		
		  _xmlNode = $(xmlNode);
		
		  this.BaseLoad(_xmlNode);
			
		} catch(err){Log.Add("IF.Load", err, LogType.Error);}	  	
  }
  
  
	this.GetProperties = function(){
    try{
	
  	  var properties = this.GetBaseProperties();

      //properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("J2"), this.GetJ2, this.SetJ2);
      properties[properties.length] = new PropertiesEditor.Property(PropertyFields.GetProperty("Condition"), this.GetCondition, this.SetCondition);

  		return properties;

		} catch(err){Log.Add("IF.GetProperties", err, LogType.Error);}		

	}
	
  this.GetCondition = function(){
	  try{
		
      return $(_xmlNode.find(">values>v")[0]).text();

		} catch(err){Log.Add("IF.GetCondition", err, LogType.Error);}			
  }
	
  this.GetJ2 = function(){
    try{
      return $(_xmlNode.find(">j")[1]).text();
		} catch(err){Log.Add("BaseComponent.GetJ2", err, LogType.Error);}				
  }	
 
  this.SetCondition = function(newCondition)
  {
	 	try{

      var cdata = RuleXML.GetNewCDATA(newCondition);
      
      var elem = RuleXML.GetNewElement('v');
      
      $(elem).append(cdata);
			
      _xmlNode.find('>values')[0].replaceChild(elem, _xmlNode.find(">values>v")[0]);

		} catch(err){Log.Add("IF.SetCondition", err, LogType.Error);}    
  }

  this.SetJ2 = function(newValue){
    try{
      $(_xmlNode.find(">j")[1]).text(newValue);
		} catch(err){Log.Add("BaseComponent.SetJ2", err, LogType.Error);}				
  }  

 
}







