Exent = function() {
};


Exent.Counter = function(spanId, avgDownloadsPerDay, interval, baseCount, baseDate) {

	var m_AvgDownloadsPerDay = "";
	var m_UpdateInterval = "";
	var m_BaseCount = "";
	var m_BaseDate = "";
	var m_spanId = "";
	/*
	 * Return true if and only if strDate is in format dd/mm/yy HH:MM:ss (24hrs)
	 */
	function validateDateFormat(strDate) {
		var formatRegEx = new RegExp(/(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[12])\/\d{2} ([01][0-9]|2[0123]):[012345][0-9]:[012345][0-9]/);
		var isValid = strDate.match(formatRegEx);
		return isValid;
	}

	/*
	 * Parse date string to date object
	 */
	function parseDate(strDate) {
		var isValid = validateDateFormat(strDate);

		if (!isValid) {
			return null;
		}

		var day = new Number(strDate.substr(0, 2));
		var month = new Number(strDate.substr(3, 2)) - 1;
		var year = new Number("20" + strDate.substr(6, 2));
		var hours = new Number(strDate.substr(9, 2));
		var minutes = new Number(strDate.substr(12, 2));
		var seconds = new Number(strDate.substr(15, 2));
		var milliseconds = 0;

		var date = new Date(year, month, day, hours, minutes, seconds, milliseconds);
		return date;
	}

	/*
	 * Add commas to the count number
	 */
	function getCountString(count) {
		count += '';
		x = count.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}

	/*
	 * Calculate the current count number and return the count number with commas
	 */
	function getCount() {
		var startDate = parseDate(m_BaseDate);
		var startTime = startDate.getTime();
		var currentDate = new Date();
		var currentTime = currentDate.getTime();

		var timePassed = currentTime - startTime;
		var secondsPassed = Math.round(timePassed / 1000);

		var secondsInDay = 86400;
		var avgDownloadsPerSecond = m_AvgDownloadsPerDay / secondsInDay;

		var count = m_BaseCount + Math.ceil(avgDownloadsPerSecond * secondsPassed);
		var countString = getCountString(count);
		return countString;
	}

	/*
	 * Update the counter element with the new count
	 */
	function updateCounter() {
		document.getElementById(m_spanId).innerHTML = getCount();
		window.setTimeout(updateCounter, m_UpdateInterval);
	}

	/*
	 * Run the counter
	 */
	function init(spanId, avgDownloadsPerDay, interval, baseCount, baseDate) {		
		//init default params
		m_spanId = spanId;
		m_AvgDownloadsPerDay = (avgDownloadsPerDay)? avgDownloadsPerDay : Exent.Counter.Defines.AvgDownloadsPerDay;
		m_UpdateInterval = (interval)? interval : Exent.Counter.Defines.UpdateInterval;
		m_BaseCount = (baseCount)? baseCount : Exent.Counter.Defines.BaseCount;
		m_BaseDate = (baseDate)? baseDate : Exent.Counter.Defines.BaseDate;		

		updateCounter();
	}

	init(spanId, avgDownloadsPerDay, interval, baseCount, baseDate);
};

Exent.Counter.Defines = {
	AvgDownloadsPerDay : 20000, // Average downloads per day
	UpdateInterval : 2000, // Counter update interval in milliseconds
	BaseCount : 87254521, // Starting number on the counter
	BaseDate : "06/06/11 10:11:12" // Counting start Date in format dd/mm/yy // HH:MM:ss (24hrs)
};

