var downloadCount = "???";
var eclipseCount = "???";
var cliCount = "???";
var samplesCount = "???";
var repositoryCount = "???";
var phpCount = "???";
var javaCount = "???";
var request = false;
var loadTimer = null;
var loadInterval = 86400000;   // Once an day
var updateTimer = null;
var updateInterval = 60000;   // Once an minute
var basicView = true;
var totals = null;

// A little function to take an integer string, strip out current formatting,
// and reformat it according to a pattern string. '#' characters in the pattern
// are substituted with digits from the integer string, other pattern characters
// are output literally into the returned string. This particular function
// was written by Dave Ruske (www.ruske.net) and can be used for any commercial
// or non-commercial purpose, free of charge, with or without crediting the
// author. In case you were wondering...
function formatInteger( integer, pattern )
{
	var result = '';

	integer = integer.toString();
	
	integerIndex = integer.length - 1;
	patternIndex = pattern.length - 1;

	while ( (integerIndex >= 0) && (patternIndex >= 0) )
	{
		var digit = integer.charAt( integerIndex );
		integerIndex--;
		
		// Skip non-digits from the source integer (eradicate current formatting).
		if ( (digit < '0') || (digit > '9') )  continue;
	
		// Got a digit from the integer, now plug it into the pattern.
		while ( patternIndex >= 0 )
		{
			var patternChar = pattern.charAt( patternIndex );
			patternIndex--;
			
			// Substitute digits for '#' chars, treat other chars literally.
			if ( patternChar == '#' )
			{
				result = digit + result;
				break;
			}
			else
			{
				result = patternChar + result;
			}
		}
	}
	
	return result;
}

function loadData( url, handler ) 
{
	if (!request)
	{
		if (window.XMLHttpRequest)
		{
		          // If IE7, Mozilla, Safari, etc: Use native object
		          request = new XMLHttpRequest()
		}
		else 
		{
			if (window.ActiveXObject)
			{
		          // ...otherwise, use the ActiveX control for IE5.x and IE6
		          request = new ActiveXObject("Microsoft.XMLHTTP"); 
		    }
		}
	}
	
	if (request)
	{
		request.onreadystatechange = handler;
		request.open( "GET", url );
		request.setRequestHeader( "Cache-Control", "no=cache" );
		request.send( null );
	}
}

function handleUpdate()
{

    if ( request.readyState ==4 )
    {
	if ( request.status == 200 )
	{
		totals = eval( '(' + request.responseText + ')');
		formatData();
	}
	
	if (basicView)
	{
		hideExtended(null);
	}
	else
	{
		showExtended(null);
	}
    }
}

function formatData()
{
	downloadCount= formatInteger( parseInt(totals.totalDownloads), "###,###,###,###" );
	eclipseCount= formatInteger( parseInt(totals.eclipse), "###,###,###,###" );
	cliCount= formatInteger( parseInt(totals.commandLine), "###,###,###,###" );
	samplesCount= formatInteger( parseInt(totals.samples), "###,###,###,###" );
	repositoryCount= formatInteger( parseInt(totals.repository), "###,###,###,###" );
	phpCount= formatInteger( parseInt(totals.eclipsePHP), "###,###,###,###" );
	javaCount= formatInteger( parseInt(totals.eclipseJava), "###,###,###,###" );
}

function initiateUpdate( event ) 
{
	loadData( "/ZeroStats/resources/counterStats2", handleUpdate );
}

function updateEstimate( event )
{
	totals.totalDownloads = totals.totalDownloads + totals.totalGrowth;
	totals.eclipse = totals.eclipse + totals.eclipseGrowth;
	totals.eclipseJava = totals.eclipseJava + totals.eclipseJavaGrowth;
	totals.eclipsePHP = totals.eclipsePHP + totals.eclipsePHPGrowth;
	totals.commandLine = totals.commandLine + totals.commandLineGrowth;
	totals.samples = totals.samples + totals.samplesGrowth;
	totals.repository = totals.repository + totals.repositoryGrowth;
	
	formatData();
	hideExtended(null);
}

function visitZero()
{
   window.open( "http://www.projectzero.org/download" );
}

function initialLoad(event)
{
	initiateUpdate(event);
}

function hideExtended(evt) 
{
	basicView = true;
//	document.getElementById("backdrop").src = "/ZeroStats/images/backdrop.png"; 
    document.getElementById("counter").innerHTML = downloadCount;
	document.getElementById("counter").className = "counter";
} 

loadTimer = setInterval( "initiateUpdate( null )", loadInterval );
updateTimer = setInterval( "updateEstimate( null )", updateInterval );
