/*
 * JQuery text scrolling plugin
 * 
 * Version 1.0
 * 
 * by Christian Bruun - 2011
 * 
 * Like it/use it? Send me an e-mail: rockechris@rockechris.com
 * 
 * License: None. Use and abuse. Comes with no warranty, of course!
 * 
 * 
 * Options:
 * text:		Array of texts to display			Default: ''
 * duration:	Animation duration					Default: 500 ms
 * pause:		Time to display each text			Default: 2500 ms
 * easing:		Easing to use on animations			Default: "easeOutElastic" if jQuery.ui is loaded, or "swing"
 * align:		Alignment used on texts				Default: 'center'
 * 
 */
(function($) {
	$.fn.flipShow = function(options) {
		var defaults = {
			text:		'',
			duration:	500,
			pause:		2500,
			easing:		jQuery.ui ? 'easeOutElastic' : 'swing',
			align:		'center'
		}
		var opts = $.extend(defaults, options);
		
		return this.each(function() {
			if(opts.text.constructor.toString().match(/array/i) != null && typeof(opts.text.length) != 'undefined') {
				var flipShowParent = this, h = $(this).height(), w = $(this).width(), i = 0;
				
				// Hijack $this
				$(flipShowParent).html('').append('<div id="flipShowChild"></div>');
				
				// fill flipShowChild
				i = opts.text.length-1;
				for(i; i >= 0 ; i--) {
					$('#flipShowChild').append('<div class="flipShowText">' + opts.text[i] + '</div>');
					if($('.flipShowText').last().width()  > w) { w = $('.flipShowText').last().width(); }
					if($('.flipShowText').last().height() > h) { h = $('.flipShowText').last().height(); }
				}
				var tmpHeight = 0 - (opts.text.length * h);
				
				// the animation
				var flip = function() {
					if(i >= opts.text.length) {
						$('#flipShowChild').css({ 'top': tmpHeight + 'px' });
						i = 0;
					}
					$('#flipShowChild').animate({ top: '+='+h }, opts.duration, opts.easing, function() { setTimeout(flip, opts.pause); });
					i++;
				};
				
				// Start animation
				i = 0;
				$(flipShowParent).css({ 'position': 'relative', 'minHeight': h, 'minWidth': w, 'overflow': 'hidden', 'textAlign': opts.align });
				$('#flipShowChild').css({ 'position': 'absolute', 'width': '100%', 'top': tmpHeight + 'px' });
				
				if($.support.leadingWhitespace) { setTimeout(flip, opts.pause); }
				else {
					// IE = Satan
					$(window).bind('load',function () {
						$('.flipShowText').each(function() {
							if($(this).height() > h) { h = $(this).height(); }
							if($(this).width()  > w) { w = $(this).width(); }
						});
						tmpHeight = 0 - (opts.text.length * h);
						$(flipShowParent).css({ 'height': h, 'width': w });
						$('#flipShowChild').css({ 'top': tmpHeight + 'px' });
						setTimeout(flip, opts.pause);
					});
				}
			}
		});
	}
})(jQuery); 

