/*
Bob Hanson 12:29 PM 12/22/2002

adapted for Jmol 4:16 PM 9/17/2007

Note: these data are from PM3 calculations of cyclohexane using CAChe.
First, three maps were created. One going from chair to twist boat (cycmap),
one going from twist to twist (twist1), then one going from twist to chair (twist2).
These three maps were aligned and exported to animated xyz files using the MORPH command line option of origami.exe.
(the MORPH command option outputs xyz files aligned based on atoms 1, 3, and 5.)
morphxyz.exe was then used to interpolate 3 structures in between each calculated structure,
and also to construct a single 717-structure xyz file for animation.
This file is an adaptation of the output from morphxyz.exe, which was used
to set the x,y,z default rotation.

That said, it should be realized that the boat is NOT a required conformation enroute from
Chair I to Chair II. Indeed, this has been elegantly shown by Hens Borkent. See 
http://www.cmbi.kun.nl/wetche/organic/cyclohexane/
for an exceptionally nice treatment of the cyclohexane chair/chair interconversion.

The graphic used here is adapted from FG02_010_compfig.JPG.
It was made somewhat smaller by rearranging some of the graphical 
elements.

*/

defaultscript="reset;select *; zoom 150; wireframe 10; spacefill 50;moveto /* time, axisAngle */ 0.0 { 774 512 372 148.31} /* zoom, translation */  120.0 0.0 0.0  /* center, rotationRadius */ {-0.0024999976 0.72249997 -1.214} 4.3989015 /* navigation center, translation, depth */ {0.0 0.0 0.0} 7.538616 -2.5870478 50.0;"
fps=16
going=0
dir=1
lastframe=0
isuser=false

function writeChime(){
 var s="<embed frank=no align=abscenter src=c6movie.xyz name=c6chime width=270 height=200"
 s+="\n animfps=16 bgcolor=#FFFFFF startanim=falsecolor3d=cpk options3d=specular"
 s+="\n animmode=loop AnimFrameCallback=JSAnimcontrol"
 s+="\n script=\""+defaultscript+"\"\n>"
 document.write(s)
}

function createmap(name){
	var s="<map name="+name+">"
	var r=40
	for(var i=1;i<Curve.length-1;i++){if(Curve[i].length>3){
		s+="\n<area shape=circle coords='"+Curve[i][3]+","+Curve[i][4]+","+r+"' href='javascript:cshow("+Curve[i][0]+")'>\n"
	}}
	s+="\n</map>"
	document.writeln(s)
}

var FM=new Array();
FM['0']='chair I'
FM['1']='chair I'
FM['61']='half-chair'
FM['105']='twist-boat'
FM['161']='boat'
FM['193']='twist-boat'
FM['289']='half-chair'
FM['373']='chair II'
FM['429']='half-chair'
FM['529']='twist-boat'
FM['573']='boat'
FM['613']='twist-boat'
FM['657']='half-chair'
FM['717']='chair I'

var FYI=new Array();
FYI["chai"]="In the chair conformation, six groups are equatorial and six groups are axial. All of the bonds around the ring are staggered. It is the lowest-energy conformation."
FYI["boat"]="The boat conformation, with two eclipsed ring bonds and two \"flagpole\" groups (shown on the model in red and blue) in particularly close contact, is the second-highest energy conformation."
FYI["twis"]="In the twist-boat conformation, there are no eclipsing interactions, but the dihedral angles around the ring are still not 60 degrees, so the energy is higher than that of the chair conformation."
FYI["half"]="The half-chair conformation is particularly high in energy. Five of the ring carbons are very close to being in the same plane, and there are three nearly eclipsed interactions. It is the highest-energy conformation."

//these are x,y coordinates on the gif for the pt, then x,y for center of picture

Curve=new Array(0 
,[1,40,181,90,200]
,[61,139,53,87,32]
,[105,210,114,204,139]
,[161,245,95,244,59]
,[193,280,114,288,134]
,[289,350,53,405,38]
,[373,449,181,410,197]
,[429,350,53]
,[529,280,114]
,[573,245,95]
,[613,210,114]
,[657,139,53,]
,[717,40,181]
,[718,40,181]
)


function cshow(n){
 isuser=true
 executeScript("c6chime","animation off;anim frame "+n)
 going=0
 dir=1
}

