// TWoP: Google Search
// Copyright (c) 2006-2007 Orbona
// Version 1.0
// Release Date: 2007-10-22
//
// See also: http://www.orbona.com/greasemonkey/
//
// Original file name: twop_google.user.js
// Please reference the original file name when contacting me regarding this
// script.
//
// This software is licensed under the CC-GNU GPL:
// http://creativecommons.org/license/cc-gpl
//
// -----------------------------------------------------------------------------
// DESCRIPTION
//
// Adds a Google search to the "showTopic" and "showForum" pages at Television
// Without PIty. The Google search is added immediately before the native TWoP
// search form.
//
// If searching from the topic pages, searches are performed within that topic.
// On any other page, searches are performed per site; I currently am not sure
// how to restrict searches to a forum/subforum.
// -----------------------------------------------------------------------------

// ==UserScript==
// @name            TWoP: Google Search
// @description     Adds a "Google Search" to TWoP forum pages
// @namespace       http://www.orbona.com/greasemonkey/
// @include         http://forums.televisionwithoutpity.com/index.php?show*
// ==/UserScript==

// -----------------------------------------------------------------------
// This function intercepts the form submit.  Adds the appropriate
// advanced search parameters, which will restrict the search to the site
// or the topic, to the user-entered query string and then allows the
// submit to proceed.
// -----------------------------------------------------------------------
function googleSubmit(event) {
   var target = event ? event.target : this;
   if (target.name=='googlesearch') {
       var thedomain=window.location.host;
       target.elements[1].value= window.location.href.indexOf('showtopic')>0  ? target.elements[1].value + ' site:' + thedomain + ' intitle:"' + window.document.title + '"' : target.elements[1].value + ' site:' + thedomain;
   }
   this._submit();
}

// ----------------------------------------------------------------------------
// Main function that does everything described in the description area above.
// ----------------------------------------------------------------------------
function doIt() {

   var searchElem = document.getElementsByName('search');
   var googleSearchElem, inputElemH1, inputElemSearch, submitElem;

   if (searchElem && searchElem[0]) {

      googleSearchElem = document.createElement('form');

      googleSearchElem.action = 'http://www.google.com/search';
      googleSearchElem.name= 'googlesearch';
      googleSearchElem.style.display = 'inline';

      inputElemH1=document.createElement('input');
      inputElemH1.type='hidden';
      inputElemH1.name='h1';
      inputElemH1.value='en';

      inputElemSearch=document.createElement('input');
      inputElemSearch.name='q';
      inputElemSearch.maxlength='2048';
      inputElemSearch.size='25';
      inputElemSearch.title='GoogleSearch';

      submitElem=document.createElement('input');
      submitElem.type='submit';
      submitElem.name='btnG';
      submitElem.value='Google Search';

      googleSearchElem.appendChild(inputElemH1);
      googleSearchElem.appendChild(inputElemSearch);
      googleSearchElem.appendChild(submitElem);

      googleSearchElem.addEventListener('submit', googleSubmit, true);
      //no need to redefine method of HTMLFormElement class since
      //no other scripts should be submitting my brand-spankin-new form

      searchElem[0].parentNode.insertBefore(googleSearchElem, searchElem[0]);
   }
}

// ------------------END OF FUNCTIONS -----------------------------

window.addEventListener("load", function() { doIt(); }, false);


