function loadAjaxApi() {
	jQuery.getScript("http://www.google.com/jsapi?key=ABQIAAAA3Lf5Z74tsAIKuYBOSlynDhTi8FOCOta_rHn6gJNHWygliXwK3BSRoau_IxgbXwL1QN6ge0jOYDZGYg", function() { initLocation(); });
}
function loadAnalytics()
{
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	jQuery.getScript(gaJsHost + "google-analytics.com/ga.js", function(){
	_gat._getTracker("UA-4703198-7")._trackPageview();
	});
}
var loc = '';
function initLocation()
{	
	if (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code == "US" &&
	      google.loader.ClientLocation.address.region)
	{
		loc = google.loader.ClientLocation.address.city + ', ' + google.loader.ClientLocation.address.region + ', ' + google.loader.ClientLocation.address.country
	} else if (google.loader.ClientLocation && google.loader.ClientLocation.address.city) {
		loc = google.loader.ClientLocation.address.city + ', ' + google.loader.ClientLocation.address.country;
	} else if (google.loader.ClientLocation) {
		loc = google.loader.ClientLocation.address.country;
	}
	
	if (google.loader.ClientLocation && $('#location').val() == '')
	{
		$('#location').val(loc);
		renderLocationDisplay(loc);
	} else if ($('#location').val() != '')
	{
		renderLocationDisplay($('#location').val());
	}
}
function renderLocationDisplay(locstr)
{
	if (locstr != '')
		$('#location_display span').html('<a href="javascript:void(0)" title="edit" onclick="editLocation()">' + locstr + '</a> <a href="javascript:void(0)" title="clear" onclick="clearLocation()">[x]</a>');
	else
		$('#location_display span').html('<a href="javascript:void(0)" title="edit" onclick="editLocation()">location not defined</a>');
}
function clearLocation()
{
	$('#location_editor').hide();
	$('#location').val('');
	renderLocationDisplay('');
}
function editLocation()
{
	$('#loc_search').removeClass('error');
	$('#loc_search').removeClass('loading');
	$('#location_editor').show();
	$('#location_editor input').val($('#location').val());
	$('#location_editor input').focus();
	$('#location_editor input').select();
}
function hideSearch()
{
	$('#location_editor').hide();
}
function doLocSearch(address)
{
	$('#loc_search').addClass('loading');
	$('#loc_search').removeClass('error');
	$.post('/loc_search.php',{address: address}, function (data) {
		if (data != 'ERROR')
		{
			$('#location_editor').hide();
			$('#location').val(data);
			renderLocationDisplay(data);
			$('#text').focus();
		} else {
			$('#loc_search').addClass('error');
			$('#loc_search').removeClass('loading');
			$('#location_editor input').focus();
			$('#location_editor input').select();
		}
	});
	return false;
}
$(document).ready(function () {
	loadAnalytics();
	loadAjaxApi();
});