// ==UserScript==
// @name           GuruguruImages
// @namespace      kiy0taka333.sakura.ne.jp
// @include        http://images.google.co.jp/images*
// ==/UserScript==

(function() {

const MIN_TIME_SPEED = 25;
const MAX_TIME_SPEED = 25;
const TIME_ACCELELERATION = 0;

const MIN_RAD_SPEED = 2 * Math.PI / 500;
const MAX_RAD_SPPED = 2 * Math.PI / 10;
const RAD_ACCELELERATION = 2 * Math.PI / 2500;

var x;
var y;
var centerX;
var centerY;

function Speed() {}

Speed.prototype = {
	'rad' : MIN_RAD_SPEED,
	'time' : MIN_TIME_SPEED,
	'fast' :
		function() {
			if ((this.time -= TIME_ACCELELERATION) <= MAX_TIME_SPEED) {
				this.time = MAX_TIME_SPEED;
				if ((this.rad += RAD_ACCELELERATION) >= MAX_RAD_SPPED) {
					this.rad = MAX_RAD_SPPED;
					c.toButter();
				}
			}
		},
	'reset' :
		function() {
			this.rad = MIN_RAD_SPEED;
			this.time = MIN_TIME_SPEED;
		}
}

var speed = new Speed();

function Container() {}

Container.prototype = {
	'elements' : new Array(),
	'add' :
		function(element) {
			this.elements.push(element);
		},
	'clockwise' :
		function() {
			this.move(speed.rad);
		},
	'antiClockwise' :
		function() {
			this.move(-1 * speed.rad);
		},
	'move' :
		function(add) {
			for (var i=0; i<this.elements.length; i++) {
				var e = this.elements[i];
				e.style.position = 'absolute';
				e.rad += add;
				e.style.top = centerY + Math.round(y * Math.cos(e.rad));
				e.style.left = centerX + Math.round(x * Math.sin(e.rad));
			}
		},
	'isButter' : false,
	'toButter' :
		function() {
			if (this.isButter) return;
			for (var i=0; i<this.elements.length; i++) {
				var e = this.elements[i];
				var f = function() {
					arguments[0].href = 'http://www.snowbrand.co.jp/products/butter/index.html';
					arguments[0].childNodes[0].src = 'http://www.snowbrand.co.jp/products/butter/01/images/p01s.jpg';
				}
				setTimeout(f, i * 500, e);
			}
			this.isButter = true;
		}
};

function Loop() {}

Loop.prototype = {
	'f' : null,
	'execute' :
		function() {
			this.f();
			var l = new Loop();
			l.f = this.f;
			if (flg) setTimeout(function() {l.execute()}, speed.time);
		}
};

var c = new Container();
var images = document.images;
var count = images.length;

var maxHeight = 0;
var maxWidth = 0;
for (var i=0; i<count; i++) {
	if (maxHeight < images[i].height) maxHeight = images[i].height;
	if (maxWidth < images[i].width) maxWidth = images[i].width;
	var a = images[i].parentNode;
	a.rad = (2 * i / (count)) * Math.PI;
	c.add(a);
}

x = Math.round(window.innerWidth / 2 - maxWidth);
y = Math.round(window.innerHeight / 2 - maxHeight);
centerX = Math.round((window.innerWidth - maxWidth) / 2);
centerY	= Math.round((window.innerHeight - maxHeight) / 2);

var flg;

document.addEventListener(
	'keypress',
	function(e) {
		if (flg) {
			if (flg == e.keyCode) speed.fast();
			return;
		}
		speed.reset();
		var f;
		switch (flg = e.keyCode) {
			case 37 /* left key */ :
				f = function() {c.antiClockwise()};
				break;
			case 39 /* right key */ :
				f = function() {c.clockwise()};
				break;
			default /* other keys */ :
				flg = 0;
				break;
		}
		if (f) {
			var l = new Loop();
			l.f = f;
			l.execute();
		}
	},
	true
);

document.addEventListener('keyup', function() {flg = 0}, true);

})();

