/*
 * Public JavaScripts for Countdown.
 */

function updateCounter() {
	jQuery('#activeCounter2').text(getCurrentValueForThisYear().toFixed(1));
}

function getCurrentValueForThisYear() {
	currentDate = new Date();
	// Reported value is by default 0 and reported on 00:00:00.000 1.1.[current year]
	var reportMonth = 0;
	var reportYear = currentDate.getFullYear();
	var reportDay = 1;
	var reportTime = 0;

	var currentMonth = currentDate.getMonth();
	var currentYear = currentDate.getFullYear();
	var currentDay = currentDate.getDate();
	// Time is calculated by using the modulus of 1000*60*60*24
	var currentTime = currentDate.getTime() % 8640000;

	// Check if there is need to iterate previous months in the year
	if (currentMonth != reportMonth || currentYear != reportYear) {
		var iMonth = reportMonth;
		var iYear = reportYear;
		var iDay = reportDay;
		var valueToAdd = 0;
		// Iterate through all previous months
		while (iMonth+""+iYear!=currentMonth+""+currentYear) {
			if (iMonth == 12) {
				iMonth = 0;
				iYear = iYear + 1;
			}
			valueToAdd = valueToAdd + getMonthEstimate(iMonth);

			iMonth = iMonth + 1;
		}
		// Handle the current month
		valueToAdd = valueToAdd + (getMonthEstimate(currentMonth)*((currentDay-1)/31));
		valueToAdd = valueToAdd + (getMonthEstimate(currentMonth)*(currentTime/(1000*60*60*24*31)));
		currentValue = 0 + valueToAdd;

		return currentValue;
	}
	else {
		var valueToAdd = getMonthEstimate(currentMonth)*((currentDay-1)/31);
		valueToAdd = valueToAdd + (getMonthEstimate(currentMonth)*(currentTime/(1000*60*60*24*31)));
		currentValue = 0 + valueToAdd;
		return currentValue;
	}
}

function getMonthEstimate(index) {
	switch (index)
	{
	case 0:
		return januaryEstimate;
		break;
	case 1:
		return februaryEstimate;
		break;
	case 2:
		return marchEstimate;
		break;
	case 3:
		return aprilEstimate;
		break;
	case 4:
		return mayEstimate;
		break;
	case 5:
		return juneEstimate;
		break;
	case 6:
		return julyEstimate;
		break;
	case 7:
		return augustEstimate;
		break;
	case 8:
		return septemberEstimate;
		break;
	case 9:
		return octoberEstimate;
		break;
	case 10:
		return novemberEstimate;
		break;
	case 11:
		return decemberEstimate;
		break;
	}
}