$(document).ready(function(){
	//lightbox			
	$("a[class*='lightbox']").colorbox();
	
	//collapser
	Collapser.init();
	
	simple_tooltip(".pod .tool-tip","tooltip");
	
	// Financial declaration form
	// Income	
	$(".js-income").blur(function(){	
		var total = 0;
		$('.js-income').each(function() {				
			if(!$(this).val().length == 0) {
	        	total += parseFloat($(this).val());
	     	}
		});
		$('#js-total_income').attr('value', total);
		surplusDeficit();
	});

	// Expenditures	
	$(".js-expenditure").blur(function(){	
		var total = 0;
		$('.js-expenditure').each(function() {				
			if(!$(this).val().length == 0) {
	        	total += parseFloat($(this).val());
	     	}
		});
		$('#js-total_expenditure').attr('value', total);
		surplusDeficit();
	});

	// Suplus/deficit
	function surplusDeficit(){
		var total = 0;
		if(!$('#js-total_income').val().length == 0) {
			var totalIncome = parseFloat($('#js-total_income').val()); 	
		}else {
			var totalIncome = 0;
		}	 
		if(!$('#js-total_expenditure').val().length == 0) {
			var totalExpenditures = parseFloat($('#js-total_expenditure').val()); 	
		}else {
			var totalExpenditures = 0;
		}
		total = totalIncome - totalExpenditures;	
		$('#js-surplus_deficit').attr('value', total);
	};	
});

function simple_tooltip(target_items, name){
	$(target_items).each(function(i){
		var tip = $(this).parents(".pod").find('.tip').html();
		var parent_id = $(this).parents(".pod").attr('id');
		
		$("body").append("<div class='"+name+"' id='"+parent_id+"-"+name+"'>"+tip+"</div>");
		var my_tooltip = $("#"+parent_id+"-"+name);
		
		if(tip != ""){ // checks if there is a tip
			$(this).parents(".pod").find('.tip').remove();
			$(this).bind('mouseover.tooltip', function(){
				my_tooltip.css({opacity:1, display:"none"}).fadeIn(400); // opacity was originally 0.8
			}).bind('mousemove.tooltip', function(kmouse){
				my_tooltip.css({left:kmouse.pageX+10, top:kmouse.pageY+5});
			}).bind('mouseout.tooltip', function(){
				my_tooltip.fadeOut(400);
			}).bind('mousedown.tooltip', function(){
				my_tooltip.fadeOut(400);
			});		
		}
	});
}
