// countries[countryIndex][0] = String:	country name
// countries[countryIndex][1] = Array:	CountrySources[]
	// CountrySources[sourceIndex][0] = String:	country source name
	// CountrySources[sourceIndex][1] = Number:	country source reported death toll
	// CountrySources[sourceIndex][3] = Number: country source percentage difference from median
	// CountrySources[sourceIndex][2] = Number:	country source grouping
// countries[countryIndex][2] = Number:	country maximum difference percentage
// countries[countryIndex][3] = Number:	country median death toll
// countries[countryIndex][4] = Array:	SourceGroups[]

// sources[sourceIndex][0] =	String: source name
// sources[sourceIndex][1] =	Array: SourceNumbers[]
	// SourceNumbers[countryIndex][0] =	String: country name
	// SourceNumbers[countryIndex][1] =	Number: reported death toll

var boxHeight = 460;	// height in px of each country's chart
var sourceHeight = 23;	// height in px of each source

var boxWidth = 400;		// width in px of each country's chart
var initialOffset = 375;	// initial offset from left of screen of country charts

var sourceListUpStatus = false;
var sourcesPerColumn = 10;

var maxGroups = Math.floor(boxHeight/sourceHeight);
var groupArray = [];

var colors = ['aqua','orange','lime','fuchsia','red','silver','blue','yellow','teal','maroon','olive','green','purple'];

/* Raw arrays of data from arguments passed through parseArguments() function */
var source = [];
var country = [];
var number = [];

/* Unique sources and countries */
var sources = [];
var sourcesBackup = [];
var countries = [];
var countriesBackup = [];

var bibliographies = [new Array('Britannica','Britannica: <em>Encyclopaedia Britannica</em>, 15th edition, 1992 printing'),new Array('Clodfelter','Clodfelter, Michael, <em>Warfare and Armed Conflict: A Statistical Reference to Casualtyand Other Figures, 1618-1991</em>'),new Array('Davies','Davies, Norman, <em>Europe A History</em> (1998)'),new Array('Eckhardt','Eckhardt, William: 3-page table of his war statistics printed in <em>World Military and Social Expenditures 1987-88</em> (12th ed., 1987) by Ruth Leger Sivard'),new Array('Ellis','Ellis, John, <em>World War II: a statistical survey</em> (Facts on File, 1993) "killed and missing\" unless otherwise noted.'),new Array('Guardian','<em>The Guardian</em> [London] 30 Apr. 1994 (wires 2 May 94), citing J. Erickson and Helmut Trotnow'),new Array('HarperCollins','<em>Harper Collins Atlas of the Second World War</em>, John Keegan, ed., (1997)'),new Array('Hochschild','Hochschild, Adam, <em>The Unquiet Ghost: Russians Remember Stalin</em> (Penguin, 1994)'),new Array('Info. Please','<em>Information Please Almanac</em> (1991 edition)'),new Array('Johnson','Johnson, Paul, <em>Modern Times</em>'),new Array('Keegan','Keegan, John, <em>The Second World War</em> (1989)'),new Array('Kinder','<em>The Anchor Atlas of World History</em> (1978)'),new Array('Messenger','Messenger, Charles, <em>The Chronological Atlas of World War Two</em> (Macmillan, 1989)'),new Array('Mazower','Mazower, Mark, <em>Dark Continent: Europe\'s Twentieth Century</em> (1998)'),new Array('Shabad','Shabad, Steven, <em>World Press Review</em> v42n7 (Jul 1995)'),new Array('S&amp;S','Small, Melvin &amp; Singer, Joel David, <em>Resort to Arms : International and Civil Wars 1816-1965</em> (1982): battle deaths.'),new Array('Urlanis','Urlanis, Boris, <em>Wars and Population</em> (1971)'),new Array('Wallechinsky','Wallechinsky, David, <em>Twentieth Century / History With the Boring Parts Left Out</em> (1995)')];

var data = [];
var dataBackup = [];

var scaleRange = 0;

