TGPolygon = Class.create();

TGPolygon.prototype =
{
    initialize : function(polygon,id)   
    {
        this.optId = id;
        this.polygon = polygon;
    },
    
    getCenter : function()
    {
        var minx = 0;
    	var miny = 0;
    	var maxx = 0;
    	var maxy = 0;
	
		if(this.polygon.getVertexCount() > 0)
	    {
	        for(var i = 0;i < this.polygon.getVertexCount();i++)
	        {
				var point = this.polygon.getVertex(i);
				
				if (i == 0)
				{
					minx = parseFloat(point.lat());
					miny = parseFloat(point.lng());
					maxx = parseFloat(point.lat());
					maxy = parseFloat(point.lng());
				}
				
				else
				{
					if (minx> parseFloat(point.lat()))
					{
						minx = parseFloat(point.lat());
					}
					else if (maxx< parseFloat(point.lat()))
					{
						maxx = parseFloat(point.lat());
					}
					if (miny> parseFloat(point.lng()))
					{
						miny = parseFloat(point.lng());
					}
					else if (maxy< parseFloat(point.lng()))
					{
						maxy = parseFloat(point.lng());
					}
				}
			}
		}
			
		return new GLatLng((parseFloat(minx) + parseFloat(maxx)) / 2,(parseFloat(miny) + parseFloat(maxy)) / 2);
    },
    
    isInPolygon : function(point) 
    {
        var isIn = 0;
    
        var npol = this.polygon.getVertexCount();
        var c = false;
        
        for (i = 0, j = npol-1; i < npol; j = i++) 
        {
          var a = this.polygon.getVertex(i);
          var b = this.polygon.getVertex(j);      
          
          if ((((a.lng()<= point.lng()) && (point.lng()<b.lng())) ||
               ((b.lng()<=point.lng()) && (point.lng()<a.lng()))) &&
              (point.lat() < (b.lat() - a.lat()) * (point.lng() - a.lng()) / (b.lng() - a.lng()) + a.lat()))
  
           c = !c;
        }
        return c;
    }
        
}

TGPolyline = Class.create();

TGPolyline.prototype =
{
    initialize : function(polyline,id)   
    {
        this.optId = id;
        this.polyline = polyline;
    }
}



IdWrapper = Class.create();

IdWrapper.prototype =
{
    initialize : function(obj,id)   
    {
        this.objId = id;
        this.obj = obj;
    }
}
    
    
function TGCircle(middle,radius,  strokeColor,  strokeWeight,  strokeOpacity,  fillColor,  fillOpacity)
{
    this.middle = middle;
    this.radius = radius;
    this.points = null;
    
    if(strokeColor)
    {
       this.strokeColor = strokeColor;
    }
    else
    {
        this.strokeColor = "#FFFFFF";
    }
    
    if(strokeWeight)
    {
        this.strokeWeight = strokeWeight;
    }
    else
    {
        this.strokeWeight = 1;
    }
    
    if(strokeOpacity)
    {
        this.strokeOpacity = strokeOpacity;
    }
    else
    {
        this.strokeOpacity = 1.0;    
    }
    
    if(fillColor)
    {
        this.fillColor = fillColor;
    }
    else
    {
        this.fillColor = "#000000";
    }
    
    if(fillOpacity)
    {
        this.fillOpacity = fillOpacity;        
    }
    else
    {
        this.fillOpacity = 1.0;        
    }
}

TGCircle.prototype = new GOverlay();

TGCircle.prototype.initialize = function(map) 
{
   this.map = map;
   this.createPoints();
   this.polygon = new GPolygon(this.points,this.strokeColor,this.strokeWeight,this.strokeOpacity,this.fillColor,this.fillOpacity);
   map.addOverlay(this.polygon);
}

TGCircle.prototype.remove = function()
{
    this.map.removeOverlay(this.polygon);
    this.polygon = null;
}

TGCircle.prototype.copy = function()
{
    return new TGCircle(this.middle,this.radius,this.strokeColor,this.strokeWeight,this.strokeOpacity,this.fillColor,this.fillOpacity);
}

TGCircle.prototype.redraw = function()
{
    return;
}

TGCircle.prototype.createPoints = function()
{
    var latConv = this.middle.distanceFrom(new GLatLng(this.middle.lat()+0.1, this.middle.lng()))/100;
	var lngConv = this.middle.distanceFrom(new GLatLng(this.middle.lat(), this.middle.lng()+0.1))/100;

	this.points = new Array();
	var step = parseInt(360/36)||10;
	for(var i=0; i<=360; i+=step)
	{
	   var pint = new GLatLng(this.middle.lat() + (this.radius/latConv * Math.cos(i * Math.PI/180)), this.middle.lng() + (this.radius/lngConv * Math.sin(i * Math.PI/180)));
	   this.points[this.points.length] = pint;
	}
}

