/* 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));
    }
});



/* This script uses: 
 * MochiKit - http://mochikit.com/
 * PlotKit - http://www.liquidx.net/plotkit/
 * though note, the plotkit used is modified in many places,
 * as the original did not properly adress many of the MochiKit
 * functions it used. This may not usually be a problem, but
 * they were not externalized to the same extent when used
 * inside of ubiquity - or that is at least the conclusion I drew.
 *
 * */
/* temporarily disabled, throws exceptions in latest trunk
_plot = new Object();
jQuery.get("http://ubiquity.skumleren.net/plot/MochiKit-1.3.1/packed/MochiKit/MochiKit.js", 
           null, 
           function(r,t,x){_plot.MochiKit=r;});
jQuery.get("http://ubiquity.skumleren.net/plot/plotkit-0.9.1/PlotKit/excanvas.js", 
           null, 
           function(r,t,x){_plot.excanvas=r;});
jQuery.get("http://ubiquity.skumleren.net/plot/plotkit-0.9.1/PlotKit/PlotKit_Packed.js", 
           null, 
           function(r,t,x){_plot.PlotKit=r;});


CmdUtils.CreateCommand({
    name: "plot",
    icon: "http://ubiquity.skumleren.net/plot/plot.png",
    homepage: "http://ubiquity.skumleren.net/",
    author: { name: "Christian Sonne", email: "cers@geeksbynature.dk"},
    license: "GPL",
    description: "Generates a plot from selected data",
    help: "Generates a plot from selected data",
    takes: {"data": noun_arb_text},
    _plot: null, // temporary plot holder
    _plotParser: function(data, limit) {
        var no       = 0;
        var maybe    = 1;
        var probably = 2;
        var yes      = 3;

        var html = data.html;
        var text = data.text;

        if (data.html.toLowerCase().indexOf("<table") != -1) {
            return this._plotParserTable(html);
        } else {
            return this._plotParserText(text);
        }
    },
    _plotParserTable: function(data) {
        return "table!";
    },
    _plotParserText: function(data) {
        return "text!";
    },
    preview: function(pblock,data) {
// Using a sandbox doesn't seem to work - document is not defined inside it, 
// which is needed by at least one of my libs
//
//        libs = Components.utils.Sandbox("chrome://ubiquity/content/browser.xul");
//        Components.utils.evalInSandbox(_plot.MochiKit,libs);
//        Components.utils.evalInSandbox(_plot.excanvas,libs);
//        Components.utils.evalInSandbox(_plot.PlotKit,libs);
        data = this._plotParser(data);
        var navigator = CmdUtils.getWindow().navigator;
        var document = window.document;
        pblock.document = document;
        eval(_plot.MochiKit);
        eval(_plot.excanvas);
        eval(_plot.PlotKit);
        try {
            pblock.innerHTML = "<p>"+data+"</p>";
            plot_div = MochiKit.DOM.DIV({"id":"plot_div", "style":"position: relative; float: left;"});
            pblock.appendChild(plot_div);

            var layout = new PlotKit.Layout("line");
            var simpleData = [[1,1],[2,4],[3,9],[4,16]];
            layout.addDataset("dataset", simpleData);
            layout.evaluate();
            var svg = PlotKit.SVGRenderer.SVG({"width": 500, "height": 300, "id":"chart"});
            plot_div.appendChild(svg);
            var chart = new PlotKit.SweetSVGRenderer(svg, layout);
            chart.render();
            var msg = "";
            pblock.appendChild(MochiKit.DOM.A(null,"Click here to add plot to document"));
        } catch (e) {
            if (e instanceof ReferenceError)
                pblock.innerHTML = "Cached preview doesn't work!<br />Please retype the command.";
            else throw e;
        }
    },
    execute: function(pblock, data) {
        CmdUtils.setSelection("<textarea>"+data.direct_object.html+"</textarea>");
//        CmdUtils.setSelection(this._plot);
    }
})*/ 



