var infoArr = new Array('abattement','abattementbis', 'kleinbeschrijf', 'registratierechten', 'hoofdsom', 'aanhorigheden', 'rechtelijn');
var loginOpen = false;

$(document).ready(function(){
	
	// Set Shadowline on Body
	$('body').addClass('setBg');
	
	if($("#header_wrapper").length > 0){
		var heightTotal = $("#top").parent(".container").height();
		var paddingTotal = parseInt($("#top").parent(".container").css('padding-top').replace('px', ''));
		
		var total = heightTotal - paddingTotal;
		$("#header_wrapper").css('background-position', '0 ' + String(total) + 'px');
	}else{
		var heightTotal = $("#top").height() + $("#message").height() + $("#mainnav").height();
		var paddingTotal = parseInt($('#message').css('padding-top').replace('px', '')) + parseInt($('#message').css('padding-bottom').replace('px', '')) + parseInt($('.container').css('padding-top').replace('px', ''));
		
		var total = heightTotal + paddingTotal;
		$('body.setBg').css('background-position', '0 ' + String(total) + 'px');
	}
	
	//$('#top').hide();
	
	if($('.rekenmodule').length > 0){
	
		$.each(infoArr, function(i){
			if($('#info_' + infoArr[i]).length > 0){
				$('#info_' + infoArr[i]).attr('title', $('#tooltip_' + infoArr[i]).html());
			}
		})
		
		
		$('a.info_link').qtip({
			position: {
				corner: {
					target: 'topRight',
					tooltip: 'bottomLeft'
				}
			},
	
			style: { 
				width: 350,
				padding: 8,
				background: '#fff',
				color: 'black',
				textAlign: 'left',
				border: {
					width: 5,
					radius: 5,
					color: '#5295C1'
				},
				tip: 'bottomLeft',
				name: 'dark' // Inherit the rest of the attributes from the preset dark style
			}
		});
	}
	
	// Group Svens
	$("#nav-login a").click(function(e){
		e.preventDefault();
		if(loginOpen == false){
			$(this).addClass('hi');
			$("#login_wrapper").slideDown();
			loginOpen = true;
		}else{
			$(this).removeClass('hi');
			$("#login_wrapper").slideUp();
			loginOpen = false;
			resetLoginForm();
		}
	});
	
	if($("#login_wrapper").length > 0){
		$("form#login").submit(function(e){
			e.preventDefault();
			
			// Testing
			$("#error_block").html("We vonden geen gegevens terug").show();
			
			$("input#username").addClass('error');
			
			/*
			$.ajax({
				type:		"POST",
				data:		$('form#login').serialize(),
				dataType:	"json",
				url:		$('form#login').attr('action'),
				success:	function(data){
					if(data.error == 1){
						
					}else{
						
					}
				}	
			});
			*/
		})
	}
	
	if($("ul#subnav").length > 0){
		if($("ul#subnav a.hi").length > 0){
			$("ul#subnav a.hi").each(function(index){
				var el1 = $(this).parent("li").parent("ul").prev("a");
				if(el1 != null){
					el1.addClass('hi');
					var el2 = el1.parent("li").parent("ul").prev("a");
					if(el2 != null) el2.addClass('hi');
				}
			})
		}
	}
	
	if($("#calendar").length > 0){
		jQuery('#calendar').datepicker({
			 numberOfMonths: [1,1],
			 showCurrentAtPos: 0,
			 beforeShowDay:function(date){
				 var addzero = (date.getDate() < 10) ? "add-zero" : "";
				 var fulldate = date.getFullYear() + "-" + addZero(date.getMonth() + 1) + "-" + addZero(date.getDate());
		
				 if(calendarDates[fulldate] != undefined){
					 return [true,"ui-state-active " + addzero, calendarDates[fulldate]["name"]];	 
				 }
				return [false, addzero];	
			},
			onSelect:function(dateTxt, inst){
				var fulldate = inst.selectedYear + "-" + addZero(inst.selectedMonth + 1) + "-" + addZero(inst.selectedDay);
				if(calendarDates[fulldate] != undefined){
					window.location = calendarDates[fulldate]["link"];
				}
			}
		});
		
	
		if(jQuery('td.ui-datepicker-current-day').hasClass('.ui-state-active')){
			var today = jQuery('#calendar').datepicker('getDate');
			var day = (today.getDate() < 10) ? "0" + String(today.getDate()) : String(today.getDate());
			var month = ((today.getMonth() + 1) < 10) ? "0" + String(today.getMonth() + 1) : String(today.getMonth() + 1);
			var year = String(today.getFullYear());
		}
	}
	
	// Blog functionality
	if($("#blog_reactions").length > 0){
		$("ul#reactions_list li").addClass("closed").removeClass("open");
		$("ul#reactions_list li h5").click(function(){
			$(this).parent('li').toggleClass("open");
			$(this).next('div.reaction').slideToggle(300, function(){
				
			})
			//$(this).parent('li').toggleClass("open");
		});
	}
});

function addZero(nr){
	return (Number(nr) < 10) ? "0" + String(nr) : String(nr);
}

function resetLoginForm(){
	$("form#login input").removeClass('error').val('');
	$("form#login input#remember").attr("checked", "");
	$("#error_block").html('').hide();
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0] == '' ? '0' : x[0];
	if(x.length > 1){
		if(x[1].length == 1) x[1] = String(x[1]) + '0';
		x2 = ',' + x[1];
	}else{
		x2 = ',00';
	}
	//x2 = x.length > 1 ? ',' + x[1] : ',00';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function removeCommas(nStr){
	var len = nStr.length;
	if(nStr.charAt(len-3) == ','){
		nStr = nStr.slice(0, -3);
	}
	return nStr.replace('.', '');
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