function doSPT(what){
 executeScript("c6chime",getSPTscript(what))
 isuser=false
}

function getSPTscript(what){
 if(what=="Continue"){
	s="animation play"
	going=1//(going==1?0:1)
	return s
 }
 if(what=="Reverse"){
	dir=-dir
	s="animation playrev"
	going=1	
	return s
 }
 if(what=="Stop"){
	going=0
	return "animation off"
 }
 if(what=="Slower"){
	fps/=2
	if(fps<1)fps=1
	going=dir
	return "animation off;anim fps "+fps+";animation play;"
 }
 if(what=="Faster"){
	fps*=2
	if(fps>128)fps=128
	going=dir
	return "animation off;anim fps "+fps+";animation play;"
 }
 if(what=="Reorient"){
	fps=16
	return defaultscript
 }
 return defaultscript
}

function executeScript(name,script){
 jmolScript(script)
}


//offsets roughly half the width and height of the dot

xoff=-9
yoff=-9

function initdivs(){
	isnn4=(document.layers?true:false)
	isie4=(document.all?true:false)
	isnn6=(!isie4 && document.getElementById?true:false)
	if(!isnn4 && !isie4 && !isnn6)alert("This page will work properly only with browsers capable of supporting layers.")
}

var anchorx=0
var anchory=0

function divanchor(s){
	var e=s
	var doc=document
	D=new Array()
	
	if(isnn4){
		if(doc[s]==null)return D
		D.left=doc[e].x
		D.top=doc[e].y
	}else{
		e=(isnn6?doc.getElementById(s):doc.all[s])
		if(e==null && isnn6)e=doc[s]
		if(e==null && isnn6)e=doc.anchors[s]
		if(e==null && isnn6)e=doc.images[s]	
		if(e==null)return D
		D.left=e.offsetLeft
		D.top=e.offsetTop
		while(e.offsetParent!=null){
			e=e.offsetParent
			D.left+=e.offsetLeft
			D.top+=e.offsetTop
		}
	}
	anchorx=D.left
	anchory=D.top
	return D
}

function divmove(s,x,y){
	var d=findlayer(s)
	if(d==null)return null
	x+=anchorx
	y+=anchory
	if(isnn4){
		d.left=x
		d.top=y
	}else{
		d.style.left=x
		d.style.top=y
	}
	return d
}

function findlayer(name){
	if(isnn4)return document.layers[name]
	if(isnn6)return document.getElementById(name)
	if(isie4)return eval('document.all.' + name)
	return false
}

function writediv(name,sinfo){
	var ds=findlayer(name)
	
	if(isnn4){
		ds.document.open()
		ds.document.write(sinfo)
		ds.document.close()
	}else{
		ds.innerHTML=sinfo
	}
}


function dodefault()
{
  initdivs()
  divanchor("graph")
  moveDot(1)
}

 //This callback function gets executed automatically every time
 // the animation advances to a new frame

function JSAnimcontrol( PlugName, FrameNo )
{
  indicateframe(FrameNo)
  return true;  
}

function indicateframe(FrameNo){
  moveDot(FrameNo)
  var s=FM[''+FrameNo]
  if(!s)return
  writediv("c6msg","<h2>"+s+"</h2>")
  document.frmcontrol.c6info.value=s+"\n\n"+FYI[s.substring(0,4)]
  if(!isuser && going && !isContinuous()&& FrameNo!=0 && (FrameNo!=1 || lastframe!=0))doSPT("Stop")
  lastframe=FrameNo
  if(FrameNo==0)dir=1
}

function isContinuous(){
 return document.frmcontrol.chkContinue.checked
}

function findN(frameno){
 if(frameno==0)return 1
 for(var i=1;i<Curve.length;i++){
  if(frameno<Curve[i][0])return i-1
 }
}

function moveDot(frameno){
 var n=findN(frameno)
 var f=(frameno-Curve[n][0])/(Curve[n+1][0]-Curve[n][0])
 var s=frameno+" "+n+" "+f
 var x=f*(Curve[n+1][1]-Curve[n][1])+Curve[n][1]
     f=(1-Math.cos(f*3.1415926))/2
 var y=f*(Curve[n+1][2]-Curve[n][2])+Curve[n][2]
 divmove("dot",x+xoff,y+yoff)
}


