/*
 * Javascript functions that manage
 * communication with AstexViewer.
 */

function toggle(s){
  document.av.execute('molecule display ' + s + ' toggle;');
}


function av_execute(command){
  document.av.execute(command);
  window.status = command;
}

function av_lipophilicity(obj){
  av_execute("run 'lipophilicity.properties';");
  av_execute("object '" + obj +"' texture lipophilicity u 7.0 aminoacid;");
}

function av_load_protein(obj){
  var command = "molecule load protein '" + obj + "_a.pdb';";
  av_execute(command);
}
function av_load_ligand(obj){
  var command = "molecule load ligand '" + obj + ".mol';";
  av_execute(command);
}
function av_load_cofactor(obj){
  var command = "molecule load cofactor '" + obj + "_c.pdb';";
  av_execute(command);
}
function av_load_water(obj){
  var command = "molecule load water '" + obj + "_d.pdb';";
  av_execute(command);
}

function av_texture(obj, tex){
  if(tex == 'off'){
    av_execute("object '" + obj + "' texture 'off';");
  }else{
    if(tex == 'simple'){
      av_execute("texture '" + tex + "' simple;");
    }else{
      av_execute("texture load '" + tex + "' '" + tex + "';");
    }
    av_execute("object '" + obj + "' texture '" + tex + "';");
  }
}

function av_transparency(obj, tr){
  av_execute("object '" + obj + "' transparency " + tr + ";");
}

function av_display(obj, tr){
  var command = "object display '" + obj + "' " + tr + ";";
  av_execute(command);
}

function av_colour(obj, tr){
  var command = "object '" + obj + "' color " + tr + ";";
  av_execute(command);
}

function av_background(tr){
  var command = "background '" + tr + "';";
  av_execute(command);
}

function av_surface_size3(tr){
  var command = "surface -solid true protein_surface white molecule protein and sphere " + tr + "  around molecule ligand*;";
  av_execute(command);
}
function av_surface_size2(tr){
  var command = "surface -solid true protein_surface white molecule protein and sphere " + tr + "  around molecule ligand or molecule cofactor;";
  av_execute(command);
}
function av_surface_size(obj, tr){
  var command = "surface -solid true protein_surface white molecule protein and sphere " + tr + "  around molecule '" + obj + "' ;";
  av_execute(command);
}
function av_mesh_size3(tr){
  var command = "surface -solid false protein_mesh white molecule protein and sphere " + tr + "  around molecule ligand*;";
  av_execute(command);
}
function av_mesh_size2(tr){
  var command = "surface -solid false protein_mesh white molecule protein and sphere " + tr + "  around molecule ligand or molecule cofactor;";
  av_execute(command);
}
function av_mesh_size(obj, tr){
  var command = "surface -solid false protein_mesh white molecule protein and sphere " + tr + "  around molecule '" + obj + "' ;";
  av_execute(command);
}



/*
 * Javascript functions that manage interaction with
 * controls on the page.
 */



/* Texture of surfaces */



function js_texture_protein(sel){
  var val = sel.options[sel.selectedIndex].value;
  if(!sel.textured){
    av_lipophilicity('protein_surface');
    sel.textured = true;
  }
  av_texture('protein_surface', val);
}
function js_texture_ligand(sel){
  var val = sel.options[sel.selectedIndex].value;
  if(!sel.textured){
    av_lipophilicity('ligand_surface');
    sel.textured = true;
  }
  av_texture('ligand_surface', val);
}
function js_texture_ligand2(sel){
  var val = sel.options[sel.selectedIndex].value;
  if(!sel.textured){
    av_lipophilicity('ligand2_surface');
    sel.textured = true;
  }
  av_texture('ligand2_surface', val);
}




/* Transparancy of surfaces */




function js_transparency_protein(sel){
  var val = sel.options[sel.selectedIndex].value;
  val = Math.round(val * 2.55);
  av_transparency('protein_surface', val);
}
function js_transparency_cartoon(sel){
  var val = sel.options[sel.selectedIndex].value;
  val = Math.round(val * 2.55);
  av_transparency('protein_schematic', val);
}
function js_transparency_cofactor(sel){
  var val = sel.options[sel.selectedIndex].value;
  val = Math.round(val * 2.55);
  av_transparency('cofactor_surface', val);
}
function js_transparency_ligand(sel){
  var val = sel.options[sel.selectedIndex].value;
  val = Math.round(val * 2.55);
  av_transparency('ligand_surface', val);
}
function js_transparency_ligand2(sel){
  var val = sel.options[sel.selectedIndex].value;
  val = Math.round(val * 2.55);
  av_transparency('ligand2_surface', val);
}
function js_transparency_water(sel){
  var val = sel.options[sel.selectedIndex].value;
  val = Math.round(val * 2.55);
  av_transparency('water_surface', val);
}





