// ==UserScript==
// @name           Gmail Check POP3 Mail Now 
// @author         Daniel Slaughter
// @namespace      http://www.danielslaughter.com/
// @description    Automatically pulls in 3rd party POP3 accounts a set time interval.
// @include        http*://mail.google.com/*
// ==/UserScript==

var checkEvery = 10; // number in minutes
var checkForUpdates = true; // set to false if you do not want to check for updates to this script automatically

// --------- Do not edit anything below this line (unless you're feeling dangerous) --------- //
var versionNumber = '2/14/2010';
var milliseconds = new Date();
var label = document.createElement('a');
var pop3 = new Array();

// Discover the correct iframe for the body's content (since gmail greasemonkey support is poo, and I'm just hardcore like this)
window.setTimeout(function(){
	var el = document.body.getElementsByTagName('b');
	if (el && el.length && (el[0].innerHTML == 'Gmail' || el[0].innerHTML == 'Mail')) {
		var tempHash = top.location.hash;
		top.location.hash = '#settings/accounts';
		do {
		} while (top.location.hash != '#settings/accounts')
		window.setTimeout(function(){
			// Add jQuery
			var GM_JQ = document.createElement('script');
			GM_JQ.src = 'http://jquery.com/src/jquery-latest.js';
			GM_JQ.type = 'text/javascript';
			document.getElementsByTagName('head')[0].appendChild(GM_JQ);
			// Check if jQuery's loaded
			function GM_wait() {
			    if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
				else { $ = unsafeWindow.jQuery; letsJQuery(); }
			};
			GM_wait();
			// All your GM code must be inside this function
			function letsJQuery() {
				// wait one second for the DOM to fully load (slower connections may fail to initialize the dom before this)
				var el = document.body.getElementsByTagName('b');
				var el2 = document.body.getElementsByTagName('span');
				var i=0;
				for(var j=0;j<el2.length;j++) {
					if (el2[j].innerHTML == 'Check mail now') {
						pop3[i] = $(el2[j]).clone(true);
						i++;
					}
				}
				for(var i=0;i<pop3.length;i++) {
					pop3[i].appendTo(el[0]);
				}
				init();
			};
		},1000);
	}
},3000);
function init() {
	var header = document.body.getElementsByTagName('nobr');
	milliseconds = Date.UTC(milliseconds.getYear(),milliseconds.getMonth(),milliseconds.getDate(),milliseconds.getHours(),milliseconds.getMinutes(),milliseconds.getSeconds());
	// Inject a clickable link into the top right for manual checking
	label.setAttribute('class', 'e');
	label.setAttribute('id', 'pop3fetch');
	label.innerHTML = 'POP3';
	label.addEventListener('click', function() {
		fetchPopMail();
	},true);
	header[1].innerHTML += ' | ';
	header[1].appendChild(label);
	// start the timer
	if (checkEvery) {
		timerPopMail();
	}
};
function fetchPopMail() {
	label.setAttribute('class','');
	label.innerHTML = 'POP3 Checking...';
	var d = new Date();
	milliseconds = Date.UTC(d.getYear(),d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds());
	alert(pop3.length);
	for(var i=0;i<pop3.length;i++) {
		var evnt = document.createEvent('MouseEvents');
		evnt.initEvent('click',true,true); // event type, bubbling, cancelable
		alert(i);
		pop3[i].dispatchEvent(evnt);
	}
	label.innerHTML = 'POP3 Checked ' + pop3.length;
	label.setAttribute('class','e');
	return false;
};
function timerPopMail() {
	// is it time to POP3 yet?
	var d = new Date();
	var ms = (Date.UTC(d.getYear(),d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds())) - milliseconds;
	var nextcheck = 1000;
	if (checkEvery > 0 && milliseconds < (Date.UTC(d.getYear(),d.getMonth(),d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds()))-(checkEvery*60000)) {
		nextcheck = 1000;
		fetchPopMail();
	} else {
		label.innerHTML = 'POP3 in ' + (Math.floor((checkEvery*60000-ms)/60000)>0?Math.floor((checkEvery*60000-ms)/60000):'0') + ':' + ((Math.floor((checkEvery*60000-ms)/1000)%60) < 10?'0':'') + Math.floor((checkEvery*60000-ms)/1000)%60;
	}
	// do some recursion in 'nextcheck' seconds to see if it is time to automatically POP3 yet
	window.setTimeout(function() {
		timerPopMail();
	},nextcheck);
};
