/*
* Tadas Juozapaitis ( kasp3rito@gmail.com )
*/

(function($){
$.fn.vTicker = function(options) {
	var defaults = {
		speed: 800,
		pause: 8000,
		showItems: 1,
		animation: '',
		mousePause: false,
		direction: 'v',
		width: 230,
		mniHeight: 150,
		marginleft: 5
	};

	var options = $.extend(defaults, options);

	moveUp = function(obj, height){
		obj = obj.children('ul');
    	first = obj.children('li:first').clone(true);
		width = options.width;
		marginleft = options.marginleft;
		
    	if(options.direction == 'h') {
	    	obj.animate({left: '-=' + width + 'px'}, options.speed, function() {
	        	$(this).children('li:first').remove();
	        	$(this).css('left', marginleft + 'px');
	        });
    	} else if (options.direction == 'v') {
	    	obj.animate({top: '-=' + height + 'px'}, options.speed, function() {
	        	$(this).children('li:first').remove();
	        	$(this).css('top', '0px');
	        	$(this).css('left', marginleft + 'px');
	        });
		}
		
		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() {
		obj = $(this);
		maxHeight = 0;
		minHeight = options.minHeight;
		marginleft = options.marginleft;

		obj.css({overflow: 'hidden', position: 'relative'})
			.children('ul').css({position: 'absolute', margin: 0, padding: 0});

		if(options.direction == 'h') {
			obj.children('ul').children('li').css({margin: 0, padding: 0, float: 'left', display: 'block'});				
		} else {
			obj.children('ul').children('li').css({margin: 0, padding: 0});
		}
		
		obj.children('ul').children('li').each(function(){
			if($(this).height() > maxHeight)
			{
				maxHeight = $(this).height();
			}
		});

		if(maxHeight < minHeight) {
			maxHeight = minHeight;
		}
		
		obj.children('ul').children('li').each(function(){
			$(this).height(maxHeight);
		});

		obj.height(maxHeight * options.showItems);
		
    	interval = setInterval('moveUp(obj, maxHeight)', options.pause);
		
		if(options.mousePause)
		{
			obj.bind("mouseenter",function(){
				clearInterval(interval);
			}).bind("mouseleave",function(){
				interval = setInterval('moveUp(obj, maxHeight)', options.pause);
			});
		}
	});
};
})(jQuery);