/*******************************************
 *  wpgCarousel
*****************************************/
$.fn.wpgCarousel = function(options) {
	var defaults = {
		wrap: 'circular',
		scroll: 1,
		auto: 4,
		initCallback: fnCreateTabs,
		itemVisibleInCallback: {
			onBeforeAnimation: syncTabs
		}
	};
	// Extend our default options with those provided.
	var opts = $.extend(defaults, options);
	
	/*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 != '') {
				
				$('.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');
		carousel.stopAuto();
		carousel.startAuto();
	}
	
	// Our plugin implementation code goes here.
	return this.each(function() {
		var $this = $(this);
		$this.addClass('wpg-carousel-skin-tabbed');
		/*init carousel*/
		$('.wpg-carousel-skin-tabbed').find('ul').jcarousel(opts);
	});
};

/*******************************************
 *  wpgSlider (zelfde code als wpgCarousel, alleen andere css classes om geen conflicten te krijgen)
*****************************************/

$.fn.wpgSlider = function(options) {
	var defaults = {
		wrap: 'circular',
		scroll: 1,
		auto: 4,
		initCallback: fnCreateTabs,
		itemVisibleInCallback: {
			onBeforeAnimation: syncTabs
		}
	};
	// Extend our default options with those provided.
	var opts = $.extend(defaults, options);
	
	/*create tabs*/
	function fnCreateTabs(carousel) {
		var wpgCarouselTabbed = $('.wpg-carousel-slider');
		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 != '') {
				
				$('.jcarousel-slider-control a').bind('click', function() {
					carousel.scroll($.jcarousel.intval($(this).text()));
					return false;
				});
				//prevent autoscroll on hover
				if (carousel.options.auto) {
					$(wpgCarouselTabbed).find('.jcarousel-slider-container')
						.bind('mouseenter', function() { carousel.stopAuto(); })
						.bind('mouseleave', function() { carousel.startAuto(); });
				}
			}
		}
	}
	
	/*synchronize tabs*/
	function syncTabs(carousel, li, idx, action) {
		$(li).parents('.jcarousel-slider-container').find('.jcarousel-slider-control a')
			.removeClass('current') //a a a ...
			.parent() //.jcarousel-control
				.children('a:eq('+(idx-1)+')') //a a a ...
					.addClass('current');
		carousel.stopAuto();
		carousel.startAuto();
	}
	
	// Our plugin implementation code goes here.
	return this.each(function() {
		var $this = $(this);
		$this.addClass('wpg-carousel-slider');
		/*init carousel*/
		$('.wpg-carousel-slider').find('ul').jcarousel(opts);
	});
};

/*******************************************
 *  Calculate Advertisement costs
*****************************************/

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());
}

/**********************
 *  documentReady fn
**********************/

$(document).ready(function() {
	$('.element-collection-carousel').wpgCarousel();
	$('.element-collection-image-slider').wpgSlider({buttonPrevHTML:null, buttonNextHTML:null});
	
        // menu hover effect
        $('#navigation').find('>ul >li').hover(
	      function () {
		  clearTimeout($.data(document.body, 'menutimeout'));
		  var el=this;
		$.data(document.body, 'menutimeout', setTimeout(function() {
		  $(el).siblings("li").children("ul").hide();
		  $(el).children("ul").fadeIn("normal"); }, 500));
	      },function(){
		  /* do nothing */
        });

        $('#navigation').find('>ul >li >ul >li').hover(
	      function () {
		  clearTimeout($.data(document.body, 'menutimeout'));
	      },function(){
		  var el=this;
	      clearTimeout($.data(document.body, 'menutimeout'));
		$.data(document.body, 'menutimeout', setTimeout(function() {
		  $(el).parent().parent().children("ul").hide();
		      $(el).parent().parent().siblings(".current").children("ul").fadeIn("normal"); }, 500));
        });


	/* advertisements */
	$('input[id^="advertisement_rule"]').change(calculateAmounts);
	calculateAmounts();
 	
	$('.abos-nieuwsbrief-aanmelding-form table').find('td:eq(2)').html("Tussenvoegsels<br /><span style=\"display:block;margin-top:10px;\">Achternaam <sup class=\"required\"> * </sup></span>");
});
