var tarief_1a = 0,
	tarief_1b = 0,
	tarief_1c = 0,	
	tarief_2a = 0,
	tarief_2b = 0;
	tarief_3a = 0,
	tarief_3b = 0;	
	tarief_4a = 0,
	tarief_4b = 0;	

$(function(){
	Cufon.replace('#menu li a.mainitem', {
		hover: true
	});
	Cufon.replace('h3 a, h5 a, h4 a, h2 a, #language', {
		hover: true
	});
	Cufon.replace('h2, h3, .column h4, .slider_top p, .slider_top .h2');
	
	$('#menu>li>a').mouseenter(function(e){
		$(this).siblings('.submenu').effect('slide', {direction: 'up', mode: 'show'}, 200)
		$(this).parent().addClass('hover')
	})
	$('#menu>li').mouseleave(function(e){
		var el = $(this);
		if(el.find('.submenu:visible').length){
			el.find('.submenu').effect('slide', {direction: 'up', mode: 'hide'}, 250, function(){
				el.removeClass('hover')
				Cufon.refresh('#menu li a.mainitem');
			})
		}
	})
	
	$('#slider').hover(function(){
		var targetHeight = $(this).find('li:visible .color img').height();
		$(this).find('li:visible .color').stop().animate({
			height: targetHeight
		});
		$(this).find('li:hidden .color').css({
			height: targetHeight
		})
		$('#slider ul').cycle('pause');
		if($('#slider ul li:last').index() > 0){
			$('.next, .prev').fadeIn(200)
		}
	}, function(){
		$(this).find('li:visible .color').stop().animate({
			height: 0
		});
		$(this).find('li:hidden .color').css({
			height: 0
		})
		$('#slider ul').cycle('resume');
		$('.next, .prev').fadeOut(200)
	})
	$('.colorizer').hover(function(){
		var targetWidth = $(this).find('.color img').width();
		$(this).find('.color').stop().animate({
			width: targetWidth
		});
	}, function(){
		$(this).find('.color').stop().animate({
			width: 0
		});
	})
	
	$('#newsletter .email').focus(function(){
		if($(this).val() == $(this).data('defaultValue')){
			$(this).val('');
		}
	})
	$('#newsletter .email').blur(function(){
		if(!$(this).val()){
			$(this).val($(this).data('defaultValue'))
		}
	})
	$('#newsletter').validate({
		errorPlacement: function(error, element){
			element.parent().append(error)
		}, 
		submitHandler: function(form){
			$(form).addClass('loading').find('input.submit').attr('disabled','disabled')
			$.ajax({
				type: 'get',
				url: '/site/ajax/nieuwsbrief.php',
				data: $(form).serialize(),
				success:function(result){
					$('#newsletter').removeClass('loading').append('<div class="newsletter_return">'+result+'</div>')
					$('.newsletter_return').fadeIn(400)
				}
			})
		}
	})
	
	$('#slider ul').cycle({
		fx: 'fade',
		timeout: 10000,
		speed: 2500,
		next: '.next',
		prev: '.prev'
	})
	
	// bestelformulier
	tarief_1a = $('.tarief_1a').data('tarief');
	tarief_1b = $('.tarief_1b').data('tarief');
	tarief_1c = $('.tarief_1c').data('tarief');
	tarief_2a = $('.tarief_2a').data('tarief');
	tarief_2b = $('.tarief_2b').data('tarief');
	tarief_3a = $('.tarief_3a').data('tarief');
	tarief_3b = $('.tarief_3b').data('tarief');
	tarief_4a = $('.tarief_4a').data('tarief');
	tarief_4b = $('.tarief_4b').data('tarief');	

	$('.betalingsopties input').change(function(){
		var el = $('.'+$(this).val());
		el.show().find('select, input').addClass('required')
		el.siblings().hide();
	})
	$('#bestellen select').change(function(){
		var total = 0;
		var total_value = 0;
		var product_min = 0;
		$('.calc').each(function(){
			var product_total = 0;
			$(this).find('select[value!=0]').each(function(){
				product_total += $(this).val() * window[$(this).data('tarief')];
				// abonnementen korting tot 22 mei
				// if($(this).data('tarief') == 'tarief_2a' || $(this).data('tarief') == 'tarief_2b'){
				// 	//product_min += $(this).val() * 10;
				// }
			})
			$(this).find('.last .numb').html(product_total + ',-')
			total += product_total;
		})
		total -= product_min;
		// abonnementen korting tot 22 mei
		if(product_min){
			$('.discount:hidden').slideDown(200)
			$('.discount li:last .numb').html('-'+product_min + ',-')
		} else if($('.discount:visible').length){
			$('.discount').slideUp(200).find('li:last .numb').html('0,-')
		}
		if(total != 0){
			total += 2.50;
			total_value = total;
			$('.administratiekosten li.last .numb').html('2,50')
			total = total.toString().replace('.', ',') + '0';
		} else {
			$('.administratiekosten li.last .numb').html('0,-')
			total = '0,-';
			total_value = '';
		}
		$('input[name="totalammount"]').val(total_value)
		$('.finalresult li.last .numb').fadeOut(200, function(){
			$(this).html(total).fadeIn(200)
		})
	})
	
	$('#bestellen-concertgebouw select').change(function(){		
		
		var total_abono = 0;
		var total_abono_adminkosten = 0;		
		var total_value_abono = 0;
		var total_kaart = 0;
		var total_value_kaart = 0;		
		//var product_min_abono = 0;
		var abono_adminkosten = 6;
		var total_kaart_adminkosten = 0;
		var kaart_adminkosten = 2.3;
		var kaart_max_adminkosten = 13.8;
		var total_adminkosten	= 0;
		var total_value_adminkosten	= 0;
		
		//Abonnementen
		$('.calc.abonnement').each(function(){
			var product_total_abono = 0;
			$(this).find('select[value!=0]').each(function(){
				product_total_abono += $(this).val() * window[$(this).data('tarief')];
				if(product_total_abono != parseInt(product_total_abono)) {
					html_value = parseFloat(product_total_abono).toFixed(2);
					html_value = html_value.toString().replace('.', ',');
				} else {
					html_value = product_total_abono;
				}
				$(this).closest('ul').find('.last .numb').html(html_value + ',-')							
			})
			total_abono += product_total_abono;
		})
		
		if(total_abono != 0){
			total_value_abono = total_abono;
			total_abono_adminkosten = 6;
		} else {
			total_value_abono = 0;
		}
		
		//Losse kaarten
		$('.calc.kaarten').each(function(){
			var product_total_kaart = 0;
			$(this).find('select[value!=0]').each(function(){
				product_total_kaart += $(this).val() * window[$(this).data('tarief')];
				total_kaart_adminkosten += kaart_adminkosten * $(this).val();
				if(product_total_kaart != parseInt(product_total_kaart)) {
					html_value = parseFloat(product_total_kaart).toFixed(2);
					html_value = html_value.toString().replace('.', ',');
				} else {
					html_value = product_total_kaart;
				}
				$(this).closest('ul').find('.last .numb').html(html_value + ',-')							
			})
			total_kaart += product_total_kaart;
		})
						
		if(total_kaart != 0){
			if (total_kaart_adminkosten > kaart_max_adminkosten) {
				total_kaart_adminkosten = kaart_max_adminkosten;
			}
			total_value_kaart = total_kaart;
		} else {
			total_value_kaart = 0;
		}
			
		if ((total_abono_adminkosten != 0) || (total_kaart_adminkosten != 0)) {
			total_adminkosten = total_abono_adminkosten + total_kaart_adminkosten;
			total_value_adminkosten = total_adminkosten;
			total_adminkosten = parseFloat(total_adminkosten).toFixed(2);
			total_adminkosten = total_adminkosten.toString().replace('.', ',');			
			$('.administratiekosten li.last .numb').html(total_adminkosten);
		} else {
			$('.administratiekosten li.last .numb').html('0,-')
		}
			
		//console.log('total_value_adminkosten ' + total_value_adminkosten + ' is een ' + typeof(total_value_adminkosten));	
		//console.log('total_value_kaart ' + total_value_kaart + ' is een ' + typeof(total_value_kaart));	
		//console.log('total_value_abono ' + total_value_abono + ' is een ' + typeof(total_value_abono));					
			
		$('input[name="totalammount"]').val(total_value_kaart + total_value_abono + total_value_adminkosten)
		$('input[name="totalservice"]').val(total_value_adminkosten)		
		$('.finalresult li.last .numb').fadeOut(200, function(){
			grand_total = total_value_kaart + total_value_abono + total_value_adminkosten;
			grand_total = parseFloat(grand_total).toFixed(2);
			grand_total = grand_total.toString().replace('.', ',');			
			$(this).html(grand_total).fadeIn(200)
		})
	})	
	
	// form validation any form
	// give the required input fields a class 'required', and in case of special field like email, add a class 'email'.
	// more details http://docs.jquery.com/Plugins/validation
	if($('.form').length){
		$('.form').each(function(){			
			$(this).validate({
				submitHandler: function(form){
					$(form).find("input[type='submit']").attr('disabled', 'disabled');
					form.submit();
				}
			})			
		})
	}
});
