$(document).ready(function() {
	$('.elements-collection-list').addClass('wpg-carousel-skin-tabbed');
	
	/*init carousel*/
	$('.wpg-carousel-skin-tabbed').find('ul').jcarousel({
		wrap: 'both',
		animation: 'slow',
		scroll: 1,
		auto: 4,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	/*create tabs*/
	function fnCreateTabs(carousel) {
		var wpgCarouselTabbed = $('.wpg-carousel-skin-tabbed');
		if (wpgCarouselTabbed && wpgCarouselTabbed.length > 0) {
			var length = $(wpgCarouselTabbed).find('li').length;
			var tabs = '';
			//create anchor as tabs
			for(var i = 1; i <= length; i++){
				tabs += '<a href="#">' + i + '</a>';
			}
			if (tabs != '') {
				$(wpgCarouselTabbed).find('.jcarousel-container').prepend('<div class="jcarousel-control">'+tabs+'</div>');
				$('.jcarousel-control a').bind('click', function() {
					carousel.scroll($.jcarousel.intval($(this).text()));
					return false;
				});
				//prevent autoscroll on hover
				if (carousel.options.auto) {
					$(wpgCarouselTabbed).find('.jcarousel-container')
						.bind('mouseenter', function() { carousel.stopAuto(); })
						.bind('mouseleave', function() { carousel.startAuto(); });
				}
			}
		}
	}
	
	/*synchronize tabs*/
	function syncTabs(carousel, li, idx, action) {
		$(li).parents('.jcarousel-container').find('.jcarousel-control a')
			.removeClass('current') //a a a ...
			.parent() //.jcarousel-control
				.children('a:eq('+(idx-1)+')') //a a a ...
					.addClass('current').focus();
		carousel.stopAuto();
		carousel.startAuto();
	}

	/*advertisement*/
	calculateAmounts();
	$('input[id^="advertisement_rule"]').change(calculateAmounts);
	
	function getTotalEuroAmount() {
		return "&euro; " + ($('#advertisement_amount').val()/100).toFixed(2);
	}
	
	function getSubEuroAmount() {
		return "&euro; " + ($('#advertisement_subamount').val()/100).toFixed(2);
	}
	
	function getVATEuroAmount() {
		return "&euro; " + ($('#advertisement_vat').val()/100).toFixed(2);
	}
	
	function calculateAmounts() {
		//Count number of used rules:
		var usedRules = $('input[id^="advertisement_rule"][value!=""]').length;
		//Minimal amount is 3 rules:
		if (usedRules < 3) {
			usedRules = 3;
		}
		var fixedRuleAmount = $('#advertisement_fixedruleamount').val();
		var fixedVAT = $('#advertisement_fixedvatpercentage').val();
		var subAmount = usedRules * fixedRuleAmount;
		var totAmount = Math.round(subAmount * ((100 + parseInt(fixedVAT))/100));
		var vatAmount = totAmount - subAmount;
		
		$('#advertisement_subamount').val(subAmount);
		$('#advertisement_amount').val(totAmount);
		$('#advertisement_vat').val(vatAmount);
		
		$('#advertisement_subamount_lbl').html(getSubEuroAmount());
		$('#advertisement_amount_lbl').html(getTotalEuroAmount());
		$('#advertisement_vat_lbl').html(getVATEuroAmount());
	}
});
