<!--
/*
*	File: DynEl.js
*	Incluso con: <SCRIPT SRC="DynEl.js"></SCRIPT>

*	Questo file definisce la classe DynEl, che fornisce un'API portabile
*	per molte funzionalità di Dynamic HTML.
*/

/*
*	Questa è la funzione costruttore per gli oggetti DynEl.
*	Gli argomenti sono i seguenti:
	*	window:	Loggetto window in cui l'elemento dinamico deve comparire
	*	id:	L'ID HTML per l'elemento dinamico. Deve essere unico.
	*	body:	Testo HTML che costituisce il corpo dell'elemento dinamico
	*	left:	La coordinata X opzionale iniziale dell'elemento
	*	top:	La coordinata Y opzionale iniziale dell'elemento
	*	width:	L'ampiezza opzionale dell'elemento

*	Questo costruttore crea come output un foglio stile nel documento corrente.
*	Ciò significa che può essere richiamato solo dal tag <HEAD> del documento
*	prima che qualsiasi testo sia stato visualizzato in output.
*/
function InDyn() {window.document.writeln('<STYLE TYPE="text/css">');}
function FiDyn() {window.document.writeln('</STYLE>');}
function DynEl(window, id, body, left, top, width) {
	// Memorizza alcuni argomenti per un secondo tempo.
	this.window = window;
	this.id = id;
	this.body = body;
	// Output di un foglio stile CSS-P per questo elemento.
	var d = window.document;
//	d.writeln('<STYLE TYPE="text/css">');
	d.writeln('#' + id + ' {position:absolute;');
	if (left) d.writeln('left:' + left + ';');
	if (top) d.writeln('top:' + top + ';');
	if (width) d.writeln('width:' + width + ';');
	d.writeln('}');
//	d.writeln('</STYLE>');
}

/*
*	A questo punto viene definita una serie di metodi per la classe DynEl.
*	Viene definito un gruppo di metodi se si opera in Navigator e
*	un altro gruppo di metodi se si sta lavorando in Internet Explorer.
*	Si può osservare che le API dei metodi sono le stesse in entrambi i casi;
*	solo i corpi dei metodi cambiano. In questo modo, viene definita un'API
*	portabile delle funzionalità DHTML comuni ai due browser.
*/

