﻿/* 
* 文本新闻滚动显示
* $('#tbNews').vzNewsScroll({divWidth:400,divHeight:16,delay:220});
*/
	(function($){
		var ELMS = [];
		$.fn.vzNewsScroll = function(settings) {
			settings = $.extend({}, arguments.callee.defaults, settings);
			$(this).css({"position":"relative","overflow":"hidden","width":settings.divWidth,"height":settings.divHeight});
			$(this).find("ul").css({"position":"relative","list-style-type":"none"});
			$(this).each(function(){
				this.$settings	= settings;
				this.$pause = false;
				this.$counter	= settings.beginTime;
				$(this).hover(function(){ $(this).vzNewsScrollPause(true) }, function(){ $(this).vzNewsScrollPause(false) });
				ELMS.push(this);
			});
			return this;
		};
		$.fn.vzNewsScroll.defaults = {
			beginTime: 10,
			fontSize: '12px',
			divWidth: '100%',
			divHeight: '200px',
			lineHeight: '1.5em',
			delay:	 120,
			step:	2
		};
		$.fn.vzNewsScrollPause = function(pause) {
			return this.each(function() {
				this.$pause = pause;
			});
		}
		$.fn.outerHeight=function(options) {
			if (!this[0]) 0;
			options = $.extend({ margin: false }, options || {});
			return this[0] == window || this[0] == document ?
				this.height() : this.is(':visible') ?
					this[0].offsetHeight + (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0) :
					this.height() 
						+ num(this,'borderTopWidth') + num(this, 'borderBottomWidth') 
						+ num(this, 'paddingTop') + num(this, 'paddingBottom')
						+ (options.margin ? (num(this, 'marginTop') + num(this, 'marginBottom')) : 0);
		}

		setInterval(scroll, 80);
		function scroll() {
			for (var i = 0; i < ELMS.length; i++) {
				var elm = ELMS[i];
				if (elm && !elm.$pause) {
					if (elm.$counter == 0) {
						var ul	 = $('> ul', elm)[0];
						if (!elm.$steps) {
							elm.$steps = $('> li:first-child', ul).outerHeight();
							elm.$step	= 0;
						}
						if ((elm.$steps + elm.$step) <= 0) {
							elm.$counter	 = elm.$settings.delay;
							elm.$steps		 = false;
							$(ul).css('top', '0').find('> li:last-child').after($('> li:first-child', ul));
							$('> *', ul).not('li').remove();
						} else {
							elm.$step -= elm.$settings.step;
							if (-elm.$step > elm.$steps) {
								elm.$step = -elm.$steps;
							}
							ul.style.top = elm.$step + 'px';
						}
					} else {
						elm.$counter--;
					}
				}
			}
		};
	})(jQuery);
