// ==UserScript==
// @name          Customize Google
// @namespace     http://alloscomp.com/customizegoogle.user.js
// @description	Customize a lot of different things related to the way Google operates
// @include       *.google.com*
// ==/UserScript==

(function () {

function removeElementByID(id) {
	var e = document.getElementById(id);
	if(e != null) {
		e.parentNode.removeChild(e);
	}
}

var minimizeGoogle = function() {
	var elm = document.getElementById("col1");
	//alert(elm.style.display);
	if(elm.style.getPropertyValue('display') == "none")
		elm.style.display = '';
	else
		elm.style.display = "none";
}

unsafeWindow.minimizeGoogle = minimizeGoogle;

var logoURL = 'http://www.google.com/intl/en_ALL/images/logo_sm.gif';
var faviconURL = "http://www.google.com/favicon.ico";

//Apply favicon in Firefox... needed for me
/*var eLink = document.createElement('link');
eLink.setAttribute('rel', 'shortcut icon');
eLink.setAttribute('href', faviconURL);
var head = document.getElementsByTagName('head')[0];
head.appendChild(eLink);*/

//Remove those damn redirects... they just slow things down
//This might affect their tracking in some way, but I don't use it!
if(location.href.indexOf("www.google.com/search") > -1) {
	var eLinks = document.getElementsByTagName("a");
	for(var i = 0; i < eLinks.length; i++)
		if(eLinks[i].getAttribute('class') == 'l')
			eLinks[i].removeAttribute('onmousedown');
}

// Add "Image" link below google image results
if(location.href.indexOf("images.google.com") > -1) {
    // Get a list of all A tags that have an href attribute containing the start and stop key strings.
    var googLinks = selectNodes(window.document, window.document.body, "//A[contains(@href,'/imgres?imgurl=')][contains(@href,'&imgrefurl=')]");
    
    for (var x=0; x<googLinks.length; x++) {
		// Capture the stuff between the start and stop key strings.
		var gmatch = googLinks[x].href.match(/\/imgres\?imgurl\=(.*?)\&imgrefurl\=/);

		if(gmatch) {
			googLinks[x].parentNode.innerHTML += "<br><a style='font-size:.9em;text-decoration:none !important;' href='"+decodeURI(gmatch[1])+"'>Image</a>";
		}
    }
}

// iGoogle Page Customizations
if(location.href.match(/https?:\/\/www.google.com\/ig([\?#].*)?/)) {
	//Comment this out to disable changing of iGoogle logo
	/*if(document.getElementById("regular_logo") != null) 
		document.getElementById("regular_logo").style.setProperty('background-image','url(' + logoURL + ')','important');
	else if(document.getElementById("logo") != null) { // Not the main IG page...
		var sCode = document.getElementById("logo").innerHTML;
		var start = sCode.indexOf('<img src="') + 10;
		var stop = sCode.indexOf('"',start);
		var firstPart = sCode.substring(0,start);
		var lastPart = sCode.substring(stop);
		var sNewCode = firstPart + logoURL + lastPart;
		document.getElementById("logo").innerHTML = sNewCode;
	}*/
	
	// Remove footer
	removeElementByID("footer_promos");
	removeElementByID("footerwrapinner");
	var elm = document.getElementById("modules");
	if(elm != null)
		elm.style.paddingBottom = 0;
		
	var elm = document.getElementById("footerwrap");
	if(elm != null && document.body.innerHTML.indexOf('gadget has been sent.') != -1) { // Crappy V2 check
		elm.style.height = "5px";
		elm.parentNode.parentNode.style.backgroundColor = "#ecf4fd"; // html
		elm.parentNode.style.backgroundColor = "#ffffff"; // body
	}
	
	// Fix stock gadget font
	/*var css = ".containerName {font-size: 0.9em !important;}";
	if (typeof GM_addStyle != "undefined") {
		GM_addStyle(css);
	} else if (typeof addStyle != "undefined") {
		addStyle(css);
	} else {
		var heads = document.getElementsByTagName("head");
		if (heads.length > 0) {
			var node = document.createElement("style");
			node.type = "text/css";
			node.appendChild(document.createTextNode(css));
			heads[0].appendChild(node); 
		}
	}*/
	
	// Minimize sidebar
	var l = document.getElementById("addstuff");
	if(l) {
		var b = document.createElement('a');
		b.setAttribute('href','javascript:window.minimizeGoogle();return false;');
		b.setAttribute('onclick','window.minimizeGoogle();');
		b.appendChild(document.createTextNode('Toggle Sidebar'));
		var txt = document.createTextNode(' | ');
		l.insertBefore(txt,l.firstChild);
		l.insertBefore(b,txt);
		minimizeGoogle();
	}
}

//Comment this out to disable linkification of the Google Malware Warning
if(location.href.indexOf("www.google.com/interstitial?url=") > -1 && document.title.innerHTML == "Malware Warning") {
	var theBody = document.body.innerHTML;
	var start = theBody.indexOf("continue to ") + 12;
	var stop = theBody.indexOf(" at your own risk.");
	var url = theBody.substring(start,stop);
	var newUrl = "<a href='" + url + "'>" + url + "</a>";
	var firstPart = theBody.substring(0,start);
	var lastPart = theBody.substring(stop);
	theBody = firstPart + newUrl + lastPart;
	document.body.innerHTML = theBody;
}

function selectNodes(doc, context, xpath) {
	var nodes = doc.evaluate(xpath, context, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	var result = new Array( nodes.snapshotLength );
	for (var x=0; x<result.length; x++)
		result[x] = nodes.snapshotItem(x);
	return result;
}

})();

