/* ------------------ Support file for DNA Tutorial in Jmol ---------------------------------------------------------
                      by Dr. Angel Herráez, Universidad de Alcalá, Spain
					  
	Multilingual-ready version -- thanks to  MESSAGES.JS  which MUST BE CALLED BEFORE this file

	Nothing needs translation in this UTILS.JS file.
*/


// 'Patches': alternatives that the instructor can choose from.

/* Patch #1: load either (a) or (b) model:
		(a) the classical 1d66-pwz.pdb (Martz's modification of 1d66.pdb for Chime DNA tutorial). 
			This has an error in C28, whose base is flipped so that the wrong side faces the complementary G11.
			Chime draws H-bonds to the correct atoms, which are far away. This anomaly is not easily seen, 
			since H-bonds remain in the plane of the base pair.
			Martz took advantage of this for setting one of the student questions.
			Jmol, however, does not draw such long H-bonds, but draws no H-bonds for C28 and draws H-bonds from
			G11 to the neighbour T29 on the other strand. This is easily appreciated and quite ugly.
			
			Solution adopted: use explicit 'connect' commands to 
			remove sideways Hbonds (between G11 and T29)
			and to add fake, long Hbonds similar to those Chime draws (between C28 and G11)

		(b) a new dna.pdb (Herráez) with corrected position of C28, that forms correct H-bonds in Jmol.
		
	Comment-out whichever option you don't want:
	*/
	//var DNAmodel = "1d66-pwz.pdb";
	var DNAmodel = "dna.pdb";

/* Patch #2: Choose which mouse help page to open first  (brief or expanded)
	Comment-out whichever option you don't want:
	*/
	var MouseHelp = "mouse.htm";	//brief help page
	//var MouseHelp = "mouse-det.htm";	//expanded help page

/* Patch #3: Choose whether the initial alert dialog is shown upon loading section A.
	If the alert is not shown, an echo text will be.
	Comment-out whichever option you don't want:
	*/
	//var InitialAdvice = true;
	var InitialAdvice = false;

// End of patches


var JmolPath = "..";
document.writeln('<script src="' + JmolPath + '/Jmol.js" type="text/javascript"><' + '/script>');

var JmolLang = lang;

/* Some peculiarities of Jmol and of these models require to redefine 'backbone' as 'bbone', both for DNA and base pairs;
  bases must then be selected as 'not backbone'.
On this dna.pdb:
  Third oxygen on each strand's terminal 5'P is not recognized as backbone nor bases; 
    therefore, they were manually labeled as O3P in PDB and then explicitly included.
  Further, added H on 3' ends are not recognized as backbone (see base pairs comment).
  Further, in order to accomodate also the "classic" 1d66-pwz.pdb model, 1:D.O3*, 20:E.O3* 
    were also  added to the redefined backbone set.
On these base pairs:
  H atoms in pentose belong to 'sidechain', not to 'backbone'  ('sidechain' and 'bases' are the same)
    therefore, they must be explicitly selected (using Tim's suggestion of 'within' trick).
*/

var scr_load_dna = "load " + DNAmodel + "; select !_H; hbonds calculate; select all; ";
if ( DNAmodel == "1d66-pwz.pdb" ) {
	scr_load_dna += "connect (G11:D) (T29:E) delete; ";				//removes wrong, sideways Hbonds
	scr_load_dna += "connect (G11:D.N2) (C28:E.O2) hbond; ";	//adds 3 long Hbonds between G and flipped C, as in Chime
	scr_load_dna += "connect (G11:D.N1) (C28:E.N3) hbond; ";
	scr_load_dna += "connect (G11:D.O6) (C28:E.N4) hbond; ";
}
scr_load_dna += "define bbone ( backbone or *.O3P or ( hydrogen and within(1.9,backbone) ) or 1:D.O3*,20:E.O3* ); set showHydrogens off; define current all; ";
var scr_load_at = "load at.pdb; select !_H; hbonds calculate; select all; define bbone ( backbone or ( hydrogen and within(1.9,backbone) ) ); define current all; ";
var scr_load_gc = "load gc.pdb; select !_H; hbonds calculate; select all; define bbone ( backbone or ( hydrogen and within(1.9,backbone) ) ); define current all; ";

var scr_Busy = "set echo bottom center; echo " + tx_Busy + "...; ";
var scr_Ready = "set echo off; ";
var scr_End = scr_Busy + "moveTo 1 537 -537 -651 113.8 100; " + scr_Ready;
var scr_Side = scr_Busy + "moveTo 1 705 -705 -68 172.2 100; " + scr_Ready;

// Cache images:
var zoomIn1 = new Image(15,15); zoomIn1.src='lupa-mas.gif';
var zoomIn0 = new Image(15,15); zoomIn0.src='lupa-mas-.gif';
var zoomOut1 = new Image(15,15); zoomOut1.src='lupa-menos.gif';
var zoomOut0 = new Image(15,15); zoomOut0.src='lupa-menos-.gif';

var startupZoom = 100; 
var zoom = startupZoom; 
var loadedMol = "";
var scr_ini = "set perspectiveDepth off; set windowCentered off; set labelGroup; ";

tx_SpinX += " 90°";
tx_SpinY += " 90°";


function gid(x) { return document.getElementById(x); }

