/*
* vertical news ticker
* Tadas Juozapaitis ( kasp3rito@gmail.com )
* http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
*
* update paetrik - dynamic load content from server
*/
(function($){
$.fn.vTicker = function(options) {
	var defaults = {
		speed: 700,
		pause: 4000,
		showItems: 6,
		animation: '',
		mousePause: true,
		isPaused: false,
		direction: 'up',
		height: 0,
    id: 0,
    time: 0,
    url: null
	};

	var options = $.extend(defaults, options);

	moveUp = function(obj2, height, options){
		if(options.isPaused)
			return;
		
		var obj = obj2.children('ul');
    var clone = obj.children('li:first').clone(true);
		
		if(options.height > 0){
			height = obj.children('li:first').height();
		}		
		
    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);
			if(options.height == 0){
			  obj.children('li:eq(' + options.showItems + ')').hide().fadeIn(options.speed).show();
			}
		}

    clone.appendTo(obj);
	};
	
	moveDown = function(obj2, height, options){
		if(options.isPaused)
			return;
		
		var obj = obj2.children('ul');
    var clone = obj.children('li:last').clone(true);
		
		if(options.height > 0){
			height = obj.children('li:first').height();
		}
		
		obj.css('top', '-' + height + 'px').prepend(clone);
			
    obj.animate({top: 0}, options.speed, function() {
      $(this).children('li:last').remove();
    });
		
		if(options.animation == 'fade'){
			if(options.height == 0){
				obj.children('li:eq(' + options.showItems + ')').fadeOut(options.speed);
			}
			obj.children('li:first').hide().fadeIn(options.speed).show();
		}
	};

	updateFromServer = function(obj2, height, options){
		if(options.isPaused)
			return;

    if(options.url != null){
      $.getJSON(options.url, {'id': options.id, 'time': options.time}, function(data){
        for (var i=0; i<data.length; i++){
          var obj = obj2.children('ul');

          if (!$('#'+data[i]['id']+obj2.attr('id')).length > 0){
            obj.prepend('<li id=\''+data[i]['id']+obj2.attr('id')+'\'><p>'+data[i]['string']+'</p></li>').children('li:first').css('display', 'none');

            obj.children('li:first').slideDown(options.speed);
            
            if(options.animation == 'fade'){
              if(options.height == 0){
                
              }
              obj.children('li:first').hide().fadeIn(options.speed).show();
            }

            if (obj.children('li').size() > options.showItems){
              obj.children('li:last').fadeOut(options.speed, function(){
                obj.children('li:last').remove();
                obj.children('li:last').children('p').css('border', 'none');
              });
            }
            options.id = data[i]['id'];
            options.time = data[i]['time'];
          } 
        }
      });
    }
	};
	
	return this.each(function() {
		var obj = $(this);
		var maxHeight = 0;

		obj.css({/*overflow: 'hidden', position: 'relative'*/})
			.children('ul').css({/*margin: 0, padding: 0*/})
			.children('li').css({/*margin: 0, padding: 0*/});

    /*
		if(options.height == 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);
		}else{
			obj.height(options.height);
		}
    */
		
    var interval = setInterval(function(){ 
			if(options.direction == 'up'){ 
				moveUp(obj, maxHeight, options); 
			}else if('json'){ 
        updateFromServer(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);

