////  This code is used by the code in WindFields.js.  It never changes so we keep it in a separate file.

// Constants
var zeros = "#FFFFFF";
var tens = "#F700B1";
var twenties = "#B201AA";
var thirties = "#361AAD";
var forties = "#6AD3E6";
var fifties = "#68CEB9";
var sixties = "#6CC45E";
var seventies = "#B3D84A";
var eighties = "#FDEC00";
var nineties = "#FFAA00";
var hundreds = "#FE000A";

function GetDestinationLat(lat1, brng, d) {
    var R = 6371; // Earth's radius in kilometers
    var lat2 = (180 / Math.PI) * Math.asin(Math.sin(lat1 * Math.PI / 180) * Math.cos(d / R) +
                      Math.cos(lat1 * Math.PI / 180) * Math.sin(d / R) * Math.cos(brng * Math.PI / 180));
    return lat2;
}

function GetDestinationLon(lat1, lat2, lon1, brng, d) {

    var R = 6371; // Earth's radius in kilometers
    var lon2 = lon1 + (180 / Math.PI) * Math.atan2(Math.sin(brng * Math.PI / 180) * Math.sin(d / R) * Math.cos(lat1 * Math.PI / 180),
                             Math.cos(d / R) - Math.sin(lat1 * Math.PI / 180) * Math.sin(lat2 * Math.PI / 180));
    return lon2;
}

function CreateArrow(description, index, lat1, lon1, color, windspeed, windFromDirection, humidity) {
  
    // Arrow size
    var arrowSize = 8;
    var windspeed = 4 * windspeed;
    
    // Get the new point for the main line based on the wind direction and speed
    var windToDirection = windFromDirection - 180;
    if (windToDirection < 0)
        windToDirection = windToDirection + 360;
    var lat2 = GetDestinationLat(lat1, windToDirection, windspeed);
    var lon2 = GetDestinationLon(lat1, lat2, lon1, windToDirection, windspeed);

    // Get the new point for the first arrow tip
    var arrowDir1 = windToDirection - 135;
    if (arrowDir1 < 0)
        arrowDir1 = arrowDir1 + 360;
    var lat3 = GetDestinationLat(lat2, arrowDir1, arrowSize);
    var lon3 = GetDestinationLon(lat2, lat3, lon2, arrowDir1, arrowSize);

    // Get the new point for the second arrow tip
    var arrowDir2 = arrowDir1 - 90;
    if (arrowDir2 < 0)
        arrowDir2 = arrowDir2 + 360;
    var lat4 = GetDestinationLat(lat2, arrowDir2, arrowSize);
    var lon4 = GetDestinationLon(lat2, lat4, lon2, arrowDir2, arrowSize);
       
    // Create an arrow for each wind field. Make it fat for current, skinny for 24hour, dashed for 48 hour
    var polygon;
    var label;
    if (description == 'Current') {
        polygon = new GPolygon([
            new GLatLng(lat1, lon1),
            new GLatLng(lat2, lon2),
            new GLatLng(lat3, lon3),
            new GLatLng(lat4, lon4),
            new GLatLng(lat2, lon2)
            ], color, 3, 1, color, 1);
        map.addOverlay(polygon);
        FirescapeWindFieldMarkers.push(polygon);
        label = new ELabel(new GLatLng(lat1, lon1), humidity.toString() + '%', "styleELabel");
        map.addOverlay(label);
        FirescapeHumidityMarkers.push(label);
    }
    if (description == '24Hour') {
        polygon = new GPolygon([
            new GLatLng(lat1, lon1),
            new GLatLng(lat2, lon2),
            new GLatLng(lat3, lon3),
            new GLatLng(lat4, lon4),
            new GLatLng(lat2, lon2)
            ], color, 3, 0.6, color, 0.6);
        map.addOverlay(polygon);
        FirescapeWindFieldMarkers24Hour.push(polygon);
        label = new ELabel(new GLatLng(lat1, lon1), humidity.toString() + '%', "styleELabel");
        map.addOverlay(label);
        FirescapeHumidityMarkers24Hour.push(label);
    }
    if (description == '48Hour') {
        polygon = new GPolygon([
            new GLatLng(lat1, lon1),
            new GLatLng(lat2, lon2),
            new GLatLng(lat3, lon3),
            new GLatLng(lat4, lon4),
            new GLatLng(lat2, lon2)
            ], color, 3, 0.4, color, 0.4);
        map.addOverlay(polygon);
        FirescapeWindFieldMarkers48Hour.push(polygon);
        label = new ELabel(new GLatLng(lat1, lon1), humidity.toString() + '%', "styleELabel");
        map.addOverlay(label);
        FirescapeHumidityMarkers48Hour.push(label);
    }       
}
