var map;
var default_lat = 37.062500;
var default_lng = -95.677068;
var zcta_array = [
	'zcta',
	'population',
	'area_code',
	'households',
	'city',
	'avg_house_value',
	'state',
	'avg_house_income',
	'county',
	'persons_per_house',
	'population_white',
	'population_black',
	'elevation',
	'population_hispanic',
	'aliases',
	'msa',
	'msa_name',
	'pmsa'
];

function mapperOnLoad() {
	map = new GMap2(document.getElementById('map'));
    map.enableContinuousZoom();
    map.enableDoubleClickZoom();
    map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GOverviewMapControl());
	map.addControl(new GScaleControl());
	pt = new GLatLng(default_lat,default_lng);
    
	GEvent.addListener(map, 'click', function(marker,point) {
		addPoly(point);
	});

	map.setCenter(pt,4);
	resizeMap(document.zip_form.type);
}

function addPoly(point) {
	toggleLoading();
	if(document.zip_form['type'].value=='county') {
		getURL('GET','xml/countyclick.php?lng='+point.x+'&lat='+point.y,'loadCountyPoly','xml');
	} else {
		getURL('GET','xml/zipclick.php?lng='+point.x+'&lat='+point.y,'loadZipPoly','xml');
	}
}

function loadCountyPoly(xmlDoc) {
	map.clearOverlays();
	var info = xmlDoc.documentElement.getElementsByTagName('info');
	var resultObj = document.getElementById('result');
	if(info.length > 0) {
		var countyname = info[0].getAttribute('countyname');
		var statename = info[0].getAttribute('statename');
		var countycode = info[0].getAttribute('countycode');
	
		var polylines = xmlDoc.documentElement.getElementsByTagName('polyline');
		var points = [];
		for(var i=0; i<polylines.length; i++) {
			points.push(new GLatLng(parseFloat(polylines[i].getAttribute('lat')),parseFloat(polylines[i].getAttribute('lng'))));
		}

		map.addOverlay(new GPolyline(points,'#339900',3,1));

		var marker = xmlDoc.documentElement.getElementsByTagName('marker');
		point = new GLatLng(parseFloat(marker[0].getAttribute('lat')),parseFloat(marker[0].getAttribute('lng')));
		map.setCenter(point,8);

		resultObj.style.color = '#390';
		resultObj.style.fontWeight = 'bold';
		document.getElementById('result').innerHTML = countyname+', '+statename+' ('+countycode+')';
		document.getElementById('datafile').innerHTML = '<a href="xml/county.php?zip='+document.zip_form.zip.value+'">Download Data File</a>';
	} else {
		document.getElementById('result').innerHTML = '<b>Zip Code Invalid!</b>';
		document.getElementById('datafile').innerHTML = '';
	}
	toggleLoading();
}

function loadZipPoly(xmlDoc) {
	map.clearOverlays();
	var info = xmlDoc.documentElement.getElementsByTagName('info');
	var resultObj = document.getElementById('result_zip');
	if(info.length > 0) {
		for(var i = 0; i < zcta_array.length; i++) {
			eval("var "+zcta_array[i]+" = (info[0].getAttribute(zcta_array[i])!='') ? info[0].getAttribute(zcta_array[i]): 'N/A'");
		}

		var polylines = xmlDoc.documentElement.getElementsByTagName('polyline');
		var points = [];
		for(var i=0; i<polylines.length; i++) {
			points.push(new GLatLng(parseFloat(polylines[i].getAttribute('lat')),parseFloat(polylines[i].getAttribute('lng'))));
		}
		map.addOverlay(new GPolyline(points,'#339900',2,1));

		var marker = xmlDoc.documentElement.getElementsByTagName('marker');
		point = new GLatLng(parseFloat(marker[0].getAttribute('lat')),parseFloat(marker[0].getAttribute('lng')));
		map.setCenter(point,10);

		var top_html = zcta;
		if(city!='N/A' && state!='N/A') {
			top_html += ' ('+city+', '+state+')';
		}
		document.getElementById('result').innerHTML = top_html;

		var side_html = '<h1>Zip Info</h1>' +  // needed better formatting
			'<p><b>ZCTA:</b> '+zcta+'</p>' +

			'<p><b>City:</b> '+city+'<br/>' +
			'<b>County:</b> '+county+'<br/>' +
			'<b>State:</b> '+state+'<br/>' +
			'<b>Area Code(s):</b> '+area_code+'</p>' +

			'<p><b>Population:</b> '+population+'<br/>' +
			'<b>Population White:</b> '+population_white+'<br/>' +
			'<b>Population Black:</b> '+population_black+'<br/>' +
			'<b>Population Hispanic:</b> '+population_hispanic+'</p>' +

			'<p><b>Households:</b> '+households+'<br/>' +
			'<b>Avg. House Value:</b> '+avg_house_value+'<br/>' +
			'<b>Avg. House Income:</b> '+avg_house_income+'<br/>' +
			'<b>Persons Per House:</b> '+persons_per_house+'</p>' +
		
			'<p><b>Elevation:</b> '+elevation+'<br/>' +
			'<p><b>Aliases:</b> '+aliases+'<br/>' +
			'<p><b>MSA:</b> '+msa+'<br/>' +
			'<p><b>MSA Name:</b> '+msa_name+'<br/>' +
			'<p><b>PMSA:</b> '+pmsa+'</p>';
		
		/*
		for(var i = 0; i < zcta_array.length; i++) {
			side_html += '<p><b>'+zcta_array[i]+':</b> ';
			side_html += eval(zcta_array[i]);
			side_html += '</p>';
		}
		*/

		document.getElementById('result_zip').innerHTML = side_html;

		document.getElementById('datafile').innerHTML = '<a href="xml/zip.php?zip='+document.zip_form.zip.value+'">Download Data File</a>';
	} else {
		document.getElementById('result').innerHTML = '<b>Zip Code Invalid!</b>';
		document.getElementById('datafile').innerHTML = '';
		document.getElementById('result_zip').innerHTML = '';
	}
	toggleLoading();
}

function form_validate(formobj) {
	if(!validateRequired(formobj['zip'],'text','Zip is a required field.')) { return false; }
	toggleLoading();
	if(formobj['type'].value=='county') {
		getURL('GET','xml/county.php?zip='+formobj['zip'].value,'loadCountyPoly','xml');
	} else {
		getURL('GET','xml/zip.php?zip='+formobj['zip'].value,'loadZipPoly','xml');
	}
}

function resizeMap(formobj) {
	var map_div = document.getElementById('map');
	var zip_div = document.getElementById('result_zip');

	if(formobj.value=='county') {
		map_div.style.width = '890px';
		zip_div.style.visibility = 'hidden';
	} else {
		map_div.style.width = '700px';
		zip_div.style.visibility = 'visible';
	}
	map.checkResize();
}

function toggleLoading() {
	var x,y;
	var div = document.getElementById('loading');
	var div_img = document.getElementById('loadingImg');
	if(div.style.display=='none') {
		if(self.innerHeight) {
			x = self.innerWidth;
			y = self.innerHeight;
		} else if(document.documentElement && document.documentElement.clientHeight) {
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		} else if(document.body) {
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
	
		div.style.padding = '0';
		div.style.width = x-16+'px';
		div.style.height = y+'px';
		div.style.display = 'block';
		div_img.style.paddingTop = (y/2-100)+'px';
	} else {
		div.style.display = 'none';
	}
}