// Create the chart
function chart()
{

	var tempArray = [];
	
	// Find the maximum percentage difference
	// for each of the countries, and the highest
	// one becomes the max/min of the scale
	for (countryIndex=0;countryIndex<countries.length;countryIndex++)
	{					
		
		for (sourceIndex=0;sourceIndex<countries[countryIndex][1].length;sourceIndex++)
		{
			tempArray.push(parseFloat(countries[countryIndex][1][sourceIndex][1]));
		}
		
		currentMaxDiffRange = maxDiffRange(tempArray);
		countries[countryIndex][2] = currentMaxDiffRange;
		countries[countryIndex][3] = MEDIAN(tempArray);
		
		if (currentMaxDiffRange>scaleRange)
		{
			scaleRange = parseFloat(currentMaxDiffRange);
		}
		
		multiQuickSort(countries[countryIndex][1], 1);
		
		tempArray = [];
		
	}
		
	scaleRange += 30;
	
	groupSources();
	
	multiQuickSort(countries, 2);
	countries.reverse();
	
	// make backups of the original Source/Country lists
	sourcesBackup = sources;
	countriesBackup = countries;
	$("#chartbox").html(drawCountries());
}

// recreate the chart after an event triggers change in the data
function reChart()
{

	scaleRange = 0;
	data = dataBackup;
	var newData = [];
	var uncheckedSources = [];
	var uncheckedCountries = [];
	source = [];
	country = [];
	number = [];
	countries = [];
	sources = [];
	groupArray = [];
	
	for (sI=0;sI<sourcesBackup.length;sI++)
	{
		currentBoxCheckedStatus = $('#sourceBox' + sI).attr('checked');
		// DEBUG alert(currentBoxCheckedStatus);
		if (currentBoxCheckedStatus != true)
		{
			uncheckedSources.push(sourcesBackup[sI][0]);
			// DEBUG alert(sourcesBackup[sI][0]);
		}
	}
	
	for (cI=0;cI<countriesBackup.length;cI++)
	{
		currentBoxCheckedStatus = $('#countryBox' + cI).attr('checked');
		
		if (currentBoxCheckedStatus != true)
		{
			uncheckedCountries.push(countriesBackup[cI][0]);
		}
		
	}
	
	for (dI=0;dI<data.length;dI++)
	{
		if (((jQuery.inArray(data[dI][0],uncheckedSources)) == (-1)) && ((jQuery.inArray(data[dI][1],uncheckedCountries)) == (-1)))
		{
			source.push(data[dI][0]);
			country.push(data[dI][1]);
			number.push(data[dI][2]);
			newData.push(new Array(source[source.length-1],country[country.length-1],number[number.length-1]));
		}
		else
		{
			//DEBUG alert('CHECKING');
		}
	}
	
	data = newData;
	
	createUniqueArraysSuccess = createUniqueArrays();
	
	if (!(createUniqueArraysSuccess)) {alert('There was an error creating source and country lists');}
	
	var tempArray = [];
	
	// Find the maximum percentage difference
	// for each of the countries, and the highest
	// one becomes the max/min of the scale
	for (countryIndex=0;countryIndex<countries.length;countryIndex++)
	{					
		
		for (sourceIndex=0;sourceIndex<countries[countryIndex][1].length;sourceIndex++)
		{
			tempArray.push(parseFloat(countries[countryIndex][1][sourceIndex][1]));
		}
		
		currentMaxDiffRange = maxDiffRange(tempArray);
		countries[countryIndex][2] = currentMaxDiffRange;
		countries[countryIndex][3] = MEDIAN(tempArray);
		
		if (currentMaxDiffRange>scaleRange)
		{
			scaleRange = parseFloat(currentMaxDiffRange);
		}
		
		multiQuickSort(countries[countryIndex][1], 1);
		
		tempArray = [];
		
	}
		
	scaleRange += 50;
	
	groupSources();
	
	multiQuickSort(countries, 2);
	countries.reverse();
	
	$("#chartbox").html(drawCountries());
	
}

