var sangarBaseClass; ;(function($) { sangarBaseClass = function(base, opt) { /** * Function: getImgHeight */ this.getImgHeight = function(width,index,totalLength) { if(opt.continousSliding) { index = index % (totalLength / 3); // modulus, for continousSliding } var Twidth = base.imgWidth[index]; var Theight = base.imgHeight[index]; var minusResize = Twidth - width; var percentMinus = (minusResize / Twidth) * 100; var height = Theight - (Theight * percentMinus / 100); return height; } /** * Function: getImgWidth */ this.getImgWidth = function(height,index,totalLength) { if(opt.continousSliding) { index = index % (totalLength / 3); // modulus, for continousSliding } var Twidth = base.imgWidth[index]; var Theight = base.imgHeight[index]; var minusResize = Theight - height; var percentMinus = (minusResize / Theight) * 100; var width = Twidth - (Twidth * percentMinus / 100); return width; } /** * Function: playVideo */ this.playVideo = function() { var video = base.$currentSlide.children('video'); if(video[0]) { base.setVideoCentered(video); video[0].load(); video[0].currentTime = 0.1; if(! base.$prevSlide) //if first slide { video[0].play(); } else { setTimeout(function() { video[0].play(); }, opt.animationSpeed); } if(opt.html5VideoNextOnEnded) { video[0].onended = function(e) { base.shift('next'); }; } else { video.attr('loop','loop'); } } // pause prev video if(base.$prevSlide) { base.pauseVideo(base.$prevSlide); } } /** * Function: pauseVideo */ this.pauseVideo = function(slide) { // html 5 video var video = slide.children('video'); if(video[0]) { setTimeout(function() { video[0].pause(); }, opt.animationSpeed); } // vimeo and youtube var iframe = slide.children('iframe'); if(iframe[0]) { setTimeout(function() { var src = iframe.attr('src'); iframe.attr('src',''); iframe.attr('src',src); }, opt.animationSpeed); } } /** * Function: setVideoCentered */ this.setVideoCentered = function(currentSlide) { var domVideo = currentSlide[0]; var attr = currentSlide.attr('centered'); if (typeof attr === typeof undefined || attr === false) { // show loading base.setLoading(base.$currentSlide,'show'); domVideo.onloadedmetadata = function() { var vidWidth = this.videoWidth; var vidHeight = this.videoHeight; var minusResize = base.sangarWidth - vidWidth; var percentMinus = (minusResize / vidWidth) * 100; var realHeight = vidHeight + (vidHeight * percentMinus / 100); var margin = (realHeight - base.origHeight) / 2; currentSlide .css('margin-top','-' + margin + 'px') .attr('realWidth',base.sangarWidth) .attr('realHeight',realHeight) .attr('centered','true'); // fadeOut loading base.setLoading(base.$currentSlide,'fadeOut'); }; } else { var vidWidth = parseInt(currentSlide.attr('realWidth')) var vidHeight = parseInt(currentSlide.attr('realHeight')); var minusResize = base.sangarWidth - vidWidth; if(minusResize < 0) minusResize * -1; var percentMinus = (minusResize / vidWidth) * 100; var realHeight = vidHeight + (vidHeight * percentMinus / 100); var margin = (realHeight - base.origHeight) / 2; currentSlide .css('margin-top','-' + margin + 'px') .attr('realWidth',base.sangarWidth) .attr('realHeight',realHeight); // force hide/fadeOut the loading element if it still there base.setLoading(base.$currentSlide,'fadeOut'); } } /** * Function: setLoading */ this.setLoading = function(el,status) { var loading, loadingHTML = '
', loadingStyle = { 'position': 'absolute', 'width': '100%', 'height': '100%', 'background': opt.background, 'z-index': '99', 'top': '0px', 'left': '0px' }, isLoaded = el.children('.sangar-slider-loading').length, fadeTime = 400; switch(status) { case 'show': if(! isLoaded) el.append(loadingHTML); loading = el.children('.sangar-slider-loading'); loading.css(loadingStyle); break; case 'hide': if(isLoaded) { loading = el.children('.sangar-slider-loading'); loading.remove(); } break; case 'fadeIn': if(! isLoaded) el.append(loadingHTML); loading = el.children('.sangar-slider-loading'); loading .hide() .css(loadingStyle) .fadeIn(fadeTime); break; case 'fadeOut': if(isLoaded) { loading = el.children('.sangar-slider-loading'); loading.fadeOut(fadeTime,function(){ setTimeout(function() { loading.remove(); }, fadeTime); }); } break; default: // silent } } /** * Function: calculateHeightWidth */ this.calculateHeightWidth = function(widthonly) { // sangarWidth base.sangarWidth = base.$sangar.innerWidth(); var minusResize = opt.width - base.sangarWidth; var percentMinus = (minusResize / opt.width) * 100; // sangarHeight base.sangarHeight = opt.height - (opt.height * percentMinus / 100); // base.origHeight if(opt.fixedHeight) { base.origHeight = base.sangarHeight < opt.height ? base.sangarHeight : opt.height; } else { base.origHeight = base.sangarHeight; } // force height if(opt.forceHeight) { base.sangarHeight = opt.height; base.origHeight = opt.height; } } /** * Function: setupSize */ this.setupSize = function(reinit) { var maxWidth = reinit ? base.sangarWidth : opt.width; var height = reinit ? base.sangarHeight : opt.height; // width if(reinit && !opt.scaleSlide) { maxWidth = opt.width; } else if(opt.scaleSlide) { maxWidth = '100%'; realWidth = base.$sangar.width(); var minusResize = opt.width - realWidth; var percentMinus = (minusResize / opt.width) * 100; var realHeight = opt.height - (opt.height * percentMinus / 100); height = realHeight; } // height if(opt.fixedHeight) { height = base.sangarHeight < opt.height ? base.sangarHeight : opt.height; } else { height = base.sangarHeight; } // height for bullet or pagination if(opt.pagination == 'content-horizontal') { var containerHeight = height + base.$pagination.outerHeight(true); } else { var containerHeight = height; } // apply size base.$el.css({ 'height': containerHeight + 'px', 'max-width': maxWidth + 'px' }); base.$sangarWrapper.css({ 'height': containerHeight + 'px' }); base.$sangar.css({ 'height': height + 'px', 'max-width': maxWidth + 'px' }); } /** * Function: setupSizeAndCalculateHeightWidth */ this.setupSizeAndCalculateHeightWidth = function(reinit) { base.calculateHeightWidth(); // re-calculate new width & height base.setupSize(true); // Re-initialize size, scale or not base.calculateHeightWidth(); // re-calculate new width & height // vertical text pagination base.sangarWidth = base.verticalTextPaginationSetWidth(); } /** * Function: css3support */ this.css3support = function() { var element = document.createElement('div'), props = [ 'perspectiveProperty', 'WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective' ]; for ( var i in props ) { if ( typeof element.style[ props[ i ] ] !== 'undefined' ) { base.vendorPrefix = props[i].replace('Perspective', '').toLowerCase(); return opt.jsOnly ? false : true; } } return false; } /** * Function: doLoading */ this.doLoading = function(forceLoading) { // get first slide if(opt.continousSliding) { var firstSlide = base.$slideWrapper.children('.slideWrapperInside.swi2nd').children().eq(0); } else { var firstSlide = base.$slideWrapper.children().eq(0); } base.setLoading(base.$sangarWrapper,'show'); if(forceLoading) { base.setupSizeAndCalculateHeightWidth(); showAllElements(); base.setupSize(); base.$pagination.hide(); showLoading(); } else { if(base.firstRun) { hideLoading(); base.firstRun = false; // show pagination base.$pagination.show(); // Start timer setTimeout(function() { base.startTimer(); }, 1000); } else { showLoading() setTimeout(function() { hideLoading(); }, 1000); } } /** * Functions */ function hideLoading() { // show loading base.setLoading(base.$sangarWrapper,'fadeOut'); base.$slideWrapper .css({ "display": "block" }) base.$sangar.css({ 'background-image': "none", 'z-index': '0' }); } function showLoading() { base.$slideWrapper.hide(); base.$sangar.css({ 'background-image': '', 'z-index': '99' }); } function showAllElements() { base.$slideWrapper.children().fadeIn(function(){ base.$el.css({"display": "block"}); }) base.$sangarWrapper.children('.sangar-slideshow-content').fadeIn(function(){ base.$el.css({"display": "block"}); }) base.$sangarWrapper.children('.sangar-timer').fadeIn(function(){ base.$el.css({"display": "block"}); }) base.$sangarWrapper.children('.sangar-slider-nav').fadeIn(function(){ base.$el.css({"display": "block"}); }) base.$sangarWrapper.children('.sangar-pagination-wrapper').fadeIn(function(){ base.$el.css({"display": "block"}); }) base.$pagination.show(); } } /** * Function setCurrentSlide */ this.setCurrentSlide = function(reset) { base.isRunning = true; // prev slide if(base.$currentSlide) base.$prevSlide = base.$currentSlide; // current slide if(opt.continousSliding) { var groupClass = '.swi2nd'; if(base.slideDirection == 'next' && base.activeSlide == 0) { groupClass = '.swi3rd'; } else if(base.slideDirection == "prev" && base.activeSlide == (base.numberSlides - 1)) { groupClass = '.swi1st'; } // if reset if(reset) { groupClass = '.swi2nd'; // unset prev slide if it same as first slide if(base.$prevSlide && base.$prevSlide.parent().hasClass('swi2nd') && base.$prevSlide.attr('index') == 0) { base.$prevSlide = false; } } base.$currentSlide = base.$slideWrapper.children('.slideWrapperInside' + groupClass).children().eq(base.activeSlide); } else { // unset prev slide if it same as first slide if(reset && base.$prevSlide && base.$prevSlide.attr('index') == 0) { base.$prevSlide = false; } base.$currentSlide = base.$slideWrapper.children().eq(base.activeSlide); } } /** * Function: getTranslatePosition */ this.getTranslatePosition = function(htmlDom) { var computedStyle = window.getComputedStyle(htmlDom); var matrix = computedStyle.getPropertyValue('transform'); return decomposeMatrix(matrix); } function deltaTransformPoint(matrix, point) { var dx = point.x * matrix.a + point.y * matrix.c + 0; var dy = point.x * matrix.b + point.y * matrix.d + 0; return { x: dx, y: dy }; } function decomposeMatrix(matrix) { // @see http://gist.github.com/2052247 // calculate delta transform point var px = deltaTransformPoint(matrix, { x: 0, y: 1 }); var py = deltaTransformPoint(matrix, { x: 1, y: 0 }); // calculate skew var skewX = ((180 / Math.PI) * Math.atan2(px.y, px.x) - 90); var skewY = ((180 / Math.PI) * Math.atan2(py.y, py.x)); // regex translate x and y var mat = matrix.match(/^matrix3d\((.+)\)$/); if(mat) return parseFloat(mat[1].split(', ')[13]); mat = matrix.match(/^matrix\((.+)\)$/); var translateX = mat ? parseFloat(mat[1].split(', ')[4]) : 0; var translateY = mat ? parseFloat(mat[1].split(', ')[5]) : 0; return { translateX: translateX, translateY: translateY, scaleX: Math.sqrt(matrix.a * matrix.a + matrix.b * matrix.b), scaleY: Math.sqrt(matrix.c * matrix.c + matrix.d * matrix.d), skewX: skewX, skewY: skewY, rotation: skewX // rotation is the same as skew x }; } } })(jQuery);