/* ======================================================== */
// Journey grade definition URLs
/* ======================================================== */
var gradeUrls = {
    walking: 'https://parks.qld.gov.au/_design/design-helpers/grade-definitions/walking',
    cycling: 'https://parks.qld.gov.au/_design/design-helpers/grade-definitions/mountain-bike-and-cycling',
    horseriding: 'https://parks.qld.gov.au/_design/design-helpers/grade-definitions/horseriding'
};

/* ======================================================== */
// Fancybox park settings - Spinner
/* ======================================================== */

// check Fancybox version
if(typeof $.fancybox == 'object') {
    // Fancybox v3.5.6
	//console.log("FB3 loaded - #105480");
	$.fancybox.defaults.spinnerTpl = '<div class="fancybox-loading"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" width="86" height="86" aria-hidden="true">    <circle cx="43" cy="43" r="41" style="fill: #00748a"/>    <path d="M43,4A39,39,0,1,1,4,43,39.05,39.05,0,0,1,43,4m0-4A43,43,0,1,0,86,43,43,43,0,0,0,43,0Z" style="fill: #fff"/>  <path d="M62,55.48l-8.14-9.22H57a2.32,2.32,0,0,0,2.12-1.33,2.25,2.25,0,0,0-.34-2.45l-8-9.26h2.95a2.3,2.3,0,0,0,2.12-1.38,2.28,2.28,0,0,0-.43-2.46l-11.23-12a1.68,1.68,0,0,0-2.38,0l-11.23,12a2.28,2.28,0,0,0-.43,2.46,2.3,2.3,0,0,0,2.12,1.38h2.95l-8,9.26a2.27,2.27,0,0,0-.34,2.45A2.32,2.32,0,0,0,29,46.26h3.12L24,55.48A2.24,2.24,0,0,0,23.66,58,2.36,2.36,0,0,0,25.8,59.3H39.74v2.49l-3.08,4.93a1.63,1.63,0,0,0,1.45,2.36h9.78a1.63,1.63,0,0,0,1.45-2.36l-3.08-4.93V59.3H60.2A2.36,2.36,0,0,0,62.34,58,2.24,2.24,0,0,0,62,55.48Z" style="fill: #fff"/></svg></div>';
} else if(typeof Fancybox == 'function'){
    // Fancybox v5
    console.log("FB5 loaded - #105480");
    // Currently does not work for FB5 as spinner templates are not longer a thing
} else {
    console.log("Fancybox not loaded - #105480");
    console.log("Fancybox 5 type: " + typeof Fancybox);
    console.log("Fancybox 3 type: " + typeof $.fancybox);
}


/* ======================================================== */
// ARCGIS JS API default settings
/* ======================================================== */

var imgLoaderUrl = 'https://parks.qld.gov.au/_design/design-helpers/find-a-park/facilities-ajax-loader?SQ_ASSET_CONTENTS_RAW';

//Highlight popup style
var highlightOptions =  {
    color: '#de5e00',
    fillOpacity: 0,
    haloOpacity: 0.65
}
//Generic template. Content defined in the attribute object
var popup_template = {
    title: '{title}',
    content: '{content}'
}

//Park boundary marker
var park_boundary_symbol = {
    type: 'simple-fill',
    color: [0,116,138,0.25],
    outline: {
        color: [0,116,138,0.5],
        width: 0
    }
};

//Park marker
var park_marker_symbol = {
    type: 'picture-marker',
    width: '40px',
    height: '40px',
    url: svgDataUrlBase64('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" width="86" height="86" aria-hidden="true">    <circle cx="43" cy="43" r="41" style="fill: #00748a"/>    <path d="M43,4A39,39,0,1,1,4,43,39.05,39.05,0,0,1,43,4m0-4A43,43,0,1,0,86,43,43,43,0,0,0,43,0Z" style="fill: #fff"/>  <path d="M62,55.48l-8.14-9.22H57a2.32,2.32,0,0,0,2.12-1.33,2.25,2.25,0,0,0-.34-2.45l-8-9.26h2.95a2.3,2.3,0,0,0,2.12-1.38,2.28,2.28,0,0,0-.43-2.46l-11.23-12a1.68,1.68,0,0,0-2.38,0l-11.23,12a2.28,2.28,0,0,0-.43,2.46,2.3,2.3,0,0,0,2.12,1.38h2.95l-8,9.26a2.27,2.27,0,0,0-.34,2.45A2.32,2.32,0,0,0,29,46.26h3.12L24,55.48A2.24,2.24,0,0,0,23.66,58,2.36,2.36,0,0,0,25.8,59.3H39.74v2.49l-3.08,4.93a1.63,1.63,0,0,0,1.45,2.36h9.78a1.63,1.63,0,0,0,1.45-2.36l-3.08-4.93V59.3H60.2A2.36,2.36,0,0,0,62.34,58,2.24,2.24,0,0,0,62,55.48Z" style="fill: #fff"/></svg>')
};

