 // session constants

var TODAY = new Date();

var CLIENT_PLATFORM = "";
var CLIENT_BROWSER = "";
var CLIENT_BROWSER_VERSION = 0;

var DOC_WIDTH = 790;
var DOC_HEIGHT = 400;

var MIN_TEXT_WIDTH = 200;
var MAX_TEXT_WIDTH = 400;

var MIN_APPLET_WIDTH = 50;
var MAX_APPLET_WIDTH = 5000;



// page variables

var text_h = 400;
var text_w = 200;

var jmol_h = 400;
var jmol_w = 400;

var height_offset = 10;
// changing this seems to have no effect whatsoever on div heights

var text_layout = 'left';
var text_color = '000000';
var text_background = 'ffffff';
var text_font = 'verdana';
var text_fontsize = '1em';
var text_fontstyle = 'normal';
var text_fontweight = 'normal';

var jmol_layout = 'right';
var jmol_color = '000000';
var jmol_background = 'ffffff';
var jmol_font = 'courier';
var jmol_fontsize = 12;
var jmol_fontstyle = 'normal';
var jmol_fontweight = 'bold';

var page_title = '';

var nav_location = 'top';
var nav_style = 'link';
var nav_next = '';
var nav_previous = '';


var n = 0;
var buttons = new Array();
buttons[0] = new Array( '2thr.pdb', '' );


// the following added by Frieda
// Angel Herraez's code
// for "foolproof" spin toggle that detects spin state and acts accordingly

// replaced with the one below on 7-23-07
/* function messageProcess(a_,b_)
{	// Convert both parameters from Java strings to JavaScript strings:
        var a = (""+a_).substring("jmolApplet".length)	
        var b = ""+b_
        // a_ will start with "jmolApplet": strip and leave just the ID part
	/*  SPIN DETECTION AND TOGGLE */
	/*if ( b.indexOf("set spin") != -1 )
	{	if ( b.indexOf("spin on") != -1 ) //was spinning
		{	jmolScript("spin off", a) 
		}
		else //was not spinning
		{ 	jmolScript("spin on", a) 
		}
	}
	/*  END of SPIN DETECTION AND TOGGLE  */
//} 


function messageProcess(a_, b_)
{

   // Convert both parameters from Java strings to JavaScript strings:
   // a_ will start with "jmolApplet": strip and leave just the ID part
   var a = (""+a_).substring("jmolApplet".length)
   var b = ""+b_

   /*  SPIN DETECTION AND TOGGLE */
   var detectShowSpin = "spinFps"; // earlier was "set spin".

   if ( b.indexOf(detectShowSpin) != -1 )
   {
//    alert(b);

     if ( b.indexOf("spin on") != -1 ) //was spinning
       jmolScript("spin off", a)

     else //was not spinning
       jmolScript("spin on", a)

   }
   /*  END of SPIN DETECTION AND TOGGLE  */
}


// functions

function initSessionConstants() {

	if (typeof window.innerWidth != "undefined") {
		DOC_WIDTH = window.innerWidth;
		DOC_HEIGHT = window.innerHeight;
	}

	else if (typeof document.documentElement.offsetWidth != "undefined") {
		DOC_WIDTH = document.documentElement.offsetWidth; 
		DOC_HEIGHT = document.documentElement.offsetHeight; 
	}

	else {
		DOC_WIDTH = document.body.offsetWidth; 
		DOC_HEIGHT = document.body.offsetHeight; 
	}
	
	CLIENT_BROWSER_VERSION = parseFloat(navigator.appVersion);
	
	if ((navigator.appVersion).toLowerCase().indexOf("mac") != -1)
		CLIENT_PLATFORM =  "mac";
	else if ((navigator.appVersion).toLowerCase().indexOf("win") != -1)
		CLIENT_PLATFORM =  "win";
	else 
		CLIENT_PLATFORM =  "other";

	var app = navigator.appVersion.toLowerCase();
	if ((app.indexOf("msie") != -1) || (app.indexOf("explorer") != -1)) 
		CLIENT_BROWSER = "ie";
	else if (app.indexOf("netscape") != -1) CLIENT_BROWSER = "netscape";
	else if (app.indexOf("safari") != -1) CLIENT_BROWSER = "safari";
	else if (app.indexOf("chimera") != -1) CLIENT_BROWSER = "chimera";
	else if (app.indexOf("mozilla") != -1) CLIENT_BROWSER = "mozilla";
	else CLIENT_BROWSER = "unknown";

}


