var h = 0;
var r = 0;

function pos(div,i,c) {
	while(i < 0) {
		i = i + c;
	}
	while(i >= c) {
		i = i - c;
	}
	var w = 0;
	w = 895;
	var l = 0;
	var o = {};
	z = Math.round( (c - Math.abs( ((c-1)/2)-i) ));
	u = z * 50;
	l = ( i )*(w / c) + Math.round(w/(c*2)) - Math.round(u/2);
	t = 200 - (z * 20);
	s = z * 100;
	y = Math.round( ((c-1)/2)-i );
	o = {
		"left":l+"px",
		"width":u+"px",
		"top":t+"px",
		"zIndex":s,
		"sign":y 
	};
	return o;
}
function ani(img,o,t,fc) {
	m = {
		"left":o.left,
		"width":o.width,
		"top":o.top
	}
	jQuery(img).animate(m,t,fc);
	jQuery(img).css("z-index",o.zIndex);			
	jQuery(img).attr("anipos",o.sign);
}
function rot(div,p,t,fc) {
	jQuery(div).css("position","relative");
	var c = 0;
	jQuery(div+" img").each(function(){
		c++;
		jQuery(this).css("position","absolute");					
	});				
	var i = p;
	jQuery(div+" img").each(function(){
		var o = pos(div,i,c);
		if(o.sign == 0) jQuery(this).addClass('reflect');
		if ((i-p) == (c-1)) {
			ani(this,o,t,fc);
		} else {
			ani(this,o,t,function(){return false;});
		}					
		i++;
	});					
}

jQuery("document").ready(function(){				

	rot("#rotation",r);
	
	jQuery("#rotation img").click(function(){					
		var i = 0;
		d = parseInt(jQuery(this).attr("anipos"));
		if (d>0) {
			d = Math.abs(d);
			for (i=0;i<d;i++) {
				setTimeout(
					'r++;rot("#rotation",r,300,function(){});',
					400*i
				);
			}
		} else if (d<0) {
			d = Math.abs(d);
			for (i=0;i<d;i++) {
				setTimeout(
					'r--;rot("#rotation",r,300,function(){});',
					400*i
				);
			}					
		}
	});
	
});