//Campgrounds marker
var campground_marker_symbol = {
    type: 'picture-marker',
    width: '30px',
    height: '30px',
    url: svgDataUrlBase64('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" width="86" height="86">    <circle cx="43" cy="43" r="41" style="fill: #333"/>    <path d="M43,4A39,39,0,1,1,4,43,39.05,39.05,0,0,1,43,4m0-4A43,43,0,1,0,86,43,43,43,0,0,0,43,0Z" style="fill: #fff"/>  <path d="M43,23.49V56.62H24.25v-8.4l-4.91,6,.73,1.46-1.86,1L15.69,52l1.87-1,.65,1.3,6-7.5L43,21.1,61.67,44.86l6,7.5.73-1.3,2,1.14-2.68,4.7-1.86-1,.89-1.62-5-5.76v8.07h-9c-2.76,0-3.61-1.22-4.34-5.84L43,23.49" style="fill: #fff"/></svg>')
};

// Attractions marker
var attraction_marker_symbol = {
    type: 'picture-marker',
    width: '30px',
    height: '30px',
    url: svgDataUrlBase64('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" width="86" height="86">    <circle cx="43" cy="43" r="41" style="fill: #333"/>    <path d="M43,4A39,39,0,1,1,4,43,39.05,39.05,0,0,1,43,4m0-4A43,43,0,1,0,86,43,43,43,0,0,0,43,0Z" style="fill: #fff"/>  <path d="M56.05,30.22h2.5s.25,1.08.58,2.41L63,49a23.65,23.65,0,0,1,.54,4.86V59.4a11.7,11.7,0,0,1-.29,3.12c-.16.33-2.12.66-3.49.66H53.27a11.65,11.65,0,0,1-3.12-.29c-.33-.17-.67-2.16-.67-3.49V49.46c0-1.37-.62-2.45-1.41-2.45s-1.41-1-1.41-2.28v-12a2.46,2.46,0,0,1,2.49-2.45h6.9m-9.39-2.37h7.9V22.82h-7.9ZM30,30.22H27.49s-.24,1.08-.58,2.41L23.09,49a27.78,27.78,0,0,0-.58,4.86V59.4a11.7,11.7,0,0,0,.29,3.12c.16.37,2.16.66,3.49.66h6.44a11.65,11.65,0,0,0,3.12-.29c.37-.17.67-2.16.67-3.49V49.46c0-1.37.62-2.45,1.41-2.45s1.41-1,1.41-2.28v-12a2.46,2.46,0,0,0-2.49-2.45H30m1.49-7.48h7.9v5.11h-7.9Zm10.19,7.4h2.78v14.3H41.63Z" style="fill: #fff"/></svg>')
};

// Journeys marker
var journey_marker_symbol = {
    type: 'picture-marker',
    width: '30px',
    height: '30px',
    url: svgDataUrlBase64('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" width="86" height="86">    <circle cx="43" cy="43" r="41" style="fill: #333"/>    <path d="M43,4A39,39,0,1,1,4,43,39.05,39.05,0,0,1,43,4m0-4A43,43,0,1,0,86,43,43,43,0,0,0,43,0Z" style="fill: #fff"/>  <path d="M53.73,46.88H44.81a3,3,0,0,1,0-6h8.92S62.65,31,62.65,26.07a8.92,8.92,0,0,0-17.84,0C44.81,28.44,46.87,32,49,35H44.81a8.92,8.92,0,1,0,0,17.83h8.92a3,3,0,0,1,0,6H32.32a67,67,0,0,1-4.4,5.95H53.73a8.92,8.92,0,1,0,0-17.84Zm0-23.79a3,3,0,1,1-3,3A3,3,0,0,1,53.73,23.09ZM24,40.93a8.93,8.93,0,0,0-8.92,8.92c0,4.93,8.92,14.87,8.92,14.87s8.92-9.94,8.92-14.87A8.93,8.93,0,0,0,24,40.93Zm0,11.89a3,3,0,1,1,3-3A3,3,0,0,1,24,52.82Z" style="fill: #fff"/></svg>')
};

//Park alert
var park_alert_marker_symbol = {
    type: 'picture-marker',
    width: '36px',
    height: '36px',
    url: svgDataUrlBase64('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" width="86" height="86">  <path d="M84.36,67.42,51,10c-2-3.37-5-5.28-8.1-5.28S36.9,6.58,35,10L1.67,67.42c-2,3.44-2.22,6.88-.61,9.63S5.8,81.33,9.7,81.33H76.26c3.9,0,7-1.46,8.71-4.28S86.35,70.78,84.36,67.42Z" style="fill: #fbad33"/>  <path d="M46.68,69a5.47,5.47,0,0,1-7.33-.08A4.83,4.83,0,0,1,37.67,65a5.28,5.28,0,0,1,5.27-5.27A5.4,5.4,0,0,1,46.68,69Z"/>  <path d="M48.06,36.54,46.84,50.6a11.4,11.4,0,0,1-1,4.21,3.4,3.4,0,0,1-6.11,0,13.84,13.84,0,0,1-.92-4.28l-1-13.84c-.15-2.67-.23-4.58-.23-5.8a6.09,6.09,0,0,1,1.53-4.36,5.34,5.34,0,0,1,4.05-1.6c2.6,0,3.67,1.22,4.2,2.29a12.64,12.64,0,0,1,.92,5.35A34.3,34.3,0,0,1,48.06,36.54Z"/></svg>')
};

