/*
* vertical news ticker
* Tadas Juozapaitis ( kasp3rito@gmail.com )
* http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
* Extended by Abdelaziz Bennouna http://www.tellibus.com, based on
* comment from Mike Leber (http://www.jaxfront.com/)
* 2010/11/10 at 14:51
* 'I have done a little extension:
* if height is set to -1 the height of each element is calculated dynamically. Means not every entry has the same height!'
*/

(function($){
$.fn.vTicker = function(options) {
var defaults = {
speed: 700,
pause: 4000,
showItems: 3,
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('ul');

var clone = obj.children('li:first').clone(true);

if(options.height > 0)
{
height = obj.children('li:first').height();
}

/* Abdelaziz Bennouna (tellibus) 2011/03/07
* Computing new total height in case auto height is off
*/
if(options.height == -1) { newHeight = obj2.height() + obj.children('li').eq(options.showItems).height() - height + 'px'; obj2.animate({height: newHeight});}
/* End of Abdelaziz Bennouna (tellibus) 20110307 */

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)
{
/*
Comment from José
2011/03/02 at 00:27
Fix to JQuery 1.4.4 ->
Search line:
f.children("li:eq("+e.showItems+")").hide().fadeIn(e.speed)
" obj.children('li:eq(' + options.showItems + ')').hide().fadeIn(options.speed).show();"
Replace to:
f.children("li:eq("+e.showItems+")").fadeIn(e.speed)
" obj.children('li:eq(' + options.showItems + ')').fadeIn(options.speed);"
*/
obj.children('li:eq(' + options.showItems + ')').fadeIn(options.speed);
}
}

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();
}
};

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});

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);
}
/* Abdelaziz Bennouna (tellibus) 2011/03/07
* Total height for all items in case auto height is off
*/
else if(options.height == -1)
{
var totalHeight = 0;
for(i = 0; i < options.showItems; i++)
totalHeight += obj.children('ul').children('li').eq(i).height();
obj.height(totalHeight);
}
/* End of Abdelaziz Bennouna (tellibus) 20110307 */
else
{
obj.height(options.height);
}

var interval = setInterval(function(){
/* Mike Leber 2010/11/10 at 14:51
* The height of each element is calculated dynamically
*/
var currentHeight = maxHeight;
var ulObj = obj.children('ul');
if (options.direction == 'up') {
if (options.height == -1) {
currentHeight = ulObj.children('li:first').height();
}
moveUp(obj, currentHeight, options);
} else {
if (options.height == -1) {
currentHeight = ulObj.children('li:last').height();
}
moveDown(obj, currentHeight, options);
}
/* End of Mike Leber 20101110 */
}, options.pause);

if(options.mousePause)
{
obj.bind("mouseenter",function(){
options.isPaused = true;
}).bind("mouseleave",function(){
options.isPaused = false;
});
}
});
};
})(jQuery);
