/* This plugin uses public LaTeX converters powered by MathTeX, 
 * which is written by John Forkosh ( http://www.forkosh.com/ ) 
 * and distributed under GPL
 *
 * */

CmdUtils.CreateCommand({
    name: "latex",
    icon: "http://ubiquity.skumleren.net/latex/latex.png",
    homepage: "http://ubiquity.skumleren.net/",
    author: { name: "Christian Sonne", email: "cers@geeksbynature.dk"},
    license: "GPL",
    description: "Replaces LaTeX equation with a rendered version of same",
    help: "If you're in an editable text area, replaces an equation with a rendered version of same",
    takes: {"equation": noun_arb_text},
    _getEqUrl: function(equation) {
        var params = {formdata: equation.text};
        var Url = new Array("http://www.forkosh.dreamhost.com/mathtex.cgi?",
                            "http://www.problem-solving.be/cgi-bin/mathtex.cgi?",
                            "http://www.cyberroadie.org/cgi-bin/mathtex.cgi?",
                            "http://ubiquity.skumleren.net/cgi-bin/mathtex.cgi?");
        return Url[Math.floor(Url.length*Math.random())] + jQuery.param( params );
    },
    _genEqHTML: function(equation) {
        var src = "<img title='" +equation.text+ "' alt='" +equation.text+ "' src='" + this._getEqUrl(equation) +"' style='vertical-align: middle;' />";
        return src;
    },
    preview: function( pblock, equation ) {
        var msg = "Inserts LaTeX equation instead of text: <br /><span style='background: white; padding: .5em; display: inline-block;'>";
        msg += this._genEqHTML(equation);
        msg += "</span><br />"
        pblock.innerHTML = msg;
    },
    execute: function(equation) {
        CmdUtils.setSelection(this._genEqHTML(equation));
    }
});
