
var charger=true;

// submit de la barre d'outils
function soumettre() {
	if (document.form1.version.value=='') parent.page.location.href='versions.htm'; 
	else if (document.form1.livre.value.length!=3)
		{document.form1.livre.value='';parent.page.location.href='livres.htm';}
	else if (charger) ouvrir(); //uniquement si a changé
	else reference(true);

	return false;
}

var RACINE_BIBLES='../';

// appelé par soumettre si flag 'charger'
function ouvrir() {

	var l_chemin_livre=RACINE_BIBLES+document.form1.version.value.toUpperCase()+'/'+document.form1.livre.value.toUpperCase()+'.htm';
	var l_chapitre=document.form1.chapitre.value;
if (document.form1.xsl.checked)
	appliquerXSL(document.form1.feuillestyle.options[document.form1.feuillestyle.selectedIndex].value);
//	parent.page.location.href="/util/sablotron2.php?xml=/"+l_chemin_livre+"&xsl=/xsl/chapitre.xsl&parametres="+escape("chapitre="+l_chapitre);
	else {
		parent.page.location.href=l_chemin_livre;
	}

if (parent.name.substring(0,5)=='fille') //fenêtre fille
	parent.frameset1.rows="0,*";

init()
//setTimeout('init()',3000); //pour compatibilité IE4 et 5. (appel normal= onload de la frame "page")
}

/**
  Appelée par onload après chargement d'un livre
 */
function init() {
	if (document.form1.xsl.checked) return;
	if (!charger) return; //évite appel une 2 fois 

	//Lecture de VERSION et LIVRE et FeuilleStyle par analyse URL
	var r=parent.page.location.href.match(/([A-Za-z][A-Za-z][A-Za-z])\/([A-Z1-3][A-Z][A-Z])\.htm/);
	if (!r) return;
	version=r[1];
	livre=r[2];

	document.form1.version.value = version;
	document.form1.livre.value = livre;
	var l_monSelect=document.form1.feuillestyle;

	if (parent.page.document.styleSheets.length>0)
		l_monSelect.options[0].value=parent.page.document.styleSheets[0].href.match(/charte\/(.*)\.css/)[1]; 
	//on extrait "hebreu" ou "grec" ou "style"... (nom de la feuille de style prédéfinie)
	if (l_monSelect.selectedIndex!=0) appliquerStyle(l_monSelect.options[l_monSelect.selectedIndex].value);
	reference(true);
	document.form1.chapitre.focus();
	charger=false;
}
/** appelée par onChange du Select des styles et init() */
function appliquerStyle(p_style) {
	//if (parent.page.location.href.substr(parent.page.location.href.length-4)!='.htm') return;
	if (document.form1.xsl.checked) appliquerXSL(p_style)
	if (!parent.page.location.href.match(/([A-Z0-9]+)\/([A-Z0-9]+)\.htm/i)) return;
	//if (!parent.page.valide) return; comment faire?
	var l_url = '../charte/'+p_style+'.css';
	parent.page.document.styleSheets[0].href = l_url;
	charger=false;
}
// appelé par init, soumettre, clic sur numéro (à chaque changement de réf), resize
function reference(avecSynchro) {
  //alert('ref '+avecSynchro);
	var cn=document.form1.chapitre.value;
	var vn=document.form1.verset.value;
	l_ancre='#'+cn;
	//l_ancre='#'+cn+(vn=='0'?'':'.'+vn);
	//parent.page.location.hash=l_ancre;
	l_location=parent.page.location;
	l_location.href=l_location.pathname+l_location.search+l_ancre;

	var chapitres,versets,i,j;
	chapitres = parent.page.document.getElementsByTagName("blockquote");

/*
	for (i=Math.min(cn,chapitres.length);i>0;i--) {
		if (parseInt(chapitres[i-1].firstChild.firstChild.nodeValue)==cn) break; //<cn> cas normal: i=cn. pas trouvé i=0
	}

	if (i==0) return;
*/

	i=eval(cn);
	versets=chapitres[i-1].getElementsByTagName("u");

	for (j=Math.min(vn,versets.length);j>0;j--) 
		//if (parseInt(versets[j-1].innerText)<=vn) break; //<vn> cas normal: j=vn. pas trouvé verset précédent
		if (parseInt(versets[j-1].firstChild.nodeValue)<=vn) break; //<vn> cas normal: j=vn. pas trouvé verset précédent

	if (j>0) 
			versets[j-1].scrollIntoView(true); 
	else	
			chapitres[i-1].scrollIntoView(true);
			

	 if (avecSynchro) synchroniserFilles();

	//On change barre de titre de la fenêtre
	parent.document.title=document.form1.livre.value+' '+document.form1.chapitre.value+(document.form1.verset.value>1?'.'+document.form1.verset.value:'')+' ['+document.form1.version.value+']';
	//parent.page.focus();

}

