﻿// jQuery object to sequence a list of tagline items using a drop-bounce effect
// Selects an item at random from the child elements as the starting point
// and then proceeds sequentially through the items.
//
// Typical Use:
// <ul id="taglineItems">
//     <li>Line 1</li>
//     <li>Line 2</li>
// </ul>
//
// $('#taglineItems').taglineSequencer();
//
(function ($) {
	jQuery.fn.taglineSequencer = function (settings) {
		// configuration settings
		//
		settings = jQuery.extend({
			initTime: 500,
			dropTime: 1500,
			delayTime: 4000,
			exitTime: 400
		}, settings);

		$.fn.initTime = function () {
			return settings.initTime;
		};

		$.fn.dropTime = function () {
			return settings.dropTime;
		};

		$.fn.delayTime = function () {
			return settings.delayTime;
		};

		$.fn.exitTime = function () {
			return settings.exitTime;
		};

		$.fn.nextTaglineItem = function () {
			if (++itemIndex >= itemLength) {
				itemIndex = 0;
			}
			$('.rotatingItems').animate({ 'top': '100px' }, settings.exitTime, 'jswing', function () {
				$('.rotatingItems').css('top', '-80px');
				$('.rotatingItems').html(baseElement.children(':eq(' + itemIndex + ')').html());
				$(this).animateTaglineItem();
			});
		}

		$.fn.endTaglineItem = function () {
			setTimeout($(this).nextTaglineItem, settings.delayTime);
		}

		$.fn.animateTaglineItem = function () {
			$('.rotatingItems').animate({ 'top': '20px' }, settings.dropTime, 'easeOutBounce', $(this).endTaglineItem);
		}

		$.fn.createEffectArea = function () {
			baseElement.after('<div class=\'rotatingContainer\'><div class=\'rotatingItems\'></div></div>');
		}

		var baseElement = $(this);
		var itemIndex = 0;
		var itemLength = $(this).children().length;
		if (itemLength > 0) {
			$(this).createEffectArea();
			itemIndex = Math.floor(Math.random() * itemLength);
			$('.rotatingItems').html(baseElement.children(':eq(' + itemIndex + ')').html());
			setTimeout($(this).animateTaglineItem, settings.initTime);
		}
	};

	return $(this);
})(jQuery);