/* Make surfaces */


function js_protein_surface_size(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='1'){
    av_execute('surface -solid true protein_surface white molecule protein;');
    }else if(val>=5 && val<=15){
    av_surface_size('ligand', val);
    }else if(val>=50 && val<=150){
    val = val * 0.1;
    av_surface_size('cofactor', val);
    }else{
    val = val * 0.01;
    av_surface_size2(val);
    }
}

function js_protein_surface(obj){
  if(obj.checked){
    av_execute('surface -solid true protein_surface white molecule protein and sphere 7 around molecule ligand* or molecule cofactor;');
    av_execute('object protein_surface texture distance v molecule ligand* or molecule cofactor;');
    av_execute('object protein_surface texture vrange 0 4.5;');
    av_execute('object protein_surface clip v;');
    av_execute('object protein_surface backface on;');
    obj.surface = true;
  }else{
    av_display('protein_surface', 'off');
  }
}

function js_protein_mesh_size(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='1'){
    av_execute('surface -solid false protein_surface white molecule protein;');
    }else if(val>=5 && val<=15){
    av_mesh_size('ligand', val);
    }else if(val>=50 && val<=150){
    val = val * 0.1;
    av_mesh_size('cofactor', val);
    }else{
    val = val * 0.01;
    av_mesh_size2(val);
    }
}
function js_protein_mesh(obj){
  if(obj.checked){
    av_execute('surface -solid false protein_mesh white molecule protein;');
    obj.surface = true;
  }else{
    av_display('protein_mesh', 'off');
  }
}
function js_cofactor_surface(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid true cofactor_surface white molecule cofactor;');
      obj.surface = true;
    }else{
      av_display('cofactor_surface', 'on');
    }
  }else{
    av_display('cofactor_surface', 'off');
  }
}
function js_cofactor_mesh(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid false cofactor_mesh white molecule cofactor;');
      obj.surface = true;
    }else{
      av_display('cofactor_mesh', 'on');
    }
  }else{
    av_display('cofactor_mesh', 'off');
  }
}
function js_ligand_surface(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid true ligand_surface white molecule ligand;');
      obj.surface = true;
    }else{
      av_display('ligand_surface', 'on');
    }
  }else{
    av_display('ligand_surface', 'off');
  }
}
function js_ligand_mesh(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid false ligand_mesh white molecule ligand;');
      obj.surface = true;
    }else{
      av_display('ligand_mesh', 'on');
    }
  }else{
    av_display('ligand_mesh', 'off');
  }
}
function js_ligand2_surface(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid true ligand2_surface white molecule ligand2;');
      obj.surface = true;
    }else{
      av_display('ligand2_surface', 'on');
    }
  }else{
    av_display('ligand2_surface', 'off');
  }
}
function js_ligand2_mesh(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid false ligand2_mesh white molecule ligand2;');
      obj.surface = true;
    }else{
      av_display('ligand2_mesh', 'on');
    }
  }else{
    av_display('ligand2_mesh', 'off');
  }
}
function js_water_surface(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid true water_surface white molecule water;');
      obj.surface = true;
    }else{
      av_display('water_surface', 'on');
    }
  }else{
    av_display('water_surface', 'off');
  }
}
function js_water_mesh(obj){
    if(obj.checked){
    if(!obj.surface){
      av_execute('surface -solid false water_mesh white molecule water;');
      obj.surface = true;
    }else{
      av_display('water_mesh', 'on');
    }
  }else{
    av_display('water_mesh', 'off');
  }
}






/* Cartoon representation */