// Takes arguments in the format (source, country, number, ...)
// and sends sources to the source[] array, countries to the
// country[] array, and numbers to the number[] array
function parseArguments()
{
	var items = parseArguments.arguments.length;
	var loopNumber = 0;
	var currentLoop = 1;
	var typeError = false;
	for (i = 0;i < items;i++)
	{
		switch (currentLoop) {
			case 1:  
				if (isNaN(parseArguments.arguments[i])) {
					source.push(parseArguments.arguments[i]);
				}
				else {
					typeError = true;
				}
				break;
			case 2: 
				if (isNaN(parseArguments.arguments[i])) {
					country.push(parseArguments.arguments[i]);
				}
				else {
					typeError = true;
				}
				break;
			case 3:  
				if (!(isNaN(parseArguments.arguments[i]))) {
					number.push(parseArguments.arguments[i]);
				}
				else {
					typeError = true;
				}
				break;
		}
		if (typeError) {break;}
		if (currentLoop == 3) {currentLoop = 1;}
		else currentLoop++;
	}
	
	for (i=0;i<source.length;i++)
	{
		data[i] = new Array(source[i],country[i],number[i],0);
	}
	
	dataBackup = data;
	
	if (typeError) {alert('A value of the wrong type was submitted. Please check to make sure all sources and countries are not numbers, and all numbers do not contain letters.');}
	
	if (!(createUniqueArrays())) {alert('There was an error creating source and country lists');}
	
}

// Populates the "sources" and "countries" array with
// only the unique values
function createUniqueArrays()
{

	var sourceItems = source.length;
	var countryItems = country.length;
	var countryNumbers = [];
	var sourceNumbers = [];
	
	if ((sourceItems == 0) || (countryItems == 0)) {return false;}
	
	// Add unique sources to sources[] array
	for (i = 0;i < sourceItems;i++)
	{
		sourceNumbers = [];
		if ((multiExistsInArray(source[i],sources)) == false)
		{
			
			for (dataIndex=0;dataIndex<data.length;dataIndex++)
			{
				if (data[dataIndex][0]==source[i])
				{
					sourceNumbers.push(new Array(data[dataIndex][1],data[dataIndex][2]));
				}
			}
			sources.push(new Array(source[i],sourceNumbers));
		}
	}
	
	// Add unique countries to countries[] array
	for (i = 0;i < countryItems;i++)
	{
		countryNumbers = [];
		// DEBUG alert(multiExistsInArray(country[i],countries));
		if ((multiExistsInArray(country[i],countries)) == false)
		{
			// DEBUG alert(country[i] + ' ' + multiExistsInArray(country[i],countries));
			for (dataIndex=0;dataIndex<data.length;dataIndex++)
			{
				if (data[dataIndex][1]==country[i])
				{
					countryNumbers.push(new Array(data[dataIndex][0],data[dataIndex][2]));
				}
			}
			countries.push(new Array(country[i],countryNumbers));
		}
	}
	
	return true;
}

// Draw the charts for the countries
function drawCountries()
{

	var outputHTML = '';
	var currSourceHTML = '';
	var countryDistance = initialOffset;
	for (countryIndex=0;countryIndex<countries.length;countryIndex++)
	{
		
		outputHTML += '<div class=\"countryChart\" id=\"country' + countryIndex + '\" style=\"left: ' + countryDistance + 'px;\" >';
		outputHTML += '<h3>' + countries[countryIndex][0] + '</h3>';
		
		sourceDistance = 5;
		// TO-DO: CREATE GROUPS, LOOP THROUGH GROUPS TO GENERATE OUTPUT
		for (groupNumber=0;groupNumber<=(maxGroups);groupNumber++)
		{
			currSourceHTML = '';
			for (sourceIndex=0;sourceIndex<countries[countryIndex][1].length;sourceIndex++)
			{
				if (countries[countryIndex][1][sourceIndex][3] == groupNumber)
					currSourceHTML += ', <span class=\"sourceName\"><span class=\"trigger\">' + countries[countryIndex][1][sourceIndex][0] + '</span><span class=\"popup\">Reported Death Toll: ' + addCommas(countries[countryIndex][1][sourceIndex][1]) + getBibliography(countries[countryIndex][1][sourceIndex][0]) + '</span></span>';
				
			}
			
			if (currSourceHTML != '')
			{
				currSourceHTML = currSourceHTML.substring(2,currSourceHTML.length);
				outputHTML += '<span class=\"source\" style=\"top: ' + sourceDistance + 'px;\"><span class=\"percent\">' + groupArray[groupNumber+1] + '%</span>' + currSourceHTML + '</span>';
				currSourceHTML = '';
			}
			
			sourceDistance += parseInt(sourceHeight);
			
		}
				
		outputHTML += '<span class=\"source\" style=\"top: -7px;\"><span class=\"percent\">' + scaleRange + '%</span></span>';
		outputHTML += '<span class=\"source\" style=\"top: 454px;\"><span class=\"percent\">-' + scaleRange + '%</span></span>';
		outputHTML += '</div>';
		
		countryDistance += boxWidth;
	}
	
	return outputHTML;
}

