/* Lazy Slider for Joomla!
--------------------------------------------------------------
 Copyright (C) 2017 AddonDev. All rights reserved.
 Website: https://addondev.com
 GitHub: https://github.com/philip-sorokin
 Developer: Philip Sorokin
 Location: Russia, Moscow
 E-mail: philip.sorokin@gmail.com
 Created: October 2017
 License: GNU GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
--------------------------------------------------------------- */

jQuery(function(e){"use strict";function t(e,t){for(var a=e.length-1,n=a;n>=0;n--)if(e[n]&&!1===t.call(e[n],a-n))return e[n]}function a(e,a,n){t(a.split(" "),function(){l.addEventListener?e.addEventListener(this,n):e.attachEvent("on"+this,function(t){(t=o.event).preventDefault=function(){t.returnValue=!1},n.call(e,t)})})}function n(e,t){return-1!==e.className.indexOf(t)&&e.className.match(new RegExp("(^|\\s)"+t+"(\\s|$)"))}function i(e,t){if(l.getElementsByClassName)return e.getElementsByClassName(t);for(var a=0,i=[],r=e.getElementsByTagName("*"),o=r.length;a<o;a++)n(r[a],t)&&i.push(r[a]);return i}function r(e){"number"==typeof e&&(e=this),e.parentNode.removeChild(e)}"undefined"!=typeof MooTools&&Element.implement({slide:function(e,t){return this}});var o=window,l=document,s=50,u=o.lazySlider,c="data-lazyslider-src",d=l.getElementById("lazySlider"),h=i(d,"lazySlider-play-buttons");0!==u.show_playbuttons?t(h,function(){t(this.getElementsByTagName("button"),function(){n(this,"lazySlider-btn-play")?a(this,"click",function(){u.started&&u.paused&&(u.paused=!1,e(d).carousel("next").carousel("cycle"))}):a(this,"click",function(){u.paused=u.started})})}):t(h,r);var f=i(d,"lazySlider-nav-indicators");0!==u.show_indicators?t(f,function(){t(i(this,"lazySlider-nav-indicator"),function(){a(this,"click",function(){u.started&&e(d).carousel(parseInt(this.getAttribute("data-goto")))})})}):t(f,r);var p=i(d,"lazySlider-nav-arrows");function m(e){"number"==typeof e&&(e=this),a(e,"mousedown mousemove",function(t){t.preventDefault(),e.setAttribute("draggable",!1)})}0!==u.show_navigation?t(p,m):t(p,r),0===u.show_loader&&t(i(d,"lazySlider-loader"),r);var y=i(d,"carousel-inner")[0],v=i(d,"lazyslide"),g=i(d,"lazySlider-controls");t(g,m),t(v,function(){this.setAttribute("width",u.width),this.setAttribute("height",u.height),this.style["max-height"]=u.height,this.style.height="auto",m(this)});var x={pause:null,keyboard:!1,interval:void 0===u.interval?3e3:u.interval<1e3?1e3:u.interval};function z(){u.started=!0,e(d).carousel(x),u.autoplay||e(d).carousel("pause"),u.parallax&&N()}function w(e){switch(e){case 1:return"carousel-fade";case 2:return"carousel-listbox"}return""}function _(e){e.style.display=y.offsetWidth<u.controls_min_width?"none":""}function b(e){"number"==typeof e&&(e=this);var n=e.getAttribute(c);e.removeAttribute(c);var r=i(e.parentNode,"lazySlider-caption");if(r.length){r=r[0];var s=new XMLHttpRequest;s.open("GET",r.getAttribute("data-path"),!0),s.onreadystatechange=function(){if(4==s.readyState&&200==s.status){var e=l.createElement("div");e.innerHTML=s.responseText;var a=i(e,"lazySlider-caption");if(a.length){var n=(a=a[0]).style.cssText.split(";");t(["top","left","right","bottom"],function(){r.style[this]="auto"});for(var o,u=0;u<n.length;u++)(o=n[u].match(/^\s*([^:\s*]+)\s*:\s*([^:]+)$/))&&(r.style[o[1]]=o[2]);r.innerHTML=a.innerHTML}else r.innerHTML=s.responseText}},_(r),a(o,"resize",function(){_(r)}),s.send(null)}E(e,n)}function S(){var e=y.offsetWidth<u.controls_min_width?"none":"";t(g,function(){this.style.display=e})}S(),a(o,"resize",S);var E=function(e,t){e.setAttribute("src",t)};function N(){a(o,"scroll",A),a(o,"resize",T);var e=100+u.parallax_zoom,t="#lazySlider .carousel-inner:not(.lazySlider-parallax) .item {height: auto !important} .lazySlider-parallax .item {overflow: hidden} .lazySlider-parallax .lazyslide {width: "+e+"% !important; max-width: "+e+"% !important; margin-left: -"+Math.round(u.parallax_zoom/2)+"% !important; max-height: none !important; height: auto !important; position: absolute; "+u.parallax_direction+": 0}",n=l.getElementsByTagName("head");if(n.length){var i=l.createElement("style");i.setAttribute("id","lazySlider-parallax-css"),i.setAttribute("type","text/css"),i.styleSheet?i.styleSheet.cssText=t:i.appendChild(l.createTextNode(t)),n[0].appendChild(i)}T()}function T(){var e,a;a="lazySlider-parallax",-1!=(e=y).className.indexOf(a)&&(e.className=e.className.replace(new RegExp("(^|\\s*)"+a+"(?=\\s|$)","g"),"")),y.offsetWidth>=u.parallax_min_width&&(t(v,function(){this.parentNode.style.height=y.offsetHeight+"px"}),y.className+=" lazySlider-parallax",A())}function A(){var e=function(e){var t=0;do{t+=e.offsetTop||0}while(e=e.offsetParent);return t}(y),a=e+y.offsetHeight*u.parallax_depth/100,n=o.pageYOffset||l.documentElement.scrollTop||l.body.scrollTop,i=o.innerHeight||l.documentElement.clientHeight||l.body.clientHeight,r=y.offsetHeight*u.parallax_zoom/100;if(e>i){var s=-(e-n-i);s<0&&(s=0),a-=e-i}else s=n;var c=s*r/a;c>r&&(c=r),c<0&&(c=0),t(v,function(){this.style[u.parallax_direction]=-Math.round(c)+"px"})}if(u.stop_on_hover&&(x.pause="hover"),u.auto_resize){var H=u.auto_resize_quality,C=u.auto_resize_maxage;E=function(e,t){for(var a=u.width,n=a;n>y.offsetWidth;n-=300);n!=a&&(n+=300);var i,r=parseInt(n*u.height/u.width),o="";(i=t.match(/\?(.+)$/))&&(o="&"+i[1],t=t.replace(/\?.+$/,"")),u.parallax&&(n+=n/100*u.parallax_zoom,r+=r/100*u.parallax_zoom),t=u.root+"/index.php?option=com_ajax&format=raw&module=lazyslider&s="+encodeURIComponent(t)+"&w="+n+"&h="+r+"&q="+H+"&a="+C+o,e.setAttribute("src",t)};var M=function(){var e=y.offsetWidth,a=u.height*e/u.width;t(v,function(){this.style.height=a+"px"})};M(),a(o,"resize",M)}u.keyboard_control&&a(l.body,"keydown",function(t){var a=37==t.keyCode?"prev":39==t.keyCode?"next":null;a&&e(d).carousel(a)});var k,L,$=e.fn.carousel.Constructor.prototype.slide,B=!1;function W(t,a){t>a&&t-s>a?e(d).carousel("next"):t<a&&t+s<a&&e(d).carousel("prev")}e.fn.carousel.Constructor.prototype.slide=function(t,n){if(this.$element.get(0)===d){if(u.paused&&!this.paused)return e(d).carousel("pause"),this;if(u.lazy_load){var i=this.$element.find(".item.active"),r=n||i[t](),o="prev"===t&&0==i.data("order")?this.$element.find(".item").last():r[0],s=e(o).find("img["+c+"]").get(0);if(s){var h=e(o).data("order");a(s,"load error",function(){B=!1,e(d).find(".lazySlider-loader").hide(),e(d).carousel(h),this.paused||u.paused||e(d).carousel("cycle")}),B=!0,e(d).find(".lazySlider-loader").show(),e(d).carousel("pause"),b(s)}}}if(!B){if(l.addEventListener){var f=u.effect;if(-1===f)do{m=0,y=2;var p="slide "+w(f=Math.floor(Math.random()*(y-m+1))+m)}while(d.className==p);else p="slide "+w(f);d.className=p}$.apply(this,arguments)}var m,y;return this},u.lazy_load?(a(v[0],"load error",z),b(v[0])):(t(v,b),z()),a(d,"selectstart",function(e){e.preventDefault()}),"ontouchstart"in o?(a(d,"touchstart",function(e){k=e.touches[0].pageX,L=k}),a(d,"touchmove",function(e){L=e.touches[0].pageX}),a(d,"touchend",function(e){W(k,L)})):(a(d,"mousedown",function(e){k=e.clientX}),a(d,"mousemove",function(e){L=e.clientX}),a(d,"mouseup",function(e){W(k,L)}))});
