// JavaScript Document

function DosiPoly(Nr, Dosi, SPLat, SPLng)
{
	//private Eigenschaften
	var self = this;
	var DosiVal = parseInt(Dosi);
	var color = getColorValue(DosiVal);
	var SchwerP = null;
	if(SPLat != null && SPLng != null)
	{
		SchwerP = new GLatLng(parseFloat(SPLat), parseFloat(SPLng), true);
	}
	//true, wenn Polygon als Overlay
	var Added = false;
	var modified = false;
	var modArray = false;
	var bEditing = false;
	var Nummer = parseInt(Nr);
	var Points = new Array();
	//var Polygon = new GPolygon([], color, 1, 1, color, 0.2);
	var Polygon = null;
	var bdelete = false;
	var m_Area = 0.0;
	//privilegierte oeffentliche Methode
	//Neuen Punkt hinzufuegen
	this.InitPoly = function()
	{
		Polygon = new GPolygon([], color, 1, 1, color, 0.2);
	}
	this.NewPoint = function(latlng)
	{
		if(!Added)
		{
			this.AddOverlay();
		}
		if (latlng)
		{
			this.NewArrayPoint(Polygon.getVertexCount(), latlng.lat(), latlng.lng());
			Polygon.insertVertex(Polygon.getVertexCount(), latlng);
		}
	}
	this.NewArrayPoint = function(i, lat, lng)
	{
		var NewPoint = new Array;
		NewPoint.push(lat);
		NewPoint.push(lng);
		Points.splice(i, 0, NewPoint);
	}
	this.AddPoint = function(Lat, Lng)
	{
		//var latlng = new GLatLng(parseFloat(Lat), parseFloat(Lng), true);
		//this.NewPoint(latlng);
		var NewPoint = new Array;
		NewPoint.push(parseFloat(Lat));
		NewPoint.push(parseFloat(Lng));
		Points.push(NewPoint);
	}
	this.DigitPoint = function(latlng)
	{
		if(Polygon == null)
		{
			Polygon = new GPolygon([], color, 1, 1, color, 0.2);
		}
		if(!Added)
		{
			this.AddOverlay();
		}
		if (latlng)
		{
			if(Polygon.getVertexCount() < 1)
			{
				this.NewArrayPoint(Polygon.getVertexCount(), latlng.lat(), latlng.lng());
				Polygon.insertVertex(Polygon.getVertexCount(), latlng);
				this.NewArrayPoint(Polygon.getVertexCount(), latlng.lat(), latlng.lng());
				Polygon.insertVertex(Polygon.getVertexCount(), latlng);
			}
			else
			{
				this.NewArrayPoint(Polygon.getVertexCount()-1, latlng.lat(), latlng.lng());
				Polygon.insertVertex(Polygon.getVertexCount()-1, latlng);
			}
			modified = true;
			modArray = true;
		}
	}
	//Auf Karte zeichnen
	this.AddOverlay = function()
	{
		g_map.addOverlay(Polygon);
		Added = true;
		if(bEditing)
		{
			this.enableEditing();
		}
	}
	this.RemoveOverlay = function()
	{
		if(Added)
		{
			if(Polygon != null)
			{
				Polygon.disableEditing();
			}
			g_map.removeOverlay(Polygon);
			Added = false;
		}
	}
	this.DeleteAllPoints = function()
	{
		this.RemoveOverlay();
		bEditing = false;
		Polygon = new GPolygon([], color, 1, 1, color, 0.2);
		while(Points.length != 0)
		{
			Points.pop();
		}
		modified = true;
		m_Area = 0.0;
		SchwerP = null;
	}
	this.DeletePoint = function()
	{
		if(Polygon != null)
		{
			if(Polygon.getVertexCount() > 4)
			{   
				Polygon.enableEditing();
				Polygon.deleteVertex(Polygon.getVertexCount()-2);
				Polygon.disableEditing();
			}
			else
			{
				//Wenn Polygon 4 oder weniger Punkte hat, wird es komplett geloescht.
				this.DeleteAllPoints();
				this.AddOverlay();
			}
			modified = true;
			modArray = true;
		}
	}
	this.ChangeDosi = function(Dosi)
	{
		DosiVal = parseInt(Dosi);
		color = getColorValue(DosiVal);
		modified = true;
		//Neu zeichnen
		this.RemoveOverlay();
		var TempPoly = new GPolygon([], color, 1, 1, color, 0.2);
		g_map.addOverlay(TempPoly);
		for(var i=0; i<Polygon.getVertexCount(); i++)
		{
			TempPoly.insertVertex(i, Polygon.getVertex(i));
		}
		Polygon = TempPoly;
		Added = true;
		this.enableEditing();
	}
	this.IsModified = function()
	{
		if(bEditing || modified)
		{
			this.GPoly2Array();
			return true;
		}
		return false;
	}
	this.SetModified = function(bVal)
	{
		modified = bVal;
		modArray = bVal;
	}
	this.IsAdded = function()
	{
		if(Added && Polygon.getVertexCount() > 3)
		{
			return true;
		}
		else
		{
			return false;
		}
			
	}
	this.GetSchwerP = function()
	{
		this.CalcSchwerP();
		return SchwerP;
	}
	this.CalcSchwerP = function()
	{
		if(Points.length>1)
		{
			var Lat=0.0;
			var Lng=0.0;
			var m=1.0/(1.0*Points.length-1);
			for(var j=0; j<Points.length-1; j++)
			{
				Lat+= Points[j][0];
				Lng+= Points[j][1];
			}
			Lat*=m;
			Lng*=m;
			SchwerP = new GLatLng(Lat, Lng, true);
		}
	}
	this.GetVertex = function(i)
	{
		if(i<Polygon.getVertexCount())
		{
			return Polygon.getVertex(i);
		}
		else
		{
			return null;
		}
	}
	this.GetVertexCount = function()
	{
		return Polygon.getVertexCount();
	}
	this.GetDosi = function()
	{
		return DosiVal;
	}
	this.GetArea = function()
	{
		if(Polygon != null && Polygon.getVertexCount() > 3)
		{
			m_Area = Polygon.getArea()/10000.0;
			return m_Area;
		}
		else
			return m_Area;
	}
	//Flaeche fuer Beschriftung, Flaeche wird nur ausgegeben, wenn Polygon != null
	this.GetAreaWrite = function()
	{
		if(Polygon != null && Polygon.getVertexCount() > 3)
		{
			m_Area = Polygon.getArea()/10000.0;
			return m_Area;
		}
		else
			return 0.0;
	}
	this.SetArea = function(ar)
	{
		m_Area = parseFloat(ar);
	}
	this.disableEditing = function()
	{
		if(Polygon != null)
		{
			Polygon.disableEditing();
		}
		bEditing = false;
	}
	this.enableEditing = function()
	{
		if(Added)
		{
			Polygon.enableEditing();
			bEditing = true;
			modified = true;
			modArray = true;
		}
	}
	this.GetPolygon = function()
	{
		return Polygon;
	}
	this.GetNr = function()
	{
		return Nummer;
	}
	this.SetNr = function(FlNr)
	{
		Nummer = parseInt(FlNr);
	}
	this.SetDelete = function(del)
	{
		bdelete = del;
	}
	this.GetDelete = function()
	{
		return bdelete;
	}
	this.GetEnableEdit = function()
	{
		return bEditing;
	}
	//String fuer json Array fuer php
	this.GetDataArray = function()
	{
		var data = '"'+Nummer+'":{"Ha":'+this.GetArea()+', "Wert":'+DosiVal+', "Punkte":[';
		for(i=0; i<Points.length-1; i++)
		{
			var Punkt = '{"N":"'+Points[i][0]+'", "E":"'+Points[i][1]+'"},';
			data += Punkt;
		}
		if(Points.length>0)
		{
			var Punkt = '{"N":"'+Points[Points.length-1][0]+'", "E":"'+Points[Points.length-1][1]+'"}]}';
			data += Punkt;
		}
		else
		{
			var Punkt = ']}';
			data += Punkt;
		}
		return data;
	}
	this.GetPointCount = function()
	{
		return Points.length;
	}
	this.GetPointLat = function(i)
	{
		Idx = parseInt(i);
		if(Idx < Points.length)
		{
			return Points[Idx][0];
		}
		else
		{
			return null;
		}
	}
	//Longitudinal wert
	this.GetPointLng = function(i)
	{
		Idx = parseInt(i);
		if(Idx < Points.length)
		{
			return Points[Idx][1];
		}
		else
		{
			return null;
		}
	}
	this.GPoly2Array = function()
	{
		if(Polygon != null)
		{
			while(Points.length != 0)
			{
				Points.pop();
			}
			var latlng = null;
			for(var i=0; i<Polygon.getVertexCount(); i++)
			{
				latlng = Polygon.getVertex(i);
				var NewPoint = new Array;
				NewPoint.push(latlng.lat());
				NewPoint.push(latlng.lng());
				Points.push(NewPoint);
			}
			m_Area = Polygon.getArea()/10000.0;
			modArray = false;
		}
	}
	//Werte aus Array in Polygon eintragen
	this.Array2GPoly = function()
	{
		if(Polygon == null)
		{
			Polygon = new GPolygon([], color, 1, 1, color, 0.2);
			this.AddOverlay();
			for(var i=0; i<Points.length; i++)
			{
				if(Points[i].length == 2)
				{
					var latlng = new GLatLng(Points[i][0], Points[i][1], true);
					Polygon.insertVertex(i, latlng);
				}
			}
			//this.AddOverlay();
			if(bEditing == true)
			{
				this.enableEditing();
			}
		}
	}
	//Checked ob Polygon innerhalb der Karten Grenzen ist.
	this.IsOnMap = function(maxlat, minlat, maxlng, minlng)
	{
		var OnMap = false;
		for(var i=0; i<Points.length; i++)
		{
			if(Points[i][0]<maxlat && Points[i][0]>minlat && Points[i][1]<maxlng && Points[i][1]>minlng)
			{
				OnMap=true;
				i = Points.length;
			}
		}
		if(OnMap == true)
		{
			/*if(Polygon == null)
			{
				this.Array2GPoly();
			}*/
			return true;
		}
		else
		{
			this.SetPolyNull();
		}
		return false;
	}
	this.SetPolyNull = function()
	{
		this.RemoveOverlay();
		Polygon = null;
	}
	this.CheckArray = function()
	{
		if(bEditing || modArray)
		{
			this.GPoly2Array();
		}
	}
}

