// ==UserScript==
// @name	gmailEnlargePreview
// @author	keigo
// @version	0.1
// @namespace	http://greasemonkey.hippo.fm/gmailEnlargePreview
// @include	https://mail.google.com/*
// @include	http://mail.google.com/*
// @description	Enlarge image attachment for Gmail.
// ==/UserScript==

// Bugs..
// When the thread has many mails and many attachments, it could failed to convert links.
// I could not recognize the bug correctly. (maybe it occurs when the thread has more than 5 mails and it is expanded. )
// I doubt using 'gmail.getActiveViewElement().getElementsByTagName("img");' is wrong but how do I do it ?

window.addEventListener('load', function() {
	if (unsafeWindow.gmonkey) {
		unsafeWindow.gmonkey.load('1.0', function(gmail) {
			function rewriteImgTags() {
				if (gmail.getActiveViewType() == "cv") {
					var attachImgs = gmail.getActiveViewElement().getElementsByTagName("img");
					for (i = 0; i < attachImgs.length; i++) {
						if (attachImgs[i].className == "tFroq") {
							attachImgs[i].width = "500";
							attachImgs[i].src.match(/(.*)\?(.*)attid=([0-9.]+)&(.*)=([a-z0-9]+)$/); // $1 for google apps, $3 for attid, $5 for uniqueid of mail.
							attachImgs[i].src =  RegExp.$1 + "?attid=" + RegExp.$3 + "&disp=emb&view=att&th=" + RegExp.$5;
						}
					}

				}
			}
			gmail.registerViewChangeCallback(rewriteImgTags);
			rewriteImgTags();
		});
	}
}, true);

