// JavaScript Document
function formatInt(input){
  var num = input.value;
 
  if(!isNaN(num)){
    if(num.indexOf('.') > -1) {
      input.value = input.value.substring(0,input.value.length-1);
    }
  } else {
    input.value = input.value.substring(0,input.value.length-1);
  }
}

jQuery.fn.myEqualH = function(){
	/* var tallest = 0;
	jQuery(this).each(function(){
		if(jQuery(this).height() > tallest)
		{
			tallest = jQuery(this).height();
		}
	});
	jQuery(this).height(tallest);
	return tallest; */
}


var vluchtCount;
var vluchten;
var vlucht;
var vluchtenKGArr;
var vluchtenBedrArr;

$(document).ready(function(){
	
	$('.page-id-2560, .page-id-2962, .page-id-2964').addClass('printPage');
	
	var logo = $('.printPage #logo');
	$('.printPage').prepend(logo);
	
	$('.current_page_item, .current_page_ancestor').addClass('active');
	
	if( $('body').is('.single-project') || $('body').is('.single-dorp') )
	{
		$('.page-item-80').addClass('active');
	}
	
	if( $('body').is('.single-nieuws') )
	{
		$('.page-item-58, .page-item-92').addClass('active');
	}
	
	$('.mainMenu ul li:first').addClass('first');
	$('.mainMenu ul li:last').addClass('last');
	
	var lastIndex = $('.mainMenu ul li').size()-1;
	
	//Make active items show as active
	$('.mainMenu ul li').each(function(idx)
	{
		if($(this).is('.active'))
		{
			if(idx == 0)
			{
				$(this).addClass('activeFirst');
			}
				else
			{
				if(idx == lastIndex)
				{
					$(this).addClass('activeLast');
				}
				var prevIdx = idx-1;
				$('.mainMenu ul li:eq('+prevIdx+')').addClass('beforeActive');
			}
		}
	});
	
	$('.mainMenu ul li').hover(function(){
	
		if(!$(this).is('.active'))
		{
			var nextIdx = $(this).index()+1;
			if( $('.mainMenu ul li:eq('+nextIdx+')').is('.active') )
			{
				$(this).addClass('beforeActiveHover');
			} else {
				$(this).addClass('hover');
			}
			
			if($(this).is('.first'))
			{
				$(this).addClass('firstHover');
			}
			
			if($(this).is('.last'))
			{
				$(this).addClass('lastHover');
			}
			
			var prevIdx = $(this).index()-1;
			
			if(prevIdx != -1)
			{
				if( $('.mainMenu ul li:eq('+prevIdx+')').is('.active') )
				{
					$('.mainMenu ul li:eq('+prevIdx+')').addClass('afterActiveHover');
				} else {
					$('.mainMenu ul li:eq('+prevIdx+')').addClass('beforeHover');
				}
			}
		}
		
	},function(){
	
		if(!$(this).is('.active'))
		{
			var nextIdx = $(this).index()+1;
			if( $('.mainMenu ul li:eq('+nextIdx+')').is('.active') )
			{
				$(this).removeClass('beforeActiveHover');
			} else {
				$(this).removeClass('hover');
			}
			
			if($(this).is('.first'))
			{
				$(this).removeClass('firstHover');
			}
			
			if($(this).is('.last'))
			{
				$(this).removeClass('lastHover');
			}
			
			var prevIdx = $(this).index()-1;
			if(prevIdx != -1)
			{
				if( $('.mainMenu ul li:eq('+prevIdx+')').is('.active') )
				{
					$('.mainMenu ul li:eq('+prevIdx+')').removeClass('afterActiveHover');
				} else {
					$('.mainMenu ul li:eq('+prevIdx+')').removeClass('beforeHover');
				}
			}
		}
		
	});
	
	$('ul.children').each(function(){
		$(this).find('li:last').addClass('last');
	});
	
	$('.Answer').hide();
	$('.Question a').click(function(event){
		event.preventDefault();
		$(this).parent().next().toggle(200,function(){eqHNow()});
	});
	
	$('#CustomFields_2_4').focusin(function(){
		var val = $(this).attr('value');
		if(val == 'naam')
		{
			$(this).attr('value', '');
		}
	});
	
	$('#CustomFields_2_4').focusout(function(){
		var val = $(this).attr('value');
		if(val == '')
		{
			$(this).attr('value', 'naam');
		}
	});
	
	$('#inputEmail').focusin(function(){
		var val = $(this).attr('value');
		if(val == 'e-mailadres')
		{
			$(this).attr('value', '');
		}
	});
	$('#inputEmail').focusout(function(){
		var val = $(this).attr('value');
		if(val == '')
		{
			$(this).attr('value', 'e-mailadres');
		}
	});
	$('.nieuwsbrForm input').ezMark();
	$('#main .left, #main .content').myEqualH();
	
	$('.element input').focusin(function(){
		$(this).parent().find('.txtBalon').fadeIn(300);
	});
	
	$('.element input').focusout(function(){
		$(this).parent().find('.txtBalon').fadeOut(300);
	});
	
	////////////////////////////////////////////
	
	$('#autok').mouseover(function(){
		$(this).parent().find('.txtBalon1').show();
	});
	
	$('#autok').mouseout(function(){
		$(this).parent().find('.txtBalon1').hide();
	});
	
	$('#autom').mouseover(function(){
		$(this).parent().find('.txtBalon2').show();
	});
	
	$('#autom').mouseout(function(){
		$(this).parent().find('.txtBalon2').hide();
	});
	
	
	$('#autog').mouseover(function(){
		$(this).parent().find('.txtBalon3').show();
	});
	
	$('#autog').mouseout(function(){
		$(this).parent().find('.txtBalon3').hide();
	});
	
		////////////////////////////////////////////
	
	$('#vliegk').mouseover(function(){
		$(this).parent().find('.txtBalon4').show();
	});
	
	$('#vliegk').mouseout(function(){
		$(this).parent().find('.txtBalon4').hide();
	});
	
	$('#vliegm').mouseover(function(){
		$(this).parent().find('.txtBalon5').show();
	});
	
	$('#vliegm').mouseout(function(){
		$(this).parent().find('.txtBalon5').hide();
	});
	
	
	$('#vliegg').mouseover(function(){
		$(this).parent().find('.txtBalon6').show();
	});
	
	$('#vliegg').mouseout(function(){
		$(this).parent().find('.txtBalon6').hide();
	});
	
		
		////////////////////////////////////////////
	
	$('#huisk').mouseover(function(){
		$(this).parent().find('.txtBalon7').show();
	});
	
	$('#huisk').mouseout(function(){
		$(this).parent().find('.txtBalon7').hide();
	});
	
	$('#huism').mouseover(function(){
		$(this).parent().find('.txtBalon8').show();
	});
	
	$('#huism').mouseout(function(){
		$(this).parent().find('.txtBalon8').hide();
	});
	
	
	$('#huisg').mouseover(function(){
		$(this).parent().find('.txtBalon9').show();
	});
	
	$('#huisg').mouseout(function(){
		$(this).parent().find('.txtBalon9').hide();
	});
	
		$('#feestk').mouseover(function(){
		$(this).parent().find('.txtBalon19').show();
	});
	
	$('#feestk').mouseout(function(){
		$(this).parent().find('.txtBalon19').hide();
	});
	
	
	$('#feestg').mouseover(function(){
		$(this).parent().find('.txtBalon20').show();
	});
	
	$('#feestg').mouseout(function(){
		$(this).parent().find('.txtBalon20').hide();
	});
	
	$('#douche').mouseover(function(){
		$(this).parent().find('.txtBalon10').show();
	});
	
	$('#douche').mouseout(function(){
		$(this).parent().find('.txtBalon10').hide();
	});
	
	$('#laptop').mouseover(function(){
		$(this).parent().find('.txtBalon11').show();
	});
	
	$('#laptop').mouseout(function(){
		$(this).parent().find('.txtBalon11').hide();
	});
	
	
	$('#desktop').mouseover(function(){
		$(this).parent().find('.txtBalon12').show();
	});
	
	$('#desktop').mouseout(function(){
		$(this).parent().find('.txtBalon12').hide();
	});
	
		$('#stroom').mouseover(function(){
		$(this).parent().find('.txtBalon13').show();
	});
	
	$('#stroom').mouseout(function(){
		$(this).parent().find('.txtBalon13').hide();
	});
	
	$('#gas').mouseover(function(){
		$(this).parent().find('.txtBalon14').show();
	});
	
	$('#gas').mouseout(function(){
		$(this).parent().find('.txtBalon14').hide();
	});
	
	
	$('#partner').mouseover(function(){
		$(this).parent().find('.txtBalon15').show();
	});
	
	$('#partner').mouseout(function(){
		$(this).parent().find('.txtBalon15').hide();
	});
	
	$('#hond').mouseover(function(){
		$(this).parent().find('.txtBalon16').show();
	});
	
	$('#hond').mouseout(function(){
		$(this).parent().find('.txtBalon16').hide();
	});
	
	$('#kat').mouseover(function(){
		$(this).parent().find('.txtBalon17').show();
	});
	
	$('#kat').mouseout(function(){
		$(this).parent().find('.txtBalon17').hide();
	});
	
	
	$('#vis').mouseover(function(){
		$(this).parent().find('.txtBalon18').show();
	});
	
	$('#vis').mouseout(function(){
		$(this).parent().find('.txtBalon18').hide();
	});
	
	$('#dir1').hover(function(){
		$(this).parent().find('.txtBalon21').show();
	},function(){
		$(this).parent().find('.txtBalon21').hide();
	});
	
	$('#dir2').hover(function(){
		$(this).parent().find('.txtBalon22').show();
	},function(){
		$(this).parent().find('.txtBalon22').hide();
	});
	
	$('#dir5').hover(function(){
		$(this).parent().find('.txtBalon23').show();
	},function(){
		$(this).parent().find('.txtBalon23').hide();
	});
	
	$("#indEnery").attr('disabled','disabled');
	
	var indEnergy = false;
	
	var huisKG = 0;
	var huisBedrag = 0;
	var huisAfronding = 0;
	
	var gasKG = 0;
	var stroomKG = 0;
	
	
	$('.huis .element input').change(function(){
		if($(this).is('#gasverbr'))
		{
			gasKG = parseFloat($(this).attr('value')*1.78);
			if(isNaN(gasKG))
			{
				gasKG = 0;
				$(this).attr('value','');
			}
		} 
		if($(this).is('#stroomverbr'))
		{
			calcStroomKg();
		}
		if($(this).is('#groeneStroom'))
		{
			if($(this).attr('checked'))
			{
				stroomKG = 0;
			} else {
				calcStroomKg();
			}
		}
		
		function calcStroomKg(){
			stroomKG = parseFloat($('#stroomverbr').attr('value')*0.554);
				if(isNaN(stroomKG))
				{
					stroomKG = 0;
					$('#stroomverbr').attr('value','');
				}
		}
		
		huisKG = gasKG + stroomKG;
		huisAfr = kgAfr(huisKG)
		huisBedrag = kgToEur(huisKG);
						
		$(this).parent().parent().parent().find('.kg').html(parseInt(huisKG)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ huisBedrag);
		
		$('.huisTotaal .uitstoot').html(parseInt(huisKG)+' kg');
		$('.huisTotaal .bedrag').html('&euro; ' + huisBedrag);
		$('.huisTotaal .afronding').html(parseInt(huisAfr)+' kg');
		
		//$('.huisTotaal .compenseer').html(Math.ceil(huisKG/1000) + ' ton');
		var ton = Math.round(huisKG/1000);
		ton = (ton == 0) ? 1 : ton;
		
		$('#qtyuitstoot_mijn_huis1349061613').attr('value', ton);
		$('#uitstoot_huis').attr('value', huisKG);
		
		if(parseInt(huisKG) != 0)
		{
			$("#indEnery").attr('disabled','');
		} else {
			if(parseInt(vervoerKG) == 0)
			{
				indEnergy = false;
				$("#indEnery").attr('checked', false);
				$("#indEnery").attr('disabled','disabled');
			}
		}
		calcIndIndEnery();
	});
	
	var vervoerKG = 0;
	var vervoerBedrag = 0;
	$('#km, .vervoer input:radio, #kmTrein, #kmBus').change(function(){
		var brandstof = $('#km').parent().parent().find('input:radio:checked').val();
		
		switch(brandstof)
		{
			case 'benzine':
				var fact = 0.218;
			break;
			case 'diesel':
			case 'LPG':
				var fact = 0.2;
			break;
			default:
				var fact = 0;
			break;
		}
		
		vervoerKG = parseFloat($('#km').val()*fact);
		
		var factTrein = 0.042;
		var treinKG = parseFloat($('#kmTrein').val()*factTrein);
		
		vervoerKG += treinKG;
		
		var factBus = 0.076;
		var busKG = parseFloat($('#kmBus').val()*factBus);
		
		vervoerKG += busKG;
		
		if(isNaN(vervoerKG))
		{
			vervoerKG = 0;
			$('#km').attr('value','');
		}
		vervoerBedrag = kgToEur(vervoerKG);
		vervoerAfronding = kgAfr(vervoerKG);
		
		$('#km').parent().parent().parent().find('.kg').html(parseInt(vervoerKG)+' kg');
		$('#km').parent().parent().parent().find('.bedrag').html('&euro; '+ vervoerBedrag);
		
		$('.vervoerTotaal .uitstoot').html(parseInt(vervoerKG)+' kg');
		$('.vervoerTotaal .bedrag').html('&euro; '+ vervoerBedrag);
		$('.vervoerTotaal .afronding').html(parseInt(vervoerAfronding)+' kg');
		
		if(parseInt(vervoerKG) != 0)
		{
			$("#indEnery").attr('disabled','');
		} else {
			if(parseInt(huisKG) == 0)
			{
				indEnergy = false;
				$("#indEnery").attr('checked', false);
				$("#indEnery").attr('disabled','disabled');
			}
		}
		
		//$('.vervoerTotaal .compenseer').html(Math.ceil(vervoerKG/1000) + ' ton');
		var ton = Math.round(vervoerKG/1000);
		if((vervoerKG/1000) < 0.5)
		{
			ton = 1;
		}
				
		$('#qtyuitstoot_mijn_vervoer2051766082').attr('value', ton);
		$('#uitstoot_vervoer').attr('value', vervoerKG);
		calcIndIndEnery();
	});
	
	var totalVluchtKg = 0;	
	$('.vlucht input, .vlucht select').change(function(){
		var zoneFactor = 0;
		var zoneValue = parseInt($('#zoneSelect').val());
		var pplValue = parseInt($('#aantalPers').val());
		
		switch(zoneValue)
		{
			case 1:
				zoneFactor = 509;
			break;
			case 2:
				zoneFactor = 872;
			break;
			case 3:
				zoneFactor = 2368;
			break;
			case 4:
				zoneFactor = 3857;
			break;
			case 5:
				zoneFactor = 5069;
			break;
		}
		totalVluchtKg = zoneFactor*pplValue;
		
		var vluchtBedr = kgToEur(totalVluchtKg);
		var vluchtKgAfr = kgAfr(totalVluchtKg);
		
		$(this).parent().parent().parent().find('.kg').html(totalVluchtKg+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ vluchtBedr);
		
		$('.vluchtenTotaal .uitstoot').html(totalVluchtKg+' kg');
		$('.vluchtenTotaal .bedrag').html('&euro; '+ vluchtBedr);
		$('.vluchtenTotaal .afronding').html(parseInt(vluchtKgAfr)+' kg');
		
		$('#qtyuitstoot_mijn_vluchten35444924').attr('value', vluchtKgAfr/1000);
		$('#uitstoot_vluchten').attr('value', totalVluchtKg);
		
		calcIndIndEnery();
	});
	
	$('a.compenseer').click(function(e){
		e.preventDefault();
	});
	
	$('#directTon').change(function(){
		var inPut = $(this).val();
		var directKg = inPut * 1000;
		var directBedr = kgToEur(directKg);
		var dirKgAfr = kgAfr(directKg);
		
		$(this).parent().parent().parent().find('.kg').html(parseInt(directKg)+' kg');
		$(this).parent().parent().parent().find('.bedrag').html('&euro; '+ directBedr);
		
		$('.directTotaal .uitstoot').html(parseInt(directKg)+' kg');
		$('.directTotaal .bedrag').html('&euro; '+ directBedr);
		$('.directTotaal .afronding').html(parseInt(dirKgAfr)+' kg');
		
		var ton = Math.round(directKg/1000);
		
		if(inPut < 0.5) 
		{
			ton = 1;
		}
		
		$('#qtyaantal_ton1482090478').attr('value', ton);
		
		calcIndIndEnery();
	});
	
	
	
	$('#indEnery').change(function(){
		
		
		if($(this).attr('checked'))
		{
			indEnergy = true;
			
		} else {
			indEnergy = false;
		}
		calcIndIndEnery();
	});
	
	
	function calcIndIndEnery(){
		if(indEnergy)
			{
				var totalAllKG = (huisKG+vervoerKG+totalVluchtKg)*2.18;
				var totalAllBedr = kgToEur(totalAllKG);
				
			} else {
				var totalAllKG = 0;
				var totalAllBedr = 0;
			}
			
			var dirTotalAllAfr = kgAfr(totalAllKG);
			
			$('#indEnery').parent().parent().parent().find('.kg').html(parseInt(totalAllKG)+' kg');
			$('#indEnery').parent().parent().parent().find('.bedrag').html('&euro; '+ totalAllBedr);
			
			$('.indirectTotaal .uitstoot').html(parseInt(totalAllKG)+' kg');
			$('.indirectTotaal .bedrag').html('&euro; '+ totalAllBedr);
			$('.indirectTotaal .afronding').html(parseInt(dirTotalAllAfr)+' kg');
			
			var totalTon = Math.round(totalAllKG/1000);
			
			$('#qtyindirecte-energie258880039').attr('value',totalTon);
			$('#uitstoot_indirect').attr('value',totalAllKG);
	}
	
	$('.more-link').html('Lees verder...');
	
	$('#footprint').attr({
		'target'	: '_blank'
	});
	
	/* $('.page-id-52 #footprint').bind("submit", function(){
		$.ajax({
			type        : "POST",
			cache       : false,
			url         : "/?page_id=2560",
			data        : $(this).serializeArray(),
			success: function(data) {
				$.fancybox(data);
			}
		});
		return false;
	}); */
	
});