// Checks to see if the string "checkString" exists
// in the array "checkArray". It ignores case
// ONLY FOR createUniqueSources
function multiExistsInArray(checkString,checkArray)
{
	for (searchIndex=0;searchIndex<checkArray.length;searchIndex++)
	{
		if (checkArray[searchIndex][0] == checkString)
		{
			return true;
		}
	}
	
	return false;
	
}

// Returns the percentage difference of the two numbers
// fed into it
function percentageDiff(number1,number2)
{
	
	// Not sure why this is necessary, but
	// the function returns a near-zero number
	// every time unless the numbers are typed
	// as floats.
	arg1 = parseFloat(number1);
	arg2 = parseFloat(number2);
	
	numerator = (arg1 - arg2);
	numerator = Math.abs(numerator);
	
	divisor = ((arg1+arg2)/2);
	
	theDiff = ((numerator/divisor)*100);
	
	return parseInt(theDiff.toFixed(3));
}

// Determine grouping for source based on percentage
// difference from median for that country
function groupSources()
{

	var groupPercentages = (parseFloat(scaleRange)*2)/parseFloat(maxGroups);
	var currGroup;
	
	groupArray[0] = parseFloat(scaleRange);
	
	for (groups=1;groups<maxGroups+1;groups++)
	{
		currPercent = groupArray[groups-1] - groupPercentages;
		currPercent = currPercent.toFixed(2);
		groupArray.push(parseFloat(currPercent));
	}
	
	for (countryIndex=0;countryIndex<countries.length;countryIndex++)
	{	
		medianToll = parseInt(countries[countryIndex][3]);
		// DEBUG alert('Grouping country: ' + countries[countryIndex][0]);
		for (sourceIndex=0;sourceIndex<countries[countryIndex][1].length;sourceIndex++)
		{
			sourceToll = parseInt(countries[countryIndex][1][sourceIndex][1]);
			
			if (medianToll < sourceToll)
				sourceMedianDiff = percentageDiff(medianToll,sourceToll);
			else
				sourceMedianDiff = percentageDiff(medianToll,sourceToll) * (-1);
				
			currGroup = -1;
			
			for (groupIndex=0;groupIndex<groupArray.length;groupIndex++)
			{
				
				if ((sourceMedianDiff < groupArray[groupIndex]) && (sourceMedianDiff >= groupArray[groupIndex+1]))
					currGroup = groupIndex;
			
			}			
			
			countries[countryIndex][1][sourceIndex][2] = sourceMedianDiff;
			countries[countryIndex][1][sourceIndex][3] = currGroup;
			
		}
		
	}

}

// Creates checkbox list of sources
function listSources()
{
	
	var sourceBoxHTML = '';
	multiQuickSort(sources, 0);
	columnSources = 0;
	
	sourceBoxHTML += '<div class=\"column\">';
	
	for (sI=0;sI<sources.length;sI++)
	{
	
		if (columnSources == sourcesPerColumn)
		{
			sourceBoxHTML += '</div><div class=\"column\">';
			columnSources = 0;
		}
		
		sourceBoxHTML += '<div class=\"sourceBox\"><input class="sourceCheck" type=\"checkbox\" name=\"sourceBox' + sI + '\" id=\"sourceBox' + sI + '\" checked=\"checked\" />';
		sourceBoxHTML += '<label class=\"sourceLabel\" for=\"sourceBox' + sI + '\">' + sources[sI][0] + '</label></div>';
		
		columnSources += 1;
		
	}
	
	sourceBoxHTML += '</div>';
	
	$('#sourceBox').html(sourceBoxHTML);
}

