// Cool Running: 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: coolrunning_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 pages on coolrunning. The Google search is
// added at the top of all pages.
//
// If searching from the forum (e.g. Gear, Newbie Cafe) pages, searches are
// performed within that forum.  On any other pages, searches are performed
// per site.
// ----------------------------------------------------------------------------

// ==UserScript==
// @name            Cool Running: Google Search
// @description     Adds a "Google Search" to Cool Running topic pages.
// @namespace       http://www.orbona.com/greasemonkey/
// @include         http://www.coolrunning.com/*
// ==/UserScript==
// ----------------------------------------------------------------------------
// This function intercepts the form submit.  Adds the appropriate
// advanced search parameters, which will restrict the search to
// the site or the forum, to the user-entered query string and
// then allows the submit to proceed.
// ----------------------------------------------------------------------------
function googleSubmit(event) {
   var theForumNum;
   var target = event ? event.target : this;

   var regexForumNum = /.+&number=(\d+).+/i;

   theForumNum = regexForumNum.exec( window.location.href)
   if (theForumNum) theForumNum = theForumNum[1];

   if (target.name=='googlesearch') {
      if (theForumNum)
         target.elements[1].value += ' site:coolrunning.com/Forums/Forum' + theForumNum +'/HTML';
      else
         target.elements[1].value +=' site:' + window.location.host;
   }
   this._submit();
}

// ----------------------------------------------------------------------------
// Main function that does everything described in the description area above.
// ----------------------------------------------------------------------------
function doIt() {
   var mainElem = document.getElementsByName('m_nav_home');

   var activeHeader, googleSearchElem, inputElemH1, inputElemSearch, submitElem;

   var links=document.evaluate("//A[contains(@href,'www.active.com')]", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,  null);
   if (links.snapshotLength > 0)
      activeHeader = links.snapshotItem(0);

   if (mainElem && mainElem[0]) {
      mainElem = mainElem[0];

      if (activeHeader) {

         var activeLink = document.createElement('a');
         activeLink.href= activeHeader.href;
         activeLink.style.marginRight= '3em';
         activeLink.innerHTML = 'Visit active.com';
         activeHeader.parentNode.removeChild(activeHeader);

         mainElem.parentNode.insertBefore(activeLink, mainElem);
      }

      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

      mainElem.parentNode.insertBefore(googleSearchElem, mainElem);
   }
}
// ------------------END OF FUNCTIONS -----------------------------

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