(function($){
	$.fn.StoryStacker = function (options) {
		var settings = {
			'rotateDelay': 5000,
			'tabsSelector': '.ui-tabs-nav li a',
			'tabsOptions': {fx:{opacity: "toggle"}}
		};
		if (options)
			$.extend(settings, options);
		
		return this.each(function () {
			// unfortunately, the tabs' clicks don't bubble up, so in order to allow hovering to 
		    // restart, we have to attach our own click handler to the tabs and set a 
		    // flag to make sure no restarting occurs. Otherwise UI tabs() "forgets" that the
			// user had selected a tab and restarts on hoverout.
			var $this = $(this), notEnded = 1;
		    function startRotation() {
		    	notEnded && $this.tabs(settings.tabsOptions).tabs("rotate", settings.rotateDelay, false);
			}
		    function stopRotation() { $this.tabs("rotate", 0, false); }
		    function endRotation() {
		    	stopRotation();
		    	$this.unbind("hover");
	    		notEnded = 0;
		    }
		    $this.hover(stopRotation, startRotation);
			$(settings.tabsSelector, this).click(endRotation);
			$this.click(endRotation);
		    startRotation();
            $('.ui-tabs-panel .info', this).click(function () {
                location.href = $('a', this)[0].href;
            });
		});
	};
})(jQuery);