var maxZoom = startupZoom*8, minZoom = startupZoom/8;
function zoomer(z) {
	var newZoom = zoom * z;
	var zoomCommand = "zoom " + zoom + "; " ;
	if ( (z>1 && newZoom <= maxZoom) || (z<1 && newZoom >= minZoom) ) {
		zoomCommand += "zoomTo 2 *" + z + "; ";
		zoom = newZoom;
	}
	if (zoom>=maxZoom )	{
		gid('zoomIn').src = zoomIn0.src;
		gid('btnZoomIn').disabled = true;
	} else {
		gid('zoomIn').src = zoomIn1.src;
		gid('btnZoomIn').disabled = false;
	}
	if (zoom<=minZoom )	{
		gid('zoomOut').src = zoomOut0.src;
		gid('btnZoomOut').disabled = true;
	} else {
		gid('zoomOut').src = zoomOut1.src;
		gid('btnZoomOut').disabled = false;
	}

	jmolScript(scr_Busy + zoomCommand + scr_Ready);
}

function resetZoom() {
	zoom = startupZoom;
	gid('zoomIn').src = zoomIn1.src;
	gid('btnZoomIn').disabled = false;
	gid('zoomOut').src = zoomOut1.src;
	gid('btnZoomOut').disabled = false;
}

function writeZoomBtns() {
	document.write('<button type="button" id="btnZoomIn" onClick="zoomer(2)"><img src="lupa-mas.gif" id="zoomIn" alt="+" title=""></button>&nbsp;');
	document.write('<button type="button" id="btnZoomOut" onClick="zoomer(1/2)"><img src="lupa-menos.gif" id="zoomOut" alt="-" title=""></button>');
}

function buildJmol(j) {
	document.title = "ABCD".charAt(j) + ". " + pageTitles[j];
	jmolInitialize(JmolPath);
	jmolApplet("100%", 'language="' + JmolLang + '"; set zoomLarge on; ' + startUp(pageNames[j]) );
}

function startUp(pag) {
	zoom=startupZoom;
	if ( loadedMol=="dna" ) {
		return "set echo off; script " + pag + ".spt; ";
	}	else {
		loadedMol = "dna";
		var tx = scr_ini;
		if (!InitialAdvice && pag=="dnapairs") {
			var tx2 = tx_Hello.replace(/\n\n/, "|");
			tx += "set echo hello 50% 100%; set echo hello center; color echo yellow; echo " + tx2 + "; ";
		}
		return scr_load_dna + tx + "script " + pag + ".spt; ";
	}
}

function footer() {
	mouseHelp(); 
	showVersion();
}

function mouseHelp() {
	var tx1 = '<a href="javascript:void(window.open(\'mouse/' + MouseHelp + '\', \'mouse\', \'resizable, scrollbars\'))" style="padding-right:20px;" title="' + tx_MouseHint + '">'+
		tx_Help + '<img src="mouse/mouse.gif" border="0" align="absmiddle" alt="' + tx_MouseHint + '"></a>';
	document.write(tx1);
}

function showVersion() {
	document.write('<div style="margin-top:0.5em;"><a href="javascript:alert(\'' + tx_About_full + '\')">' + tx_About_short + '</a></div>');
}

function spc(n) {
	for (var j=0; j<n; j++) { document.write("&nbsp;"); }
}


// These are used by dnapairs.htm and dnacode.htm :

var curSelection = "all";
function thicken(thickness) {
	var c0 = "";
	var c1 = "spacefill off; wireframe 0.07; define temp selected; select current; hbonds 0.07; select temp; ";
	if (curSelection=="hbonds") {
		c0 = "define temp selected; select current; " + c1 + "hbonds " + thickness + "; select temp; ";
	} else {
		c0 = c1 + "select " ;
		if (curSelection=='all') {
			c0 += "all; hbonds " + thickness  + "; ";	//Hbonds thickness follows global thickness
		} else {
			c0 += curSelection + "; ";
		}
		c0 += "wireframe " + thickness + "; ";
	}
	jmolScript(c0);
}

function setSelection(s,formu) {
	curSelection = s;
	if (s=='hbonds') { formu.Spacefill.disabled = true; }
	else { formu.Spacefill.disabled = false; }
	if (s==="") { curSelection="all"; return; }	//special for DNA>AT and DNA>GC buttons (and reset button, maybe no longer used); also after Codons
	thicken(0.3);
}

function makeLangLink(x,p) { 
	var y = x.replace('[','<a href="'+p+'">');
	y = y.replace(']','</a>. ');
	return y;									 
}
function writeLangLinks() {
	var p_gen = "../dna";
	var p_es = "../../../model4/dna/index.htm";
	var p_pt = "../../../pt/model4/inicio.htm";
	if (lang=="es") {
		p_gen = "../../en/model4/dna"; 
		p_pt = "../../pt/model4/inicio.htm";
	} else if (lang=="pt_BR") {
		p_gen = "../../../en/model4/dna"; 
		p_es = "../../../model4/dna/index.htm";
	}	
	var langs = allLangs.split(" "); 
	var p , tx="" , j , k;
	for (j in langs) { 
		k = langs[j]; 
		if (k!=lang) { 
			if (k=="es") { p = p_es; }
			else if (k=="pt_BR") { p = p_pt; }
			else { p = p_gen + (k=="en"?"":"_"+k) + "/index.htm"; }
			tx += makeLangLink(tx_Available[k],p); 
		}
	}
	document.writeln(tx);
}

