/*
 * Copyright (C) 2009-2010 Eran Hodis, Jaime Prilusky
 * Proteopedia functions
 *
 * The goal is to define functions unique to Proteopedia here,
 * also, any changes to functions in Jmol.js or MediaWiki:Common.js can be performed by
 * overriding the functions here.
 *
 */ 
 
/* Jaime Prilusky 2008, added this here, removed from JmolMediaWiki.js */
function newAppletWindow(id) {
  var sm = ""  +Math.random(); 
  sm = sm.substring(2,10);
  var url  = "/wiki/extensions/Jmol/JmolPopup.html";
  if (id != undefined) { url = url + "?id=" + id + "&";}
  var name = "jmol_" + sm;
  var options = "menubar=yes,resizable=1,scrollbars,alwaysRaised,width=600,height=600,left=50";
  var newwindow = window.open(url,name,options);
  if (window.focus) {newwindow.focus()}
  return false;
}

/* Eran Hodis Feb 2009, added this here, removed from JmolMediaWiki.js */
/* function for Consurf chain buttons in Structure Box */
function ConsurfChainButton(elementClicked) {
	// get the suffix of the Jmol applet on which we want to act by checking the target suffix added onto a child Jmol button's id
	//var targetSuffix = String(elementClicked);
	var strStart = elementClicked.lastIndexOf("_");
	var targetSuffix = elementClicked.slice(strStart + 1);
	var chainLetter = elementClicked.slice(0,strStart);
	strStart = chainLetter.lastIndexOf("_");
	chainLetter = chainLetter.slice(strStart + 1);
	var PDBid = elementClicked.slice(0,strStart);
	strStart = PDBid.lastIndexOf("_");
	PDBid = PDBid.slice(strStart + 1);
	var displayOn  = chainLetter + ' [x]';
	var displayOff = chainLetter + ' [  ]';
	if (document.getElementById(elementClicked).value==displayOn) {
		document.getElementById(elementClicked).value=displayOff;
	} else {
		document.getElementById(elementClicked).value=displayOn;
	}
	
	// if loadedfileprev is not yet defined, define it
	jmolScript('script "/wiki/scripts_internal/set_loadedfileprev.spt"',targetSuffix);
	
	// determine the hash of the PDB file we want to display
	var PDBhash = evaluateURLsynchronous("/cgi-bin/getlateststructure?"+PDBid+"&info");
	PDBhash = PDBhash.split('\t');
	PDBhash = PDBhash[0];
	
	var jmolScriptLoadPDB = "if (loadedfileprev != \""+PDBhash+"\"); set refreshing false; load /cgi-bin/getlateststructure?"+PDBid+"; loadedfileprev = \""+PDBhash+"\"; reloadfile=true; endif;";
	var jmolScriptColorConsurf = "select protein; define ~consurf_to_do selected; consurf_initial_scene = true; script \"/wiki/ConSurf/" + PDBid.slice(1,3) + "/" + PDBid + "_consurf.spt\"; set refreshing true;";
	
	// make sure the right PDB file is loaded
	jmolScript(jmolScriptLoadPDB,targetSuffix);
	// make sure the right coloring and representations are used
	jmolScript(jmolScriptColorConsurf,targetSuffix);
	
	// set the chains to match their displayed status on their Consurf buttons
	var Buttons = document.getElementById(elementClicked).parentNode.parentNode.parentNode.getElementsByTagName("input");
	for (var i = 0; i < Buttons.length; i++) {
		strStart = Buttons[i].value.lastIndexOf(" [");
		chainLetter = Buttons[i].value.slice(0,strStart);
		if (Buttons[i].value.lastIndexOf("[x]") == -1) {
			jmolScriptSetChain = "select :"+chainLetter+" and protein; spacefill off;color translucent -1; consurf_"+chainLetter+" = 1;"
			jmolScript(jmolScriptSetChain,targetSuffix);
		} else {
			jmolScriptSetChain = "select :"+chainLetter+" and protein; spacefill on; color opaque;consurf_"+chainLetter+" = 0;";
			jmolScript(jmolScriptSetChain,targetSuffix);
		}
	}
}


