// ==UserScript==
// @name Auto search forwarder
// @author Nicolas Mendoza 
// @namespace http://my.opera.com/nicomen 
// @version 1.0
// @description  If the page cannot be found, this script forwards the
//			content of the address bar to the I'm Feeling
//			Lucky (tm) search feature from Google.
// @ujs:category browser: enhancements
// @ujs:published 2006-01-30 20:37
// @ujs:modified 2006-01-30 20:46
// @ujs:documentation http://userjs.org/scripts/browser/enhancements/auto-search-forwarder 
// @ujs:download http://userjs.org/scripts/download/browser/enhancements/auto-search-forwarder.js 
// Contact the author mendoza@pvv.ntnu.no for more information
// ==/UserScript==


/* 
 * License: E-mailware, send me an e-mail if you use it.
 */

// Timeout till script is run, Opera error pages don't trigger "load" event
var asfwd_timeout = 10; // Increase this if it doesn't work.

window.setTimeout(function(){
  // The url where you should be forwarded, use REPLACEQUERY where you want the address bar content to appear
  var asfwd_url = "http://www.google.com/search?q=REPLACEQUERY&btnI=I%27m+Feeling+Lucky";

  var asfwd_hostname = asfwd_url.match(/http:\/\/(.*?)\//)[1];
  var asfwd_body = document.getElementsByTagName("body");
  var asfwd_rem_chars = new RegExp(/[_\.]/g);

  if (location.hostname != asfwd_hostname) {
    if (asfwd_body && asfwd_body.item && asfwd_body.item(0) &&
        asfwd_body.item(0).id == "opera-error") {
      var asfwd_query = escape(location.href.match(/http:\/\/(.*)\//)[1].replace(asfwd_rem_chars," "));
      location.href = asfwd_url.replace(/REPLACEQUERY/,asfwd_query);
    }
  }
}, asfwd_timeout);