function initPageVariables() {

	DOC_WIDTH = DOC_WIDTH-22;


	text_h = DOC_HEIGHT - 30;
	// original value for this height constant was 20
	// Frieda increased to 50 to make room for control(s) beneath applet
	//IE needs 50; safari only 30; oh well
	// this seems to be the only way to change the height
	// i.e., 'setHeightOffset' in config.js seems to do nothing

	text_w = 0.40 * DOC_WIDTH;
	
	jmol_h = text_h - 20;
	jmol_w = DOC_WIDTH - text_w - 30;
		
}


function writeContainerStyle() {
	document.write(
	'<style type="text/css" media="all">' +
	'#container {' +
	'width: ' + (DOC_WIDTH) + 'px;' +
	'background-color: #FFFFFF;' +
	'}' +
	'<\/style>');
}

function setTextLayout( side ) {

	if ( side == 'right' ) {
		text_layout = 'right';
		jmol_layout = 'left';
	}
	
	else {
		text_layout = 'left';
		jmol_layout = 'right';
	}

}


function setTextWidth( pct ) {

	text_w = pct * DOC_WIDTH;
	
	if ( text_w < MIN_TEXT_WIDTH ) 
		text_w = MIN_TEXT_WIDTH;
		
	else if ( text_w > MAX_TEXT_WIDTH ) 
		text_w = MAX_TEXT_WIDTH;
	
	jmol_w = DOC_WIDTH - text_w - 25;
	
}


function setHeightOffset( offset ) {

	height_offset = offset;
	_setAutoHeight();
	
}


function setTextFontFace( font ) {
	text_font = font;
}


function setTextFontSize( sz ) {
	text_fontsize = sz;
}


function setTextFontStyle( style ) {
	text_fontstyle = style;
}


function setTextFontWeight( wt ) {
	text_fontweight = wt;
}


function setTextColor( color ) {
	text_color = color;
}


function setTextBackground( color ) {
	text_background = color;
}


function setTextAlinkColor( color ) {
	text_alinkcolor = color;
}


function setTextVlinkColor( color ) {
	text_vlinkcolor = color;
}


function setPageTitle( txt ) {
	page_title = txt;
}


function setNavFontFace( font ) {
	nav_font = font;
}


function setNavFontSize( sz ) {
	nav_fontsize = sz;
}


function setNavFontStyle( style ) {
	nav_fontstyle = style;
}


function setNavFontWeight( wt ) {
	nav_fontweight = wt;
}


function setNavColor( color ) {
	nav_color = color;
}


function setNavBackground( color ) {
	nav_background = color;
}


function setNavigationBar( loc, style ) {
	nav_location = loc;
	nav_style = style;
}


function setNavigation( next, prev ) {
	nav_next = next;
	nav_previous = prev;
}


function setJmolFontFace( font ) {
	jmol_font = font;
}


function setJmolFontSize( sz ) {
	jmol_fontsize = sz;
}


function setJmolFontStyle( style ) {
	jmol_fontstyle = style;
}


function setJmolFontWeight( wt ) {
	jmol_fontweight = wt;
}


function setJmolColor( color ) {
	jmol_color = color;
}


function setJmolBackground( color ) {
	jmol_background = color;
}



function getNavigation() {
	return nav_location;
}


function getTextHeight() {
	return text_h;
}


function getTextWidth() {
	return text_w;
}


function getTextLayout() {
	return text_layout;
}


function getTextColor( fmt ) {

	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( text_color, fmt );
}


function getTextBackground( fmt ) {

	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( text_background, fmt );

}


function getTextFontFace() {
	return text_font;
}


function getTextFontSize() {
	return text_fontsize;
}


function getTextFontStyle() {
	return text_fontstyle;
}


function getTextFontWeight() {
	return text_fontweight;
}

/* Frieda's failed attempt (so far!) to get link colors 
   into the config. js file:


function getTextAlinkColor() {

	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( text_alinkcolor, fmt );
}

function getTextVlinkColor() {

	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( text_vlinkcolor, fmt );
	
}
*/

