var _geocoder;
var _locationData;
var _address		= '';
var _city			= '';
var _region			= '';
var _country		= '';
var _lat			= '';
var _long			= '';
var _latLong		= '';

function geolocate()
{
	useGoogleLocation();
/*	try
	{
		if(navigator.geolocation)
		{
			navigator.geolocation.getCurrentPosition(handlePosition, handlePositionError);
		} else
		{
			useGoogleLocation();
		}
	} catch(error)
	{
		embed();
	}*/
}

function handlePosition(position)
{
	_geocoder		= new GClientGeocoder();
	_lat			= position.coords.latitude;
	_long			= position.coords.longitude;
	_latLong		= (_lat && _long) ? _lat+';'+_long : '';
    _locationData	= new GLatLng(_lat, _long);
    _geocoder.getLocations(_locationData, showAddress);
}

function handlePositionError(error)
{
	useGoogleLocation();
}

function showAddress(response)
{
	if (response && response.Status.code == 200)
	{
		var place	= response.Placemark[0];
		_country	= place.AddressDetails.Country.CountryNameCode;
		_region		= place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
		_city		= place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
		_address	= place.address;
		embed();
	}
}

function useGoogleLocation()
{
	try
	{
		if(google.loader.ClientLocation)
		{
			_city		= ('city' in google.loader.ClientLocation.address) ? google.loader.ClientLocation.address.city : '' ;
			_region		= ('region' in google.loader.ClientLocation.address) ? google.loader.ClientLocation.address.region : '' ;
			_country	= ('country' in google.loader.ClientLocation.address) ? google.loader.ClientLocation.address.country : '' ;
			_lat		= ('latitude' in google.loader.ClientLocation) ? google.loader.ClientLocation.latitude : '' ;
			_long		= ('longitude' in google.loader.ClientLocation) ? google.loader.ClientLocation.longitude : '' ;
			_latLong	= (lat && long) ? lat+';'+long : '';
			_address	= '';
		}
	} catch (e) 
	{
	}
}

function sendInfo()
{
	var params	= 'city='+_city+'&region='+_region+'&country='+_country+'&latLong='+_latLong+'&address='+_address+'&format='+_format;
	var XHTTP	= new XMLHttpRequest();
		XHTTP.onreadystatechange	= function()
		{
			//alert(XHTTP.readyState+' - '+XHTTP.status);
			if(success(XHTTP))
			{
			}
		}
		XHTTP.open('POST', '/splash/scripts/visit.php', true);
		XHTTP.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		XHTTP.setRequestHeader('Content-length', params.length);
		XHTTP.setRequestHeader('Connection', 'close');
		XHTTP.send(params);
}

function success(XHTTP)
{
	var bool = false;
	if(XHTTP.readyState == 4)
	{
		if(XHTTP.status == 200)
		{
			bool = true;
		}
	}
	return bool;
}