function js_schematic_protein(obj){
  if(obj.checked){
    if(!obj.schematic){
      av_execute('color_by_rainbow molecule protein; secstruc all; schematic -name protein_schematic molecule protein; color_by_atom;');
      obj.schematic = true;
    }else{
      av_display('protein_schematic', 'on');
    }
  }else{
    av_display('protein_schematic', 'off');
  }
}
function js_texture_cartoon(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='rainbow'){
      av_execute('color_by_rainbow molecule protein; secstruc all; schematic -name protein_schematic molecule protein; color_by_atom;');
      obj.schematic = true;
    }else if(val=='rainbow2'){
      av_execute('color_by_rainbow molecule protein; secstruc all; schematic -name protein_schematic -alltube true molecule protein; color_by_atom;');
        obj.schematic = true;
    }else if(val=='sstrue'){
        av_execute('schematic -name protein_schematic -colorbyss true molecule protein;');
        obj.schematic = true;
    }else{
      av_execute('schematic -name protein_schematic -colorbyss true -alltube true molecule protein;');
      obj.schematic = true;
    }
}




/* Atom displays */





function js_atomcolour(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_execute(val);
}

function js_atoms(obj){
  if(obj.checked){
    av_execute('display lines on not solvent;');
  }else{
    av_execute('display lines off not solvent;');
  }
}

function js_solvent(obj){
  if(obj.checked){
    av_execute('display lines on solvent;');
  }else{
    av_execute('display lines off solvent;');
  }
}


function js_spheres(obj){
  if(obj.checked){
    av_execute('display spheres aminoacid;');
  }else{
    av_execute('display spheres none;');
  }
}

function js_colour_protein_atoms(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='atom_green'){
      av_execute('select molecule protein;');
      av_execute('color_by_atom;');
      av_execute('select none;');
    }else if(val=='atom_grey'){
      av_execute('select molecule protein;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0xa9a9a9 atom C* and molecule protein;');
    }else if(val=='atom_black'){
      av_execute('select molecule protein;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0x000000 atom C* and molecule protein;');
    }else if(val=='atom_white'){
      av_execute('select molecule protein;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color white atom C* and molecule protein;');
    }else{
      av_execute('color '+ val + ' molecule protein;');
    }
}
function js_colour_ligand_atoms(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='atom_green'){
      av_execute('select molecule ligand;');
      av_execute('color_by_atom;');
      av_execute('select none;');
    }else if(val=='atom_grey'){
      av_execute('select molecule ligand;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0xa9a9a9 atom C* and molecule ligand;');
    }else if(val=='atom_black'){
      av_execute('select molecule ligand;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0x000000 atom C* and molecule ligand;');
    }else if(val=='atom_white'){
      av_execute('select molecule ligand;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color white atom C* and molecule ligand;');
    }else{
      av_execute('color '+ val + ' molecule ligand;');
    }
}
function js_colour_ligand2_atoms(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='atom_green'){
      av_execute('select molecule ligand2;');
      av_execute('color_by_atom;');
      av_execute('select none;');
    }else if(val=='atom_grey'){
      av_execute('select molecule ligand2;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0xa9a9a9 atom C* and molecule ligand2;');
    }else if(val=='atom_black'){
      av_execute('select molecule ligand2;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0x000000 atom C* and molecule ligand2;');
    }else if(val=='atom_white'){
      av_execute('select molecule ligand2;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color white atom C* and molecule ligand2;');
    }else{
      av_execute('color '+ val + ' molecule ligand2;');
    }
}
function js_colour_cofactor_atoms(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='atom_green'){
      av_execute('select molecule cofactor;');
      av_execute('color_by_atom;');
      av_execute('select none;');
    }else if(val=='atom_grey'){
      av_execute('select molecule cofactor;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0xa9a9a9 atom C* and molecule cofactor;');
    }else if(val=='atom_black'){
      av_execute('select molecule cofactor;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color 0x000000 atom C* and molecule cofactor;');
    }else if(val=='atom_white'){
      av_execute('select molecule cofactor;');
      av_execute('color_by_atom;');
      av_execute('select none;');
      av_execute('color white atom C* and molecule cofactor;');
    }else{
            av_execute('color '+ val + ' molecule cofactor;');
    }
}
function js_colour_water_atoms(sel){
  var val = sel.options[sel.selectedIndex].value;
            av_execute('color '+ val + ' molecule water;');
}