TGCircle.prototype.toJSON = function()
{
    var json = '{"lat":' + this.middle.lat() + ',"lon":' + this.middle.lng() + ',"radius":' + this.radius + ',"strokecolor":"' + this.strokeColor + '","strokeweight":' + this.strokeWeight + ',"strokeopacity":' + this.strokeOpacity + ',"fillcolor":"' + this.fillColor + '","fillopacity":' + this.fillOpacity + "}";
    return json;
}

/********************************/

function TGRing(middle,minradius,maxradius,  strokeColor,  strokeWeight,  strokeOpacity,  fillColor,  fillOpacity,options)
{
    this.middle = middle;
    this.minradius = minradius;
    this.maxradius = maxradius;
    this.innerPoints = null;
    this.outerPoints = null;
    this.paintOuter = false;
    this.paintInner = false;
    
    if(strokeColor)
    {
       this.strokeColor = strokeColor;
    }
    else
    {
        this.strokeColor = "#FFFFFF";
    }
    
    if(strokeWeight)
    {
        this.strokeWeight = strokeWeight;
    }
    else
    {
        this.strokeWeight = 1;
    }
    
    if(strokeOpacity)
    {
        this.strokeOpacity = strokeOpacity;
    }
    else
    {
        this.strokeOpacity = 1.0;    
    }
    
    if(fillColor)
    {
        this.fillColor = fillColor;
    }
    else
    {
        this.fillColor = "#000000";
    }
    
    if(fillOpacity)
    {
        this.fillOpacity = fillOpacity;        
    }
    else
    {
        this.fillOpacity = 1.0;        
    }
    
    if(options)
    {
        if(options["paintOuter"] != null)
        {
            this.paintOuter = options["paintOuter"];
        }
        
        if(options["paintInner"] != null)
        {
            this.paintInner = options["paintInner"];
        }
    }
}

TGRing.prototype = new GOverlay();

TGRing.prototype.initialize = function(map) 
{
   this.map = map;
   this.createPoints();
   var points = this.outerPoints.concat(this.innerPoints);
   points[points.length] = points[0];
   
   this.ringPolygon = new GPolygon(points,this.fillColor,0.1,this.strokeOpacity,this.fillColor,this.fillOpacity);
   map.addOverlay(this.ringPolygon);
    
   if(this.paintOuter)
   {
        this.outerPolygon = new GPolyline(this.outerPoints,this.strokeColor,this.strokeWeight,this.strokeOpacity);
        map.addOverlay(this.innerPolygon);
   }
   
   if(this.paintInner)
   {
        this.innerPolygon = new GPolyline(this.innerPoints,this.strokeColor,this.strokeWeight,this.strokeOpacity);
        map.addOverlay(this.outerPolygon);
   }
}

TGRing.prototype.remove = function()
{
    if(this.paintOuter)
    {
        this.map.removeOverlay(this.outerPolygon);
    }
    
    if(this.paintInner)
    {
        this.map.removeOverlay(this.innerPolygon);
    }
    
    this.map.removeOverlay(this.ringPolygon);
    
    this.outerPolygon = null;
    this.innerPolygon = null;
    this.ringPolygon = null;
}

TGRing.prototype.copy = function()
{
    return new TGRing(this.middle,this.minradius,this.maxradius,this.strokeColor,this.strokeWeight,this.strokeOpacity,this.fillColor,this.fillOpacity);
}

TGRing.prototype.redraw = function()
{
    return;
}

TGRing.prototype.createPoints = function()
{
    this.innerPoints = new Array();
    this.outerPoints = new Array();
    
    var latConv = this.middle.distanceFrom(new GLatLng(this.middle.lat()+0.1, this.middle.lng()))/100;
	var lngConv = this.middle.distanceFrom(new GLatLng(this.middle.lat(), this.middle.lng()+0.1))/100;

	var step = parseInt(360/36)||10;
	for(var i=0; i<=360; i+=step)
	{
	   var pintOuter = new GLatLng(this.middle.lat() + (this.maxradius/latConv * Math.cos(i * Math.PI/180)), this.middle.lng() + (this.maxradius/lngConv * Math.sin(i * Math.PI/180)));
	   this.outerPoints[this.outerPoints.length] = pintOuter;
	   
	   var pintInner = new GLatLng(this.middle.lat() + (this.minradius/latConv * Math.cos(i * Math.PI/180)), this.middle.lng() + (this.minradius/lngConv * Math.sin(i * Math.PI/180)));
	   this.innerPoints[this.innerPoints.length] = pintInner;
	}
}

TGRing.prototype.toJSON = function()
{
    var json = '{"lat":' + this.middle.lat() + ',"lon":' + this.middle.lng() + ',"minradius":' + this.minradius + ',"maxradius":' + this.maxradius + ',"strokecolor":"' + this.strokeColor + '","strokeweight":' + this.strokeWeight + ',"strokeopacity":' + this.strokeOpacity + ',"fillcolor":"' + this.fillColor + '","fillopacity":' + this.fillOpacity + '}';
    return json;
}


