/**
* Google Maps Samples
* Copyright (c) 2006-2007, Mashup Technologies, LLC
* All rights reserved.
* http://www.mashuptechnologies.com
* 
* The JavaScript code distributed with Google Maps Samples (the "Software") is licensed under the
* Lesser GNU (LGPL) open source license version 3.
* 
* http://www.gnu.org/licenses/lgpl.html
* 
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*/
/* Prototype functions */
Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		__method.apply(object, arguments);
	}
}

// Core library
var mapApp = {
	map: null,
	mapZoom: 4,
	polygons: [],

	init: function(){
		if (GBrowserIsCompatible()) {
			this.map = new GMap2(document.getElementById("mapContainer"));
			this.map.setCenter(new GLatLng(36.80, -95.88), this.mapZoom);
			this.map.addControl(new GLargeMapControl());
		}
		else
		{
			// Show error message
		}
			
	},

	/* 
	* Drow circle using polygon method
	*/
	drowCircle: function(lat, lon, opt)
	{
		// Get options
		var polyRadius = opt && opt.radius || 50;
		var strokeColor = opt && opt.strokeColor || "#CCCCCC";
		var strokeWeight = opt && opt.strokeWeight || 2;
		var strokeOpacity = opt && opt.strokeOpacity || 0.5 ;
		var fillColor = opt && opt.fillColor || "#FFCCCC";
		var fillOpacity = opt && opt.fillOpacity || 0.5;
		var id = opt && opt.id || null;

		// Get active projection
		var mapNormalProj = G_NORMAL_MAP.getProjection();
		//
		var geoPoint = new GLatLng(lat, lon);
		//convert geoPoint to G pixels
		var pointPixel = mapNormalProj.fromLatLngToPixel(geoPoint, this.mapZoom);

		var polyPoints = [];
		/* High res 
		var polyNumSides = 90;
		//polySideLength 360/polyNumSides
		var polySideLength = 4;
		*/
		var polyNumSides = 40;
		//polySideLength 360/polyNumSides
		var polySideLength = 9;
			
		for (var a = 0; a<(polyNumSides+1); a++) {
			var aRad = polySideLength*a*(Math.PI/180);
			var pixelX = pointPixel.x + polyRadius * Math.cos(aRad);
			var pixelY = pointPixel.y + polyRadius * Math.sin(aRad);
			var polyPixel = new GPoint(pixelX,pixelY);
			var polyPoint = mapNormalProj.fromPixelToLatLng(polyPixel,this.mapZoom);
			polyPoints.push(polyPoint);
		}

		var polygon = new GPolygon(polyPoints, strokeColor, strokeWeight, strokeOpacity, fillColor, fillOpacity);
		polygon.customID = id;

		this.polygons.push(polygon);
		this.map.addOverlay(polygon);
	}, 
		
	/* Find poligon where point belongs.
	* return polygon customID 
	*/
	findPolygon: function(point)
	{
		for (i=0; i < this.polygons.length; i++) {
			if (this.polygons[i].Contains(point)) return this.polygons[i].customID;
		}
		return null;
	},

	/* Find poligon and recenter map
	* return none
	*/
	navigateToPolygon: function(id)
	{
		for (i=0; i < this.polygons.length; i++) {
				
			if (this.polygons[i].customID == id) {
				this.map.setCenter(this.polygons[i].getVertex(0), 5);
				break;
			}
		}
	}
}


/* A method for testing if a point is inside a polygon
* Returns true if polygon contains point
* Algorithm described at http://alienryderflex.com/polygon/ 
*/
GPolygon.prototype.Contains = function(point) {
	var j=0;
	var oddNodes = false;
	var x = point.lng();
	var y = point.lat();
	var vertexCount = this.getVertexCount();
	for (var i=0; i < vertexCount; i++) {
		j++;
		if (j == vertexCount) {
			j = 0;
		}
		if (((this.getVertex(i).lat() < y) && (this.getVertex(j).lat() >= y)) 
			|| ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {
			if ( this.getVertex(i).lng() + (y - this.getVertex(i).lat())
				/  (this.getVertex(j).lat()-this.getVertex(i).lat())
				*  (this.getVertex(j).lng() - this.getVertex(i).lng())<x ) {
				oddNodes = !oddNodes
			}
		}
	}
	return oddNodes;
}

/* 
* A method for testing if a point is inside a polyline
*/
GPolyline.prototype.Contains = GPolygon.prototype.Contains;


