// ==UserScript==
// @name           Google Columnizer
// @namespace      http://arantius.com/misc/greasemonkey/
// @description    Reformat Google search results to appear in two columns
// @include        http://www.google.com/search?*
// @include        http://google.multiseek.net/search?*
// @require        http://arantius.com/misc/greasemonkey/imports/dollarx.js
// ==/UserScript==

var results=$x('/html/body/div//li[@class="g" or @class="g w0"]');
if (0==results.length) {
	return;
}

// get/make two columnar containers
var resultCont1=results[0].parentNode;
while (resultCont1 && 'DIV'!=resultCont1.tagName) {
	resultCont1=resultCont1.parentNode;
}
if (!resultCont1) return;
resultCont1.className='result-column';
var resultCont2=document.createElement('div');
resultCont2.className='result-column';

// put the second one in the document
resultCont1.parentNode.insertBefore(
	resultCont2, resultCont1.nextSibling
);

// move paragraphs out of the first column
// i.e.: In order to show you the most relevant results, we have omitted ...
var point=resultCont2.nextSibling;
for (var i=0, el=null; el=resultCont1.childNodes[i]; i++) {
	if ('P'!=el.tagName) continue;
	resultCont2.parentNode.insertBefore(el, point);
}

// style them
GM_addStyle(
	// make the columns work
	'div#cnt {'+
		'max-width: none !important;'+
	'}'+
	'div.result-column {'+
		'width: 49%;'+
		'float: left;'+
	'}'+
	'div.result-column + div.result-column {'+
		'padding-left: 2%;'+
	'}'+
	'div.result-column + p, div.result-column + br, '+
	'div.result-column + div.result-column + div {'+
		'clear: both;'+
	'}'+
	// remove the list item dots
	'li.g { list-style-type: none; }'+
	// squeeze a sub-result near it's parent
	'div.g[style~="margin-left:"] {'+
		'margin-top: -12px'+
	'}'+
	// make sure long URLs don't overlap
	'li.g span.a {'+
		'overflow: hidden;'+
	'}'+
	// hide ads, for room
	'div#tpa1, div#tpa2, div#tpa3, div#tads, table#mbEnd {'+
		'display: none !important;'+
	'}'+
	// hide "wiki" features
	'button.w10, button.w20, button.wci { display: none !important; }'
);

// find the middle, where to start moving.  include the parent if it is a sub
var midpoint=Math.floor(results.length/2);
try {
	if (results[midpoint].getAttribute('style').match('margin-left')) {
		midpoint--;
	}
} catch (e) { }

// make the moves
for (var i=midpoint, result=null; result=results[i]; i++) {
	resultCont2.appendChild(result);
}

// scroll to a place that fits the real content
window.scrollTo(0, 91);

// Also, as a ride-on, remove Google's click tracking.
$x('//a[@onmousedown]').forEach(function(el) {
	el.removeAttribute('onmousedown');
});
