(function () {

    var $options = {
        containerId: '#holidaycheck-widget-hotel-1-88347-de_DE-s-g-1-1---0',
        jQueryUrl: 'https://www.holidaycheck.de/assets/classic/js/jquery-3.7.1.min.js',
        cssUrl: 'https://www.holidaycheck.de/widgets/widgets.css?v=1751449187',
        fontUrl: 'https://www.holidaycheck.de/promo/css/font-opensans.css?src=admin&v=1751449187',
        ajaxCallUrl: "https://www.holidaycheck.de/widgets?step=code&type=callback&options=hotel-1-88347-de_DE-s-g-1-1---0"
    };

    //ensure jQuery is available
    var jQ;
    if ((typeof self.jQuery == 'undefined') || !jQuery.fn || !jQuery.fn.jquery || (parseFloat(jQuery.fn.jquery) < 1.8)) {

        function getScript(url, onScriptLoad) {
            var script = document.createElement('script');
            script.src = url;

            var head = document.getElementsByTagName('head')[0] || document.documentElement,
                done = false;

            // Attach handlers for all browsers
            script.onload = script.onreadystatechange = function () {
                if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
                    done = true;
                    onScriptLoad();
                    script.onload = script.onreadystatechange = null;
                }
            };
            head.appendChild(script);
        }

        getScript($options.jQueryUrl, function () {
            if (typeof self.jQuery == 'undefined') {
                if (window.console && console.error) {
                    console.error('failed to load jQuery');
                }
            } else {
                jQ = self.jQuery.noConflict(true);
                main();
            }
        });

    } else {
        //use existing jQuery
        jQ = self.jQuery;
        main();
    }

    /**
     * Adding multi line class if needed.
     *
     * @param element
     */
    function handleMultiline(widgetSize, element) {
        var widthToCompare = 0;

        /**
         * Because this is strictly dependant on how does the layout look like,
         * some of the figures are hardcoded. Should the layouts change,
         * this part of the code must be adjusted accordingly.
         */
        switch (widgetSize) {
            case 's':
                /**
                 * Subtracting a bit, so that the name is not from edge to edge.
                 */
                widthToCompare = element.parent().width() - 4;
                break;

            case 'm':
                /**
                 * This value is based on:
                 *  1) widgets width
                 *  2) homepage link width
                 *  3) .hc-widget-hotel-name margins (8px on the left, 8px on the right)
                 *
                 *  All are const values - except for a small differences depending
                 *  on selecting theme (one time you have borders, other not).
                 *  Thus additional 5px value subtracted to be sure that we have
                 *  a width capable of determining if the name fits or not.
                 *
                 * @type {number}
                 */
                widthToCompare = element.parent().parent().width() - element.parent().find('a.hc-widget-home-link').outerWidth() - 16 - 5;
                break;
        }

        if (element.outerWidth() > widthToCompare) {
            element.addClass('hc-widget-multiline');
        }
    }

    /**
     * Loader function
     */
    function main() {
        jQ(document).ready(function () {

            if (jQ('#holidaycheck_widget_font').length == 0) {
                jQ('head').append('<link rel="stylesheet" type="text/css" id="holidaycheck_widget_font" href="' + $options.fontUrl + '" />');
            }

            if (jQ('#holidaycheck_widget_css').length == 0) {
                jQ('head').append('<link rel="stylesheet" type="text/css" id="holidaycheck_widget_css" href="' + $options.cssUrl + '" />');
            }

            jQ.ajax({
                url: $options.ajaxCallUrl,
                cache: false,
                dataType: 'jsonp',
                success: function (data, textStatus) {
                    var $container = jQ($options.containerId);
                    $container.html(data.content);

                    /**
                     * Widgets S and M have a bit narrow container for Hotel / POI names.
                     * When those names are wider than the container that holds them,
                     * then .hc-widget-multiline class needs to be added to properly
                     * display that name.
                     */
                    var $hotelName = $container.find('.hc-widget-small .hc-widget-hotel-name');

                    if ($hotelName.length) {
                        handleMultiline('s', $hotelName);
                    } else {
                        $hotelName = $container.find('.hc-widget-medium .hc-widget-name-wrapper');

                        if ($hotelName.length) {
                            handleMultiline('m', $hotelName);
                        }
                    }

                    //check if the widget fits into its parent - otherwise squeeze
                    var hcWidget = jQuery(".hc-widget");
                    if (hcWidget.length && (hcWidget = hcWidget[0])) {
                        var hcWidgetParentWidth = hcWidget.parentNode.offsetWidth;
                        if (hcWidget.offsetWidth > hcWidgetParentWidth) {
                            jQ(hcWidget).css({width: hcWidgetParentWidth - 5}).addClass('hc-squeeze');
                        }
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    if (window.console && console.error) {
                        console.error('HolidayCheck Widget Error: %o', arguments);
                    }
                }
            });
        });
    }
})();