//Per prima cosa, vengono definiti i metodi di Navigator.
// Nuove istruzioni per Netscape 6.0
var xy = navigator.appVersion;
xz = xy.substring(0,4);
if (navigator.appName.indexOf("Netscape") != -1 && xz != "5.0 ") {

/*
*	Questa funzione invia in output l'elemento dinamico stesso nel documento.
*	Deve essere richiamata prima che qualsiasi altro metodo dell'oggetto DynEl
*	possa essere utilizzato.
*/
DynEl.prototype.output = function() {
	var d = this.window.document; // Variabile scorciatoia:
				      // risparmia testo da scrivere

	// Output dell'elemento all'interno di un tag <DIV>. Specifica l'id dell'elemento.
	d.writeln('<DIV ID="' + this.id + '">');
	d.writeln(this.body);
	d.writeln('</DIV>');

	// Ora, per comodità, si salva un riferimento all'oggetto Layer
	// creato da questo elemento dinamico.
	this.layer = d[this.id];
}

//	Ecco dei metodi per spostare, nascondere, sovrapporre o, comunque,
//	modificare l'elemento dinamico.
DynEl.prototype.moveTo = function(x,y) { this.layer.moveTo(x,y); }
DynEl.prototype.moveBy = function(x,y) { this.layer.moveBy(x,y); }
DynEl.prototype.show = function() { this.layer.visibility = "show"; }
DynEl.prototype.hide = function() { this.layer.visibility = "hide"; }
DynEl.prototype.setStackingOrder = function(z) { this.layer.zIndex = z; }
DynEl.prototype.setBgColor = function(color) {
//    this.layer.bgColor = color;
    this.layer.bgColor = color;
}
DynEl.prototype.setBgImage = function(image) {
    this.layer.background.src = image;
}

//	Questi metodi interrogano la posizione, la dimensione e le altre proprietà
//	dell'elemento dinamico.
DynEl.prototype.getX = function() { return this.layer.left; }
DynEl.prototype.getY = function() { return this.layer.top; }
DynEl.prototype.getWidth = function() { return this.layer.width; }
DynEl.prototype.getHeight = function() { return this.layer.height; }
DynEl.prototype.getStackingOrder = function() { 
    return this.layer.zIndex;
}
DynEl.prototype.isVisible = function() {
    return this.layer.visibility == "show";
}

/*
*	Questo metodo consente di modificare dinamicamente il contenuto
*	dell'elemento dinamico. L'argomento o gli argomenti devono essere stringhe HTML
*	destinate a diventare il nuovo corpo dell'elemento.
*/
DynEl.prototype.setBody = function() {
    for(var i = 0; i < arguments.length; i++)
        this.layer.document.writeln(arguments[i]);
    this.layer.document.close();
}

/*
*	Questo metodo registra un gestore per l'evento specificato
*	sull'elemento. L'argomento eventname deve essere il nome
*	della proprietà di un gestore di evento, quale 'onmousedown' o 'onkeypress'.
*	handler è una funzione che intraprende qualsiasi azione necessaria.
*	Poiché Navigator e IE non possiedono oggetti Event compatibili,
*	tutti i dettagli sull'evento vengono passati come argomenti alla funzione handler.
*	Quando viene chiamata, alla funzione vengono passati i seguenti nove argomenti:
*	1) Un riferimento all'oggetto DynEl object
*	2) Una stringa contenente il tipo di evento
*	3) La coordinata X del mouse, relativa al DynEl
*	4) La coordinata Y del mouse.
*	5) Il pulsante del mouse che è stato eventualmente premuto
*	6) Il codice Unicode del tasto che è stato eventualmente premuto
*	7) Un valore booleano che specifichi se è stato premuto il tasto Maiusc
*	8) Un valore booleano che specifichi se è stato premuto il tasto Ctrl
*	9) Un valore booleano che specifichi se è stato premuto il tasto Alt
*	I gestori di eventi che non sono interessati a tutti questi argomenti
*	non devono, ovviamente, dichiararli tutti nei rispettivi elenchi di argomenti.
*/
DynEl.prototype.addEventHandler = function(eventname, handler) {
	// Si dispone a catturare gli eventi in questo layer.
	this.layer.captureEvents(DynEl._eventmasks[eventname]);
	var dynel = this; 
	// Current DynEl for use in the nested function.
	// Defininisce un gestore di eventi che chiamerà il gestore specificato,
	// passando a quest'ultimo i nove argomenti specificati precedentemente.
	this.layer[eventname] = function(event) {
	return handler(dynel, event.type, event.x, event.y,
	event.which, event.which,
	((event.modifiers & Event.SHIFT_MASK) != 0),
	((event.modifiers & Event.CTRL_MASK) != 0),
	((event.modifiers & Event.ALT_MASK) != 0));
	}
}
/*
*	Questo metodo non registra il gestore di eventi specificato. Deve essere
*	richiamato con un'unica stringa come argomento, quale 'onmouseover'.
*/
DynEl.prototype.removeEventHandler = function(eventname) {
	this.layer.releaseEvents(DynEl._eventmasks[eventname]);
	delete this.layer[eventname];
}

/*
*	Questo array viene utilizzato internamente dai due metodi precedenti
*	per associare il nome dell'evento al tipo di evento.
*/
DynEl._eventmasks = {
	onabort:Event.ABORT, onblur:Event.BLUR, onchange:Event.CHANGE,
	onclick:Event.CLICK, ondblclick:Event.DBLCLICK,
	ondragdrop:Event.DRAGDROP, onerror:Event.ERROR,
	onfocus:Event.FOCUS, onkeydown:Event.KEYDOWN,
	onkeypress:Event.KEYPRESS, onkeyup:Event.KEYUP, onload:Event.LOAD,
	onmousedown:Event.MOUSEDOWN, onmousemove:Event.MOUSEMOVE,
	onmouseout:Event.MOUSEOUT, onmouseover:Event.MOUSEOVER,
	onmouseup:Event.MOUSEUP, onmove:Event.MOVE, onreset:Event.RESET,
	onresize:Event.RESIZE, onselect:Event.SELECT, onsubmit:Event.SUBMIT,
	onunload:Event.UNLOAD
}
}
/*
*	Ora si definiscono i metodi per Internet Explorer.
*	Questi metodi possiedono API identiche a quelle definite
*	prima per Netscape.
*	Quindi non vengono ripetuti i commenti precedenti.
*/
if (navigator.appName.indexOf("Microsoft") != -1 || xz == "5.0 ") {

	// Il metodo output(), sempre importante
	DynEl.prototype.output = function() {
	    var d = this.window.document; // Variabile scorciatoia:
		                              // risparmia testo da scrivere

	    // Output dell'elemento all'interno del tag <DIV>. Specifica l'id dell'elemento.	
	    d.writeln('<DIV ID="' + this.id + '">');
	    d.writeln(this.body);
	    d.writeln('</DIV>');

	    // Ora, per convenienza, vengono salvati i riferimenti all'elemento <DIV>
	    // che è stato creato e al suo elemento associato Style.
	    // Questi vengono utilizzati attraverso i metodi che seguono.
	    this.element = d.all[this.id];
	    this.style = this.element.style;
	}

	// Metodi per spostare l'oggetto dinamico
	DynEl.prototype.moveTo = function(x,y) {
	    this.style.pixelLeft = x;
	    this.style.pixelTop = y;
	}
	DynEl.prototype.moveBy = function(x,y) {
	    this.style.pixelLeft += x;
	    this.style.pixelTop += y;
	}

	// Metodi per impostare altri attributi dell'oggetto dinamico
	DynEl.prototype.show = function() { this.style.visibility = "visible"; }
	DynEl.prototype.hide = function() { this.style.visibility = "hidden"; }
	DynEl.prototype.setStackingOrder = function(z) { this.style.zIndex = z; }
	DynEl.prototype.setBgColor = function(color) {
	    this.style.backgroundColor = color;
	}
	DynEl.prototype.setBgImage = function(image) {
	    this.style.backgroundImage = 'url('+ image +')';
	}

	// Metodi per interrogare l'oggetto dinamico
	DynEl.prototype.getX = function() { return this.style.pixelLeft; }
	DynEl.prototype.getY = function() { return this.style.pixelTop; }
	DynEl.prototype.getWidth = function() { return this.style.width; }
	DynEl.prototype.getHeight = function() { return this.style.height; }
	DynEl.prototype.getStackingOrder = function() { return this.style.zIndex; }
	DynEl.prototype.isVisible = function() {
	    return this.style.visibility == "visible";
	}

	// Modifica dei contenuti dell'oggetto dinamico.
	DynEl.prototype.setBody = function() {
	    var body = "";
	    for(var i = 0; i < arguments.length; i++) {
	        body += arguments[i]; 
	    }
	    this.element.innerHTML = body;
	}

	// Definisce un gestore di eventi.
	DynEl.prototype.addEventHandler = function(eventname, handler) {
	    var dynel = this;      // DynEl corrente da usare nella funzione annidata
	             // Imposta un gestore di eventi in IE4 che richiami il gestore specificato
	             // con i nove argomenti appropriati.
	    this.element[eventname] = function() {
	        var e = dynel.window.event;
	        e.cancelBubble = true;
	        return handler(dynel, e.type, e.x, e.y,
	                       e.button, e.keyCode,
	                       e.shiftKey, e.ctrlKey, e.altKey);
	    }
	}

	// Rimuove un gestore di eventi.
	DynEl.prototype.removeEventHandler = function (eventname) {
	    delete this.element[eventname];
	}
}
//-->