function js_atomstyle_protein(sel){
  var val = sel.options[sel.selectedIndex].value;
    av_execute('display lines on molecule protein;');
    av_execute('display sticks off molecule protein;');
    av_execute('display cylinders off molecule protein;');
    av_execute('display spheres off molecule protein;');
    if(val=='1'){
      av_execute('bond_width '+ val + ' molecule protein;');
    }else if(val=='2'){
      av_execute('bond_width '+ val + ' molecule protein;');
    }else if(val=='3'){
      av_execute('bond_width '+ val + ' molecule protein;');
    }else{
      av_execute('display '+ val + ' molecule protein;');
    }
}
function js_atomstyle_ligand(sel){
  var val = sel.options[sel.selectedIndex].value;
    av_execute('display lines on molecule ligand*;');
    av_execute('display sticks off molecule ligand*;');
    av_execute('display cylinders off molecule ligand*;');
    av_execute('display spheres off molecule ligand*;');
    if(val=='1'){
      av_execute('bond_width '+ val + ' molecule ligand*;');
    }else if(val=='2'){
      av_execute('bond_width '+ val + ' molecule ligand*;');
    }else if(val=='3'){
      av_execute('bond_width '+ val + ' molecule ligand*;');
    }else{
      av_execute('display '+ val + ' molecule ligand*;');
    }
}
function js_atomstyle_cofactor(sel){
  var val = sel.options[sel.selectedIndex].value;
    av_execute('display lines on molecule cofactor;');
    av_execute('display sticks off molecule cofactor;');
    av_execute('display cylinders off molecule cofactor;');
    av_execute('display spheres off molecule cofactor;');
    if(val=='1'){
      av_execute('bond_width '+ val + ' molecule cofactor;');
    }else if(val=='2'){
      av_execute('bond_width '+ val + ' molecule cofactor;');
    }else if(val=='3'){
      av_execute('bond_width '+ val + ' molecule cofactor;');
    }else{
      av_execute('display '+ val + ' molecule cofactor;');
    }
}
function js_atomstyle_water(sel){
  var val = sel.options[sel.selectedIndex].value;
    av_execute('display lines on molecule water;');
    av_execute('display sticks off molecule water;');
    av_execute('display cylinders off molecule water;');
    av_execute('display spheres off molecule water;');
    if(val=='1'){
      av_execute('bond_width '+ val + ' molecule water;');
    }else if(val=='2'){
      av_execute('bond_width '+ val + ' molecule water;');
    }else if(val=='3'){
      av_execute('bond_width '+ val + ' molecule water;');
    }else{
      av_execute('display '+ val + ' molecule water;');
    }
}





/* Colour surfaces */



function js_colour_protein(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('protein_surface', val);
}
function js_colour_protein_mesh(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('protein_mesh', val);
}
function js_colour_cartoon(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('protein_schematic', val);
}
function js_colour_cofactor(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('cofactor_surface', val);
}
function js_colour_cofactor_mesh(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('cofactor_mesh', val);
}
function js_colour_ligand(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('ligand_surface', val);
}
function js_colour_ligand_mesh(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('ligand_mesh', val);
}
function js_colour_ligand2(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('ligand2_surface', val);
}
function js_colour_ligand2_mesh(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('ligand2_mesh', val);
}
function js_colour_water(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('water_surface', val);
}
function js_colour_water_mesh(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_colour('water_mesh', val);
}






/* Display Hydrogen Bonds between poses and protein (and solvent)*/




function js_HB_ligand(sel){
  if(sel.checked){
    av_execute("distance -name hbonds_ligand_protein -mode pairs -from { element 7 8 and molecule ligand or (solvent and sphere 4 around molecule ligand) } -to { element 7 8 and (molecule protein or molecule cofactor or solvent) } -contact 0.3 -colour white -on 0.3 -off 0.4 -radius 0.04 -format '<justify=vh,points=16>%.1f' ;");
  }else{
    av_execute("distance -delete hbonds_ligand_protein ;");
  }
}
function js_HB_ligand2(sel){
  if(sel.checked){
    av_execute("distance -name hbonds_ligand2_protein -mode pairs -from { element 7 8 and molecule ligand2 or (solvent and sphere 4 around molecule ligand2) } -to { element 7 8 and (molecule protein or molecule cofactor or solvent) } -contact 0.3 -colour white -on 0.3 -off 0.4 -radius 0.04 -format '<justify=vh,points=16>%.1f' ;");
  }else{
    av_execute("distance -delete hbonds_ligand2_protein ;");
  }
}
function js_HB_cofactor(sel){
  if(sel.checked){
    av_execute("distance -name hbonds_cofactor -mode pairs -from { element 7 8 and molecule cofactor or (solvent and sphere 4 around molecule cofactor) } -to { element 7 8 and (molecule protein or molecule ligand or solvent) } -contact 0.3 -colour white -on 0.3 -off 0.4 -radius 0.04 -format '<justify=vh,points=16>%.1f' ;");
  }else{
    av_execute("distance -delete hbonds_cofactor;");
  }
}
function js_HB_water(sel){
  if(sel.checked){
    av_execute("distance -name hbonds_water -mode pairs -from { element 7 8 and molecule water } -to { element 7 8 and (molecule protein or molecule ligand or molecule cofactor) } -contact 0.3 -colour white -on 0.3 -off 0.4 -radius 0.04 -format '<justify=vh,points=16>%.1f' ;");
  }else{
    av_execute("distance -delete hbonds_water;");
  }
}




