/*
 *  ffcountdown.js - A simple countdown counter for Firefox download milestones.
 *
 *  History:
 *      Version 0.6:
 *          - The script has been modifies to use the est dl field in the feed
 *      Version 0.5:
 *          - Changed to countdown to the next 10th million download.
 *      Version 0.4:
 *          - Added a default counter rate.
 *          - Made minor modifications to the counter status indicator.
 *          - If you don't want the timer on, it doesn't set the timeout.
 *      Version 0.3:
 *          - Changed the timing of HTTP requests to reflect the new feed
 *             refresh rate of once per minute.
 *          - Counter increments on a "tick" instead of a fixed rate.
 *          - Added support for status elements to debug/track counter metrics:
 *              - download rate
 *              - last count
 *              - last time
 *              - HTTP request status
 *              - time until HTTP request
 *              - time between counter ticks
 *      Version 0.2:
 *          - Now calculates download rates between subsequent queries for
 *            better accuracy.
 *          - Only reformats the number on an actual increment.
 *          - Increased the default speed.
 *      Version 0.1:
 *          - Initial release.
 *
 *  Orginal script:
 *  Copyright 2005 Infocraft
 *  http://www.infocraft.com/
 *
 *  ffcountdown:
 *  Asbjørn Sloth Tønnesen
 *  http://asbjorn.biz/
 *
 *  The latest version of this script can be found at:
 *  http://ff.asbjorn.it/ffcountdown.js
 * 
 *  The latest version of the orginal script can be found at:
 *  http://www.infocraft.com/projects/ffcounter/ffcounter.js
 *
 *  You may use this script in its entirety, modify it to suit your needs,
 *  adapt its functions, or just use it for inspiration.  However, some form
 *  of acknowledgement, especially a link back to http://www.infocraft.com/, is
 *  always appreciated.
 */

/***************************  Adjustable Parameters ***************************/

var ffcountdown_url = "http://ff.asbjorn.it/feed.xml";	// The URL of your counter mirror.

var ffcountdown_timer_id = "ffcountdown";	// The id of the countdown element.
var ffcountdown_countdown_dl_id = "ffcountdown_dl";	// The id of the countdown element.
var ffcountdown_status_id = "countdownstatus";	// The id of the counter status element.
var ffcountdown_counter_id = "ffcounter";	// The id of the counter element.
var ffcountdown_goal_id = "ffcountdown_goal";	// The id of the counter element.
var ffcountdown_current = 0;

// for internal use
var ffcountdown_request;

/***************************    Public Functions    ***************************/

// This should be called from your HTML, probably as an onload="get_count();"
// event in your <body> tag.
function ffcountdown_init()
{
	ffcountdown_load_xml(ffcountdown_url);
}

/***************************    Private Functions   ***************************/

// Advances the status counter, indicating the length until the next increment.
function ffcountdown_set_countdown(time, count, dps, offset, goal, lab)
{
	var speed;
	var timer;
	var counter;
	var countdown_dl;
	timer = document.getElementById(ffcountdown_timer_id);
	if (timer)
		timer.firstChild.data = ffcountdown_format_time(time);
	counter = document.getElementById(ffcountdown_counter_id);
	if (counter)
		counter.firstChild.data = ffcountdown_format_count(count);
	countdown_dl = document.getElementById(ffcountdown_countdown_dl_id);
	if (countdown_dl)
		countdown_dl.firstChild.data = ffcountdown_format_count(goal-count);

	speed = 1000 / dps;
	offset += speed;
	if (offset > 1000) {
		time--;
		offset -= 1000;
	}
	count++;
	if (time > 0 && ffcountdown_current == lab) {
		setTimeout("ffcountdown_set_countdown(" + time + ", " + count + ", " + dps + ", " + offset + ", " + goal + ", "+lab+")", speed);
	}
}


// Adapted from the good people at ADC:
// http://developer.apple.com/internet/webcontent/xmlhttpreq.html
// this starts loading the XML data and sets up the process_request handler.
function ffcountdown_load_xml(url)
{
	var status_element = document.getElementById(ffcountdown_status_id);
	if (status_element)
		status_element.firstChild.data = "Sending Request";

	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		ffcountdown_request = new XMLHttpRequest();
		ffcountdown_request.onreadystatechange = ffcountdown_process_request;
		ffcountdown_request.open("GET", url, true);
		ffcountdown_request.send(null);

	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		ffcountdown_request = new ActiveXObject("Microsoft.XMLHTTP");
		if (ffcountdown_request) {
			ffcountdown_request.onreadystatechange = ffcountdown_process_request;
			ffcountdown_request.open("GET", url, true);
			ffcountdown_request.send();
			setTimeout("ffcountdown_load_xml("+url+")",60000);
		}
	}
}

// This function is called once the request state changes.  It pulls the
// relevant data from the response and sends to update_count().
function ffcountdown_process_request()
{
	var response;
	var timeleft;
	var dps;
	var count;
	var status;
	var goal;

	if (ffcountdown_request.readyState == 4) {
		status_element = document.getElementById(ffcountdown_status_id);
		if (status_element) {
			status = (ffcountdown_request.status == 200) ? "OK" : "Error";
			status_element.firstChild.data = "Received: " + status;
		}

		if (ffcountdown_request.status == 200) {
			response = ffcountdown_request.responseXML;
			dps = response.getElementsByTagName('description')[2].firstChild.data;
			goal = response.getElementsByTagName('description')[3].firstChild.data;
			timeleft = response.getElementsByTagName('description')[4].firstChild.data;
			count = response.getElementsByTagName('description')[8].firstChild.data;
			var objgoal = document.getElementById(ffcountdown_goal_id);
			if (objgoal)
				objgoal.firstChild.data = ffcountdown_format_count(goal);
			ffcountdown_current++;
			ffcountdown_set_countdown(timeleft, count, dps, 0, goal, ffcountdown_current);
		}
	}
}


/***************************    Utility Functions   ***************************/

// Formats a count for display by making it into a string and inserting commas.
function ffcountdown_format_count(count)
{
	count = count.toString();

	for (var i = count.length - 3; i > 0 ; i -= 3) {
		count = count.slice(0, i) + ',' + count.slice(i, count.length);
	}

	return count;
}

// Formats a time for display.
function ffcountdown_format_time(time)
{
	var hour = Math.floor(time / 3600);
	var minutes = Math.floor((time - hour*3600)/60) ;
	var seconds = time - minutes * 60 - hour * 3600;

	if (minutes < 10)
		minutes = "0" + minutes ;
	if (seconds < 10)
		seconds = "0" + seconds;

	return hour + ":" + minutes + ":" + seconds;
}
