
(function($){

 	$.fn.extend({

 		newsTickerByDG: function() {
             if(this.length<1) return false;
             $(this).wrap('<div class="news-postsWrapper" />');
             var wrapper=$(".news-postsWrapper");
             wrapper.wrap('<div class="news-controlWrapper" />');
             var newsContainer=wrapper.children("ul");
             var controlWrapper=$(".news-controlWrapper");
             var allChildren=newsContainer.children();
             var t;
             controlWrapper.append('<b class="next"></b><b class="prev"></b>');

             function startTimer(){
                 t = setInterval(function() {
                     moveToNext(newsContainer,allChildren);
                 }, 4800);
             }
             controlWrapper.children(".next").click(function(){
                 moveToNext(newsContainer,allChildren);
                 clearTimeout(t);
             });
             controlWrapper.children(".prev").click(function(){
                 moveToPrev(newsContainer,allChildren);
                 clearTimeout(t);
             });


             startTimer();
             function moveToNext(news,allChl){
                 var active=news.children(".actv");
                 if(active.length!=1){
                     active=allChl.eq(0);
                 }
                 allChl.removeClass("actv");
                 var index=allChl.index(active);
                 var next;
                 if( index>=(allChl.length-1) ){
                      next=allChl.eq(0);
                 }else{
                      next=active.next();
                 }
                 next.addClass('actv');

                 var position = next.position();
                 var h=next.height()+10;
                 news.animate({top: -parseInt(position.top)});
                 //news.parent().animate({height: h}) // OPCJONALNIE
             }
             function moveToPrev(news,allChl){
                 var active=news.children(".actv");
                 if(active.length!=1){
                     active=allChl.eq(0);
                 }
                 allChl.removeClass("actv");
                 var index=allChl.index(active);
                 var prev;
                 if( index<=0 ){
                      prev=allChl.eq(allChl.length-1);
                 }else{
                      prev=active.prev();
                 }
                 prev.addClass('actv');

                 var position = prev.position();
                 var h=prev.height()+10;
                 news.animate({top: -parseInt(position.top)})
                 //news.parent().animate({height: h}) // OPCJONALNIE
             }
  	    }
	});
})(jQuery);