// Creates checkbox list of sources
function listCountries()
{
	
	var countryBoxHTML = '';
	multiQuickSort(countries, 0);
	columnSources = 0;
	
	countryBoxHTML += '<div class=\"column\">';
	
	for (cI=0;cI<countries.length;cI++)
	{
	
		if (columnSources == sourcesPerColumn)
		{
			countryBoxHTML += '</div><div class=\"column\">';
			columnSources = 0;
		}
		
		countryBoxHTML += '<div class=\"countryBox\"><input class="countryCheck" type=\"checkbox\" name=\"countryBox' + cI + '\" id=\"countryBox' + cI + '\" checked=\"checked\" />';
		countryBoxHTML += '<label class=\"countryLabel\" for=\"countryBox' + cI + '\">' + countries[cI][0] + '</label></div>';
		
		columnSources += 1;
		
	}
	
	countryBoxHTML += '</div>';
	
	$('#countryBox').html(countryBoxHTML);
	
}

// Returns a string with the bibliographical
// information for a source matching the name
// provided
function getBibliography(sourceName)
{
	var bibliography = '';
	
	for (b=0;b<bibliographies.length;b++)
	{
		if (sourceName == bibliographies[b][0])
		{
			bibliography += '<br />';
			bibliography += '<span class=\"bibliography\">' + bibliographies[b][1] + '</span>';
			return bibliography;
		}
		else
		{
			bibliography = '';
		}
		
	}
	
	return bibliography;
	
}

// Calculates the maximum percentage difference
// for this particular array of numbers
function maxDiffRange(diffArray)
{

	arrayMin = MIN(diffArray);
	arrayMax = MAX(diffArray);
	arrayMed = MEDIAN(diffArray);
	
	if ((arrayMax - arrayMed)>(arrayMed - arrayMin))
	{
		// DEBUG alert('Doing MAX/MED difference calculation: ' + arrayMed + '/' + arrayMax);
		maxDifference = percentageDiff(arrayMed,arrayMax);
	}
	else
	{
		// DEBUG alert('Doing MIN/MED difference calculation');
		maxDifference = percentageDiff(arrayMin,arrayMed);
	}
	
	return maxDifference.toFixed(1);
	
}

// Calculates and returns the MAX value of an array of #s
function MAX(numArray)
{

	maxNum = numArray[0];
	
	for (currNum=1;currNum<numArray.length;currNum++)
	{
		if (numArray[currNum] > maxNum)
		{
			maxNum = numArray[currNum];
		}
	}
	
	return maxNum;
}

// Calculates and returns the MIN value of an array of #s
function MIN(numArray)
{

	minNum = numArray[0];
	
	for (currNum=1;currNum<numArray.length;currNum++)
	{
		if (numArray[currNum] < minNum)
		{
			minNum = numArray[currNum];
		}
	}
	
	return minNum;
}

