// The leading B_ in the name of this file stands for ButtonCallback

var loaded = "dna";
var rep = "thin"; // or thick or sf
var alertme = false;
var hatoms = false;
var bases = -1; // 0=backbone selected; 1=bases selected; -1=all selected.

/* Plan:

Hbonds will always be on for this page.  The Hbonds button will emphasize
them (thick) but there will be no way to show them as less than dotted
wireframe.

When a single AT or GC base pair is displayed, here are two modes of Hbond
display:
	'Short' bonds run from H to N/O;
	'Long' bonds run from N to N/O.
Whether short or long hbonds are displayed is dependent on whether or not
H atoms are displayed, which is controlled by the H button.
Exception: thin and thick displays always show long bonds (erasing just
the H atoms which participate in the bonding).

These select a subset of atoms, wireframing hbonds:
	backbone, bases, AT, GC, H

These select all atoms:
	thin, thick, reset, hbonds

These operate on the previously selected subset:
	spacefill (sf)

*/

function bcb_action(bi)
{
// This works in local mode but fails in server mode.
// .document. is necessary in local mode.
// Order of loading src in dnapairs.htm was critical.
// Trace.js needed to be loaded before b_pairs.js!

	if (alertme)
		alert("i=" + bi + "  rep=" + rep + "  loaded=" + loaded)

	if (bi == "b_xspin")
	{
		script = "center selected;move 90 0 0 0 0 0 0 0 5 30;";
		scriptToChime(script);
	}
	else if (bi == "b_yspin")
	{
		script = "center selected;move 0 90 0 0 0 0 0 0 5 30;";
		scriptToChime(script);
	}
	else if (bi == "b_end") // End
	{
		script = "reset;";
		if (loaded == "dna")
			script += "rotate z 90; rotate y -80;";
		else if (loaded == "gc")
			script += "view set -87.06 48.04 -179.45 192.00 -5.00 0.00 0.00;";
		else // AT
		{
			script += "view set -75.63 24.55 -178.93 190;";
			// center at_center; FAILS!
			script += "select at_center; center selected;";
		}
		script += "zoom " + zoom + ";"; // after placing center!
		scriptToChime(script);
	}
	else if (bi == "b_side")
	{
		script = "reset;";
		if (loaded == "dna")
			script += "rotate z 90; rotate y 10;";
		else if (loaded == "gc")
			script += "view set 20.69 44.05 -166.32 192.00 -5.00 0.00 0.00;";
		else // AT
		{
			script += "view set 15.90 24.55 -178.93 200;";
			// center at_center; FAILS!
			script += "select at_center; center selected;";
		}
		script += "zoom " + zoom + ";"; // after placing center!
		scriptToChime(script);
	}
	else if (bi == "b_2x")
	{
		two_x();
	}
	else if (bi == "b_half")
		half_x();
	else if (bi == "b_backbone") // Backbone: makes backbone stick and all else thin.
	// obeys hatoms and hbonds (long or short)
	{
		bases = 0;
		do_script = true;
		selectbb(do_script);
	}
	else if (bi == "b_bases")
	{
		bases = 1;
		do_script = true;
		selectbb(do_script);
	}
	else if (bi == "b_hbonds")
	{
		rep = "thin";
		if (hatoms)
			script = "select all;";
		else
			script = "select not hydrogen;";
		script += "spacefill off; wireframe 0; set bonds off;";
		if (loaded == "dna")
			script += "hbonds 0.3;";
		else
				script += bp2("xx_lh_tk");
		scriptToChime(script);
	}

	else if (bi == "b_thin") // forces all to wireframe 0
	{
		rep = "thin";
		bases = -1;
		script = "set bonds off;"; // show double bonds as single
		if (loaded != "dna" && hatoms == false)
			script += "select not hydrogen;";
		else
			script += "select all;";
		script += "spacefill off; wireframe 0; hbonds off; hbonds;";
		if (loaded != "dna")
			script += bp2("xx_lh_tn");
		scriptToChime(script);
	}
	else if (bi == "b_thick") // forces all to wireframe 0.3.  Maintains hatoms and
	// hbonds (long or short)
	{
		rep = "thick";
		bases = -1;
		script = "set bonds off;"; // show double bonds as single
		if (loaded != "dna" && hatoms == false)
			script += "select not hydrogen;";
		else
			script += "select all;";
		script += "spacefill off; wireframe 0.3;" +
			" hbonds off; hbonds 0.2;";
		if (loaded != "dna")
			script += bp2("xx_lh_tk");
		scriptToChime(script);
	}
	else if (bi == "b_spacefill") // (sf): spacefill's currently selected atoms,
	// eg all, bases, or backbone.
	{
		rep = "sf";
		script = "set bonds off;"; // show double bonds as single
		script += "wireframe off; hbonds 0.5;";
		if (!hatoms)
			script += "select selected and not hydrogen;";
		script += "spacefill;";		
		scriptToChime(script);
	}
	else if (bi == "b_at" || bi == "b_gc") // AT, GC: loads pair PDB w/ double bonds and
	// renders as B&S without H's nor Hbonds.
	{
		if (loaded == "at" || loaded == "gc") // pair to dna
		{
			if (loaded == "at")
			{
				if (bi == "b_gc")
					alert("Please press AT first.");
				else
				{
					if (isIE)
					{
						waiting_to_load = "1d66-pwz.pdb";
						waiting_script = s_at_uncut;
						scriptToChime(s_at_unpair);
					}
					else
					{
						scriptToChime(s_at_unpair + "load 1d66-pwz.pdb\n" + s_at_uncut);
					}

					loaded = "dna";
				}
			}
			else
			{
				if (bi == "b_at")
					alert("Please press GC first.");
				else
				{
					if (isIE)
					{
						waiting_to_load = "1d66-pwz.pdb";
						waiting_script = s_gc_uncut;
						scriptToChime(s_gc_unpair);
					}
					else
					{
						scriptToChime(s_gc_unpair + "load 1d66-pwz.pdb\n" + s_gc_uncut);
					}

					loaded = "dna";
				}
			}
		}
		else // dna to pair
		{
//			warn_c2b2();
			rep = "bs";
			bases = -1;
			hatoms = false;
			zoom = 200;
			if (bi == "b_at")
			{
				if (isIE)
				{
					waiting_to_load = "at.mol";
					waiting_script = s_at_pair;
					scriptToChime(s_at_cut);
				}
				else
					scriptToChime(s_at_cut + "load at.mol\n" + s_at_pair);

				loaded = "at";
			}
			else // GC
			{
				if (isIE)
				{
					waiting_to_load = "gc.mol";
					waiting_script = s_gc_pair;
					scriptToChime(s_gc_cut);
				}
				else
					scriptToChime(s_gc_cut + "load gc.mol\n" + s_gc_pair);

				loaded = "gc";
			}
		}
	}
	else if (bi == "b_h") // H: Toggles presence of hydrogens maintaining representation.
	{
		script = "";
		if (loaded != "dna")
		{
			if (!hatoms)
			{
				hatoms = true;
				if (bases != -1) // backbone or bases selected
				{
					do_script = false;
					script += selectbb(do_script);
				}
				else
				{
					var script = "select all;";
					if (rep == "thin")
						script += " wireframe 0;";
					if (rep == "thick")
						script += " wireframe 0.3;";
					if (rep == "bs")
					{
						script += " wireframe 0.05;";
						script += " spacefill 0.3;";
					}
					if (rep == "sf")
						script += " wireframe off; spacefill;";
				}

				script += bp2("xx_sh_dt"); // turn on short dotted hbonds
				// turn off long hbonds (temp was defined by xx_sh_dt.spt)
				// gc_lhb was defined when GC was created (gc_pair.spt)
				// at_lhb was defined when AT was created (at_pair.spt)
				if (loaded == "gc")
					script += "select gc_lhb\n"
				else // AT
					script += "select at_lhb\n"

				script += "wireframe off\nselect temp\n";

				scriptToChime(script);
			}
			else
			{
				script = "restrict not hydrogen;";
				script += bp2("xx_lh_dt"); // turn on long dotted hbonds
				scriptToChime(script);
				hatoms = false;
			}
		}
		else
			alert("Sorry, hydrogens can be\n" +
			"shown only for AT or GC.");
	}

	else if (bi == "b_reset") // Reset
	{
		if (isNetscape)
		{
			script = "interrupt; ";	
			if (loaded != "dna")
				script += "zap; load '1d66-pwz.pdb';";
			script += "script reset.spt;";
			scriptToChime(script);
		}
		if (isIE)
		{
			if (loaded != "dna")
				loadscript("1d66-pwz.pdb", "script reset.spt;");
			else
			{
				scriptToChime("script reset.spt;");
			}
		}
		loaded = "dna";
		zoom = 200;
	}
	else if (bi == "b_alert") // alert/debug
	{
		alertme = !alertme;
	}
	else if (bi == "b_ff") // fast forward
	{
		ff = !ff;
		if (ff)
			scriptToChime("set delay off; set move off;");
		else
			scriptToChime("set delay on; set move on;");
		var amsg = "You have toggled fast-forward mode ";
		amsg += (ff? "ON":"OFF");
		if (ff)
			amsg += "\nDelays will be skipped and moves will be immediate.";
		alert(amsg);
	}
	
}
function bp(s)
{
	s = substitute(loaded, "xx", s);
	return ("script '" + s + ".spt';");
}
function bp2(s) // for IE
{
	s = substitute(loaded, "xx", s); // e.g. xx_bases -> at_bases
	return (eval("s_" + s)); // e.g. s_at_bases defined in s_pairs.js
}
function selectbb(do_script)
{
	rep = "thin";
	// WIREFRAME ALL INCLUDING HBONDS 
	// set bonds off: show double bonds as single
	script = "select all; spacefill off; wireframe 0; set bonds off;";

	// SELECT BONDS TO THICKEN
	if (loaded == "dna")
	{
		script += "hbonds off; hbonds;";
		if (bases)
			script += "select not (backbone or hydrogen);"; //fake hydrogens on OH ends
		else
			script += "select backbone and not hydrogen;";
		script += "wireframe 0.3;";
	}
	else
	{
		// THE ORDER OF THESE ACTIONS IS CRITICAL
		if (!hatoms) // turn off hydrogens in wireframe not to be thickened
			script += "restrict not hydrogen;";

		// select bonds to be thickened
		script += bp2("xx_bases"); // leaves lhb's selected
		if (!bases)
			script += "select not selected;";			

		if (!hatoms)
			script += "select selected and not hydrogen;";
		// thicken the selected bonds
		script += "wireframe 0.3;";

		// fixup the hbonds
		if (hatoms)
			script += bp2("xx_sh_dt"); // short dotted from H atom to N/O
		else
			script += bp2("xx_lh_dt"); // long dotted from N to N/O
	}
	if (do_script)
	{
//		if (confirm("b_pairs #1:\n" + script))
			scriptToChime(script);
	}
	return(script);
}
