// source --> https://www.djroki.dk/wp-content/plugins/organic-customizer-widgets/public/js/organic-widgets-public.js?ver=1.4.17 
(function ($) {
  'use strict'

  /* Check The Background Brightness --------------------- */
  function checkBrightness () {
    $('.organic-widgets-section').not('.organic-widgets-content-slideshow-section').each(function (brightness) {
      $(this).backgroundBrightness()
    })
  }

  /* Add Section Highlighting When Hovering Over Edit Shortcuts --------------------- */
  function editShortcutHoverBorder () {
    setTimeout(function () {
      $('.organic-widget .customize-partial-edit-shortcut').hover(function () {
        $('<div class="organic-widgets-preview-highlighter"></div>').insertAfter($(this).siblings('.organic-widgets-section'))
      }, function () {
        $('.organic-widgets-preview-highlighter').remove()
      })
    }, 2000)
  }

  /* Group Groupable Widgets --------------------- */
  function groupGroupableWidgets () {
    // Loop through all groups
    $('.organic-widgets-groupable-first').each(function () {
      var firstGroupItem = $(this)
      var parentContainer = firstGroupItem.closest('.organic-widget')
      var groupID = $(this).data('group-id')
      var group = $('*[data-group-id="' + groupID + '"]')
      var numItems = group.length
      if (numItems == 1 || numItems == 0) {
        var itemClass = 'organic-widgets-single'
      } else if (numItems == 2 || numItems == 4) {
        var itemClass = 'organic-widgets-half'
      } else {
        var itemClass = 'organic-widgets-third'
      }

      parentContainer.before('<div class="organic-widgets-section organic-widgets-group"><div class="organic-widgets-group-container" data-group-id="' + groupID + '"></div></div>')

      // Get container
      var container = $('.organic-widgets-group-container[data-group-id="' + groupID + '"]')
      var containerWrapperSection = container.closest('.organic-widgets-section')

      // Loop through all elements of that group
      group.each(function () {
        var groupableWidget = $(this).closest('.organic-widget')
        if ($(this).hasClass('organic-widgets-groupable-first')) {
          var bgColor = $(this).css('backgroundColor')
          var bgImage = $(this).css('backgroundImage')
          $(this).css('backgroundColor', '')
          $(this).css('backgroundImage', '')
          containerWrapperSection.css('backgroundColor', bgColor)
          containerWrapperSection.css('backgroundImage', bgImage)
        } else {
          $(this).css('backgroundColor', '')
        }
        if (!$(this).hasClass('organic-widgets-groupable-widget')) {
          $(this).addClass('organic-widgets-groupable-widget')
        }
        groupableWidget.addClass(itemClass)
        container.append(groupableWidget)
      })
      $('.organic-widgets-group .organic-widgets-featured-content-section .organic-widgets-content').addClass('organic-widgets-card')
    })
    checkBrightness()
  }

  function bgVidSize () {
    $('.organic-widgets-video-bg-container').each(function () {
      var $this = $(this)
      if ($this.height() / $this.width() < 0.6666666666666667) {
        $this.children('.organic-widgets-video-bg-center').css({
          height: '300%',
          top: '-100%',
          width: '100%',
          left: '0'
        })
      } else {
        $this.children('.organic-widgets-video-bg-center').css({
          width: '300%',
          left: '-100%',
          height: '100%',
          top: '0'
        })
      }
    })
  }

  function editShortcutHoverBorderReady () {
    // WP Customizer
    if (typeof wp.customize != 'undefined') {
      wp.customize.bind('preview-ready', _.defer(function () { editShortcutHoverBorder() }))
    }
  }

  $(document)
    .ready(checkBrightness)
    .ready(groupGroupableWidgets)
    .ready(bgVidSize)
    .ready(editShortcutHoverBorderReady)
    .ajaxComplete(checkBrightness)
  $(window)
    .resize(bgVidSize)
})(jQuery);
// source --> https://www.djroki.dk/wp-content/plugins/organic-customizer-widgets/public/js/jquery.backgroundbrightness.js?ver=6.9.4 
/*
 *  backgroundBrightness.js
 *
 *  Copyright 2016, Jesse Lee & Organic Themes w/ snippets from ColourBrightness.js
 *  Released under the WTFPL license
 *  https://organicthemes.com
 *
 *  Version: 1.0
 */

