(function(jQuery) {
	
	jQuery.fn.rotate = function(options) {
		var opts = jQuery.extend({}, jQuery.fn.rotate.defaults, options);
		
		return this.each(function() {
			var $this	= jQuery(this);
			var vars	= jQuery.fn.extend({
				coords: {
					box: {
						top:	null,
						middle:	null,
						bottom:	null
					},
					startPos:	null,
					middlePos:	null,
					endPos:		null
				},
				opts: opts
			});
			
			
			$this
				.css('overflow', 'hidden');
			
			vars.coords.box.top	= 0;
			vars.coords.box.middle	= Math.floor($this.outerHeight() / 2);
			vars.coords.box.bottom	= $this.outerHeight();
			

			$this.children(opts.elm).extend(vars);
			$this.children(opts.elm).each(doRotate);
		});
	};
	
	
	function doRotate() {
		var $this = jQuery(this);
		
		$this.coords.startPos	= $this.coords.box.bottom;
		$this.coords.middlePos	= $this.coords.box.middle - Math.floor($this.outerHeight() / 2);
		$this.coords.endPos	= $this.coords.box.top - $this.outerHeight();
		
		$this
			.css('position', 'absolute')
			.css('top', $this.coords.startPos.toString() + 'px')
			.css('-moz-opacity', $this.opts.startOp.toString())
			.css('filter', 'alpha(opacity=' + $this.opts.startOp.toString() + ')')
			.animate({top: $this.coords.middlePos, opacity: $this.opts.endOp}, $this.opts.speedIn)
			.animate({top: $this.coords.middlePos}, $this.opts.pause)
			.animate({top: $this.coords.endPos, opacity: $this.opts.startOp}, $this.opts.speedOut, doRotate);
	}
	
	jQuery.fn.rotate.defaults = {
		elm: '.rotate',
		startOp: 0,
		endOp: 1,
		speedIn: 3000,
		speedOut: 3000,
		pause: 3000
	};
	
})(jQuery);
