// ==UserScript==
// @name           Kuvaton.com autoscaler
// @namespace      http://gm.taistelumarsu.org/
// @description    Skaalaa kuvia automaagisesti
// @include        http://kuvaton.com/bshit/*
// @include        http://kuvaton.com/totallycrap/*
// ==/UserScript==

var imgs = document.getElementsByTagName('img');
var max = screen.availWidth*0.8;
tries = 0;
while (tries < 10) {
	setTimeout(function() {
	allComplete = true;
	for (i = 0; i < imgs.length; i++) {
		if (imgs[i].parentNode.wrappedJSObject.className == 'kuvaboxi') {
			if (imgs[i].complete) {
				if (imgs[i].width > max) {
					kerroin = max/imgs[i].width;
					imgs[i].alt = kerroin;
					imgs[i].width = imgs[i].width*kerroin;
					if (imgs[i].height == 0) {
						imgs[i].height = imgs[i].height*kerroin;
					}
					imgs[i].addEventListener("dblclick", function() {
						if (typeof(this.alt) == 'undefined') {
							return;
						}
						if (this.width > max) {
							this.width = max;
							if (this.height == 0) {
								this.height = this.height*this.alt;
							}
						} else {
							this.width = this.width/this.alt;
							if (this.height == 0) {
								this.height = this.height/this.alt;
							}
						}
					}, false);
				}
			} else {
				allComplete = false;
			}
		}
	}
	if (allComplete) {
		return;
	}
	}, 500);
	tries++;
}
