// LDD Super Search
// version 0.1
// 2005-04-19
// Released under the GPL license: http://www.gnu.org/copyleft/gpl.html
// See http://www.jakehowlett.com/codestore/wiki/index.php?title=GreaseMonkey
//
// Changelog:
// 0.1 (2005-04-19) -- Original release.
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts, select this script,
// and click Uninstall.
//
// --------------------------------------------------------------------
//
// This script is loosely based on the work of Julian Robichaux, without
// whom I wouldn't have known to use a "snapshot" result type with my
// XPath query and it wouldn't have worked.
//
// --------------------------------------------------------------------
// ==UserScript==
// @name            LDD Super Search
// @namespace       http://www.jakehowlett.com/codestore/wiki/index.php?title=GreaseMonkey
// @description     Keeps current search string in Query field and opens results in new windows.
// @include         http://*lotus.com/ldd/*/search?SearchView*
// ==/UserScript==

(function() {
 
 var i=0;
 
 //This is the XPath used to find all search result links.
 var findPattern = "//table[2]//table[2]//table[2]//tr/td[4]/font/a";
 
 //Find all links and return a "snapshot" of the set of nodes.
 var resultLinks = document.evaluate( 
  			 findPattern,
  			 document,
  			 null,
  			 XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  			 null
 			); 
 
 //loop each link (node), add a new image and change the link's target
 
 while ( (res = resultLinks.snapshotItem(i) ) !=null ){
  
  //create a new element for EACH node
  var expander = document.createElement("td");
  expander.style.cursor = "pointer";
  expander.onclick=openNewWindow;
  expander.innerHTML = " <img src=/icons/viewsort.gif width=16 height=16 /> ";
  expander.setAttribute("url", res.getAttribute("href"));
  
  //Get a handle on the <tr> element and insert a new cell
  res.parentNode.parentNode.parentNode.appendChild(expander);
  
  //res.setAttribute("target", "_blank");
  
  i++;
 }
 
 
 //resize query box and add search term to it.
 document.forms[0].newquery.size="22";
 document.forms[0].newquery.value=document.forms[0].Query.value;


 // openNewWindow
 // Used to open links in new windows.
 // Not really needed but may be useful in future
 // if I work out how to launch new TABS
 function openNewWindow() {
  window.open(this.getAttribute("url"));
 }
 
})();