var geolocationBlocking = false; //Whether or not this feature is turned on

var geocoder;
var visitor_city;
var visitor_region;
var visitor_county;
var visitor_country;
var visitor_countrycode;
var visitor_blocked = false;

var blockedLocations = new Array();
blockedLocations.push(new blockedLocation("", "Somerset", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Hunterdon", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Bergen", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Passaic", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Morris", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Hudson", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Union", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Middlesex", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "Warren", "NJ", "US"));
blockedLocations.push(new blockedLocation("", "New York", "NY", "US"));

if (geolocationBlocking)
{
	google.load("maps", "3", {other_params:'sensor=false', callback: initializeGeoLocationBlocking});
}
function initializeGeoLocationBlocking() {
	geocoder = new google.maps.Geocoder();
	if(google.loader.ClientLocation)
	{
		//visitor_lat = google.loader.ClientLocation.latitude;
		//visitor_lon = google.loader.ClientLocation.longitude;
		visitor_country = google.loader.ClientLocation.address.country;
		
		visitor_city = google.loader.ClientLocation.address.city;
		visitor_region = google.loader.ClientLocation.address.region;
		visitor_countrycode = google.loader.ClientLocation.address.country_code;
		
		geocoder.geocode({'address': visitor_city + ", " + visitor_region + ", " + visitor_country}, getMoreLocationInfo);
	}
	else
	{
		//Location not detected
		window.location = "http://mtlive.liquidchurchonline.com";
	}
}
function getMoreLocationInfo(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
		visitor_county = results[0].address_components[1].short_name;
		//document.body.innerHTML += (visitor_city + ", " + visitor_county  + ", " + visitor_region+ ", " + visitor_countrycode);
		updateBlockedStatus();
    }
}
function blockedLocation(city, county, region, country)
{
	this.city = city;
	this.county = county;
	this.region = region;
	this.country = country;
}
function updateBlockedStatus()
{
	visitor_blocked = false;
	for(var i=0; i<blockedLocations.length; i++)
	{
		if (blockedLocations[i].country == visitor_countrycode)
		{
			if (blockedLocations[i].region == "") //If region is blank, block based on whole country
			{
				visitor_blocked = true;
			}
			else if (blockedLocations[i].region == visitor_region) //If region equals the user's region
			{
				if (blockedLocations[i].county == "" && blockedLocations[i].city == "") //If county or city is not specified, assume we're blocking based on the entire state
				{
					visitor_blocked = true;
				}
				else if (blockedLocations[i].county == visitor_county) //If county equals the user's county
				{
					if (blockedLocations[i].city == "") //If city is blank, assume we're blocking the entire county
					{
						visitor_blocked = true;
					}
					else if (blockedLocations[i].city == visitor_city) //If the city is equal to the user's city
					{
						visitor_blocked = true;
					}
				}
			}
		}
	}
	handleBlockedStatus();
}
function handleBlockedStatus(){
	if (visitor_blocked)
	{
		window.location = "http://live.liquidchurchonline.com/Default_MNC.aspx";
	} else {
		window.location = "http://mtlive.liquidchurchonline.com/";
	}
}