// Eran Hodis, added this here 2010, removed from authorTabs.js & Jmol.js
function getHTTPObject() 
{
	var http = false;
	//Use IEs ActiveX items to load the file.
	if(typeof ActiveXObject != "undefined") {
		try {http = new ActiveXObject("Msxml2.XMLHTTP");}
		catch (e) {
			try {http = new ActiveXObject("Microsoft.XMLHTTP");}
			catch (E) {http = false;}
		}
	//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
	} else if (XMLHttpRequest) {
		try {http = new XMLHttpRequest();}
		catch (e) {http = false;}
	}
	return http;
}

// added by EH 2010, added this here, removed from Jmol.js
function changeColorKeyDiv(html,jmolAppletSuffix) {
	var colorKeyDivID = "colorKeyDiv";
	if (jmolAppletSuffix) {
		colorKeyDivID += "_" + jmolAppletSuffix; 
	}
	document.getElementById(colorKeyDivID).innerHTML = html;
}



// EH 2010, added this here, removed it from Jmol.js
function evaluateURLsynchronous(url) {
	url = String(url);
	
	params = "";
	
	var http = getHTTPObject();
	
	http.open("GET",url,false); // false = synchronous, true = asynchronous
	
	//send the proper header information along with the request
	http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length",params.length);
	http.setRequestHeader("Connection","close");
	
	//http.onreadystatechange = function () {
	//	if(http.readyState == 4 && http.status == 200) {
	//		x = http.responseText;
	//		x = String(x);
	//	}
	//}
	
	http.send(params);
	
	// waits for previous line to finish before executing following line
	return http.responseText;
	
}

// EH 2010, added this here, overrides version in MediaWiki:Common.js
function collapseTable( tableIndex )
 {
     var Button = document.getElementById( "collapseButton" + tableIndex );
     var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
     if ( !Table || !Button ) {
         return false;
     }
 
     var Rows = Table.getElementsByTagName( "tr" );
 
     if ( Button.firstChild.data == collapseCaption ) {
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = "none";
         }
         Button.firstChild.data = expandCaption;
     } else {
         if ( hasClass( Table, "ConSurf")) {   // this if clause and its contents added by EH 2008/2009
              var Buttons = Table.getElementsByTagName( "input" ) ;
              for (var i = 0; i < Buttons.length; i++) {
                   Buttons[i].value = Buttons[i].value.replace("[  ]","[x]");
                   strStart = Buttons[i].value.lastIndexOf(" [");
                   chainLetter = Buttons[i].value.slice(0,strStart);
                   jmolScriptSetConsurfChainVar = "consurf_"+chainLetter+" = 0;";
                   jmolScript(jmolScriptSetConsurfChainVar,targetSuffix);
              }
              // get the suffix of the Jmol applet on which we want to act by checking the target suffix added onto a child Jmol button's id
              var targetSuffix = String(Buttons[0].id);
              strStart = targetSuffix.lastIndexOf("_");
              targetSuffix = targetSuffix.slice(strStart + 1);

              //now figure out the currently loaded file in the Jmol applet
              fromJmol = jmolGetPropertyAsArray("fileName","all",targetSuffix);
			  var currFile = fromJmol;
			  currFile = String(currFile);
			  strStart = currFile.lastIndexOf("\/cgi-bin");
			  currFile = currFile.slice(strStart);
              currFile = currFile + "&info";
              currFile = evaluateURLsynchronous(currFile);
              currFile = currFile.split('\t');
              currFile = String(currFile[3]);
			  currFile = currFile.split('\.');
              currFile = currFile[0];

              //now color it in consurf colors
              jmolScript("select protein; define ~consurf_to_do selected; consurf_initial_scene = true; script \"/wiki/ConSurf/" + currFile.slice(1,3) + "/" + currFile + "_consurf.spt\"",targetSuffix);
         }
         for ( var i = 1; i < Rows.length; i++ ) {
             Rows[i].style.display = Rows[0].style.display;
         }
         Button.firstChild.data = collapseCaption;
     }
 }

