// ==UserScript==
// @name		Google User Persitance Thing
// @namespace		http://www.rhyley.org/gm/
// @description		Add a drop-down box with your google ID's to the google login form.
// @include		http://*.google.co*
// @include		https://*.google.co*
// ==/UserScript==

// ** Add your user ID's to this array
names = new Array("Put","Your","User","ID","Here");
var i,k;
window.myLogin = null;
window.myPassw = null;
opts = new Array();


function gm_xpath(what,where) {
	return document.evaluate(what,where,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
}

myLogin = gm_xpath("//input[@name='Email']",document);
myLogin = (myLogin)? myLogin.snapshotItem(0) : null;
myPassw = gm_xpath("//input[@name='Passwd']",document);
myPassw = (myPassw)? myPassw.snapshotItem(0) : null;


buildLoginThing = function() {
	if (names[0] == 'Put'){
		alert('You must configure the script before it will \nwork propery. Go to \"Manage User Scripts\" and\nuse the \"Edit\" button to change the script.');
		return;
	}
	
	var theSelect = document.createElement("select");
	theSelect.setAttribute("name","Email");
	theSelect.setAttribute("style","width:10em");
	//theSelect.setAttribute("onchange","if (this.selectedIndex == this.options.length-1) killLoginThing(this); else password.focus()");
	theSelect.addEventListener('change', function() {
		if (this.selectedIndex == this.options.length-1) {
		    window.setTimeout(function() {
			var elmNew = document.createElement("input");
			elmNew.type = "text";
			elmNew.name = "Email";
			elmNew.setAttribute("style","width:10em");
			theSelect.parentNode.replaceChild(elmNew, theSelect);
			elmNew.focus();
		    }, 0);
		}
		else {
		    password.focus();
		}
	}, true);
	
	for (var i in names) {
		opts[i] = document.createElement("option")
		opts[i].setAttribute("value",names[i]);
		opts[i].innerHTML = names[i];
		theSelect.appendChild(opts[i]);
	}
	opts[i] = document.createElement("option")
	opts[i].innerHTML = "Other...";
	theSelect.appendChild(opts[i]);
	myLogin.parentNode.replaceChild(theSelect, myLogin);
}



if (myLogin && myPassw){ 
	buildLoginThing();
	myPassw.setAttribute("style","width:10em");
	setTimeout(function (){myPassw.focus()},100);
}
