/*///////////////////////////////////////////////////////////////////////// 
 * jsTeXrender - An JavaScript LaTeX render 
 * Copyright (C) 2008 Waipot Ngamsaad 

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

//File: jstexrender.js v0.4
//Date: 10/14/2008
//Author: Waipot Ngamsaad <waipot@ngamsaad.name>
//Website: http://www.waipot.ngamsaad.name

//*** Don't change this line, if you don't know what it exactly is ***
//LatexRender server URL, without trail slash (/)
//var latexrender_server = "http://www.yourequations.com/eq.latex"; 
//var latexrender_server = "http://latex.codecogs.com/gif.latex";
var latexrender_server = "http://www.yourequations.com/cgi-bin/mimetex.cgi";

function makeLink(latex_code) {
	var str = latex_code;
	//Clean code
	str.replace(/<br>/gi,"");
	str.replace(/<br \/>/gi,"");
	//Create img tag
	//latex_img = "<img src=\""+ latexrender_server +"?"+ str +"\" alt=\""+ str +"\" class=\"eq_latex\" align=\"middle\" border=\"0\" />";	
	latex_img = "<img src=\""+ latexrender_server +"?"+ str +"\" title=\""+ str +"\" alt=\""+ str +"\" class=\"eq_latex\" align=\"middle\" border=\"0\" />";	
	/*latex_img = "<a href=\"javascript:void(0)\"><img src=\""+ latexrender_server +"?"+ str +"\" title=\""+ str +"\" alt=\""+ str +"\" class=\"eq_latex\" align=\"middle\" border=\"0\" onclick=\"newWindow=window.open('http://www.yourequations.com/latexcode.php?code="+ escape(str) +"','latexCode','toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,width=375,height=200,left=200,top=100');\" /></a>";*/

	return latex_img;
}

function renderTeX(tag) {
	var eqn = window.document.getElementsByTagName(tag);
	for (var i=0; i<eqn.length; i++) {
		if (eqn[i].getAttribute("lang") == "eq.latex") { 
			//Show Equation image, easy isn't it?
			if ( !eqn[i].innerHTML.match(/<img.*?>/i) )
				eqn[i].innerHTML = makeLink(eqn[i].innerHTML);
		} /*else if (eqn[i].getAttribute("lang") == "eq.latex.src") {
			//Just show only LaTeX code
			//eqn[i].innerHTML = "&lt;pre lang=\"eq.latex\"&gt;\n " +eqn[i].innerHTML+ "&lt;/pre&gt;";
		}*/
	}
}

//Run
renderTeX("pre");
renderTeX("code");

