// source --> https://huurbootloosdrecht.nl/wp-content/plugins/preloader-plus//assets/js/preloader-plus.min.js?ver=2.2.1 
/*! preloader-plus 01-04-2018 */

jQuery(function($){var perfData=window.performance.timing,EstimatedTime=-(perfData.loadEventEnd-perfData.navigationStart),time=100*parseInt(EstimatedTime/1e3%60),preloader=document.querySelector(".preloader-plus");$("body").prepend(preloader);var progBar=document.querySelector(".prog-bar"),duration=time,counter=document.getElementById("preloader-counter");!function(element,start,end,duration){var range=end-start,current=start,increment=end>start?1:-1,stepTime=Math.abs(Math.floor(duration/range)),obj=element,timer=setInterval(function(){if(current<end&&(current+=increment),null!==obj&&(obj.style["transition-duration"]="0.001s",obj.style.width=current+"%"),null!==counter&&(counter.innerHTML=current+"%"),current==end&&perfData.loadEventEnd>0||perfData.loadEventEnd>0){var endLoading=setInterval(function(){current+=increment,null!==obj&&(obj.style.width=current+"%"),null!==counter&&(counter.innerHTML=current+"%"),100==current&&(setTimeout(function(){$("body, .preloader-plus").addClass("complete")},preloader_plus.animation_delay),clearInterval(endLoading))},1);clearInterval(timer)}},stepTime)}(progBar,0,70,duration)});
// source --> https://huurbootloosdrecht.nl/wp-content/plugins/weer/public/js/weer-public.js?ver=1.0.0 
(function ($) {
    'use strict';
    /**
     * All of the code for your public-facing JavaScript source
     * should reside in this file.
     *
     * Note: It has been assumed you will write jQuery code here, so the
     * $ function reference has been prepared for usage within the scope
     * of this function.
     *
     * This enables you to define handlers, for when the DOM is ready:
     */
    $(function () {
        String.prototype.capitalize = function () {
            return this.charAt(0).toUpperCase() + this.slice(1)
        };

        var weatherWidgets = $(".weer-widget");
        for (var i = 0; i < weatherWidgets.length; i++) {
            var weatherWidget = weatherWidgets[i];
            if (weatherWidget) {
                var city = weatherWidget.getAttribute("data-city");
                var country = weatherWidget.getAttribute("data-country");
                var language = weatherWidget.getAttribute("data-language") || "dutch";
                var location_for_rest = city.replace(" ", "_") + "," + country;
                loadXMLDoc(weatherWidget, city, country, language, location_for_rest);
            }
        }

        function loadXMLDoc(weatherWidget, city, country, language, location_for_rest) {
            var xhr = new XMLHttpRequest();
            xhr.onload = function () {
                // Process our return data
                if (xhr.status >= 200 && xhr.status < 300) {
                    // Runs when the request is successful
                    handleResponse(JSON.parse(xhr.responseText), weatherWidget, city, country, language);
                } else {
                    // Runs when it's not
                    console.log(xhr.responseText);
                }
            };
            xhr.open('GET', 'https://www.weatherwp.com/api/common/publicWeatherForLocation.php?city=' + encodeURIComponent(city) + '&country=' + encodeURIComponent(country) + '&place=' + location_for_rest + '&domain=' + document.location + '&language=' + language+ '&widget=Dutch');
            xhr.send();
        }


        function handleResponse(response, weatherWidget, city, country, language) {
            var header = weatherWidget.getAttribute("data-header");
            var widgetInputWidth = weatherWidget.getAttribute("data-width");
            var days = weatherWidget.getAttribute("data-days");
            var isCurrent = weatherWidget.getAttribute("data-current");
            var isWind = weatherWidget.getAttribute("data-wind");
            var isSunrise = weatherWidget.getAttribute("data-sunrise");
            var backgroundColor = weatherWidget.getAttribute("data-background");
            if (!backgroundColor || backgroundColor === '' || backgroundColor === '#becffb') {
                backgroundColor = '#407cea';
            }
            var textColor = weatherWidget.getAttribute("data-text-color");
            if (!textColor || textColor === '') {
                textColor = 'white';
            }

            //console.log(response);
            var deepLink = response.deepLink;
            var title = "Het weer in " + city.capitalize();
            if (language === "english") {
                title = "Weather in " + city.capitalize();
            }
            if (header !== '') {
                title = header;
            }


            var temp = response.temp + '&#176;';
            var tempType = "C";
            var sunrise = '<div class="sun-label">Zonsopkomst</div><div class="sun-value">' + response.sunrise + '</div>';
            if (language === "english") {
                sunrise = '<div class="sun-label">Sunrise</div><div class="sun-value">' + response.sunrise + '</div>';
            }
            var sunset = '<div class="sun-label">Zonsondergang</div><div class="sun-value">' + response.sunset + '</div>';
            if (language === "english") {
                sunset = '<div class="sun-label">Sunset</div><div class="sun-value">' + response.sunset + '</div>';
            }
            var imageURL = response.icon;
            // var humidity = "Vochtigheid: " + response.humidity + "%";
            // if (language === "english") {
            // 	humidity = "Humidity: " + response.humidity + "%";
            // }
            var humidity = response.humidity + "%";

            // var windspeedKmph = "Windsnelheid: " + response.windspeedKmph + "Kmph";
            // if (language === "english") {
            // 	windspeedKmph = "Wind Speed: " + response.windspeedKmph + "Kmph";
            // }
            var windspeedKmph = response.windspeedKmph + "Km/h";

            // var chanceofrain = "Kans op regen: " + response.chanceofrain + "%";
            // if (language === "english") {
            // 	chanceofrain = "Chance for rain: " + response.chanceofrain + "%";
            // }
            var chanceofrain = response.chanceofrain + "%";
            var daysForecast = response.days;
            var description = response.description;


            // set tight if user asked for not 100%
            if (widgetInputWidth === 'tight') {
                $(weatherWidget).addClass('tight');
            } else {
                $(weatherWidget).addClass('maxwidth');
            }



            //mainWrapElm
            var mainWrapElm = document.createElement("div");

            mainWrapElm.setAttribute("style", "background-color: " + backgroundColor + " ;color:" + textColor);

            //title
            var titleElm = document.createElement("div");
            titleElm.innerHTML = title;
            titleElm.setAttribute("class", "weather_widget_title");
            mainWrapElm.appendChild(titleElm);

            // wrap deep link
            var mainDeepLinkElm = document.createElement("a");
            mainDeepLinkElm.setAttribute("href", deepLink);
            mainDeepLinkElm.setAttribute("target", "_blank");
            mainDeepLinkElm.setAttribute("title", title);


            if (isCurrent === "on") {
                // center wrap
                var centerWrapElm = document.createElement("div");
                centerWrapElm.setAttribute("class", "weather_center_wrap");

                // temp wrap
                var tempWrap = document.createElement("div");
                tempWrap.setAttribute("class", "weather_temp_wrap");

                // temp
                var tempElm = document.createElement("span");
                tempElm.innerHTML = temp;
                tempElm.setAttribute("class", "weather_temp");
                tempWrap.appendChild(tempElm);

                // temp type
                var tempTypeElm = document.createElement("span");
                tempTypeElm.innerHTML = tempType;
                tempTypeElm.setAttribute("class", "weather_temp_type");
                tempWrap.appendChild(tempTypeElm);

                // image
                var imageWrapElm = document.createElement("div");
                var imageElm = document.createElement("img");
                imageElm.setAttribute("src", imageURL);

                imageElm.setAttribute("class", "weather_image");
                imageWrapElm.setAttribute("class", "weather_image_wrap");
                imageWrapElm.appendChild(imageElm);

                centerWrapElm.appendChild(imageWrapElm);
                centerWrapElm.appendChild(tempWrap);
                mainWrapElm.appendChild(centerWrapElm);


                // description
                var descriptionElm = document.createElement("div");
                descriptionElm.innerHTML = description;
                descriptionElm.setAttribute("class", "weather_description");
                // mainWrapElm.appendChild(descriptionElm);
            }


            if (isWind === 'on') {
                var weatherDataElm = document.createElement("div");
                weatherDataElm.setAttribute("class", "weather_data_wrap"); //"");

                // humidity
                var humidityElm = document.createElement("div");
                humidityElm.innerHTML = humidity;
                humidityElm.setAttribute("class", "weather_humidity"); //");
                weatherDataElm.appendChild(humidityElm);
                // wind
                var windElm = document.createElement("div");
                windElm.innerHTML = windspeedKmph;
                windElm.setAttribute("class", "weather_wind"); //");
                weatherDataElm.appendChild(windElm);
                // rain
                var chanceofrainElm = document.createElement("div");
                chanceofrainElm.innerHTML = chanceofrain;
                chanceofrainElm.setAttribute("class", "weather_rain"); //");
                weatherDataElm.appendChild(chanceofrainElm);

                // add to dom
                mainWrapElm.appendChild(weatherDataElm);
            }

            if (isSunrise === 'on') {
                var sunSection = document.createElement("div");
                sunSection.setAttribute("class", "sun-section");


                // sunrise
                var sunriseElm = document.createElement("div");
                sunriseElm.innerHTML = sunrise;
                sunriseElm.setAttribute("class", "weather_sunrise");
                sunSection.appendChild(sunriseElm);

                // sunset
                var sunsetElm = document.createElement("div");
                sunsetElm.innerHTML = sunset;
                sunsetElm.setAttribute("class", "weather_sunset");
                sunSection.appendChild(sunsetElm);


                mainWrapElm.appendChild(sunSection);
            }

            // background mask
            var daysBGWrap = document.createElement("div");
            daysBGWrap.setAttribute("class", "widget-bg-mask");
            daysBGWrap.setAttribute("style", "background-color: " + backgroundColor + " ;color:" + textColor);
            mainWrapElm.appendChild(daysBGWrap);


            // days
            if (Number(days) > 0) {
                var daysWrap = document.createElement("div");
                daysWrap.setAttribute("class", "weather_days_wrap"); //"");

                for (var i = 0; i < Number(days); i++) {
                    var dayElm = document.createElement("div");
                    var dayElmWidth = "33.3%";
                    if (Number(days) === 2 || Number(days) === 4) {
                        dayElmWidth = "50%";
                    }
                    dayElm.setAttribute("class", "weather_day_wrap"); //"width: " + dayElmWidth + "; text-align: center;margin: 10px 0px;");
                    // day name
                    var dayNameElm = document.createElement("div");
                    dayNameElm.innerHTML = daysForecast[i].dayName;
                    dayNameElm.setAttribute("class", "weather_day_name"); //"");
                    dayElm.appendChild(dayNameElm);

                    // day image
                    var dayImageWrapElm = document.createElement("div");
                    var dayImageElm = document.createElement("img");
                    dayImageElm.setAttribute("class", "weather_day_image"); //"");
                    dayImageElm.setAttribute("src", daysForecast[i].icon);
                    dayImageWrapElm.setAttribute("class", "weather_day_image_wrap"); //"");
                    dayImageWrapElm.appendChild(dayImageElm);
                    dayElm.appendChild(dayImageWrapElm);

                    // day temp
                    var dayTempElm = document.createElement("div");
                    dayTempElm.innerHTML = daysForecast[i].min + "/" + daysForecast[i].max + '&#176;' + "C";
                    dayTempElm.setAttribute("class", "weather_day_temp"); //"");
                    dayElm.appendChild(dayTempElm);
                    daysWrap.appendChild(dayElm);
                }
                mainWrapElm.appendChild(daysWrap);
            }


            mainDeepLinkElm.prepend(mainWrapElm);

            weatherWidget.prepend(mainDeepLinkElm);


            // get widget width
            var widgetWidth = $(weatherWidget).width();
            var widthClass = "regular";
            if (widgetWidth < 200) {
                widthClass = "super-small";
            } else if (widgetWidth < 300) {
                widthClass = "small";
            }
            var ratio = widgetWidth / 375;

            $(weatherWidget).find('.main_wrap').addClass(widthClass);


            if (widgetWidth > 500) {
                $(weatherWidget).find('.weather_sunrise').css({"margin-top": "20px"});
                $(weatherWidget).find('.weather_sunset').css({"margin-top": "20px"});
            }
            var sunNewHeight = ratio * 70;
            var sunNewPadding = ratio * 10;
            var sunStyle = ""
            if (widgetWidth > 375) {
                $(weatherWidget).find('.sun-section').css({"height": sunNewHeight + "px", "padding": "0 " + sunNewPadding + "%"});
            }
            if (widgetWidth > 500) {
                $(weatherWidget).find('.sun-section').css({
                    "height": "115px",
                    "margin": "70px auto 0 auto",
                    "max-width": "500px",
                    "background-position": "center"
                });
            }

            // set the height of the mask image
            var newHeight = ratio * 65;
            $(weatherWidget).find('.widget-bg-mask').css({
                "height": newHeight + "px"
            });
        }
    });

})(jQuery);