//------------------------------ document.ready
$(function() {
	window.regions = {
		africa: {
			GH: { 
					name: 'Ghana',
					'with': ['DFID', 'Bank of Ghana', 'GSS'],
					tech: ['MySQL', 'Paradox', 'Java (Desktop, Micro, Servlet)', 'Delphi/Pascal (Win32)'],
					area: ['CPI (Consumer Price Index)', 'Consumer Confidence Survey']
				},
			GM: { 
					name: 'Gambia',
					'with': ['ECOWAS', 'Gambia Bureau of Statistics'],
					tech: ['MySQL', 'Java (Destkop, Micro, Servlet)', 'Pascal(Delphi)'],
					area: ['CPI (Consumer Price Index)']						
				},
			SN: { 
					name: 'Senegal',
					'with': ['ECOWAS', 'ANSD'],
					tech: ['MySQL', 'Java (Destkop, Micro, Servlet)', 'Pascal(Delphi)'],
					area: ['CPI (Consumer Price Index)']
				},
			TG: { 
					name: 'Togo',
					'with': ['ECOWAS', 'CENETI'],
					tech: ['MySQL', 'Java (Destkop, Micro, Servlet)', 'Pascal(Delphi)'],
					area: ['CPI (Consumer Price Index)']
				},
			NG: { 
					name: 'Nigeria',
					'with': ['ECOWAS', 'National Bureau of Statistics'],
					tech: ['MySQL', 'Java (Destkop, Micro, Servlet)', 'Pascal(Delphi)'],
					area: ['CPI (Consumer Price Index)']					
				},
			TZ: { 
					name: 'Tanzania',
					'with': ['UNIDO', 'National Bureau of Statistics'],
					tech: ['SQL Server', 'C#/.NET'],
					area: ['Industrial Survey']										
				},
			MW: { 
					name: 'Malawi',
					'with': ['European Commission', 'National Bureau of Statistics'],
					tech: ['MySQL', 'Paradox', 'Java (Destkop, Micro, Servlet)', 'Pascal(Delphi)'],
					area: ['CPI (Consumer Price Index)']					
				}
		},
		
	    middle_east: {
			LB: { 
					name: 'Lebanon',
					'with': ['UNIDO', 'Ministry of Industry'],
					tech: ['SQL Server', 'C#/.NET'],
					area: ['Industrial Survey']													
				},
			IQ: { 
					name: 'Iraq',
					'with': ['UNIDO', 'Ministry of Industry'],
					tech: ['SQL Server', 'C#/.NET', 'Java (Micro, Servlet)'],
					area: ['Industrial Survey']					
				},
			AE: { 
					name: 'UAE (Dubai)',
					'with': ['DCCI (Dubai Chamber of Commerce)'],
					tech: ['MySQL', 'Oracle', 'Java (Micro, Servlet)', 'Pascal(Delphi)'],
					area: ['CPI (Consumer Price Index)']					
					
				},
			SA: { 
					name: 'Saudi Arabia',
					'with': ['UNIDO', 'Ministry of Industry'],
					tech: ['SQL Server', 'MS-Access', 'C#/.NET'],
					area: ['Industrial Survey']					
				}
		},
	
		asia: {
			PK: { 
					name: 'Pakistan',
					'with': ['INAS Project (Dutch Government)', 'Federal Bureau of Statistics'],
					tech: ['Paradox', 'COBOL'],
					area: ['National Accounts']										
				},
			LK: { name: 'Maldives',
					'with': ['ADB (Asian Development Bank)', 'UNDP', 'World Bank', 'Ministry of Planning & Development'],
					tech: ['MySQL', 'Java (Destkop, Micro, Servlet)', 'Pascal(Delphi)', 'Blaise', 'Paradox'],
					area: ['CPI (Consumer Price Index)', 'PPI (Producer Price Index)', 
					     'Large & Small Establishment Surveys', 'VPA (Vulnerability & Poverty Assessment)',
					     'HIES (Household Integrated Economic Survey']						
				}
		}
	};
	
	window.messages = {
		main: "Of my 20 years' IT work, the last ten I have been working primarily with statistics offices in the developing world "+
		    '(the map below indicates the countries, subject areas, technologies and institutions.) This has involved automating surveys '+
		    'and price index management (via <a href="http://www.prima-online.org/">PRIMA</a>.) I have developed PRIMA along with Willem van den Andel '+
		    '(<a href="http://www.andel-consultants.com/">Andel Consultants</a>) who has designed the statistical/economic framework. In addition, I '+
		    'am experimenting with a number of new web based technologies the links for which are given below.',
		
		survey_automation: 'For the last ten years, I have been providing consulting services in the area of survey automation working with various international '+
			'aid agencies (UN, ADB, World Bank, DFID.) I have worked with the statistics offices in Africa, Middle-East and Asia. and Pakistan. '+
			'The work involves designing, developing and transferring technology for the data entry and tabulation of surveys (household, '+
			'agriculture, small and large enterprise) using both desktop computers and PDAs (hand-held computers.)',
		
		prima: 'Willem van den Andel and I have developed the PRIMA price index management system. PRIMA&nbsp;is a set of flexible price '+
			'index tools that can be used for a variety of index systems including consumer and wholesale price indices. It can also be used for the '+
			'preparation of production indices for industry, agriculture, etc. It is being used at the Ghana Statistical Service, Bank of Ghana, '+
			'Maldives Ministry of Planning and Development, and Malawi National Statistical Office. In 2008 PRIMA was chosen by ECOWAS to be '+
			'implemented in four West African states (Nigeria, Senegal, The Gambia, and Togo) as part of a pilot project.',
			
		open_source: 'Keeping in mind the licensing burdens on developing countries, I try and maximize the use of open source software in the '+
			'statistical offices I work with. I emphasize Java development and MySQL databases. More recently I have started experimenting '+
			'with a number of open source APIs (Google, ECB Visualization) and development technologies (Adobe Flex, Android SDK.) '+
			'Links for some of the experiments are given below.',

		visualization: "Playing around with Google Visualization API and ECB Flex API (for SDMX data) with data from the World Bank and UNIDO"
	}

    window.chart = new google.visualization.ImageChart(document.getElementById('map_div'));
    window.region = 'africa';
    addCountryLinksForRegion();
	drawChart();

	$('#message').html(window.messages.main);
	
	$('#chart_links a').hover(function(e){
		addCountryLinksForRegion(e);
		drawChart(e);
	});

	$('.main_links a,.other_links a').mouseover(function(e){
		if (window.messages[$(e.target).attr('tag')] !== undefined)
			$('#message').html(window.messages[$(e.target).attr('tag')]);
		else
			$('#message').html(window.messages['main']);
	});
	
	$('.main_links a,.other_links a').mouseout(function(e){
		$('#message').html(window.messages['main']);
	});
});

