// ==UserScript==
// @name           KuruKuruMixi
// @namespace      http://ma.la/
// @author         ma.la <timpo@ma.la>
// @include        http://mixi.jp/*
// ==/UserScript==
// ver 0.2 @ 2005-06-07
//  もっとくるくるー。
// ver 0.1 @ 2005-06-06
//  くるくるー。

(function()
{
	var all = GM_getValue('all', '');
	var check = function(e){
		GM_setValue('all', e.target.checked);
		alert("please reload")
	};
	var span = document.createElement("span");
	var input = document.createElement("input");
	input.type = "checkbox";
	input.checked = all;
	input.addEventListener("click",check,true);
	with(span.style){
		position = "fixed";
		bottom = "0px";
		right = "0px";
		display = "none";
	}
	span.appendChild(document.createTextNode("yes,I'm crazy"));
	span.appendChild(input);
	document.body.appendChild(span);

	var do_rotate = function(e){
		var obj = e.target;
		if(e.ctrlKey == true){
			span.style.display = "inline";
		}
		if(obj.getAttribute("now_rotate") != 1){
			obj.setAttribute("now_rotate",1);
			var ro = new Rotate(obj);
			ro.rotate_x(ro);
		}
	};
	var Rotate = function(obj){
		this.target = obj;
		this.o_width  = obj.offsetWidth;
		this.o_height = obj.offsetHeight;
		this.c_width = obj.offsetWidth;
		this.rotate_x = function(r){
			r.c_width -= Math.ceil(r.o_width/20) + 1;
			if(r.c_width < 0){r.c_width = 0}
			var m = Math.ceil(r.o_width - r.c_width)/2 -1;
			with(obj.style){
				width = r.c_width + "px";
				height = r.o_height + "px";
				marginLeft = m + "px";
				marginRight = (r.o_width - r.c_width - m -1) + "px"
			}
			var f = arguments.callee;
			var func = function(){f(r)};
			if(r.c_width > 0){
				setTimeout(func,10);
			}else{
				r.rev_rotate_x(r);
			}
		}
		this.rev_rotate_x = function(r){
			r.c_width += Math.ceil(r.o_width/20) + 1;
			if(r.c_width > r.o_width){r.c_width = r.o_width}
			var m = Math.ceil(r.o_width - r.c_width)/2 -1;
			with(obj.style){
				width = r.c_width + "px";
				height = r.o_height + "px";
				marginLeft = m + "px";
				marginRight = (r.o_width - r.c_width - m -1) + "px"
			}
			var f = arguments.callee;
			var func = function(){f(r)};
			if(r.c_width < r.o_width){
				setTimeout(func,10);
			}else{
				r.target.setAttribute("now_rotate",0);
			}
		}
	};
	var images;
	if (all == true){
		var images = document.evaluate(
			'//img[contains(@src, "img")]',
			 document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
		);
	}else{
		var images = document.evaluate(
			'//img[contains(@src, "photo")]',
			 document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
		);
	}
	for (var i = 0; i < images.snapshotLength; i++)
	{
		var img = images.snapshotItem(i);
		img.addEventListener("mouseover",do_rotate,true)
	}
})();