function getPageTitle() {
	return page_title;
}


function getNavColor( fmt ) {

	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( nav_color, fmt );
}


function getNavBackground( fmt ) {

	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( nav_background, fmt );
}


function getNavFontFace() {
	return nav_font;
}


function getNavFontSize() {
	return nav_fontsize;
}


function getNavFontStyle() {
	return nav_fontstyle;
}


function getNavFontWeight() {
	return nav_fontweight;
}


function getButtonCount() {
	return buttons.length - 1;
}


function getJmolLayout() {
	return jmol_layout;
}


function getJmolWidth() {
	return jmol_w;
}


function getJmolHeight() {
	return jmol_h;
}




function getJmolColor( fmt ) {
	
	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( jmol_color, fmt );
}


function getJmolBackground( fmt ) {

	if ( !fmt ) 
		var fmt = '';
		
	return _getColor( jmol_background, fmt );
}


function getJmolFontFace() {
	return jmol_font;
}


function getJmolFontSize() {
	return jmol_fontsize;
}


function getJmolFontStyle() {
	return jmol_fontstyle;
}


function getJmolFontWeight() {
	return jmol_fontweight;
}




function getResetSpt() {
// frieda added some commands
	var spt = 'restrict none; select all; color atoms opaque;' + 
	'halos off;dots off;monitors off;hbonds off;ssbonds off;' + 
	'labels off;set echo off;measurements off;set labeloffset 0 0;' ;
	
	return spt;

}


function getReorientSpt() {

	var spt = 'reset;';
	return spt;
	
}


function getDisplaySpt() {

	var spt = 'set specular 5;' + 
	'set ambient 40;' + 
	'set diffuse 75;' + 
	'background ' + getJmolBackground('jmol') + ';' + 
	'font labels ' + getJmolFontSize() + ' ' + getJmolFontFace() + ' ' + getJmolFontWeight() + ';' + 
	'color labels ' + getJmolColor('jmol') + ';' ;
	
	
	// following if statement added by Frieda
	// this is so when a page loads, 
	// the echo "Now Showing View 1" appears in the applet.
	// getDisplaySpt is called whenever a page loads,
	// so it is a good place to add this.
	// BUT on the index (contents) page
	// there is no view 1, just a default view, so we suppress this echo.
	// note var myPage is also used in deciding whether to put a Contents button on the page.
	
	if ( myPage !== 'index' ) {

		spt += 'font echo 16 sans bold; color echo [xcccccc]; ' +
		'set echo viewNumber 100% 100%; ' +
		'echo Now Showing View 1; ';
	}
	
	return spt;	
}


function getDefaultSpt() {

	var spt = 'select protein;color chain;backbone 0.5;' + 
	'select nucleic;color cpk;wireframe 0.4;' + 
	'select (a and not backbone);color blue;' + 
	'select (c and not backbone);color red;' + 
	'select (t and not backbone);color yellow;' + 
	'select (g and not backbone);color green;' + 
	'select (u and not backbone);color brown;' + 
	'select (hetero and not solvent);color cpk;wireframe 0.4;spacefill 40%;' + 
	'select solvent;color bluetint;wireframe 0.4;spacefill 40%;';

	return spt;	
}


function setStart( pdb, spt ) {

	buttons[0][0] = pdb;

	if ( !spt ) 
		buttons[0][1] = getDefaultSpt();
	else 
		buttons[0][1] = spt;

}


function getStart() {
	var spt = '';
	
	//if ( buttons[num][0] != jmol_pdb ) (this line commented out by Tim)
	
	spt += 'load ' + buttons[0][0] + ';';
	// frieda removed double quotes because when loading multiple pdb files
	// it is better to put double quotes in original command
	
	spt += getResetSpt();
	spt += getReorientSpt();
	spt += getDisplaySpt();
	spt += buttons[0][1];
	
	return spt;	
}

// note 
// num indicates the number of the button that is currently being clicked
// n indicates the number of each button as the buttons were being written
// so when a page is done being written, n = the number of the last button on that page
// and does not change its value again (until next page written)

