jQuery(document).ready(function($){
	$('.newsticker').each(function(){
		$(this).newsticker();
	});
	$('.carousel-news').each(function(i){
		$(this).children('ul').attr('id','newsslider' + i);
	});
	$('.carousel-news-auto ul').jcarousel({
		auto: 10, // Hastighed mellem billedskift //
		scroll:1, // Skiftehastighed //
        	wrap: 'last',
		initCallback: buildButtonsAuto,
		itemLastInCallback: updateButtonsAuto
	});
	$('.carousel-news ul').jcarousel({
		scroll: 1,
		initCallback: buildButtons,
		itemLastInCallback: updateButtons
		//wrap: 'circular'
	});
	
	
	
	$('.tx-rgtabs-pi1').each(function(){
		$(this).find('h4:empty').each(function(){
			$(this).next('div').remove();
			$(this).remove();
		});
		$(this).find('h4').next('div:empty').each(function(){
			$(this).prev('h4').remove();
			$(this).remove();
		}); 
	});
	
	
});


function buildButtons(carousel, state) {
	// Build buttons
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);
	var buttonsNo = carousel.options.size;
	var howMany = carousel.options.scroll;
	
//	console.log(buttonsNo + ' ' + howMany);
	
	if(buttonsNo > howMany){
	
		// Find containing div, and make a ul inside
		carObj.parents('.carousel-news').append('<div class="jcarousel-control ' + carId + '"></div>');
		
		// Append controls to div
		for(button = 1; button <= buttonsNo; button = button+howMany){
			jQuery('.' + carId).append('<a href="#"><span>'+(button)+'</span>&nbsp;</a>');
		}
		// Bind click function to buttons
	 	jQuery('.' + carId + ' a').bind('click', function(event) {
	 		event.preventDefault();
			//var scrollTo = jQuery.jcarousel.intval(jQuery(this).text());
			var scrollTo = $.jcarousel.intval(jQuery(this).text());
			carousel.scroll(scrollTo);
		    return false;
		});
	} else {
		carObj.parents('.carousel-news').find('.jcarousel-prev, .jcarousel-next').hide();
	}
}

function updateButtons(carousel, item, idx, state) {
	// Update the position of the buttons
	var position = Math.ceil(idx / carousel.options.scroll);
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);

//console.log(position + ' ' + idx);
	
	// Find the div, then find controls for that div and then update the active button
	carObj.parents('.carousel-news').find('.jcarousel-control a').removeClass('active').eq(position-1).addClass('active');
}

function buildButtonsAuto(carousel, state) {
	// Build buttons
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);
	var buttonsNo = carousel.options.size;
	var howMany = carousel.options.scroll;
	
//	console.log(buttonsNo + ' ' + howMany);
	
	if(buttonsNo > howMany){
	
		// Find containing div, and make a ul inside
		carObj.parents('.carousel-news-auto').append('<div class="jcarousel-control ' + carId + '"></div>');
		
		// Append controls to div
		for(button = 1; button <= buttonsNo; button = button+howMany){
			jQuery('.' + carId).append('<a href="#"><span>'+(button)+'</span>&nbsp;</a>');
		}
		// Bind click function to buttons
	 	jQuery('.' + carId + ' a').bind('click', function(event) {
	 		event.preventDefault();
			//var scrollTo = jQuery.jcarousel.intval(jQuery(this).text());
			var scrollTo = $.jcarousel.intval(jQuery(this).text());
			carousel.scroll(scrollTo);
			carousel.startAuto(0);
		    return false;
		});
	} else {
		carObj.parents('.carousel-news-auto').find('.jcarousel-prev, .jcarousel-next').hide();
	}
	
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


function updateButtonsAuto(carousel, item, idx, state) {
	// Update the position of the buttons
	var position = Math.ceil(idx / carousel.options.scroll);
	var carId = carousel.list.context.id;
	var carObj = jQuery('#' + carId);

//console.log(position + ' ' + idx);
	
	// Find the div, then find controls for that div and then update the active button
	carObj.parents('.carousel-news-auto').find('.jcarousel-control a').removeClass('active').eq(position-1).addClass('active');
}

