/*
 * vimperator-open-google-docs-viewer.js
 * vimperator-open-google-docs-viewer.js is Firefox extension Vimperator Plugin.
 *
 * file created in 2009/10/14 20:01:53.
 * LastUpdated :2009/10/14 20:33:04.
 * author iNo (http://www.serendip.ws/)
 *
 ************ USAGE **************
 *
 * :gdv
 * :googleDocsViewer
 *
 ************ COPYRIGHT **************
 *
 * Copyright (C) 2009 iNo http://www.serendip.ws/
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

liberator.modules.commands.addUserCommand(["gdv", "googleDocsViewer"], "Open PDF file with Google Docs Viewer",
    function(args) {
        if (!liberator.modules.buffer.URL || liberator.modules.buffer.URL == 'about:blank'){
            return false;
        }

        var growlNotification = function(count) {
            var alertsService = Components.classes["@mozilla.org/alerts-service;1"]
                                    .getService(Components.interfaces.nsIAlertsService);
            alertsService.showAlertNotification("chrome://global/skin/icons/information-32.png", "Google Docs Viewer links count", count);
        };

        var url = liberator.modules.buffer.URL;
        var doc = window.content.document;
        (function() {
            if (url.indexOf('http://docs.google.com/') == -1) {
                var counter = 0;
                var exts = ['pdf', 'ppt', 'tif'];
                var xpath = (function() {
                    var x = '//a[';
                    for (var i=0; i<exts.length; i++) {
                        x += i == 0 ? '' : ' or ';
                        x += 'substring(@href, string-length(@href) - string-length(".' + exts[i] + '") + 1) = ".' + exts[i] + '"';
                    }
                    x += ']';
                    return x;
                })();
                var elems = doc.evaluate(xpath,
                                doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

                for (var i=0,len=elems.snapshotLength; i<len; i++) {
                    var elem = elems.snapshotItem(i);
                    if (elem.href.indexOf('http://docs.google.com/viewer?url=') == -1) {
                        var icon = doc.createElement('IMG');
                        var anc = doc.createElement('A');
                        icon.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTM5jWRgMAAAFHSURBVDiNlZOxTgJBEIa/3Ry8gTyD76C9FQbi9cTCRGw1sTQ2FkZKDkKxyRWUNhb2voU9zXFAJwW7h2Mhd+DdhcM/mWQ3O/PvN7NZxUZhGAZAl/0adDqdm9ITY4ys1+u9YYwRY0ywW6fTRZIkAMxmM+I4Jo5jptMpURQRRREAzWaTJEm6o9EoKBg45wBQSv0JrTVKKQCstbTbbZxz3X6/HwCo4ce3VPQNwNWpMJlMqNVq1Ot1xuMxwEA9vR9mcH9WTOv1enjzJbxc7C++e4X5fI7Ir4mI0Gg0sNbiLb7gMqwmSGciItlMVqsV3mIJb+UvC8B5sDXIa0OgOHmuJtBaZy2kygg+H8vnePywvTUlSNvYIYCj2yJeXmUtZATfw3ICfV0k2JVzDi+f+B8Ca+124/u+iEjlh0pDRMT3fclsW63WId85r8EPLeTd8KdMB6AAAAAASUVORK5CYII=';
                        anc.insertBefore(icon, anc.firstChild);
                        anc.href = 'http://docs.google.com/viewer?url=' + encodeURIComponent(elem.href);
                        anc.target = '_blank';
                        elem.parentNode.insertBefore(anc, elem.nextSibling);
                        counter++;
                    }
                }
                growlNotification(counter);
            }
        })();

    }, {
        argCount: "?",
        hereDoc: false,
        bang: false,
        count: false,
        literal: false
    }
);

/*
vim:fdl=0 fdm=marker:ts=4 sw=4 sts=0:
*/

