/*
 * jQuery JavaScript Library Marquee Plus 1.0
 * http://mzoe.com/
 * Copyright (c) 2009 MZOE
 * Dual licensed under the MIT and GPL licenses.
 * Date: 2009-05-13 18:54:21
 */
(function($) {
	$.fn.marquee = function(o) {
		//获取滚动内容内各元素相关信息
		o = $.extend({
			speed:		parseInt($(this).attr('speed')) || 30, // 滚动速度
			step:		parseInt($(this).attr('step')) || 1, // 滚动步长
			direction:	$(this).attr('direction') || 'up', // 滚动方向
			pause:		parseInt($(this).attr('pause')) || 1000 // 停顿时长
		}, o || {});
		var dIndex = jQuery.inArray(o.direction, ['right', 'down']);
		if (dIndex > -1) {
			o.direction = ['left', 'up'][dIndex];
			o.step = -o.step;
		}
		var mid;
		var div = $(this); // 容器对象
		var divWidth = div.innerWidth(); // 容器宽
		var divHeight = div.innerHeight(); // 容器高
		var ul = $("ul", div);
		var li = $("li", ul);
		var liSize = li.size(); // 初始元素个数 alert("li size: " + liSize);
		var liWidth = li.width(); // 元素宽
		var liHeight = $("li:first", ul).height(); // 元素高
		var width = liWidth * liSize;
		var height = liHeight * liSize;
		/*var width=0, height=0;
		li.each(function(){width += $(this).width();height+= $(this).height();});*/
		//alert("liSize:"+liSize+",liWidth:"+liWidth+",liHeight:"+liHeight);
		if ((o.direction == 'left' && width > divWidth) || 
			(o.direction == 'up' && height > divHeight)) {
			// 元素超出可显示范围才滚动
			if (o.direction == 'left') {
				ul.width(2 * liSize * liWidth);
				if (o.step < 0) div.scrollLeft(width);
			} else {
				ul.height(2 * liSize * liHeight);//
				if (o.step < 0) div.scrollTop(height);
			}
			ul.append(li.clone()); // 复制元素
			mid = setInterval(_marquee, o.speed);
			div.hover(
				function(){clearInterval(mid);},
				function(){mid = setInterval(_marquee, o.speed);}
			);
		}
		function _marquee() {// 滚动
			if (o.direction == 'left') {
				var l = div.scrollLeft();
				if (o.step < 0) {
					div.scrollLeft((l <= 0 ? width : l) + o.step);
				} else {
					div.scrollLeft((l >= width ? 0 : l) + o.step);
				}
				if (l % liWidth == 0) _pause();
			} else {
				var t = div.scrollTop();
				if (o.step < 0) {
					div.scrollTop((t <= 0 ? height : t) + o.step);
				} else {
					div.scrollTop((t >= height ? 0 : t) + o.step);
				}
				if (t % liHeight == 0) _pause();
			}
		}
		function _pause() {// 停顿
			if (o.pause > 0) {
				var tempStep = o.step;
				o.step = 0;
				setTimeout(function() {o.step = tempStep;}, o.pause);
			}
		}
	};
})(jQuery);


//滚动插件
(function($){
$.fn.extend({
	Scroll:function(opt,callback){
		//参数初始化
		if(!opt) var opt={};
		var _this=this.eq(0).find("ul:first");
		var	lineH=_this.find("li:first").height(), //获取行高
			line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10), //每次滚动的行数，默认为一屏，即父容器高度
			speed=opt.speed?parseInt(opt.speed,10):500, //卷动速度，数值越大，速度越慢（毫秒）
			timer=opt.timer?parseInt(opt.timer,10):3000; //滚动的时间间隔（毫秒）
		if(line==0) line=1;
		var upHeight=0-line*lineH;
		//滚动函数
		scrollUp=function(){
			_this.animate({marginTop:upHeight},speed,function(){
				for(i=1;i<=line;i++){_this.find("li:first").appendTo(_this);}
				_this.css({marginTop:0});
			});
		}
		//鼠标事件绑定
		_this.hover(function(){clearInterval(timerID);},function(){timerID=setInterval("scrollUp()",timer);}).mouseout();
	}
})
})(jQuery);

/*
* Tadas Juozapaitis ( kasp3rito@gmail.com )
*/
(function($){
$.fn.vTicker = function(options) {
	var defaults = {
		speed: 700,
		pause: 4000,
		showItems: 3,
		animation: '',
		mousePause: true,
		isPaused: false
	};

	var options = $.extend(defaults, options);

	moveUp = function(obj2, height){
		if(options.isPaused)return;
		var obj = obj2.children('ul');
		first = obj.children('li:first').clone(true);
		obj.animate({top: '-=' + height + 'px'}, options.speed, function() {
			$(this).children('li:first').remove();
			$(this).css('top', '0px');
		});
		if(options.animation == 'fade') {
			obj.children('li:first').fadeOut(options.speed);
			obj.children('li:last').hide().fadeIn(options.speed);
		}
		first.appendTo(obj);
	};

	return this.each(function() {
		var obj = $(this);
		var maxHeight = 0;
		obj.css({overflow: 'hidden', position: 'relative'})
			.children('ul').css({position: 'absolute', margin: 0, padding: 0})
			//.children('li').css({margin: 0, padding: 0})
			;
		obj.children('ul').children('li').each(function(){
			if($(this).height() > maxHeight) maxHeight = $(this).height();
		});
		obj.children('ul').children('li').each(function(){$(this).height(maxHeight);});
		obj.height(maxHeight * options.showItems);
		var interval = setInterval(function(){ moveUp(obj, maxHeight); }, options.pause);
		if(options.mousePause) {
			obj.bind("mouseenter",function(){
				options.isPaused = true;
			}).bind("mouseleave",function(){
				options.isPaused = false;
			});
		}
	});
};
})(jQuery);