(function ($) {

    "use strict";

    $.fn.extend({

      backgroundBrightness: function (contentSelectors, threshold) {

      // RGB Brightness Threshold
      var threshold = 160;

      // Background Object
      var el = $(this);

      // Content Targets
      if ( ! contentSelectors ) {
        contentSelectors = false;
      } else if ( !( contentSelectors.constructor === Array) ) {
        contentSelectors = [contentSelectors];
      }

      //Loop Through all Items
      $.each(el, function(key, value){

        //Initialize
        var r,g,b,brightness;

        //Get Background Styles for element
        var bgStyles = getBackgroundStyles( $(value) );
        var colour = bgStyles.bgColor;
        var image = bgStyles.bgImage;
        var targetedElement = bgStyles.targetedElement;

        // If bg image found, calculate luminosity of image
        if ( image ) {
          getBackgroundImageLuminosity( targetedElement, contentSelectors, threshold);
        }
        // If no bg image, calculate bg color brightness
        else if ( colour ) {
          if (colour.match(/^rgb/)) {
            colour = colour.match(/rgba?\(([^)]+)\)/)[1];
            colour = colour.split(/ *, */).map(Number);
            r = colour[0];
            g = colour[1];
            b = colour[2];
          } else if ("#" === colour[0] && 7 === colour.length) {
            r = parseInt(colour.slice(1, 3), 16);
            g = parseInt(colour.slice(3, 5), 16);
            b = parseInt(colour.slice(5, 7), 16);
          } else if ("#" === colour[0] && 4 === colour.length) {
            r = parseInt(colour[1] + colour[1], 16);
            g = parseInt(colour[2] + colour[2], 16);
            b = parseInt(colour[3] + colour[3], 16);
          }

          brightness = (r * 299 + g * 587 + b * 114) / 1000;

          if (brightness < threshold) {
            // white text
            targetedElement.removeClass("ocw-bg-light").addClass("ocw-bg-dark");
          } else {
            // black text
            targetedElement.removeClass("ocw-bg-dark").addClass("ocw-bg-light");
          }

        }

      });

      function getBackgroundImageLuminosity(el, contentSelectors, threshold ) {

        var bg = el.css("backgroundImage");
        bg = bg.replace(/^url\(['"]?/, "").replace(/['"]?\)$/, "");

        getImageLightness( bg, el ,function(brightness,el){

          // Set classes to add and to remove
          if (brightness < threshold) {
            var addClass = "ocw-bg-dark";
            var removeClass = "ocw-bg-light";
          } else {
            var addClass = "ocw-bg-light";
            var removeClass = "ocw-bg-dark";
          }

          // Add and remove classes
          // If applying to self
          if (!contentSelectors ){
            if ( !el.hasClass("ocw-bg-img") ) el.addClass("ocw-bg-img");
            el.removeClass(removeClass);
            el.addClass(addClass);
          }
          // If applying to custom selectors
          else {
            contentSelectors.forEach(function(contentSelector){
              if ( !$(contentSelector).hasClass("ocw-bg-img") ) $(contentSelector).addClass("ocw-bg-img");
              $(contentSelector).removeClass(removeClass);
              $(contentSelector).addClass(addClass);
            });
          }

          }); // End getImageLightness(){}

      } // End getBackgroundImageLuminosity(){}

      function getImageLightness(imageSrc, imgObject, callback) {
        var img = document.createElement("img");
        img.crossOrigin = "Anonymous";
        img.src = imageSrc;
        img.style.display = "none";
        // document.body.appendChild(img); // Adds HTML img tag in footer twice

        var colorSum = 0;

        img.onload = function() {
          // create canvas
          var canvas = document.createElement( "canvas" );
          canvas.width = this.width;
          canvas.height = this.height;

          var ctx = canvas.getContext( "2d" );
          ctx.drawImage(this,0,0);

          var imageData = ctx.getImageData( 0, 0, canvas.width, canvas.height );
          var data = imageData.data;
          var r,g,b,avg;

          for ( var x = 0, len = data.length; x < len; x += 4 ) {
            r = data[x];
            g = data[x + 1];
            b = data[x + 2];

            avg = Math.floor( (r + g + b) / 3 );
            colorSum += avg;
          }

          var brightness = Math.floor(colorSum / (this.width * this.height));
          callback(brightness, imgObject);
        }
      } // End getImageLightness(){}

      /* ======== ColourBrightness Addons =========*/

      function getBackgroundStyles(el) {
        var bgColor = false;
        var bgImage = false;
        while(el[0].tagName.toLowerCase() !== "html") {
          bgColor = ( ! el.css("backgroundColor") || el.css("backgroundColor") === "rgba(0, 0, 0, 0)" || el.css("backgroundColor") === "transparent" ) ? false : el.css("backgroundColor");
          bgImage = ( ! el.css("backgroundImage") || el.css("backgroundImage") === "" || el.css("backgroundImage") === "none" ) ? false : el.css("backgroundImage");
          if ( bgColor || bgImage ) {
            break;
          }
          el = el.parent();
        }
        return { bgColor: bgColor, bgImage: bgImage, targetedElement: el };
      }

    } // End backgroundBrightness(){}

  })

})( jQuery );