// Countdown Script
// by LatecomerX <latecomerx@gmail.com> [latecomerx.com]
// edited by: Chris

// the comments here are written while assuming the variable "countdown_latency" is 60 seconds

// options - start

// value added to X to be decreased over the first 60 seconds
var countdown_add_to_x = 0.004;

// time interval to update decrease rate
var countdown_latency = 300;  //cmc: devide by 5 (because update_y() function now happens 5 times a second)  therefore latency = 1 min

// number of decimal place in the formatted version of Y
var countdown_dp = 6;

// options - end

// update the value in Y
function update_y () {
	var x = document.getElementById('x').innerHTML; // get X from DOM
	if (!isNaN(x)) { // if X is loaded (is a number)
		if (document.getElementById('message_y').innerHTML) { // if Y exists
			if (countdown_sec_count == countdown_latency) { // if second counter reaches the limit
				countdown_sec_count = 0; // reset second counter
				countdown_y = countdown_x_first_sec; // update the decimal number (not the formatted one) version of Y to a copy of X 60 secs ago
				countdown_decrease = (countdown_x_first_sec - x) / countdown_latency; // calculate decrease rate for the next 60 seconds
				countdown_x_first_sec = x; // update a variable which copies X, which will be used 60 seconds later
			}
			if (countdown_decrease > 0) { // if there is a decrease rate
				countdown_y -= countdown_decrease; // update the decimal no. version of Y according to decrease rate
				document.getElementById('message_y').innerHTML = format_y(); // update the formatted version of Y in the page
				countdown_sec_count++; // add a second to the counter
			} else { // if there is no decrease rate (freeze Y)
				countdown_sec_count = countdown_latency; // keep second counter at limit so that countdown_decrease can be updated as soon as there is a drop in X
			}
		} else { // if Y does not exist (right after the loading of X)
			countdown_y = parseFloat(x) + countdown_add_to_x; // create Y by adding X to a specified value
			document.getElementById('message_y').innerHTML = format_y(); // display Y in the page
			countdown_x_first_sec = x; // update a variable which copies X, which will be used 60 seconds later
		}
	}
	
	return true;
}

// format Y with set decimal point and explode digits after the decimal point into HTML <span>'s
function format_y() {
	var dp_shift = Math.pow(10, countdown_dp); // calculate the number to multiply and divide Y with
	var y_str = String(Math.round(countdown_y * dp_shift) / dp_shift); // multiple Y with dp_shift, round it to a whole number, then divide Y by the same number again, then convert it into a string for manipulation - a string basically means text
	var y_dp_pos = y_str.indexOf('.'); // get the position of the decimal place
	var y_str_len = y_str.length; // get the number of characters of the string
	var y_html = ''; // initialize variable
	var i = 0; // initialize variable
	var digit = ''; // initialize variable
	y_html += y_str.substr(0, y_dp_pos + 1); // copy the part of the string from the start to the decimal place to the y_html variable
	while (i++ < countdown_dp) { // a loop to format each number place after the decimal point with HTML <span>'s
		digit = y_str.charAt(y_dp_pos + i); // get the digit (a number place after the decimal point) according to changing offset
		if (!digit) { // if digit is not set (due to truncated zeroes)
			digit = 0; // set digit to 0
		}
		y_html += '<span class="counter_number' + i + '">' + digit + '</span>'; // add the formatted text onto the y_html variable
		digit = ''; // reset the variable "digit"
	}
	
	return y_html; // return the variable for output when the function is called
}

// initializing variables
var countdown_sec_count = 0;
var countdown_decrease = countdown_add_to_x / countdown_latency;
var countdown_x_first_sec = 0.000;
var countdown_y = 0.000;

// create Y in a <p>
//document.write('<p>Y: <span id="message_y"></span></p>');

