$(document).ready(function() {
	// Set up the popup boxes
	$('#popup1 a, #popup2 a, #popup3 a').fancybox({
		overlayShow: true,
		frameHeight: 768,
		frameWidth: 1124
	});

	// Whenever the input or select elements are changed, retrieve content from server
	$('select, #IsSelfEmployed input').bind('click', function() {
		$.fn.getContent();
	});
	
	var timeout = undefined;
	
	$('#AnnualIncome input').bind('keypress', function() {
		if(timeout != undefined) {
			clearTimeout(timeout);
		}
		timeout = setTimeout(function() {
			$.fn.getContent();
		}, 500);
	});
});

// Retrieve the JSON encoded content from the server
$.fn.getContent = function() {
	var age = $('#Age select').val();
	var income = currency2Decimal($('#AnnualIncome input').val());
	var isSelfEmployed = $('#IsSelfEmployed input').attr('checked') ? '1' : '0';

	// Get data from the server as a JSON object, and call the update content function
	$.getJSON($('base').attr('href') + 'home/calculate?age=' + age + '&income=' + income + '&selfemployed=' + isSelfEmployed, function(data) {
		$.fn.updateContent(data);
	});
}

// Update the content with the data we retrieved from the server
$.fn.updateContent = function(data) {
	
	// Four (4%) amounts
	var fourUserContribution = data.Four.UserContribution;
	var fourEmployerContribution = data.Four.EmployerContribution;
	var fourInterest = data.Four.Interest;
	var fourGovernmentContribution = data.Four.GovernmentContribution;
	var fourTotal = data.Four.Total;
	
	// Four (8%) amounts
	var eightUserContribution = data.Eight.UserContribution;
	var eightEmployerContribution = data.Eight.EmployerContribution;
	var eightInterest = data.Eight.Interest;
	var eightGovernmentContribution = data.Eight.GovernmentContribution;
	var eightTotal = data.Eight.Total;
	
	// Cash (non-KiwiSaver) amounts
	var cashUserContribution = data.Cash.UserContribution;
	var cashEmployerContribution = data.Cash.EmployerContribution;
	var cashInterest = data.Cash.Interest;
	var cashGovernmentContribution = data.Cash.GovernmentContribution;
	var cashTotal = data.Cash.Total;
	
	var imgURL = 'http://chart.apis.google.com/chart?cht=bvs&chd=t:'
		+ cashUserContribution + ',' + fourUserContribution + ',' + eightUserContribution
		+ '|' + cashInterest + ',' + fourInterest + ',' + eightInterest
		+ '|' + cashGovernmentContribution + ',' + fourGovernmentContribution + ',' + eightGovernmentContribution
		+ '|' + cashEmployerContribution + ',' + fourEmployerContribution + ',' + eightEmployerContribution
		+ '&chds=0,800000&chco=126082,1b90c3,58beea,c7e9f8&chs=300x175&chbh=70,30&chf=c,lg,90,F3FAFB,1,FFFFFF,0|bg,lg,90,F3FAFB,1,FFFFFF,0';

	$('#cash-total').text('$' + cashTotal);
	$('#ksfour-total').text('$' + fourTotal);
	$('#kseight-total').text('$' + eightTotal);

	$('#bar_graph').fadeOut();
	$('.loading').show();

	$('#bar_graph').attr('src', imgURL).bind('load', function() {
		$('#bar_graph').fadeIn();
		$('.loading').hide();
	});
}

function currency2Decimal(num) {
	var noJunk = '';
	var withDollar = '';
	var foundDecimal = 0;
	var foundAlphaChar = 0;
	num += '';

	if(num == '') { return(0); }
	for(var i = 0; i <= num.length; i++) {
		var thisChar = num.substring(i, i+1);
		if(thisChar == '.') {
			foundDecimal = 1;
			noJunk = noJunk + thisChar;
		}
		if((thisChar < '0') || (thisChar > '9')) {
			if((thisChar != '$') && (thisChar != '.') && (thisChar != ',') && (thisChar != ' ') && (thisChar != '')) foundAlphaChar = 1;
		} else {
			withDollar = withDollar + thisChar
			noJunk = noJunk + thisChar
		}
		if((thisChar == '$') || (thisChar == '.') || (thisChar == ',')) {
			withDollar = withDollar + thisChar
		}
	}
	if(foundDecimal) { return parseFloat(noJunk); }
	else if(noJunk.length > 0) { return parseFloat(noJunk); }
	else return 0;
}
