﻿function GPSUnit(id, name, colorRGB, markerIcon) {
	this.ID = id;
	this.ColorRGB = colorRGB;
	if (this.ColorRGB == ''){this.ColorRGB = '#FF0000';}
	this.Name = name;
	this.Markers = [];
	this.Path = null;
	this.MarkersAreVisible = false;
	this.PathIsVisible = false;
	this.MarkerIcon = markerIcon;
	this.ActivityImageIDs = [];
	this.WeatherTypeID = 0;
	this.WeatherTemp = '';
	
	this.GetLastMarker = function (){
		if (!this.Markers){return;}	
		return this.Markers[this.Markers.length-1];
	};
	
	this.ClearMarkers = function(map){
		if (!this.Markers){return;}
		
		for(i=0;i<this.Markers.length;i++){
			map.removeOverlay(this.Markers[i].GoogleMarker);
		}
		this.Markers = [];
		if (this.Path){map.removeOverlay(this.Path);}
		this.Path = null;
	};
	
	this.AddMarker = function(map, m){
		this.Markers.push(m);
		map.addOverlay(m.GoogleMarker);
		if (!this.MarkersAreVisible){m.GoogleMarker.hide();}else{m.GoogleMarker.show();}
	};
	
	this.CalculatePath = function(){
		if (!this.Markers){return;}
		var pts = [];
		for(i=0;i<this.Markers.length;i++){
			var mark = this.Markers[i].GoogleMarker;
			pts.push(mark.getLatLng());
		}
		this.Path = new GPolyline(pts, this.ColorRGB, 3, 1);
	};
	
	this.ShowMarker = function(markerIndex, show){
		if (!this.Markers){return;}
		var mark = this.Markers[markerIndex];
		if (!mark){return;}
		if (show){mark.GoogleMarker.show();}else{mark.GoogleMarker.hide();}
	}
	
	this.RenderMarkers = function(map, show){
		if (!this.Markers){return;}
		if (this.MarkersAreVisible==show){return;}
		
		for(i=0;i<this.Markers.length;i++){
			var mark = this.Markers[i].GoogleMarker;
			if (show){mark.show();}else{mark.hide();}
		}
		this.MarkersAreVisible = show;
	};
	
	this.RenderPath = function(map, show){
		if (!this.Path){return;}
		if (this.PathIsVisible==show){return;}
		if (show){map.addOverlay(this.Path);}else{map.removeOverlay(this.Path);}
		this.PathIsVisible = show;
	};
}

function Marker(whenRecorded, googleMarker){
	this.GoogleMarker = googleMarker;
	this.WhenRecorded = whenRecorded;
	this.Bearing = 361;
	this.KilometresPerHour = 0;
	
	this.GetBearingDescription = function(){
		var directions = ['N','NE','E','SE','S','SW','W','NW'];
		if (this.Bearing<0 || this.Bearing>359){return '';}
		var startBearing = this.Bearing;
		if (startBearing<0){startBearing = startBearing + 360;}
		var i = Math.round(startBearing / 45);
		if (i>directions.length-1){i=0;}
		return directions[i];;
	}
}

function GPSUnitCollection(unitArray){
	this.Units = unitArray;
	
	this.GetUnitByID = function(unitID){
		for (count=0;count<this.Units.length;count++){
			var unit = this.Units[count];
			if (unit.ID==unitID){return unit;}
		}
	};
	
	this.SetUnit = function (Index, Unit){
		this.Units[Index] = Unit;
	};
}

function MapArea(id, name, colorRGB) {
	this.ID = id;
	this.Name = name;
	this.ColorRGB = colorRGB;
	if (this.ColorRGB == ''){this.ColorRGB = '#FF0000';}
	this.AreaIsVisible = false;
	this.Points = [];
	this.Area = null;
	
	this.GetBounds = function(){
		if (!this.Area) this.CreateArea();
		if (!this.Area) return;
		return this.Area.getBounds();
	}

this.CreateArea = function() {
	if (!this.Points) { return; }
	if (this.Points.length < 2) { return; }
    var pts = this.Points;

    // Close loop
    var firstPoint = pts[0];
    var lastPoint = pts[pts.length - 1];
    if (firstPoint.lat() != lastPoint.lat() || firstPoint.lng() != lastPoint.lng()) {
        pts.push(firstPoint);
    }

    this.Area = new GPolygon(pts, this.ColorRGB, 5, 1, this.ColorRGB, 0.4);
}

this.AddPoint = function(lat, lon) {
    var pt = new GLatLng(parseFloat(lat), parseFloat(lon));
    this.Points.push(pt);
}


this.ClearPoints = function() {
    this.Points = [];
    this.Area = null;
}
	
	this.DeleteLastPoint = function(){
		if (!this.Points) return;
		if (this.Points.length == 0) return;
		this.Points.pop();
	}

	this.Render = function(map, show) {
	    if (!this.Points) { return; }
	    if (this.Points.length < 2) { return; }
	    if (this.AreaIsVisible == show) { return; }
	    if (show) {
	        if (!this.Area) {
	            this.CreateArea();
	        }
	        map.addOverlay(this.Area);
	    } else {
	        if (this.Area) { map.removeOverlay(this.Area); }
	    }

	    this.AreaIsVisible = show;
	};

	this.GetPolyline = function() {
	    if (!this.Points) { return; }
	    if (this.Points.length < 2) { return; }
            var pts = this.Points;

        // Close loop
        var firstPoint = pts[0];
        var lastPoint = pts[pts.length - 1];
        if (firstPoint.lat() != lastPoint.lat() || firstPoint.lng() != lastPoint.lng()) {
            pts.push(firstPoint);
        }

        return new GPolyline(pts, this.ColorRGB, 5);
	};
}

function MapAreaCollection(){
	this.Areas = [];
	
	this.GetAreaByID = function(areaID){
		for (count=0;count<this.Areas.length;count++){
			var mapArea = this.Areas[count];
			if (mapArea.ID==areaID){return mapArea;}
		}
	};
	
	this.Length = function(){
		if (!this.Areas) return 0;
		return this.Areas.length;
	}
	
	this.GetAreaByIndex = function(index){
		return this.Areas[index];
	}
	
	this.AddArea = function (mapArea){
		this.Areas.push(mapArea);
	}
}


