// ==UserScript==
// @name          Gaia Online Image Resizer v2.5
// @namespace     http://www.gorgarath.com/greasemonkey/
// @description   Resizes images on Gaia Online's forums to prevent page stretching.
// @include       *.gaiaonline.com/forum/viewtopic.php*
// @include       *.gaiaonline.com/profile/privmsg.php*
// @include       *.gaiaonline.com/profile/index.php?view=profile*

// ==/UserScript==
//
// The images are shown in their original size when you click on them.
//
// Based off of an user script for phpBB in general, just re-optimised it for Gaia Online
// and for working on quoted images.
//
// And I also decided to ad the ability to make the quoted images even smaller to reduce
// some of the picture quote tower sizes nicely.
//
// Installation notes:
//
// The default maximum width of images is set to 500px which is the max width for
// pictures in signatures.
//
// The 300px width is an arbitrary width that I thought looked nice on a 1024x768
// screen setting for images that have been quoted.
//
// The 480px width seemed to fit nicer than the 500px setting for profile pages so that's
// what I set it to here.

maxWidth = 500;		// maximum width of images in pixels for posts
maxWidthQ = 300;	// maximum width of images for quoted images
maxWidthS = 480;	// maximum width of images for signatures in profile

var aSpan = document.getElementsByTagName('span');
for (i=0; i<aSpan.length; i++) {
	if (aSpan[i].className == 'postbody') {
		var aImg = aSpan[i].getElementsByTagName('img');
		for (j=0; j<aImg.length; j++) {
			aImg[j].style.maxWidth = maxWidth + 'px';
			aImg[j].addEventListener('click', function(event) {
					if (event.currentTarget.style.maxWidth == 'none')
						event.currentTarget.style.maxWidth = maxWidth + 'px';
					else
						event.currentTarget.style.maxWidth = 'none';
				}, false);
		}
	} else if (aSpan[i].className == 'gen') {
		var aImg = aSpan[i].getElementsByTagName('img');
		for (j=0; j<aImg.length; j++) {
			aImg[j].style.maxWidth = maxWidth + 'px';
			aImg[j].addEventListener('click', function(event) {
				if (event.currentTarget.style.maxWidth == 'none')
					event.currentTarget.style.maxWidth = maxWidth + 'px';
				else
					event.currentTarget.style.maxWidth = 'none';
			}, false);
		}
	}
}

var aTd = document.getElementsByTagName('td');
for (i=0; i<aTd.length; i++) {
	if (aTd[i].className == 'quote') {
		var aImg = aTd[i].getElementsByTagName('img');
		for (j=0; j<aImg.length; j++) {
			aImg[j].style.maxWidth = maxWidthQ + 'px';
			aImg[j].addEventListener('click', function(event) {
					if (event.currentTarget.style.maxWidth == 'none')
						event.currentTarget.style.maxWidth = maxWidthQ + 'px';
					else
						event.currentTarget.style.maxWidth = 'none';
				}, false);
		}
	}
}

var aP = document.getElementsByTagName('p');
for (i=0; i<aP.length; i++) {
	var aImg = aP[i].getElementsByTagName('img');
	for (j=0; j<aImg.length; j++) {
		aImg[j].style.maxWidth = maxWidth + 'px';
		aImg[j].addEventListener('click', function(event) {
			if (event.currentTarget.style.maxWidth == 'none')
				event.currentTarget.style.maxWidth = maxWidthS + 'px';
			else
				event.currentTarget.style.maxWidth = 'none';
		}, false);
	}	
}
