﻿//tryMatch
function InitRegionMatch() {
	MatchedRegion.prototype.getMatchedRegion = function () {
		var testPolygon;
		var latlng = null;
		var matchedRegion = new regionObject();
		var location = "Showing default location for map.";
		if (google.loader.ClientLocation) {
			zoom = 13;
			var point = { x: google.loader.ClientLocation.latitude, y: google.loader.ClientLocation.longitude };
			//point = { x: 46.088470, y: -74.113770 }; //montreal
			//point = { x: 45.675484, y: -75.959473 }; //ottawa
			//point = { x: 50.457504, y: -104.633789 }; //regina-central canada

			this.regionName = matchRegionPoint(point.x, point.y);
			this.lat = point.x || "lat not found";
			this.lon = point.y || "lon not found";

		} else {
			//google loader did not initilize
		}
	}
}

function MatchedRegion() {
	this.regionName = "";
	this.lat = "";
	this.lon = "";
}

function regionObject() {
	this.value = "";
}

function isPointInPoly(poly, pt) {
	for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
		((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
        && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
        && (c = !c);
	return c;
}


function matchRegionPoint(lat, lon) {
	var output = "";
	var regionMatched;
	var points = null;
	var point = { x: lat, y: lon };
	//var point = { x: 46.088470, y: -74.113770 }; //testing - montreal
	//var point = { x: 45.675484, y: -75.959473 }; //testing - DNE
	for (var i = 0; i < regions.length; i++) {
		points = regions[i].vertices;
		if (isPointInPoly(points, point)) {
			regionMatched = true;
			return regions[i].name;
		}
	}
}
