/*
 * vertical news ticker
 * Tadas Juozapaitis ( kasp3rito@gmail.com )
 * http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
 */
(function($) {
	$.fn.vTicker = function(options) {
		var defaults = {
			speed : 800,
			pause : 2000,
			showItems : 2,
			animation : '',
			mousePause : true,
			isPaused : false,
			direction : 'up',
			height : 0
		};

		var options = $.extend(defaults, options);

		moveUp = function(obj2, height, options) {
			if (options.isPaused)
				return;

			var obj = obj2.children('dl');

			var clone = obj.children('dt:first').clone(true);

			if (options.height > 0) {
				height = obj.children('dt:first').height();
			}

			obj.animate( {
				top : -height
			}, options.speed, function() {
				$(this).children('dt:first').remove();
				$(this).css('top', '0px');
			});

			if (options.animation == 'fade') {
				obj.children('dt:first').fadeOut(options.speed);
				if (options.height == 0) {
					obj.children('dt:eq(' + options.showItems + ')').hide()
							.fadeIn(options.speed).show();
				}
			}

			clone.appendTo(obj);
		};

		moveDown = function(obj2, height, options) {
			if (options.isPaused)
				return;

			var obj = obj2.children('dl');

			var clone = obj.children('dt:last').clone(true);

			if (options.height > 0) {
				height = obj.children('dt:first').height();
			}

			obj.css('top', '-' + height + 'px').prepend(clone);

			obj.animate( {
				top : 0
			}, options.speed, function() {
				$(this).children('dt:last').remove();
			});

			if (options.animation == 'fade') {
				if (options.height == 0) {
					obj.children('dt:eq(' + options.showItems + ')').fadeOut(
							options.speed);
				}
				obj.children('dt:first').hide().fadeIn(options.speed).show();
			}
		};

		return this.each(function() {
			var obj = $(this);
			var maxHeight = 0;

			obj.css( {
				overflow : 'hidden',
				position : 'relative'
			}).children('dl').css( {
				position : 'absolute',
				margin : 0,
				padding : 0
			}).children('dt').css( {
				margin : 0,
				padding : 0
			});

			if (options.height == 0) {
				obj.children('dl').children('dt').each(function() {
					if ($(this).height() > maxHeight) {
						maxHeight = $(this).height();
					}
				});

				obj.children('dl').children('dt').each(function() {
					$(this).height(maxHeight);
				});

				obj.height(maxHeight * options.showItems);
			} else {
				obj.height(options.height);
			}

			var interval = setInterval(function() {
				if (options.direction == 'up') {
					moveUp(obj, maxHeight, options);
				} else {
					moveDown(obj, maxHeight, options);
				}
			}, options.pause);

			if (options.mousePause) {
				obj.bind("mouseenter", function() {
					options.isPaused = true;
				}).bind("mouseleave", function() {
					options.isPaused = false;
				});
			}
		});
	};
})(jQuery);
