var resultArray=new Array();
var actualPageVertical = 0;
var nextInactiveVertical = false;
var sizeAll = 0;
	  
jQuery(document).ready(function($) {		
		
	function newscarouselvertical_initCallback(carousel) {
		$(".newscarouselverticalBox .jcarousel-container-vertical").height("280");
		$(".newscarouselverticalBox .jcarousel-container-vertical").width("auto");
		$(".newscarouselverticalBox .jcarousel-clip-vertical").height("280px");

		jQuery('.mycarouselvertical-prev a').addClass('notactive'); 
        
       jQuery('.mycarouselvertical-next').bind('click', function() {
			if(thisVerticalCarousel.animating || nextInactiveVertical) return false;
			jQuery('.mycarouselvertical-prev').fadeTo('fast', 1);
			actualPageVertical = actualPageVertical + 1;
			$('.loadervertical').show();
			newscarouselvertical_itemCallback();
			jQuery('.mycarouselvertical-prev a').removeClass('notactive'); 

			return false;
		});
	
		jQuery('.mycarouselvertical-prev').bind('click', function() {
			thisVerticalCarousel = $('.newscarouselvertical').data('jcarousel');
			if(thisVerticalCarousel.animating) return false;
			actualPageVertical = actualPageVertical - 1;
			if(actualPageVertical<1){
				jQuery('.mycarouselvertical-prev a').addClass('notactive');
			}			
			if(actualPageVertical<0){
				jQuery('.mycarouselvertical-prev a').addClass('notactive');
				actualPageVertical=0;
			}
			else{
				jQuery('.mycarouselvertical-next a').removeClass('notactive'); 
				newscarouselvertical_Prev();
			}
			return false;
		});
		
	};

	
	function newscarouselvertical_itemCallback()
	{
		thisVerticalCarousel = $('.newscarouselvertical').data('jcarousel');
		thisVerticalCarousel.lock();
		var sorting1 	 		=  $('.sortingVertical').val();
		var verein 				=  $('.vereinVertical').val();
		var limit		 		=  $('.limitVertical').val();
		var visualiationtype	=  $('.visualiationtypeVertical').val();
		var backPid				=  $('.backPid').val();
		var tpyeOfNewsToView	=  $('.tpyeOfNewsToViewVertical').val();		
		
		$.getJSON("index.php?id=1&eID=user_newsaddons_items&pageVertical="+actualPageVertical+"&limit="+limit+"&sorting1="+sorting1+"&verein="+verein+"&tpyeOfNewsToView="+tpyeOfNewsToView+"&backPid="+backPid+"&visualiationtype="+visualiationtype+"",
		   function(events){
		   	   if(events){
		   	   	   newscarouselvertical_itemAddCallback(thisVerticalCarousel, thisVerticalCarousel.first, thisVerticalCarousel.last,events);
		   	   }
		   	   else{
		   	   	   jQuery('.newscarouselverticalBox').html('<span class="error">Keine Daten vorhanden</span>');
		   	   }
			}
		);            
	};

	
	function newscarouselvertical_itemAddCallback(carouselvertical, first, last, events){
		var howManyResults = $(events).size();
		var limit		 =  $('.limitVertical').val();
		var startIdx = (actualPageVertical*limit);
		if(howManyResults>0){
			$.each(events,function(i) {
				var index = startIdx+i+1;
				if(sizeAll == index ){
					jQuery('.mycarouselvertical-next a').addClass('notactive');
					nextInactiveVertical = true;
				}
				else{
					jQuery('.mycarouselvertical-next a').removeClass('notactive');
				}
				
				carouselvertical.add(index, newscarouselvertical_getItemHTML(this, index));
			});
			carouselvertical.size(startIdx+howManyResults);			
			carouselvertical.unlock();
			if(actualPageVertical>0){ 
				$('.loadervertical').show();
				carouselvertical.next();
			}
		}
		else{
			jQuery('.mycarouselvertical-next a').addClass('notactive');
			nextInactiveVertical = true;
			actualPageVertical = actualPageVertical - 1; 
			carouselvertical.lock();
		}
		if(!hasNextItem){
			jQuery('.mycarouselvertical-next a').addClass('notactive');
			nextInactiveVertical = true;
		}
		
		//patch for using with chrome too... 
		carouselvertical.reload();
		//end patch 
		$('.loadervertical').hide();
	}
	
	function newscarouselvertical_Prev(){
		jQuery('.mycarouselvertical-next').fadeTo('fast', 1);
		jQuery('.mycarouselvertical-next a').show();
		nextInactiveVertical = false;
		carouselvertical = $('.newscarouselvertical').data('jcarousel');
		carouselvertical.prev();
		$('.loadervertical').hide();
	}
	
	
	/**
	 * Item html creation helper.
	 */
	function newscarouselvertical_getItemHTML(item, thisIndex)
	{
		hasNextItem = item.hasNextItem;
		link = item.url;
		returnString='<a href="'+link+'" title="'+item.title+'" style="padding:0px;margin-right:10px;float: left;">'+item.image+'</a><span class="slideNewsTitle"><a href="'+link+'" title="'+item.title+'" style="padding:0px;">'+item.title+'</a></span><br /><span class="slideNewsBodytext">'+item.datetime+' '+item.bodytext+'<a href="'+link+'" title="'+item.title+'" style="padding:0px;">  deplü</a></span>';
		return returnString;
	};
	


		
   $("<img />").attr("src", "typo3conf/ext/user_newsaddons/Resources/Public/Images/ajax-loader.gif").appendTo(".loadervertical");
   
   
   var limit = $('.limitVertical').val();
   $('.newscarouselvertical').jcarousel({
   	   vertical: true,   		   
	   scroll: limit,
	   size: 0,
	   offset: 1,
	   visible: limit,
	   animation: 1800,
	   easing: "swing",
	   // This tells jCarousel NOT to autobuild prev/next buttons
	   buttonNextHTML: null,
	   buttonPrevHTML: null,
	   initCallback:newscarouselvertical_initCallback

   });
   newscarouselvertical_itemCallback();
});