function writeButton( num ) {

	document.write( '<p>' );
	document.write( buttons[num][5] );

	var spt = '';
	var rst = ( buttons[num][3] ).toLowerCase();
	
	if ( ( rst == 'reset' ) || ( rst == 'y' ) || ( rst == 'yes' ) ) {
	
		// spt += 'load "' + buttons[num][0] + '";';
		// Frieda commented this out  
		// because loading multiple files makes reloading pdb files unnecessary
		// if reinstate, watch out for problems due to quotes

		spt += getResetSpt();
		spt += getReorientSpt();
		spt += getDisplaySpt();
	}

	if ( rst == 'display' ) {
	// frieda added this extra condition to rst
	// it allows instantaneous change of structure, with no change in position
	// instead of structure flashing/repositioning before change appears
	
		spt += getResetSpt();
	}
	
	spt += "set echo viewNumber 100% 100%; echo Now Showing " + buttons[num][4] + ";" ;
	// added by Frieda
	
	spt += buttons[num][1];
	 // Eric added: When a button is clicked, any still-running script should
   // be unconditionally killed.
   spt = "exit;" + spt;

	switch ( buttons[num][2] ) {
		
		case 'link'		: 
			jmolLink( spt, buttons[num][4] );
			break;
		
		case 'button'	: 
			jmolButton( spt, buttons[num][4] );
			break;
		
		case 'radio'	: 
			jmolRadio( spt, ( '&nbsp;&nbsp;' + buttons[num][4] ), false );
			break;
	
		case 'checkbox': 
			jmolCheckbox( spt, buttons[num][7], ( '&nbsp;&nbsp;' + buttons[num][4] ), false );
			break;
			
	}
	
	document.write( '&nbsp;&nbsp;' + buttons[num][6] );
	writeButtonDetailsLink(num);
	document.write( '<\/p>' );
	
}


function writeNavigation() {

	var writeStr = '';
	
	writeStr += '<p class="nav">';
	
	switch ( nav_style ) {
	
		case 'button'	: 
			if ( nav_previous != '' ) {
				writeStr += 
				'<input type="button" value="&lt;- Previous" ' + 
				'onclick="location.href=\'' + nav_previous + '\'" \/>' + 
				'&nbsp;&nbsp;&nbsp;';
			}
			if ( nav_next != '' ) {
				writeStr += 
				'<input type="button" value="Next -&gt;" ' + 
				'onclick="location.href=\'' + nav_next + '\'" \/>';
			}
			
			break;

		case 'link'	: 
			if ( nav_previous != '' ) {
				writeStr += 
				'<a href="' + nav_previous + '" ' + 
				'title="Go back to the previous page.">&lt;- Previous</a>' + 
				'&nbsp;&nbsp;&nbsp;';
			}
			if ( nav_next != '' ) {
				writeStr += 
				'<a href="' + nav_next + '" ' + 
				'title="Go to the next page.">Next -&gt;</a>';
			}
			break;

	}

	writeStr += '<\/p>';

// Frieda added the Contents button code below
// Contents is always a button, even if Next and Previous are links
// Contents button does not appear on Contents page if var myPage is set to 'index'

	if (myPage != 'index') {
		writeStr += 
		'<p class="nav"><input type="button" value="Contents" ' +
		'onclick="location.href=\'collagen_index.htm\'" \/><\/p>';
		//collagen_ added to href by karl
	}
	else {
		writeStr = writeStr; 
	}
	document.write( writeStr );
	
}



function _setAutoHeight() {

	text_h = DOC_HEIGHT - height_offset - 20;
	jmol_h = text_h;
	
}


function _getColor( raw, fmt ) {

	//alert( 'getColor with raw=' + raw + ', fmt=' + fmt );
	
	if ( !fmt ) 
		var fmt = '';
		
	var color = raw;
	var word = 0;
	
	if ( color.search( '/[g-zG-Z]/' ) != -1 ) 
		word = 1;
	else 
		word = 0;
		
	switch ( fmt ) {
	
		case 'css'	: 
			if ( !word ) 
				color = '#' + color;
			break;

		case 'html'	: 
			if ( !word ) 
				color = '#' + color;
			break;

		case 'jmol'	: 
			if ( !word ) 
				color = '[x' + color + ']';
			break;

	}
	
	return color;

}


