// Copyright 2008-2009 (C) Otto de Voogd - http://www.7is7.com/
//
// FxCounter 0.2.3.b
//
// Original URL: http://www.7is7.com/software/firefox/fxcounter_lib.js
// Information: http://www.7is7.com/software/firefox/downloadcounter.html
//
//----------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// See license: http://www.gnu.org/licenses/gpl.html
//----------------------------------------------------------------------
//
// Firefox Download Counter JavaScript integration library
//
// Settings and usage:
//
// fxc_separator - sets your thousands separator (a comma by default).
// fxc_style - sets the style of the counter.
// fxc_feed - sets the file that contains the latest download count.
// fxc_lib - sets the filename of the library (this file itself).
// fxcStart(id,freq) - shows current estimated total in object 'id'
// where 'freq' (optional) sets the number of milliseconds between updates
// (0 for none, undefined meaning per download).
//

var fxc_separator = ',';
var fxc_style = '';
var fxc_feed = 'fxcounter.js';
var fxc_lib = 'fxcounter_lib.js';

//
// The Library
//
// Don't change anything below this line unless you want to change the
// actual program.
//

var timeoutTOT;
var timeoutVAL;
var fxc_feedurl;

function fxcFeedURL() {
	var scripts = document.getElementsByTagName('script');
	for (var i=0; i< scripts.length; i++) {
		if (scripts[i].src.match(fxc_lib)) {
			fxc_feedurl = scripts[i].src.replace(fxc_lib,fxc_feed);
			break;
		}
	}
}

function fxcLoadValues() {
	var vfile = document.createElement('script');
	vfile.src = fxc_feedurl+'?'+new Date().getTime();
	vfile.type='text/javascript';
	document.getElementsByTagName('head')[0].appendChild(vfile);
}

function fxcReloadValues() {
	if ('undefined'!=typeof(timeoutVAL)) { clearTimeout(timeoutVAL); }
	if ('undefined'==typeof(fxc_updintv)) {
		timeoutVAL=setTimeout('fxcReloadValues()',200);
	} else {
		timeoutVAL=setTimeout('fxcLoadValues()',fxc_updintv*1000);
		timeoutVAL=setTimeout('fxcReloadValues()',fxc_updintv*1000);
	}
}

function fxcNumber(num) {
	num=num+''; // force num to be a string
	for(var i=3; i<num.length; i+=3) {
		num=num.replace(/(^|\s)(\d+)(\d{3})/,'$2'+fxc_separator+'$3');
	}
	return num;
}

function fxcZeroFill(val) {
	return ((val<10)?'0'+val:val);
}

function fxcDateString(ms) {
	var ts = new Date(ms);
	var wd = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[ts.getDay()];
	var yr = ts.getFullYear();
	var mon = fxcZeroFill(ts.getMonth()+1);
	var dat = fxcZeroFill(ts.getDate());
	var hrs = fxcZeroFill(ts.getHours());
	var min = fxcZeroFill(ts.getMinutes());
	var tz_sign = ((ts.getTimezoneOffset>0)?'-':'+');
	var tz = Math.abs(ts.getTimezoneOffset());
	var tz_hrs = fxcZeroFill(Math.floor(tz/60));
	var tz_min = fxcZeroFill(tz-Math.floor(tz/60)*60);
	return (wd+' '+yr+'-'+mon+'-'+dat+' '+hrs+':'+min+' ['+tz_sign+tz_hrs+':'+tz_min+']');
}

function fxcTitle(id) {
	document.getElementById(id).setAttribute('title','Estimate based on '+fxcNumber(fxc_total)+' downloads at a rate of '+Math.round(fxc_dps*10)/10+' per second on '+fxcDateString(fxc_timestamp*1000));
}

function fxcStyle(id) {
	document.getElementById(id).style.cssText=fxc_style;
}

function fxcEstimate() {
	var now = new Date().getTime();
	return Math.floor(fxc_total + (now-fxc_timestamp*1000)*fxc_dps/1000);
}

function fxcTotal(id,freq,ts) {
	if ('undefined'==typeof(fxc_total)) {
		timeoutTOT=setTimeout('fxcTotal("'+id+'",'+freq+','+ts+')',200);
	} else {
		if (ts != fxc_timestamp) {
			fxcTitle(id);
			if (0<freq) { // not user set
				freq=(fxc_dps>20)?50:1000/fxc_dps;
			}
			ts = fxc_timestamp;
		}
		document.getElementById(id).firstChild.nodeValue=fxcNumber(fxcEstimate());
		if (0!=freq) { timeoutTOT=setTimeout('fxcTotal("'+id+'",'+freq+','+ts+')',Math.abs(freq)); }
	}
}

function fxcStart(id,freq) {
	fxcStyle(id);
	fxcFeedURL();
	if ('undefined'!=typeof(freq)) { freq=-freq; } // user set freq negative
	else { freq=100; } // default value
	if ('undefined'==typeof(timeoutVAL)) { fxcLoadValues(); }
	if (0!=freq) { fxcReloadValues(); }
	fxcTotal(id,freq,0);
}

var fxc_version='0.2.3.b';

// TIAF!
