var jmolwindow;
function openjmolwindow(url)
{
	jmolwindow=window.open(url,'PTL3jmol','height=550,width=700');
	if (window.focus) {jmolwindow.focus()}
}
var newWindow;
function makeNewJmolWindow(cif,solid,size,dummy) {
	if (!newWindow || newWindow.closed) {
		newWindow = window.open("","Jmolwindow","status,height=550,width=700");
		if (!newWindow.opener) {
		newWindow.opener = window;
		}
		// force small delay for IE to catch up
		setTimeout(writeToJmolWindow(cif,solid,size,dummy), 50);
	} else {
	// window's already open; bring to front
	newWindow.focus();
	}
}
function writeToJmolWindow(cif,solid,size,dummy) {
    // assemble content for new window
    var newContent =  '<HTML><HEAD>';
	newContent += '	<script type="text/javascript" src="../../../jmol/Jmol.js"> </script>';
	newContent += '	<script type="text/javascript" src="../../../ptl3_functions.js"> </script>';
	newContent += '	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">';
	newContent += '	<title>Crystal Lattices of Element</title>';
	newContent += '</HEAD>';
	newContent += '<BODY>';
	newContent += '<br> <form> <center> ';
// when theres a jmol working these html lines are not shown
	newContent += '<script type="text/javascript"> ptl3jmol("'+cif+'","'+solid+'","'+size+'","'+dummy+'"); </script>';
	newContent += '<br> </center> </form> ';
	newContent += '</body> </HTML>';
    // write HTML to new window document
    newWindow.document.write(newContent);
// If I uncomment this line Jmol freezes and it is not interactive
//    newWindow.document.close(); // close layout stream
}
function ptl3jmol(cif,solid,size,dummy){
        jmolInitialize("../../../jmol");
        jmolSetAppletColor("black");

	if ( dummy != "pdb" ){
		if ( solid == "molecular" ){
                	// this is the display for molecular solids (i.e. N, O) (range without normalizing the cell (444 666 0) will
                        	// show the molecules inside the solute)
	                jmolApplet(size, "load "+cif+" {444 666 0} range 10; set displayCellParameters off; move 0 360 0 0 0 0 0 0 5 ");
	        }
	        if ( solid == "metal" ){
                	// this is the display for metals and monoatomic solids (Fe, Ar) i.e. without bonds
	                jmolApplet(size, "load "+cif+" {3 3 3}; wireframe off; set displayCellParameters off; move 0 360 0 0 0 0 0 0 5 ");
	        }
	        if ( solid == "covalent" ){
                	// this is the display for covalent solids (carbon, boron) i.e. with bonds
	                jmolApplet(size, "load "+cif+" {3 3 3}; set displayCellParameters off; move 0 360 0 0 0 0 0 0 5 ");
	        }
       		jmolBr();                                                      
                                                                               
                jmolRadioGroup([
                ["display unitcell; center unitcell", "Display unit cell"],
                ["display *; centerAt AVERAGE ", "Display lattice","checked"],
                ]);
	}
	if ( dummy == "pdb" ){
	        jmolApplet(size, "load "+cif+"; move 0 360 0 0 0 0 0 0 5 ");
	}

        jmolBr();

        // Display size 
        jmolRadioGroup([
        ["spacefill off; wireframe on", "wireframe"],
        ["spacefill 20%; wireframe 0.15", "ball & stick","checked"],
        ["spacefill 100%; wireframe off", "Van der Waals"],
        ]);

        jmolBr();

        // a button to reset (not sure if we need it)
        jmolButton("reset", "Reset to original orientation");

        jmolBr();

        // a checkbox to spin it
        jmolCheckbox("spin on", "spin off", "spin");

        jmolBr();
        
	if ( dummy != "pdb" ){
        	// displays a box to display the Cell parameters
        	jmolCheckbox("set displayCellParameters on", "set displayCellParameters off", "Display cell parameters");
        	jmolBr();
	}

        // show 3 random distances
        jmolCheckbox("set measurements on ;set measurements angstroms; measure 1 2; measure 1 3; measure 1 4 ", "measurements delete", "Display distances in the cell");
        // I need to display the closest distance between two atoms or the coordination sphere

        jmolBr();
        jmolBr();

        jmolHtml("Double click on atoms to measure distances");

        jmolBr();
}
