/* ------------------ 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 remian 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 + "; hbonds calculate; "
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; hbonds calculate; define bbone ( backbone or ( hydrogen and within(1.9,backbone) ) ); define current all; "
var scr_load_gc = "load gc.pdb; hbonds calculate; 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°"


/*
The zoom value in the move command is ADDED to the present zoom.
So, to go from 100% to 200%, +100.
To go from 100% to 50%, -50.
*/

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 )	
	{	document.images['zoomIn'].src = zoomIn0.src
		document.getElementById('btnZoomIn').disabled = true
	}
	else 
	{	document.images['zoomIn'].src = zoomIn1.src
		document.getElementById('btnZoomIn').disabled = false
	}
	if (zoom<=minZoom )	
	{	document.images['zoomOut'].src = zoomOut0.src
		document.getElementById('btnZoomOut').disabled = true
	}
	else 
	{	document.images['zoomOut'].src = zoomOut1.src
		document.getElementById('btnZoomOut').disabled = false
	}

	jmolScript(scr_Busy + zoomCommand + scr_Ready);
}
function zoomerOld(z)
{	var newZoom = zoom * z;
	//var zoomCommand = "zoom " + parseInt(zoom) + "; " ;	// Jmol did not accept decimals in zoom
	var zoomCommand = "zoom " + zoom + "; " ;	// Jmol did not accept decimals in zoom
	if ( (z>1 && newZoom <= maxZoom) || (z<1 && newZoom >= minZoom) )
	{	//zoomCommand += "move 0 0 0 " + parseInt(newZoom-zoom) + " 0 0 0 0 2 30 10; ";
		zoomCommand += "move 0 0 0 " + (newZoom-zoom) + " 0 0 0 0 2 30 10; ";
		// Jmol did not accept decimals in zoom
		zoom = newZoom;
	}
	if (zoom>=maxZoom )	
	{	document.images['zoomIn'].src = zoomIn0.src
		document.getElementById('btnZoomIn').disabled = true
	}
	else 
	{	document.images['zoomIn'].src = zoomIn1.src
		document.getElementById('btnZoomIn').disabled = false
	}
	if (zoom<=minZoom )	
	{	document.images['zoomOut'].src = zoomOut0.src
		document.getElementById('btnZoomOut').disabled = true
	}
	else 
	{	document.images['zoomOut'].src = zoomOut1.src
		document.getElementById('btnZoomOut').disabled = false
	}

	jmolScript(scr_Busy + zoomCommand + scr_Ready);
}

function resetZoom()
{	zoom = startupZoom
	document.images['zoomIn'].src = zoomIn1.src
	document.getElementById('btnZoomIn').disabled = false
	document.images['zoomOut'].src = zoomOut1.src
	document.getElementById('btnZoomOut').disabled = false
}

function writeZoomBtns()
{	document.write('<button type="button" id="btnZoomIn" style="width:25px;" onClick="zoomer(2)"><img src="lupa-mas.gif" name="zoomIn" alt="+" title=""></button>&nbsp;')
	document.write('<button type="button" id="btnZoomOut" style="width:25px;" onClick="zoomer(1/2)"><img src="lupa-menos.gif" name="zoomOut" alt="-" title=""></button>')
}

function buildJmol(j)
{	document.title = "ABCD".charAt(j) + ". " + pageTitles[j]
	var x, y
	if (self.innerWidth)
	{	x = self.innerWidth; y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) // IE6 Strict Mode
	{	x = document.documentElement.clientWidth; y = document.documentElement.clientHeight;
	}
	else if (document.body && document.body.clientWidth)  // other IE and IE6 Quirks Mode (e.g., without declarating <!DOCTYPE HTML )
	{	x = document.body.clientWidth; y = document.body.clientHeight;
	}
	else { x=750; y=550 }

	jmolInitialize(JmolPath)
	document.write('<div id="JmolPane" class="JmolPanels">')
	jmolApplet("100%", 'language="' + JmolLang + '"; set zoomLarge on; ' + startUp(pageNames[j]) )
	document.write("</div>")
}

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)
}

/*  resizable panels: */
	var side = "top"	
	var h = 85			// percent of window height assigned to Jmol panel
//      --------------

var cssTx = '<style type="text/css">'
cssTx += '#JmolPane  { left:0px; width:100%; top:' + ( (side=="top") ? "0px" : ((100-h)+"%") ) + '; height:' + h + '%;} '
cssTx += '#mainPane  { left:0px; width:100%; top:' + ( (side=="top") ? (h+"%") : "0px" ) + '; height:' + (100-h) + '%;} '
cssTx += '</style>'
document.writeln(cssTx)