///////////////////// Fin évènements ///////////////////
//Ouvre nouvelle fenêtre sur ref sélectionnée
//Appel par "alt-G" ou clic sur bouton "ouvrir"
function go()
{
	ref=parent.page.document.selection.createRange().text;
	if (ref!='') document.form1.ref.value=ref;
	ref=document.form1.ref.value;
	open('index.htm?ref='+escape(ref));
}

// non utilisée pour l'instant
function regle(element,affichage)
{
	if (!parent.page.document.styleSheets[0].addRule) 
		parent.page.document.styleSheets[0].addRule = new Function('element','regle', 'this.insertRule(element +" {"+ regle +"}",this.cssRules.length )') 

	parent.page.document.styleSheets(0).addRule(element,'display:'+affichage);
	reference(false);
}

//////////////////// Gestion de la synchronisation à partir d'une fenêtre mère

var fille=new Array();
var noFille=0;

//Appelée par appui sur bouton correspondant
function ouvreFille(version) {
	var ls_argument='';
	//if (version!='') ls_argument="?version="+version;
	noFille++;

	ls_options='toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes'

	ls_taille='width='+(screen.width-20)/2+',height='+(screen.height-100)/2+',top='+(screen.height-30)*((1+noFille)%2)/2+',left='+(screen.width)/2; //Le %2 sert à ouvrir la fille une fois en haut, une fois en bas

	ls_nom='fille'+noFille+'_'+Math.floor(Math.random()*1e12); //évite interférences si plusieurs visionneuses.
	fille[noFille]=open("index.htm"+ls_argument,ls_nom,ls_options+','+ls_taille);
	
	document.images["copie"].src = '../charte/copie1.gif';

	
}

//Appelée par reference(true)
function synchroniserFilles() {
	for (var i=1;i<=noFille;i++) {
		if (fille[i].closed) continue; //fenêtre fille a été fermée 

		if (charger) fille[i].haut.charger=true;		
		fille[i].haut.document.form1.livre.value=document.form1.livre.value;
		fille[i].haut.document.form1.chapitre.value=document.form1.chapitre.value;
		fille[i].haut.document.form1.verset.value=document.form1.verset.value;
		fille[i].haut.soumettre();
	}
}

function appliquerXSL(p_style) {
	var l_chemin_livre=document.form1.version.value.toUpperCase()+'/'+document.form1.livre.value.toUpperCase()+'.htm';
	var l_version=document.form1.version.value;
	var l_livre=document.form1.livre.value;
	var l_chapitre=document.form1.chapitre.value;
	var l_verset=document.form1.verset.value;
	//if (parent.page.location.href.substr(parent.page.location.href.length-4)!='.htm') return;
	//if (!parent.page.location.href.match(/([A-Z0-9]+)\/([A-Z0-9]+)\.htm/i)) return;
	//if (!parent.page.valide) return; comment faire?
	var l_fichier_xsl = 'xsl/'+p_style+'.xsl';
	//parent.page.document.styleSheets[0].href = l_url;
	l_resultat=parseur(l_chemin_livre,l_fichier_xsl,'version',l_version,'livre',l_livre,'chapitre',l_chapitre,'verset',l_verset);
	top.page.document.open();
	top.page.document.write(l_resultat);
	top.page.document.close();
}

function parseur(xml,xsl,param1,valeur1,param2,valeur2,param3,valeur3,param4,valeur4) {

	var xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
	var xslDoc = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
	var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");

	if (xml.length<700) xmlDoc.load(xml); //la var xml est une url
	else xmlDoc.loadXML(xml);

alert(xml)
alert(xsl)
	if (xsl.length<800)	xslDoc.load(xsl);
	else xslDoc.loadXML(xsl);

	xslTemplate.stylesheet = xslDoc;

	var xslProc = xslTemplate.createProcessor();
	xslProc.input = xmlDoc;

	if (param1) xslProc.addParameter(param1,valeur1);
	if (param2) xslProc.addParameter(param2,valeur2);
	if (param3) xslProc.addParameter(param3,valeur3);
	if (param4) xslProc.addParameter(param4,valeur4);

	xslProc.transform();
	return xslProc.output;
}