//------------------------------ addCountryLinksForRegion
function addCountryLinksForRegion(e){
	if (e !==  undefined)
		window.region = $(e.target).attr('region');
	
	var sHtml = '';
	
	for (var sCntryCode in window.regions[window.region]){
		sHtml += '<div><a href="#" country="'+sCntryCode+'">'+
		window.regions[window.region][sCntryCode]['name']+'</a></div>';
	}
	
	$('#country_links').html(sHtml);
	$('#country_links a').hover(function(e){
		window.country = $(e.target).attr('country');
		showCountryDetails();
	});
}

//------------------------------ drawChart
function drawChart(e){
	$('#map_div').empty();
	
	var sRegion = 'africa';
	if (e !== undefined)
		sRegion = $(e.target).attr('region');
	
	var options = {cht: 't', chtm: sRegion, chs: '440x220', chco: 'FFFFFF,FFAD00,FF0000', 
			enableEvents:true};

	var dataTable = new google.visualization.DataTable();
	
	dataTable.addColumn('string');
	dataTable.addColumn('number');
    var iVal=1;
	for (var sCntryCode in window.regions[sRegion])
		dataTable.addRow([sCntryCode, iVal++]);
	
	window.chart.draw(dataTable, options);
	google.visualization.events.addListener(window.chart, 'onmouseover', mouseOverHandler);
}

//------------------------------ mouseOverHandler
function mouseOverHandler(event)  {
	if (event.region in window.regions[window.region]){
		window.country = event.region;		
		showCountryDetails();
	}
}

//------------------------------ showCountryDetails
function showCountryDetails(){
	$('#country').html(window.regions[window.region][window.country].name);
	showSpecificDetails('with');
	showSpecificDetails('tech');
	showSpecificDetails('area');
}

//------------------------------ showSpecificDetails
function showSpecificDetails(sTag){
	var sHtml = '';
	var sHtml = '';
	for (var i=0, len=window.regions[window.region][window.country][sTag].length; i<len; ++i){
		sHtml += '<div>'+window.regions[window.region][window.country][sTag][i]+'</div>';
	}
	$('#'+sTag).html(sHtml);
	
}

