/*	AVAILABLE DATA
	thisCommand_obj
		data						This command's data
		run()						This function
		nextEvent()			Function that triggers the next sibling command
		storeTimeout()	Function that stores this command's timeout ID   (used to stop the script)
			var waitId = setTimeout( parentScript.nextEvent, thisCommand_obj.data.value*1000 );
			thisCommand_obj.storeTimeout( waitId );
*/
//#include "gameProgram/parseVariablePaths.as"
#include "gameFunctions/nestedEval.as"
	// evalPath
	// resolveContainer
	// getImpliedValue

// run()
define_removeSwf = function( thisCommand_obj, preserveSwfloader ){
	
	
	// remove the target swf's command from storage
	if(!preserveSwfloader)
		ROOT.swfLoader.remove( thisCommand_obj.data );
	if(TRACE_SCRIPT)
		trace("removeSwf (old) named: "+thisCommand_obj.data.name+"  from: "+thisCommand_obj.data.target);
	
	var targetName = nestedEval(thisCommand_obj.data.name, "RAM", "_this");
	
	
	// if:  missing variable name
	// ignore this command & instantly continue the script
	if(targetName==""  ||  targetName==undefined)		return VOW.make().keep();
	

	// if:  has variable name

	// if "auto" command has just started, wait a bit before potentially removing this sprite  (via flags)	(this prevents crashing the game)
	var autoDelay = 1;
	var endTime = getTimer();
	var elapsedTime = endTime - startTime;
	if(TRACE_SCRIPT)
		trace("\t elapsedTime: "+elapsedTime);
	if(isNaN(elapsedTime)  ||  elapsedTime>autoDelay)
	{// if:  time has passed since "auto" script started		(prevent game crash)
		// figure out the target container
		var defaultContainer = "OVERLAY";
		target_str = nestedEval(thisCommand_obj.data.target, "RAM", "_this");
		var startAt = target_str.indexOf(".");
		if(startAt==-1)
		{// if:  path has NO objects
			var container = defaultContainer;
			var containerIsGlobal = false;
			for(var nam in _global){
				if(nam == target_str){
					containerIsGlobal = true;
					break;		// stop searching
				}// if:  Path is a global object
			}// for:  each global object
			var theRest = target_str;
			var target = (containerIsGlobal) ? eval(theRest) : eval(container+"."+theRest);
			if(target_str=="this")
				var target = _this;
		}// if:  path has NO objects
		else
		{// if:  path has objects
			var containerName = target_str.substr(0, startAt);
			var container = nestedEval(containerName, "RAM", "_this");
			var theRest = target_str.substr( startAt+1 );
			var target = eval(container+"."+theRest);
		}// if:  path has objects
		
		
		// if it's a sprite, remove it from update list  (so variables don't restore it)
		if(target == SPRITES  &&  target[targetName].remove){
			target[targetName].remove();
		}else{
			// remove the specified movieClip		(WARNING:  onUnload() might get called more than once!!! )
			target[targetName].unload();
			target[targetName].onUnload();
			target[targetName].removeMovieClip();
			//target[targetName]._name = null;
		}
		
		
		// wait for promise  (a promise is used because this command may attempt to wait and then re-start itself)
		// thisCommand_obj.nextEvent();
		return VOW.make().keep();
		
		
	}// if:  time has passed since "auto" script started		(prevent game crash)
	else
	{// if:  NO time has passed since the "auto" script began
		
		
		// wait for promise
		var prom = VOW.make();
		if(TRACE_SCRIPT)
			trace("\t too early, wait for 1 millisecond");
		// setTimeout( thisCommand_obj.run, autoDelay, thisCommand_obj );
		setTimeout( function(){
			var inner_prom = define_removeSwf( thisCommand_obj, preserveSwfloader );
			inner_prom.then = prom.keep;
		}, autoDelay );
		return prom;
		
		
	}// if:  NO time has passed since the "auto" script began
}// define_removeSwf()