(function($) {
	
	$.fn.rotate = function(options) {
		
		var total = $(this).children('.featureImages').children().length - 1;
		
		var defaults = {
		 	end: total,
			which: 0,
			prev: total,
			pause: 8000,
			fadeSpeed: 2000,
			transitionSpeed: 800
		};
			
		var options = $.extend(defaults, options);
				
		return this.each(function() {
			
			// setup variables + main containers
			var $frame = $(this).find('.frame').children(),
					$featureImages = $(this).find('.featureImages'),
					$image = $featureImages.children(),
					$link = $(this).find('.property_info').children(),
					rounds = 0;
			
			// init first item
			$frame.animate({opacity: 0, left: -50 }, 0).eq(options.which).animate({opacity: 1, left: 0 }, 0);
			
			// image init
			$image.eq(options.which).appendTo($featureImages);
			
			$image.eq(options.prev).before($image.eq(options.which)).animate({width: 0}, {duration: 0 })
			
			// link init
			$link.animate({opacity: 0, left: -2000 }, 0).eq(options.which).animate({opacity: 1, left: 67 }, 0);
			
			(function fadeit() {
				
				// message in the frame (top right)
				$frame.eq(options.prev)
					.animate({opacity: 0, left: 50}, options.transitionSpeed)
					.animate({left: -50 }, 0);
					
				$frame.eq(options.which).animate({opacity: 1, left: 0}, options.transitionSpeed);
				
				// wipe effect on the image (left to right)
				if (rounds !== 0) {
					$image.eq(options.which).animate({width: 947}, 0);
					$image.eq(options.prev).before($image.eq(options.which)).animate({width: 0}, {duration: options.transitionSpeed*2, easing: 'easeInOutCubic' });
				}
				
				rounds = 1;
				
				// Changing the link
				$link.eq(options.prev)
					.animate({opacity: 0, left: 134}, options.transitionSpeed)
					.animate({left: -2000 }, 0);
					
				$link.eq(options.which)
					.animate({left: 0}, 0)
					.animate({opacity: 1, left: 67}, options.transitionSpeed);
				
				options.which = (options.which == options.end) ? 0 : options.which+1;
				options.prev = (options.which == 0) ? options.end : options.which-1;
				
				
				setTimeout(fadeit, options.pause);
				
			})();
				
		});
	
	};
})(jQuery);
