var CMD="#COMMAND#"; // some special tag
var LNG=9; // length of CMD

var started=0; // boolen to show command processor is started or not
var cmd_array = new Array(); // holds array of commands 
var previous_status=""; // holds real (previous) status text
var bInitialized=0; // first we should initialize Page Holder

function GetMainWindow(wnd)
{

	while(wnd && (wnd.parent != wnd) && (wnd.parent !=  0)  )
		wnd = wnd.parent;

	return(wnd);
};

function Answer()
{
	try
	{
		if((String(window.event.srcElement.tagName)).toLowerCase() == "body") // clicked on body
		{
// process Answer Commands from Page Holder
			var temps = new String(document.title);
			if(temps.substr(0,LNG) == CMD)
			{
				temps=temps.substr(LNG, temps.length-LNG);
				setTimeout(temps,1); // directly put that command!
				window.event.returnValue = false;
			}
		}
	}
	catch(e){;}; // just ignore them
};

function Processor(incoming)
{
	if(!IsInitialized() )
	{
		setTimeout("Processor(\""+incoming+"\")",1000); // check it later again
		return;
	}
		
	started=1; // processor is started	
	var next = incoming;
	while(1)
	{
		try{
			if(next == "")// not a self-call
				next = cmd_array.shift(); // get oldest element
			if(next == undefined)
				break; // something wrong
		}
		catch(e) // we suppose that array is empty
		{
			break;
		}

		var wnd = GetMainWindow(window);
		var st = new String(wnd.status);
		if(st.substr(0,LNG) == CMD)
		{
			setTimeout("Processor(\""+next+"\")",30); // check it later again
			return;
		}
		previous_status=wnd.status;			
		wnd.status=CMD+next;
		next="";
	}
	started=0; // processor is stopped
	
// extra step because of multithreading
	if((cmd_array.length>0) && (!started))
		Processor(""); // restart it again
};
			
function Command(command)
{
	if((!command) || (command == "") || (command == undefined))
		return; // mistake or diversion, do nothing

	cmd_array.push(command); // put command to the end of chain

	if(!started) // start command processor
		Processor("");
};

function ClearStatus()
{
	var st = new String(previous_status);
	if(st.substr(0,LNG) == CMD)
		return;

	GetMainWindow(window).status=previous_status;
};

function InitializeDll()
{
	if(IsInitialized() )
		return;

// send dll name to Page Holder directly, without using Command to avoid multiple calls
	var command = CMD+"SetDll?DllName="+dll;
	var wnd = GetMainWindow(window);
	
	if((wnd.status != command) && (previous_status != command) )
	{
		var st = new String(wnd.status);
		if(st.substr(0,LNG) != CMD)
			previous_status=wnd.status;

		wnd.status=command;
	}
};

function PostInit(){;};
function OnInit(command){ if(command) this.PostInit = command;};
function Initialized()
{
	bInitialized=1; // recieved answer from Page Holder about Dll initialization
	GetMainWindow(window).status = "";
	PostInit(); // process post init command - if any
};

function IsInitialized()
{
	return(bInitialized);
};
