

/**
 * Variabili globali.
 */
var ready=true;			 // indica se l'applicazione è pronta a ricevere un comando.

var map=null;					// mappa corrente
var engine=null;		 // engine di comunicazione con java

var automa=null;

var consolePosX=0;
var consolePosY=0;

var mapWidth;
var mapHeight;
var mapOffsetX;
var mapOffsetY;

var eventHandlers=new Array();
var eventHandlersStatus=new Array();

var initialMapGui=new Array();

var otherMapGui=new Array();

var extensions=new Array();

var commands=new Array();

var outputDiv=null;
var fileLoadedFunction=null;
var appendFile=false;

var mapArray= new Array();			 // array mappe
var mapInfo=new InfoContainer();
var eventListeners=new EventListeners();

var guiVersion='';

function addExtensionHTML(html) {
	if(document.getElementById('extensionsDiv'))
		document.getElementById('extensionsDiv').innerHTML+=html;
}

function copyLoadedFrame() {
	if(loadingFrame.document.readyState!='complete')
		setTimeout("copyLoadedFrame()",500);
	else {
		
		if(appendFile) {
			outputDiv.innerHTML+=loadingFrame.document.body.innerHTML;
			
		} else
			outputDiv.innerHTML=loadingFrame.document.body.innerHTML;
		/*if(loadingFrame.document.body.onrowsdelete)
			loadingFrame.document.body.onrowsdelete.apply();*/
		if(automa)
			automa.goOn();
	}
	
}

function loadFile(fileName,out,append) {
	outputDiv=document.getElementById(out);
	appendFile=append;
	loadingFrame.location.href=fileName;										
	setTimeout("copyLoadedFrame()",500);
}

// Funzione di continuazione per gli automi.
function goOn() {
	map.end();
	
	if(automa && automa.valid)
		automa.goOn();
	else
		actionEnd();
}


// Funzioni generali di gestione GUI.
function hideMapGui() {
	for(var pos in initialMapGui) {
		var divName=initialMapGui[pos];
		document.getElementById(divName).style.visibility='hidden';
	}

	for(var pos in otherMapGui) {
		var divName=otherMapGui[pos];
		document.getElementById(divName).style.visibility='hidden';
	}
}

function initMapGui() {
	for(var pos in otherMapGui) {
		var divName=otherMapGui[pos];
		document.getElementById(divName).style.visibility='hidden';
	}

	for(var pos in initialMapGui) {
		var divName=initialMapGui[pos];
		
		document.getElementById(divName).style.visibility='visible';
	}

	disableAllEvents();
	if(document.getElementById('initial')) {
		document.getElementById('initial').style.visibility='hidden';
		document.getElementById('initialWaiting').style.visibility='hidden';
	}
	//InitialiseScrollableArea();
}

/**
 * invocata in seguito alla pressione del tasto destro del mouse. disabilita il menu contestuale.
 */
function stopMenu() {
	return false;
}

/**
 * invocata in seguito alla pressione di un tasto sulla tastiera. cerca di bloccare i tasti di IE.
 */
function fncKeyStop() {	
	window.event.cancelBubble=true;
	window.event.returnValue=false;
	return false;
}

//document.oncontextmenu=stopMenu;
//document.onkeydown=fncKeyStop;


/**
 * Funzione di risposta alle chiamate java. Delega la gestione all'engine.
 */
function receiveResponse() {
	if(engine.hasReceivedResponse())
		engine.receiveResponse();
	else
		setTimeout("receiveResponse()",500);	
}



/**
 * Funzione richiamata al termine di un'azione utente.
 */
function actionEnd() {
	//alert('end');
	
	//document.getElementById('loadmessage').style.visibility='hidden';
	eventListeners.fireEvent('wait','off');
	ready=true;
	
}

function disableAllEvents() {
	for(var pos in eventHandlers) {
		var eventHandler=eventHandlers[pos];
		disableEvent(eventHandler);
		
	}
}

function endToggle() {
	
	if(mapInfo.getProperty('defaultEvent')) {
		var found=false;
		for(var pos in eventHandlersStatus) {
			if(eventHandlersStatus[pos])
				found=true;
		}
		if(!found) {
			eval(mapInfo.getProperty('defaultEvent')+'Toggle(true)');
		}

	}
}

function disableEvent(event) {
	eventHandlersStatus[event]=false;
//	document.flashMovie.setVariable(event+'Selected','false');
}

function enableEvent(event,exclude) {
	eventHandlersStatus[event]=true;
	for(var pos in eventHandlers) {
		var eventHandler=eventHandlers[pos];
		if(eventHandler!=event && eventHandlersStatus[eventHandler] && (!exclude || exclude!=eventHandler)) {
			disableEvent(eventHandler);
			eval(eventHandler+'Toggle(true)');
		}
	}
}

/*function quit()
{
	currentEnvironment='quit';
	if(currentEnvironment=='quit') {
		if(map)
			map.clear();
		engine.sendMsg("quit",null,null);
	}
	actionEnd();
}*/

//commands['closeErrore']='closeErrore';

function waitingOn() {
	ready=false;
	eventListeners.fireEvent('wait','on');
	//document.getElementById('loadmessage').style.visibility='visible';
}

/**
 * Gestisce le chiamate dalla GUI flash (attraverso LiveConnect).
 * La funzione viene richiamata dalla equivalente VBScript.
 */
function doCommand(command, args) {
	
	if(ready) {
		waitingOn();
		var funName=commands[command];
		if(!funName) {
			alert('azione non riconosciuta: '+command+','+args);
			actionEnd();
		} else {
			if(args && (typeof args=='string'))
				args=args.replace(/\'/g,'\\\'');
			eval(funName+'(\''+args+'\')');			
		}
	}
	return;
}




