// ==UserScript==
// @name		UWhub on Major Search Engines
// @namespace		http://uwhub.ca/
// @description		Adds a UWhub search button to Google, Yahoo!, MSN, Ask Jeeves, and AOL (somewhat)
// @include		http://*google.*
// @include		http://*yahoo.*
// @include		http://*msn.*
// @include		http://*ask.*
// @include		http://*aol.*
// ==/UserScript==

function dosearch() {
	var t;
	//get the query textbox - exact same code as earlier
	if (site('yahoo')) { t = f.elements.namedItem('p'); }
	else if (site('aol')) { t = f.elements.namedItem('query'); }
	else { t = f.elements.namedItem('q'); }
	var l = 'http://uwhub.ca/?q=' + t.value;	
	if (location.href.indexOf('/image')>=0 || location.href.indexOf('.images')>=0 || location.href.indexOf('/imghp')>=0 || location.href.indexOf('pictures')>=0) { l += '&vertical=images'; }
	location.href = l;
	}

function fid(id) {
	id = id.split(',');
	var f;
	for (var i=0; i<id.length; i++) { f = document.getElementById(id[i]); if (f) { return f; } }
	return false;
	}
function fname(n) {
	n = n.split(',');
	var f;
	for (var i=0; i<n.length; i++) { f = document.forms.namedItem(n[i]); if (f) { return f; } }
	return false;
	}
function fidn(id, n) {
	f = fid(id);
	if (!f) { f = fname(n); }
	return f;
	}
function get_form() {
	var f = get_form_by_site();
	if (!f) { f = document.forms[0]; }
	return f;
	}
function get_form_by_site() {
	if (site('google')) { return fname('gs,f'); }
	if (site('yahoo')) { return fidn('s', 's,w,yhdr_form'); }
	if (site('ask')) { return fid('f1,ft'); }
	if (site('msn')) { return fid('sf,qf,search'); }
	return false;
	}

function site(str) {
	if (location.href.indexOf(str) != -1) { return true; }
	return false;
	}

var f = get_form();
if (f) {
	var t;
	if (site('yahoo')) { t = f.elements.namedItem('p'); }
	else if (site('aol')) { t = f.elements.namedItem('query'); }
	else { t = f.elements.namedItem('q'); }
	if (t) { //yes there's a query textbox
		var inp = f.getElementsByTagName('input');
		if (inp.length == 0) { inp = document.getElementsByTagName('input'); }
		for (var i=0; i<inp.length; i++) { if (inp[i].type == 'submit') { var sub = inp[i]; break; } }
		if (sub) { //yes there's a submit buttion
			var b = document.createElement('input');
			b.type = 'button';
			b.value = 'UWhub';
			b.style.margin = '0 3px';
			b.addEventListener('click', dosearch, true);
			b.className = sub.className;
			if (site('aol')) { t.style.width='275px'; }
			//actually add the button to the page
			var dd = document.getElementById('srch_btn_go'); //for some search.msn.com pages
			if (dd) {
				b.style.background = 'url(http://search.msn.com/s/sbtnbk.gif) repeat-x !important';
				b.style.color = '#fff';
				b.style.fontWeight = 'bold';
				b.style.padding = '0 3px 2px 3px';
				dd.parentNode.insertBefore(b, dd.nextSibling);		
				}
			else {
				sub.parentNode.insertBefore(b, sub.nextSibling);
				}
			} // if (sub)
		} // if (t)
	} // if (f)