var resultArray=new Array();
var actualPage = 0;
var nextInactive = false;
var sizeAll = 0;
var actualPageVertical = 0;
	  
jQuery(document).ready(function($) {
		jQuery('.newscarousel-prev').addClass('notactive'); 
		function newscarousel_initCallback(carousel) {
			$(".newscarouselverticalBox .jcarousel-container-vertical").height("280");
			$(".newscarouselverticalBox .jcarousel-container-vertical").width("auto");
			$(".newscarouselverticalBox .jcarousel-clip-vertical").height("280px");
			jQuery('.newscarousel-next').bind('click', function() {
				
				if(thisCarousel.animating || nextInactive) return false;
				actualPage = actualPage + 1; 
				newscarousel_itemCallback();
				$('#loader').show();
				jQuery('.newscarousel-prev').removeClass('notactive'); 
				return false;
			});
		
			jQuery('.newscarousel-prev').bind('click', function() {
				thisCarousel = $('.newscarousel').data('jcarousel');
				if(thisCarousel.animating) return false;
				actualPage = actualPage - 1;
				if(actualPageVertical<1){
					jQuery('.newscarousel-prev').addClass('notactive');
				}
				if(actualPage<0){
					actualPage=0;
				}
				else{
					jQuery('.newscarousel-next').removeClass('notactive'); 				
					newscarousel_Prev();
				}
				return false;
			});			
		};

	
	function newscarousel_itemCallback()
	{	
		thisCarousel = $('.newscarousel').data('jcarousel');
		thisCarousel.lock();
		var sorting1			=  $('.sorting1').val();
		var verein				=  $('.verein').val();
		var limit				=  $('.limit').val();
		var backPid				=  $('.backPid').val();
		var tpyeOfNewsToView	=  $('.tpyeOfNewsToView').val();
		
		$.getJSON("index.php?id=1&eID=user_newsaddons_items&page="+actualPage+"&limit="+limit+"&backPid="+backPid+"&tpyeOfNewsToView="+tpyeOfNewsToView+"&sorting1="+sorting1+"&verein="+verein+"",
		   function(events){			  
		   	   if(events){
		   	   	   newscarousel_itemAddCallback(thisCarousel,thisCarousel.first, thisCarousel.last, events);
		   	   }
		   	   else{
		   	   	   jQuery('.outerslidenewsbox3images').html('<span class="error">Keine Daten vorhanden</span>');
		   	   	   jQuery('.outerslidenewsbox2images').html('<span class="error">Keine Daten vorhanden</span>');
		   	   }
			}
		);            
	};
	
	function newscarousel_itemAddCallback(carousel,first, last, events){
		var howManyResults = $(events).size();
		var limit		 =  $('.limit').val();
		var startIdx = (actualPage*limit);
		if(howManyResults>0){
			$.each(events,function(i) {
				var index = startIdx+i+1;
				if(sizeAll == index ){
					jQuery('.newscarousel-next').addClass('notactive');
					nextInactive = true;
				}
				else{
					jQuery('.newscarousel-next').removeClass('notactive');
				}
				
				
				carousel.add(index, newscarousel_getItemHTML(this, index));
			});
			carousel.size(startIdx+howManyResults);
			carousel.unlock();
			if(actualPage>0){ 
				$('#loader').show();
				carousel.next();
			}
		}
		else{
			jQuery('.newscarousel-next').addClass('notactive');
			actualPage = actualPage - 1; 
			carousel.unlock();
		}

		if(!hasNextItem){
			jQuery('.newscarousel-next').addClass('notactive');
			nextInactive = true;
		}
		
		//patch for using with chrome too... 
		carousel.reload();
		//end patch 
		$('#loader').hide();
		
		
		
		
	}
	
	function newscarousel_Prev(){
		jQuery('.outerslidenewsbox2images .slideNext').show();
		jQuery('.outerslidenewsbox3images .slideNext').show();
		nextInactive = false;
		carousel = $('.newscarousel').data('jcarousel');
		carousel.prev();
		$('#loader').hide();
	}
	
	
	/**
	 * Item html creation helper.
	 */
	function newscarousel_getItemHTML(item, thisIndex)
	{
		hasNextItem = item.hasNextItem;
		link = item.url;
		returnString='<a href="'+link+'" title="'+item.title+'" style="padding:0px;">'+item.image+'</a><br /><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>';
		//returnString='<img src="uploads/pics/'+item.image+'" width="200px" height="110px"/>';
		return returnString;
	};
	
   $("<img />").attr("src", "typo3conf/ext/user_newsaddons/Resources/Public/Images/ajax-loader.gif").appendTo("#loader");
   
   var limit = $('.limit').val();

	$('.newscarousel').jcarousel({
		scroll: limit,
		size: 0,
		visible: limit,
		animation: 1500,
		easing: "swing",
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback:newscarousel_initCallback
	});
	newscarousel_itemCallback();
});
