// reference.js
// riferimento biblico o enciclico

// © 2001 - Copyright by CathoMedia
// Tutti i diritti riservati

// *********************************** //
// *** Enumeratore delle proprietą *** //
// *********************************** //

// valori di type
var refBibbia = 0, refConcilio = 1, refEnciclica = 2, refCatechismo = 3;
// valori di subType
var refText = 0, refNotes = 1, refParallels = 2;

// ***************** //
// *** L'oggetto *** //
// ***************** //

function Reference(type, expression) {
	
	// Proprietą pubbliche
	this.language = "italiano";
	this.subType = refText;
	
	// Tutto a zero
	this.type = refBibbia;
	this.book = "";
	this.chapter = "";
	this.verse = "";
	this.simplex = "";
	this.complex = "";
	
	// imposta le proprietą
	this.setValue(type, expression);
	
}

// ************************** //
// *** Metodi e proprietą *** //
// ************************** //

Reference.prototype.setValue = Reference_setValue;
Reference.prototype.navigateTo = Reference_navigateTo;

// ******************************** //
// *** Le sottofunzioni interne *** //
// ******************************** //

// Naviga al riferimento contenuto
function Reference_navigateTo() {
	var path = "../", divId;

	// a seconda del "type"
	switch (this.type) {
		case refBibbia:
			path = path + "bibbia/" + this.language + "/";
			break;
		case refConcilio:
			path = path + "concilio_vaticano_2/" + this.language + "/";
			break;
		case refEnciclica:
			if (this.pope == null)
				this.pope = "giovanni_paolo_2";
			path = path + "encicliche/" + this.pope + "/" + this.language + "/";
			break;
		case refCatechismo:
			path = path + "catechismo/" + this.language + "/";
			break;
		default:
			alert("Insieme di documenti non trovato (type=" + this.type + ")!");
			break;
	}
	
	divId = this.chapter;
	if (this.Verse != "")
		divId = divId + "_" + this.verse;

	// a seconda del "subType"
	switch (this.subType) {
		case refText:
			path = path + this.book + ".htm";
			if (this.chapter + this.verse != "")
				path = path + "#" + "v" + this.chapter + "_" + this.verse;
			// alert("Navigo nel frame: 'testo' all'indirizzo: " + path);
			parent.frames["workarea"].frames["testo"].location = path;
			selectBook(this.book);
			break;
		case refNotes:
			path = path + "note/" + this.book + "/" + this.chapter + ".htm?n" + divId;
			// alert("Navigo nel frame: 'note' all'indirizzo: " + path);
			parent.frames["workarea"].frames["note"].location = path;
			break;
		case refParallels:
			path = path + "paralleli/" + this.book + "/" + this.chapter + ".htm?p" + divId;
			// alert("Navigo nel frame: 'paralleli' all'indirizzo: " + path);
			parent.frames["workarea"].frames["paralleli"].location = path;
			break;
		default:
			alert("Insieme di documenti non trovato (subType=" + this.subType + ")!");
			break;
	}
	
}

// Imposta tutte le proprietą (metodo essenziale)
function Reference_setValue(type, expression) {

	if (expression != null) {
		var s = expression.toLowerCase(), bs =" ", cs =",", i;
		// alert("setValue = " + expression);

		this.type = type > 0 && type < 4 ? type : 0;
		// L'espressione passata
		this.complex = s;

		// Se contiene intervallo capitoli e/o versetti: tronca
		i = s.indexOf("-");
		if (i != -1)
			s = s.left(i);
		// Se contiene elenco versetti: tronca
		i = s.indexOf(".");
		if (i != -1)
			s = s.left(i);
		// Se č nota della citazione
		if (s.right(1) == "+")
			s = s.left(s.length - 1);
		// Se č seguente o seguenti... tronca
		if (s.right(1) == "s") {
			i = 1;
			if (s.right(2) == "ss")
				i = 2;
			s = s.left(s.length - i);
		}
		// Assegna
		this.simplex = s;
		
		// se č enciclica separatori libro e capitolo sono invertiti
		if (this.type != refBibbia) {
			bs = ","; cs = " ";
		}

		// prima occorrenza separatore di libro
		i = s.indexOf(bs);
		// Se c'č...
		if (i != -1) {
			// Estrae il libro
			this.book = s.left(i);
			// rimane...
			s = s.substr(i + 1);
			// senza spazi a sinistra
			s = s.lTrim();
			// Separatore capitolo
			i = s.indexOf(cs);
			if (i != -1) {
				this.chapter = s.left(i);
				this.verse = s.substr(i + 1);
			}
			else {
				// Solo capitolo
				this.chapter = s;
			}
		}
		else {
			// Solo libro
			this.book = s;
		}
	}
	else {
		// Tutto a zero
		this.type = refBibbia;
		this.book = "";
		this.chapter = "";
		this.verse = "";
		this.simplex = "";
		this.complex = "";
	}
	
}