/* Display contacts between poses and protein */




function js_contacts_ligand(sel){
  if(sel.checked){
    av.execute("select atom CA and byresidue sphere 4 around molecule ligand;");
    av_execute("label '<color=white,justify=vh,points=18>%f%r' current;");
    av.execute("select none;");
  }else{
    av_execute("label clear atom CA;");
  }
}
function js_contacts_ligand2(sel){
  if(sel.checked){
    av.execute("select atom CA and byresidue sphere 4 around molecule ligand2;");
    av_execute("label '<color=white,justify=vh,points=18>%f%r' current;");
    av.execute("select none;");
  }else{
    av_execute("label clear atom CA;");
  }
}
function js_contacts_cofactor(sel){
  if(sel.checked){
    av.execute("select atom CA and byresidue sphere 4 around molecule cofactor;");
    av_execute("label '<color=white,justify=vh,points=18>%f%r' current;");
    av.execute("select none;");
  }else{
    av_execute("label clear atom CA;");
  }
}
function js_contacts_water(sel){
  if(sel.checked){
    av.execute("select atom CA and byresidue sphere 4 around molecule water;");
    av_execute("label '<color=white,justify=vh,points=18>%f%r' current;");
    av.execute("select none;");
  }else{
    av_execute("label clear atom CA;");
  }
}



/* Viewer controls */



function js_antialias(sel){
  if(sel.checked){
    av_execute("view -antialias true;");
  }else{
    av_execute("view -antialias false;");
  }
}

function js_shadows(sel){
  if(sel.checked){
    av_execute("view -realspheres true; view -shadows true;");
  }else{
    av_execute("view -realspheres false; view -shadows false;");
  }
}

function js_background(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_execute("view -gradient false;");
  av_background(val);
}
function js_background2(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_execute('view -gradient true -gradientbottom black -gradienttop '+ val+';');
}





/* I/O Functions */



function js_load1(sel){
  var val = sel.options[sel.selectedIndex].value;
  av_execute("molecule remove *;");
  av_load_ligand(val);
  av_execute("display cylinders on molecule ligand;");
  av_execute("display cylinders on molecule cofactor;");
  av_execute("bond_width 3 molecule protein;");
  av_execute("select molecule ligand or molecule cofactor;");
  av_execute("center current;");
  av_execute("select none;");
}

function js_center(sel){
  var val = sel.options[sel.selectedIndex].value;
    if(val=='ligand'){
    av_execute("select molecule ligand;");
    }else if(val=='cofactor'){
    av_execute("select molecule cofactor;");
    }else if(val=='ligandcofactor'){
    av_execute("select molecule ligand or molecule cofactor;");
    }else{
    av_execute("select molecule *;");
    }
  av_execute("center current;");
  av_execute("select none;");
}


function js_writefile(sel){
  if(sel.checked){
    av_execute("write -molecule protein -file 'molecule_protein.pdb';");
    av_execute("write -molecule protein -file molecule_protein.pdb;");
    av_execute("write -molecule protein -file c:\molecule_protein.pdb;");
    av_execute("write -molecule protein -file 'c:\molecule_protein.pdb';");
  }else{
    av_execute(";");
  }
}


function js_writeimage(sel){
  if(sel.checked){
    av_execute("view -writeimage -width 500 -height 400 -sample 2 screen.bmp;");
    av_execute("view -writeimage -width 500 -height 400 -sample 2 'screen.bmp';");
    av_execute("view -writeimage -width 500 -height 400 -sample 2 c:\screen.bmp;");
    av_execute("view -writeimage -width 500 -height 400 -sample 2 'c:\screen.bmp';");
  }else{
    av_execute(";");
  }
}
