var INFO =
<plugin name="findhere" version="0.0.1"
        href="http://www.slimeden.com/2011/04/firefox/findhere"
        summary="Find from the start of current screen"
        xmlns="http://vimperator.org/namespaces/liberator">
  <author email="wuxiaoshan@gmail.com">Xiao Shan</author>
  <license href="http://people.freebsd.org/~phk/">BEER-WARE</license>
  <project name="Vimperator" minVersion="2.3"/>
  <p>
    This plugin makes inner find from the start of current screen.
  </p>
  <item>
    <tags>'findhere'</tags>
    <spec>:findhere</spec>
    <description>
        <p>This plugin makes inner find from the start of current screen.</p>
    </description>
  </item>
</plugin>;

(function() {
    function moveCaret(elem) {
	    let doc = elem.ownerDocument;
	    let win = new XPCNativeWrapper(window.content.window);
	    let sel =  win.getSelection();
	    let r = doc.createRange();

	    sel.removeAllRanges();
	    r.selectNodeContents(elem);
	    sel.addRange(r);
		r.setEnd(r.startContainer, r.startOffset);

	    modes.push(modes.CARET);
	    modes.pop();
    }

    function findFirstTarget(){
        var win = content;
	    var doc = win.document;

        var offsets = { left: 0, right: 0, top: 0, bottom: 0 };
        offsets.right  = win.innerWidth  - offsets.right;
        offsets.bottom = win.innerHeight - offsets.bottom;

        function isVisible(elem) {
            let rect = elem.getBoundingClientRect();
            if (!rect || !rect.width || !rect.height ||
                rect.top < offsets.top || rect.bottom > offsets.bottom ||
                rect.left < offsets.left || rect.right > offsets.right) {
                return false;
            }

            let computedStyle = doc.defaultView.getComputedStyle(elem, null);
            if (computedStyle.visibility != "visible" || computedStyle.display == "none") {
                return false;
            }
            return true;
        }

        hints.addMode(
	        '/',
	        'Mode used to position the text element',
	        function (elem) {}
	    );

        var mode = hints.modes['/'];
        var res = mode.matcher(doc);
        for (let elem in res) {
            //alert(elem.innerHTML);
            if (isVisible(elem) && (!mode.filter || mode.filter(elem))) {
                return elem;
            } else {

            }
        }

        /*
        Array.forEach(win.frames, function (f) {
            if (isVisible(f.frameElement)) {
                let rect = f.frameElement.getBoundingClientRect();
                generate(f, {
                    left: Math.max(offsets.left - rect.left, 0),
                    right: Math.max(rect.right - offsets.right, 0),
                    top: Math.max(offsets.top - rect.top, 0),
                    bottom: Math.max(rect.bottom - offsets.bottom, 0)
                });
            }
        }, this);
        */
    };

    group.commands.add(
        ["findhere"],
        "Find from the start of current screen",
        function(args){
            moveCaret(findFirstTarget()); 
            rangefinder.openPrompt(modes.FIND_FORWARD);
        }, 
        {
            literal: 0
        }, true);

    dactyl.execute("se eht& | se eht+=[/]:*")
})();