// Returns the median of the set of numbers in the given array
function MEDIAN(numArray)
{
	numArray.sort();
	medianIndex = numArray.length / 2;
	//alert('Medianindex: ' + numArray.length + '/' + 2 + ' = ' + medianIndex);
	if ((numArray.length % 2) == 0)
	{
		median = ((numArray[medianIndex] + numArray[medianIndex - 1])/2).toFixed(0);
		return median;
	}
	else {
		median = numArray[Math.floor(medianIndex)];
		return median;
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.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;
}

			
$(document).ready(function(){
	parseArguments('Haywood','Entire War',50000000,'Keegan','Entire War',50000000,'Messenger','Entire War',50000000,'The Times','Entire War',50000000,'Urlanis','Entire War',50000000,'DoMH','Entire War',41000000,'Kinder','Entire War',55000000,'Hammond','Entire War',55000000,'Guiness Book','Entire War',56400000,'Sivard','Entire War',40151000,'Brzezinski','Entire War',71000000,'Rummel','Entire War',84609000);
	parseArguments('Britannica','China',1310224,'Compton\'s','China',1310224,'HarperCollins','China',1324000,'Clodfelter','China',1319958,'Info. Please','China',1324516,'Ellis','China',1400000,'Eckhardt','China',2000000,'S&amp;S','China',2100000,'Wallechinsky','China',1319958,'Urlanis','China',2500000,'Encarta','China',3500000,'Rummel','China',3832000,'Kinder','China',6400000);
	parseArguments('Ellis','Poland',78000,'Encarta','Poland',120000,'Urlanis','Poland',123200,'Britannica','Poland',123178,'Davies','Poland',123178,'Compton\'s','Poland',125000,'S&amp;S','Poland',320000,'Eckhardt','Poland',600000,'Info. Please','Poland',664000,'HarperCollins','Poland',850000);
	parseArguments('HarperCollins','Germany',2850000,'Ellis','Germany',3250000,'Compton\'s','Germany',3250000,'Info. Please','Germany',3250000,'Clodfelter','Germany',3250000,'Britannica','Germany',3500000,'S&amp;S','Germany',3500000,'Encarta','Germany',3500000,'Keegan','Germany',4000000,'Kinder','Germany',4000000,'Urlanis','Germany',4500000,'Eckhardt','Germany',4750000);
	parseArguments('Keegan','India',24000,'Eckhardt','India',24000,'Britannica','India',24338,'Urlanis','India',24338,'Info. Please','India',32121,'HarperCollins','India',36092,'Ellis','India',36100,'Clodfelter','India',48675)
	parseArguments('Clodfelter','Japan',3565878,'Keegan','Japan',1200000,'S&amp;S','Japan',1250000,'Info. Please','Japan',1270000,'Eckhardt','Japan',1500000,'HarperCollins','Japan',1506000,'Encarta','Japan',1700000,'Ellis','Japan',1740000,'Compton\'s','Japan',1862499,'Urlanis','Japan',2000000);
	parseArguments('Info. Please','USSR',6115000,'Compton\'s','USSR',6750000,'Keegan','USSR',7000000,'S&amp;S','USSR',7500000,'Eckhardt','USSR',7500000,'Davies','USSR',8500000,'E&amp;D','USSR',8668400,'Overy','USSR',8668400,'Mazower','USSR',9500000,'Urlanis','USSR',10000000,'Volkogonov','USSR',10000000,'Ellis','USSR',11000000,'Britannica','USSR',11000000,'Encarta','USSR',13000000,'Kinder','USSR',13600000,'Wallechinsky','USSR',13600000,'HarperCollins','USSR',14500000,'Guardian','USSR',22000000,'Sokolov','USSR',26400000,'Gorbachev','USSR',8668000);
	parseArguments('S&amp;S','Yugoslavia',5000,'Compton\'s','Yugoslavia',75000,'Encarta','Yugoslavia',300000,'Urlanis','Yugoslavia',300000,'Britannica','Yugoslavia',305000,'Davies','Yugoslavia',305000,'Info. Please','Yugoslavia',305000,'Eckhardt','Yugoslavia',400000);
	parseArguments('Urlanis','Austria',270000,'Info. Please','Austria',280000,'Eckhardt','Austria',280000,'HarperCollins','Austria',380000);
	parseArguments('HarperCollins','Czechoslovakia',6683,'Info. Please','Czechoslovakia',6683,'Clodfelter','Czechoslovakia',6683,'Britannica','Czechoslovakia',10000,'Encarta','Czechoslovakia',10000,'Eckhardt','Czechoslovakia',30000,'Urlanis','Czechoslovakia',46000);
	parseArguments('Ellis','France',122000,'Compton\'s','France',166195,'Keegan','France',200000,'Eckhardt','France',200000,'Info. Please','France',201568,'S&amp;S','France',212500,'HarperCollins','France',210671,'Britannica','France',213324,'Davies','France',213324,'Clodfelter','France',245000,'Encarta','France',250000,'Urlanis','France',250000);
	parseArguments('S&amp;S','Greece',10000,'Eckhardt','Greece',10000,'HarperCollins','Greece',16357,'Info. Please','Greece',17024,'Ellis','Greece',18300,'Compton\'s','Greece',77200,'Britannica','Greece',88300,'Clodfelter','Greece',88300);
	parseArguments('S&amp;S','Hungary',40000,'Compton\'s','Hungary',75000,'Encarta','Hungary',120000,'Ellis','Hungary',136000,'Urlanis','Hungary',136000,'Info. Please','Hungary',147435,'Britannica','Hungary',200000,'Eckhardt','Hungary',400000);
	parseArguments('Compton\'s','Italy',129774,'S&amp;S','Italy',77500,'Info. Please','Italy',149496,'Eckhardt','Italy',150000,'Ellis','Italy',226900,'Britannica','Italy',242232,'HarperCollins','Italy',279820,'Encarta','Italy',330000,'Urlanis','Italy',400000);
	parseArguments('Eckhardt','Netherlands',6000,'S&amp;S','Netherlands',6200,'Compton\'s','Netherlands',6238,'Info. Please','Netherlands',6500,'Britannica','Netherlands',7900,'Clodfelter','Netherlands',7900,'Keegan','Netherlands',10000,'Urlanis','Netherlands',12000,'HarperCollins','Netherlands',13700,'Ellis','Netherlands',13700);
	parseArguments('Britannica','Philippines',27000,'Eckhardt','Philippines',27000);
	parseArguments('Compton\'s','Romania',80000,'Encarta','Romania',200000,'S&amp;S','Romania',295000,'Britannica','Romania',300000,'Urlanis','Romania',300000,'Davies','Romania',300000,'Eckhardt','Romania',340000,'Info. Please','Romania',519822,'Ellis','Romania',381000,'HarperCollins','Romania',519822);
	parseArguments('Keegan','United Kingdom',244000,'Britannica','United Kingdom',264443,'Davies','United Kingdom',264443,'S&amp;S','United Kingdom',270000,'HarperCollins','United Kingdom',271311,'Urlanis','United Kingdom',290000,'Ellis','United Kingdom',305800,'Eckhardt','United Kingdom',350000,'Compton\'s','United Kingdom',353652,'Info. Please','United Kingdom',357116,'Clodfelter','United Kingdom',403195);
	parseArguments('Keegan','USA',292000,'HarperCollins','USA',292100,'Britannica','USA',292131,'Compton\'s','USA',293986,'Urlanis','USA',300000,'Info. Please','USA',405399,'DoD','USA',405399,'Ellis','USA',405400,'Encarta','USA',407318,'Wallechinsky','USA',407318,'Eckhardt','USA',408000,'S&amp;S','USA',408300);
	parseArguments('Eckhardt','Albania',20000,'Urlanis','Albania',28800);
	parseArguments('Keegan','Australia',23000,'Britannica','Australia',23365,'HarperCollins','Australia',23395,'Info. Please','Australia',26976,'Ellis','Australia',29400,'S&amp;S','Australia',33826,'Eckhardt','Australia',34000,'Clodfelter','Australia',37637,'AWM','Australia',39366);
	parseArguments('Ellis','Belgium',8000,'Compton\'s','Belgium',7760,'Info. Please','Belgium',8460,'HarperCollins','Belgium',9561,'S&amp;S','Belgium',9600,'Urlanis','Belgium',10000,'Britannica','Belgium',12000,'Clodfelter','Belgium',22651,'Eckhardt','Belgium',110000);
	parseArguments('Info. Please','Bulgaria',6671,'S&amp;S','Bulgaria',10000,'Britannica','Bulgaria',10000,'HarperCollins','Bulgaria',18500,'Eckhardt','Bulgaria',20000,'Urlanis','Bulgaria',25000);
	parseArguments('Keegan','Canada',37000,'Britannica','Canada',37476,'Urlanis','Canada',37476,'Eckhardt','Canada',39000,'S&amp;S','Canada',39300,'Ellis','Canada',39300,'HarperCollins','Canada',39319,'Info. Please','Canada',42042,'Clodfelter','Canada',42666);
	parseArguments('S&amp;S','Finland',42000,'Eckhardt','Finland',45000,'Compton\'s','Finland',52609,'HarperCollins','Finland',79047,'Info. Please','Finland',79047,'Britannica','Finland',82000,'Clodfelter','Finland',82000,'Urlanis','Finland',84000,'Ellis','Finland',89900);
	parseArguments('Keegan','New Zealand',10000,'Britannica','New Zealand',10033,'Urlanis','New Zealand',10033,'Info. Please','New Zealand',11625,'HarperCollins','New Zealand',12162,'Clodfelter','New Zealand',12262,'Eckhardt','New Zealand',17000,'S&amp;S','New Zealand',17300);
	parseArguments('HarperCollins','Spain',12000);
	parseArguments('Britannica','Brazil',943,'Info. Please','Brazil',943,'HarperCollins','Brazil',943,'Clodfelter','Brazil',943,'S&amp;S','Brazil',1000,'Eckhardt','Brazil',1000);
	parseArguments('Britannica','Denmark',1800,'HarperCollins','Denmark',4339,'Info. Please','Denmark',4339);
	parseArguments('S&amp;S','Ethiopia',5000,'Eckhardt','Ethiopia',5000);
	parseArguments('Urlanis','Luxemburg',4250);
	parseArguments('S&amp;S','Mongolia',3000,'Eckhardt','Mongolia',3000);
	parseArguments('Compton\'s','Norway',1000,'S&amp;S','Norway',2000,'Ellis','Norway',2000,'Eckhardt','Norway',2000,'Info. Please','Norway',2000,'Britannica','Norway',3000,'Clodfelter','Norway',3000,'HarperCollins','Norway',4780);
	parseArguments('Info. Please','South Africa',2473,'Keegan','South Africa',6000,'Britannica','South Africa',6840,'HarperCollins','South Africa',8681,'Clodfelter','South Africa',8681,'S&amp;S','South Africa',8700,'Ellis','South Africa',8700,'Eckhardt','South Africa',9000);
	chart();
	listSources();
	listCountries();
	
	$('#footer').css({opacity: .95});
	// Popup information bubble & source changer
	$(function () {
	
		$('h1').each(function() {
			
			var h1Link = $('a', this);
			
			$(h1Link.get(0)).click(function() {
				if (sourceListUpStatus)
				{
					$('#footer').animate({
						bottom: '-244px'
					}, 'slow', 'easeOutQuad');
					
					$('#arrow').html('Show');
					
					sourceListUpStatus = false;
				}
				else
				{
					$('#footer').animate({
						bottom: '0px'
					}, 'slow', 'easeOutQuad');
					
					$('#arrow').html('Hide');
					
					sourceListUpStatus = true;
				}
			});
			
			
		});
		$('.sourceBox').each(function() {
		
			var checkBox = $('.sourceCheck', this);
			var label = $('.sourceLabel', this);
			
			var time = 350;
			
			var beingUpdated = false;
			
			$(checkBox.get(0)).change(function () {
			
				$('#chartbox').css({
					display: 'block'
				}).animate({
					opacity: 0
				}, time, 'swing'
				);
				
				reChart();
				
				$('#chartbox').animate({
					opacity: 1.0
				}, time, 'swing'
				).css({
					display: 'block',
				});
				
			});
		
		});
		$('.countryBox').each(function() {
		
			var checkBox = $('.countryCheck', this);
			var label = $('.countryLabel', this);
			
			var time = 350;
			
			var beingUpdated = false;
			
			$(checkBox.get(0)).change(function () {
			
				$('#chartbox').css({
					display: 'block'
				}).animate({
					opacity: 0
				}, time, 'swing'
				);
				
				reChart();
				
				$('#chartbox').animate({
					opacity: 1.0
				}, time, 'swing'
				).css({
					display: 'block',
				});
				
			});
		
		});
		$('.sourceName').each(function () {
			var distance = 10;
			var time = 250;
			var hideDelay = 500;
	
			var hideDelayTimer = null;
	
			var beingShown = false;
			var shown = false;
			var trigger = $('.trigger', this);
			var info = $('.popup', this).css('opacity', 0);
	
	
			$([trigger.get(0), info.get(0)]).mouseover(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				if (beingShown || shown) {
					// don't trigger the animation again
					return;
				} else {
					// reset position of info box
					beingShown = true;
	
					info.css({
						top: -55,
						left: 5,
						display: 'block',
						backgroundColor: 'white'
					}).animate({
						top: '-=' + distance + 'px',
						opacity: 0.95
					}, time, 'swing', function() {
						beingShown = false;
						shown = true;
					});
				}
	
				return false;
			}).mouseout(function () {
				if (hideDelayTimer) clearTimeout(hideDelayTimer);
				hideDelayTimer = setTimeout(function () {
					hideDelayTimer = null;
					info.animate({
						top: '-=' + distance + 'px',
						opacity: 0
					}, time, 'swing', function () {
						shown = false;
						info.css('display', 'none');
					});
	
				}, hideDelay);
	
				return false;
			});
		});
	});
});