function kgToEur(KG){
	
	KGeuro = KG
	
	if(KGeuro != 0){
		
		KG = 1000*(Math.round(KG/1000))
		var bedrRaw = (KG/1000)*12.50;
		//bedr = Math.round(bedrRaw*Math.pow(10,2))/Math.pow(10,2);
		
			if(KGeuro < 499){
				
				bedrRaw = 12.50;
			} 
	} else {
		
		bedrRaw = 0;
		
	}
		return bedrRaw;
}


function kgAfr(KG){
		KGwerkelijk = KG;
		
		if (KGwerkelijk != 0) { 
		
			KGafgerond = 1000*(Math.round(KG/1000))
			
			//bedr = Math.round(bedrRaw*Math.pow(10,2))/Math.pow(10,2);
			if(KGwerkelijk < 499){
				KGafgerond = 1000;
			} 
		
		} else {
			
			KGafgerond = 0;
		
		}
		
		return KGafgerond;
}



/* function addVlucht() {
	var newVlucht = vlucht.clone();
	vluchten.push(newVlucht);
	vluchten[vluchten.length-1].appendTo('.vluchten');
	
	vluchten[vluchten.length-1].find('input, select').change(function(){
		calcVlucht($(this));
	});
	
	eqHNow();
} */

/* function calcVlucht(element){
	var numPers = element.parent().find('#aantalPers').val();
	var retour = element.parent().find('#retour').is(':checked');
	
	var total = numPers*810;

	//vluchtenKGArr[0] = (retour) ? total : total/2 ;
	vluchtenKGArr[0] = total;
	vluchtenBedrAfronding = kgAfr(vluchtenKGArr[0]);
	vluchtenBedrArr[0] = kgToEur(vluchtenBedrAfronding);
	
	element.parent().parent().find('.kg').html(parseInt(vluchtenKGArr[0]) + ' kg');
	element.parent().parent().find('.bedrag').html('&euro; ' + vluchtenBedrArr[0]);
	
	$('.vluchtenTotaal .uitstoot').html(parseInt(vluchtenKGArr[0]) + ' kg');
	$('.vluchtenTotaal .bedrag').html('&euro; ' + vluchtenBedrArr[0]);
	$('.vluchtenTotaal .afronding').html(vluchtenBedrAfronding + ' kg');
	
	//$('.vluchtenTotaal .compenseer').html(Math.ceil(vluchtenKGArr[0]/1000) + ' ton');
	var ton = Math.round(vluchtenKGArr[0]/1000);
	$('#qtyuitstoot_mijn_vluchten35444924').attr('value', ton);
} */

function eqHNow() {
	$('#main .left, #main .content').removeAttr('style');
	$('#main .left, #main .content').myEqualH();
}

Cufon.replace('.intro h1, #main .fpContent .RBlock  h3');