//Your location
var your_location_marker_symbol = {
    type: 'picture-marker',
    width: '36px',
    height: '36px',
    url: svgDataUrlBase64('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 86 86" width="86" height="86">  <circle cx="43" cy="43" r="41" style="fill: #1b64af"/>  <path d="M43,4A39,39,0,1,1,4,43,39.05,39.05,0,0,1,43,4m0-4A43,43,0,1,0,86,43,43,43,0,0,0,43,0Z" style="fill: #fff"/>  <circle cx="43" cy="43" r="20.73" style="fill: none;stroke: #fff;stroke-width: 4px"/>  <line x1="43" y1="16.16" x2="43" y2="28.38" style="fill: none;stroke: #fff;stroke-width: 4px"/>  <line x1="43" y1="57.62" x2="43" y2="69.84" style="fill: none;stroke: #fff;stroke-width: 4px"/>  <line x1="69.84" y1="43" x2="57.62" y2="43" style="fill: none;stroke: #fff;stroke-width: 4px"/>  <line x1="28.38" y1="43" x2="16.16" y2="43" style="fill: none;stroke: #fff;stroke-width: 4px"/></svg>')
};

function svgDataUrlBase64(str) {
    return 'data:image/svg+xml;base64,' + btoa(str);
}

//Clustering settings
var clusterConfig = {
    type: 'cluster',
    clusterRadius: '235px',
    popupEnabled: false,
    //popupTemplate: {
    //    title: '{cluster_count} ' + type + 's in this area.',
    //    fieldInfos: [
    //        {
    //            fieldName: 'cluster_count',
    //            format: {
    //                places: 0,
    //                digitSeparator: true
    //            }
    //        }
    //    ]
    //},
    clusterMinSize: "40px",
    clusterMaxSize: "40px",
    labelingInfo: [
        {
            //deconflictionStrategy: "none",
            labelExpressionInfo: {
                expression: "Text($feature.cluster_count, '#,###')"
            },
            symbol: {
                type: "text",
                color: "#fff",
                font: {
                    weight: "bold",
                    size: "26px"
                },
                haloSize: 2,
                haloColor: "rgba(0, 0, 0, 0.75)",
                //yoffset: '-3px'
            },
            labelPlacement: "center-center"
        }
    ]
};
//var clusterConfig = null;

//Map point label settings
var parkLayerLabel = {
    symbol: {
        type: 'text',
        color: '#333',
        haloColor: '#fff',
        haloSize: '2px',
        lineWidth: 100,
        yoffset: '5px',
        font: {
            size: '13px',
            family: 'Arial',
            style: 'normal',
            weight: 'bold'
        }
    },
    labelPlacement: 'below-center',
    labelExpressionInfo: {
        /*expression: `
            var titleArray = Split($feature.title, '<span class="text">');
            var newTitleArray = Split(titleArray[1], '<small>');
            var cleanTitle = Concatenate(newTitleArray, TextFormatting.NewLine);
            cleanTitle = Replace(cleanTitle, "</small>", "");
            cleanTitle = Replace(cleanTitle, "</span>", "");
            cleanTitle = Split(cleanTitle, '<span class="closed">');
            return cleanTitle[0]
        `
        expression: `
            var titleArray = Split($feature.title, '<span class="text">');
            var newTitleArray = Split(titleArray[1], '<span class="small">');
            newTitleArray = Replace(newTitleArray[0], "</span>", "");
            newTitleArray = Split(newTitleArray, '<span class="closed">');
            return newTitleArray[0]
        `
        */
        expression: "return $feature.label;"
        //expression: "IIF($feature.label == '', $feature.title, $feature.label);"
    }
}

// Close park label settings
// If the feature has attribute 'closed' add a label showing 'park closed'
var parkCloseLabel = {
    symbol: {
        type: 'text',
        color: '#ffae32',
        haloColor: '#333',
        haloSize: '2px',
        yoffset: '-3px',
        font: {
            size: '13px',
            family: 'Arial',
            style: 'normal',
            weight: 'bold'
        }
    },
    labelPlacement: 'above-center',
    labelExpressionInfo: {
        expression: "IIF($feature.status == 'closed', 'Park closed', '');"
    }
}

//More info action button for popup
var moreInfoAction = {
    title: 'More information',
    id: 'more-info',
    className: 'esri-icon-description'
};

popup_template.actions = [moreInfoAction];

function moreInfo(feature) {
    var x = feature.layer.source.items.filter(function(item) {
        return item.attributes.OBJECTID == feature.attributes.OBJECTID;
    });
    //console.log(x[0].attributes);
    location.href = x[0].attributes.url;
}

var homeWidgetSettings, scaleBarSettings, fitAll;
function addGlobalWidgets(view) {
    homeWidgetSettings = {
        view: view,
        iconClass: 'esri-icon-zoom-out-fixed',
        label: 'Fit all points',
        goToOverride: function(view, goToParams) {
            return fitAll();
        }
    };
    
    scaleBarSettings = {
        view: view,
        unit: 'metric'
    };
}
