/**
 * Judo Spin
 * @name        judo-spin
 * @description 360° degrees image spinner using JavaScript.
 * @link        https://github.com/Rodgath/judo-spin
 * @author      Rodgath, https://Rodgath.com
 * @version     v1.0.3
 * @created     Jul 20, 2023
 * @updated     Aug 15, 2023
 * @copyright   Copyright (C) 2023-2023, Rodgath
 * @license     MIT
 * @licenseMIT  https://github.com/Rodgath/judo-spin/blob/main/LICENSE
 * @demoExample https://rodgath.github.io/judo-spin/demo/
 */
"use strict";function judoSpin(e,n){n||(l=e.dataset.judoSpin)&&(n=JSON.parse(l));var t,o,l,r=f((n=Object.assign({},{currImage:1,images:[],enableDragHandle:!1},n)).currImage)-1,a=!1,i=e instanceof HTMLElement||e instanceof Node?e:document.querySelector(e),s=function(){var e;e=(n.hasOwnProperty("images")&&Array.isArray(n.images)&&0<n.images.length&&n.images.forEach(function(e){var t=document.createElement("img");t.src=e.src,t.alt=e.title,i.appendChild(t)}),i.querySelectorAll("img"));return e}().length,d=360/s,c=0<r?r*d:0,u=!1,h=document.createElement("div"),p=n.enableDragHandle?document.createElement("div"):null,m=n.enableDragHandle?document.createElement("div"):null;function f(e){e=parseInt(e,10);return isNaN(e)?1:Math.abs(e)}(o=h).className="judo-spin-overlay",o.style.position="absolute",o.style.width="100%",o.style.height="100%",o.style.top="0",o.style.left="0",o.style.zIndex="1",i.appendChild(o),o.addEventListener("mouseover",function(){return o.style.cursor="e-resize"}),o.addEventListener("mouseout",function(){return o.style.cursor="auto"}),(l=i).classList.add("judo-spin-container"),l.style.position="relative",l.style.width="100%",l.style.height="auto",l.style.overflow="hidden";for(var v=i.getElementsByTagName("img"),g=0;g<v.length;g++)v[g].style.width="100%";e=i,(t=document.createElement("div")).classList.add("judo-spin-wrapper"),t.style.width="fit-content",t.style.margin="0 auto",e.parentNode.insertBefore(t,e),t.appendChild(e);h.addEventListener("mousedown",function(e){return C(e)}),h.addEventListener("touchstart",function(e){return C(e)});var x,E,y,w,C=function(e){e.preventDefault();var t="touchstart"===e.type?"touchmove":"mousemove",n="touchstart"===e.type?b:k,e=(console.log(e.type),u=!0,j(c));c=e*d,h.addEventListener(t,n)},k=(document.addEventListener("mouseup",function(){u=!1,h.removeEventListener("mousemove",k)}),document.addEventListener("touchend",function(){u=!1,h.removeEventListener("touchmove",b)}),function(e){return L(e)}),b=function(e){return L(e)},L=function(e){var t;u&&("right"===(t=function(e,t){var n;{var o;null!==S&&null!==H&&(D<(o=e-S)?n="right":o<-D&&(n="left"))}return S=e,H=t,n}(("touchmove"===e.type?e.touches[0]:e).clientX,("touchmove"===e.type?e.touches[0]:e).clientY))?r++:"left"===t&&r--,M(c=((c=r*d)%360+360)%360),n.enableDragHandle)&&(a=!0,N(e))},j=function(e){e=Math.floor(e/d);return e<0?e=s-1:s<=e&&(e=0),e},M=function(e){for(var t=Math.floor(e/d)+1,n=1;n<=s;n++)i.querySelector("img:nth-child("+n+")").style.display=n===t?"block":"none"},S=(M(c),null),H=null,D=1;function N(e){var t,n,o;a&&(t=p.getBoundingClientRect(),n=m.offsetWidth,o=p.offsetWidth-n,e="touchmove"===e.type?e.touches[0].clientX-t.left-n/2:e.clientX-t.left-n/2,e=Math.max(0,Math.min(o,e)),m.style.left="".concat(e,"px"),c=Math.floor(e*(360/o)),M(c=(c%360+360)%360))}function T(e){e.preventDefault(),a=!0,document.addEventListener("mousemove",N),document.addEventListener("touchmove",N)}function z(){a=!1,document.removeEventListener("mousemove",N),document.removeEventListener("touchmove",N)}n.enableDragHandle&&(p.id="judo-scroller",p.style.cssText="\n    position: relative;\n    width: 100%;\n    height: 12px;\n    background-color: #f1f1f1;\n    margin: 20px auto;\n    border-radius: 10px;\n    cursor: pointer;\n    box-shadow: inset 1px 1px 3px #d7d7d7;",m.id="judo-draggable",m.style.cssText="\n    position: absolute;\n    top: 0;\n    left: 0;\n    width: 60px;\n    height: calc(100% + 10px);\n    background-color: #4caf50;\n    border-radius: 20px;\n    cursor: pointer;\n    transform: translateY(-5px);\n    box-shadow: 0 0 5px rgba(0, 0, 0, 0.45);\n    display: flex;\n    align-items: center;\n    justify-content: center;",(e=n.enableDragHandle?document.createElement("span"):null).textContent="|||",e.style.cssText="\n    color: #fff;\n    position: relative;\n    height: 10px;\n    line-height: 10px;\n    overflow: hidden;\n    text-shadow: 1px 0 2px #a8a8a8;",w="\n    background-color: #fff;\n    -webkit-mask-repeat: no-repeat;\n    mask-repeat: no-repeat;\n    position: absolute;\n    top: 50%;\n    transform: translateY(-7px);\n    width: 14px;\n    height: 14px;",(x=document.createElement("span")).textContent="",E="url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' stroke-width='2' stroke='currentColor' fill='none' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath stroke='none' d='M0 0h24v24H0z' fill='none'%3E%3C/path%3E%3Cpath d='M13.883 5.007l.058 -.005h.118l.058 .005l.06 .009l.052 .01l.108 .032l.067 .027l.132 .07l.09 .065l.081 .073l.083 .094l.054 .077l.054 .096l.017 .036l.027 .067l.032 .108l.01 .053l.01 .06l.004 .057l.002 .059v12c0 .852 -.986 1.297 -1.623 .783l-.084 -.076l-6 -6a1 1 0 0 1 -.083 -1.32l.083 -.094l6 -6l.094 -.083l.077 -.054l.096 -.054l.036 -.017l.067 -.027l.108 -.032l.053 -.01l.06 -.01z' stroke-width='0' fill='currentColor'%3E%3C/path%3E%3C/svg%3E\")",x.style.cssText="\n    -webkit-mask-image: ".concat(E,";\n    mask-image:").concat(E,";\n    left: 1px;\n    ").concat(w),(E=document.createElement("span")).textContent="",y="url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' stroke-width='2' stroke='currentColor' fill='none' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath stroke='none' d='M0 0h24v24H0z' fill='none'%3E%3C/path%3E%3Cpath d='M9 6c0 -.852 .986 -1.297 1.623 -.783l.084 .076l6 6a1 1 0 0 1 .083 1.32l-.083 .094l-6 6l-.094 .083l-.077 .054l-.096 .054l-.036 .017l-.067 .027l-.108 .032l-.053 .01l-.06 .01l-.057 .004l-.059 .002l-.059 -.002l-.058 -.005l-.06 -.009l-.052 -.01l-.108 -.032l-.067 -.027l-.132 -.07l-.09 -.065l-.081 -.073l-.083 -.094l-.054 -.077l-.054 -.096l-.017 -.036l-.027 -.067l-.032 -.108l-.01 -.053l-.01 -.06l-.004 -.057l-.002 -12.059z' stroke-width='0' fill='currentColor'%3E%3C/path%3E%3C/svg%3E\")",E.style.cssText="\n    -webkit-mask-image: ".concat(y,";\n    mask-image:").concat(y,";\n    right: 1px;\n    ").concat(w),m.appendChild(e),m.appendChild(x),m.appendChild(E),p.appendChild(m),t.appendChild(p),m.addEventListener("mousedown",T),m.addEventListener("touchstart",T),document.addEventListener("mouseup",z),document.addEventListener("touchend",z),y=m.offsetWidth,y=p.offsetWidth-y,w=f(n.currImage)-1,y=Math.max(0,Math.min(y,y/s*w)),m.style.left="".concat(y,"px"))}var judoSpinElements=document.querySelectorAll("[data-judo-spin]");judoSpinElements.forEach(function(e){return judoSpin(e)});