(function($){
"use strict";
$.avia_utilities=$.avia_utilities||{};
$(function(){
if($.fn.avia_parallax){
$('.av-parallax,.av-parallax-object').avia_parallax();
}});
var AviaObjectParallaxElement=function(options, element){
if(!(this.transform||this.transform3d) ){
return;
}
this.options=$.extend({}, options);
this.win=$(window);
this.body=$('body');
this.isMobile=$.avia_utilities.isMobile,
this.winHeight=this.win.height();
this.winWidth=this.win.width();
this.el=$(element).addClass('active-parallax');
this.objectType=this.el.hasClass('av-parallax-object') ? 'object':'background-image';
this.elInner=this.el;
this.elBackgroundParent=this.el.parent();
this.elParallax=this.el.data('parallax')||{};
this.direction='';
this.speed=0.5;
this.elProperty={};
this.ticking=false,
this.isTransformed=false;
if($.avia_utilities.supported.transition===undefined){
$.avia_utilities.supported.transition=$.avia_utilities.supports('transition');
}
this._init(options);
};
AviaObjectParallaxElement.prototype =
{
mediaQueries: {
'av-mini-':		'(max-width: 479px)',
'av-small-':	'(min-width: 480px) and (max-width: 767px)',
'av-medium-':	'(min-width: 768px) and (max-width: 989px)',
'av-desktop-':	'(min-width: 990px)'
},
transform:			document.documentElement.className.indexOf('avia_transform')!==-1,
transform3d:		document.documentElement.className.indexOf('avia_transform3d')!==-1,
mobileNoAnimation:	$('body').hasClass('avia-mobile-no-animations'),
defaultSpeed:		0.5,
defaultDirections:	[ 'bottom_top', 'left_right', 'right_left', 'no_parallax' ],
transformCSSProps:	[ 'transform', '-webkit-transform', '-moz-transform', '-ms-transform', '-o-transform' ],
matrixDef:			[ 1, 0, 0, 1, 0, 0 ],
matrix3dDef:		[ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ],
_init: function(){
var _self=this;
if(typeof this.el.data('parallax-selector')!='undefined'&&this.el.data('parallax-selector')!==''){
this.elInner=this.el.find(this.el.data('parallax-selector') );
if(this.elInner.length==0){
this.elInner=this.el;
}}
if('background-image'==this.objectType){
if(this.isMobile&&this.mobileNoAnimation){
return;
}
this.elParallax.parallax='bottom_top';
this.elParallax.parallax_speed=parseFloat(this.el.data('avia-parallax-ratio') )||0.5;
}
setTimeout(function(){
_self._fetchProperties();
}, 30);
this.win.on('debouncedresize av-height-change', _self._fetchProperties.bind(_self) );
this.body.on('av_resize_finished', _self._fetchProperties.bind(_self) );
setTimeout(function(){
_self.win.on('scroll', _self._onScroll.bind(_self) );
}, 100);
},
_setParallaxProps: function(){
if('background-image'==this.objectType){
this.direction=this.elParallax.parallax;
this.speed=this.elParallax.parallax_speed;
return;
}
var all_direction=this.elParallax.parallax||'',
all_speed=this.elParallax.parallax_speed||'',
resp_direction='',
resp_speed='',
media='all';
if(this.defaultDirections.indexOf(all_direction) < 0){
all_direction='no_parallax';
}
if(typeof window.matchMedia=='function'){
$.each(this.mediaQueries, function(key, query){
var mql=window.matchMedia(query);
if(mql.matches){
media=key;
return false;
}});
}
if('all'==media){
this.direction=all_direction;
this.speed=''==all_speed ? this.defaultSpeed:parseFloat(all_speed) / 100.0;
return;
}
resp_direction=this.elParallax[ media + 'parallax' ]||'';
resp_speed=this.elParallax[ media + 'parallax_speed' ]||'';
if('inherit'==resp_direction){
resp_direction=all_direction;
resp_speed=all_speed;
}
if(this.defaultDirections.indexOf(resp_direction) < 0){
resp_direction='no_parallax';
}
this.direction=resp_direction;
this.speed=''==resp_speed ? this.defaultSpeed:parseFloat(resp_speed) / 100.0;
},
_getTranslateObject: function(element){
var translate={
type:	'',
matrix: [],
x:		0,
y:		0,
z:		0
};
$.each(this.transformCSSProps, function(i, prop){
var found=element.css(prop);
if('string'!=typeof found||'none'==found){
return;
}
if(found.indexOf('matrix') >=0){
var matrixValues=found.match(/matrix.*\((.+)\)/)[1].split(', ');
if(found.indexOf('matrix3d') >=0){
translate.type='3d';
translate.matrix=matrixValues;
translate.x=matrixValues[12];
translate.y=matrixValues[13];
translate.z=matrixValues[14];
}else{
translate.type='2d';
translate.matrix=matrixValues;
translate.x=matrixValues[4];
translate.y=matrixValues[5];
}
return false;
}else{
translate.type='';
var matchX=found.match(/translateX\((-?\d+\.?\d*px)\)/);
if(matchX){
translate.x=parseInt(matchX[1], 10);
}
var matchY=found.match(/translateY\((-?\d+\.?\d*px)\)/);
if(matchY){
translate.y=parseInt(matchY[1], 10);
}}
});
return translate;
},
_getTranslateMatrix: function(translateObj, changes){
var matrix='';
$.each(changes, function(key, value){
translateObj[key]=value;
});
if(this.transform3d){
var matrix3d=this.matrix3dDef.slice(0);
switch(translateObj.type){
case '2d':
matrix3d[0]=translateObj.matrix[0];
matrix3d[1]=translateObj.matrix[1];
matrix3d[4]=translateObj.matrix[2];
matrix3d[5]=translateObj.matrix[3];
matrix3d[12]=translateObj.x;
matrix3d[13]=translateObj.y;
break;
case '3d':
matrix3d=translateObj.matrix.slice(0);
matrix3d[12]=translateObj.x;
matrix3d[13]=translateObj.y;
matrix3d[14]=translateObj.z;
break;
default:
matrix3d[12]=translateObj.x;
matrix3d[13]=translateObj.y;
break;
}
matrix='matrix3d(' + matrix3d.join(', ') + ')';
}
else if(this.transform){
var matrix2d=this.matrixDef.slice(0);
switch(translateObj.type){
case '2d':
matrix2d=translateObj.matrix.slice(0);
matrix2d[4]=translateObj.x;
matrix2d[5]=translateObj.y;
break;
case '3d':
matrix2d[0]=translateObj.matrix[0];
matrix2d[1]=translateObj.matrix[1];
matrix2d[2]=translateObj.matrix[4];
matrix2d[3]=translateObj.matrix[5];
matrix2d[4]=translateObj.x;
matrix2d[5]=translateObj.y;
break;
default:
matrix2d[4]=translateObj.x;
matrix2d[5]=translateObj.y;
break;
}
matrix='matrix(' + matrix2d.join(', ') + ')';
}
return matrix;
},
_fetchProperties: function(){
this._setParallaxProps();
this.el.css($.avia_utilities.supported.transition + 'transform', '');
this.winHeight=this.win.height();
this.winWidth=this.win.width();
if('background-image'==this.objectType){
this.elProperty.top=this.elBackgroundParent.offset().top;
this.elProperty.height=this.elBackgroundParent.outerHeight();
this.el.height(Math.ceil(( this.winHeight * Math.abs(this.speed) ) + this.elProperty.height) );
}else{
this.elProperty.top=this.elInner.offset().top;
this.elProperty.left=this.elInner.offset().left;
this.elProperty.height=this.elInner.outerHeight();
this.elProperty.width=this.elInner.outerWidth();
this.elProperty.bottom=this.elProperty.top + this.elProperty.height;
this.elProperty.right=this.elProperty.left + this.elProperty.width;
this.elProperty.distanceLeft=this.elProperty.right;
this.elProperty.distanceRight=this.winWidth - this.elProperty.left;
}
this.elProperty.translateObj=this._getTranslateObject(this.el);
this._parallaxScroll();
},
_onScroll: function(e){
var _self=this;
if(! _self.ticking){
_self.ticking=true;
window.requestAnimationFrame(_self._parallaxRequest.bind(_self) );
}},
_inViewport: function(elTop, elRight, elBottom, elLeft, winTop, winBottom, winLeft, winRight){
return !(elTop > winBottom + 10||elBottom < winTop - 10||elLeft > winRight + 10||elRight < winLeft - 10);
},
_parallaxRequest: function(e){
var _self=this;
setTimeout(_self._parallaxScroll.bind(_self), 0);
},
_parallaxScroll: function(e){
if(( 'no_parallax'==this.direction||''==this.direction)&&! this.isTransformed){
this.ticking=false;
return;
}
var winTop=this.win.scrollTop(),
winLeft=this.win.scrollLeft(),
winRight=winLeft + this.winWidth,
winBottom=winTop + this.winHeight,
scrollPos=0,
matrix='';
if('background-image'==this.objectType){
if(this.elProperty.top < winBottom&&winTop <=this.elProperty.top + this.elProperty.height){
scrollPos=Math.ceil(( winBottom - this.elProperty.top) * this.speed);
matrix=this._getTranslateMatrix(this.elProperty.translateObj, { y: scrollPos });
this.el.css($.avia_utilities.supported.transition + 'transform', matrix);
}
this.ticking=false;
return;
}
if(( 'no_parallax'==this.direction||''==this.direction) ){
matrix=this._getTranslateMatrix(this.elProperty.translateObj, { x: 0, y: 0  });
this.el.css($.avia_utilities.supported.transition + 'transform', matrix);
this.ticking=false;
this.isTransformed=false;
return;
}
var scroll_px_toTop=Math.ceil(this.elProperty.top - winTop),
scroll_px_el=Math.ceil(winBottom - this.elProperty.top),
scrolled_pc_toTop=0,
reduceDistanceX=0,
transform={ x: 0, y: 0 };
if(this.elProperty.top < this.winHeight){
reduceDistanceX=Math.ceil(this.winHeight - this.elProperty.top);
}
if(this.elProperty.top > winBottom){
scrolled_pc_toTop=0;
scroll_px_el=0;
}else{
scrolled_pc_toTop=1 -(scroll_px_toTop + reduceDistanceX) / this.winHeight;
}
switch(this.direction){
case 'bottom_top':
scrollPos=Math.ceil(( scroll_px_el - reduceDistanceX) * this.speed);
transform.y=-scrollPos;
matrix=this._getTranslateMatrix(this.elProperty.translateObj, { y: -scrollPos });
break;
case 'left_right':
scrollPos=Math.ceil(this.elProperty.distanceRight * scrolled_pc_toTop * this.speed);
transform.x=scrollPos;
matrix=this._getTranslateMatrix(this.elProperty.translateObj, { x: scrollPos });
break;
case 'right_left':
scrollPos=Math.ceil(this.elProperty.distanceLeft * scrolled_pc_toTop * this.speed);
transform.x=-scrollPos;
matrix=this._getTranslateMatrix(this.elProperty.translateObj, { x: -scrollPos });
break;
default:
break;
}
var elInViewport=this._inViewport(this.elProperty.top, this.elProperty.right, this.elProperty.bottom, this.elProperty.left, winTop, winBottom, winLeft, winRight),
transformedInViewport=this._inViewport(this.elProperty.top + transform.y, this.elProperty.right + transform.x, this.elProperty.bottom + transform.y, this.elProperty.left + transform.x, winTop, winBottom, winLeft, winRight);
if(elInViewport||transformedInViewport){
this.el.css($.avia_utilities.supported.transition + 'transform', matrix);
}
this.ticking=false;
this.isTransformed=true;
}};
$.fn.avia_parallax=function(options){
return this.each(function(){
var obj=$(this);
var self=obj.data('aviaParallax');
if(! self){
self=obj.data('aviaParallax', new AviaObjectParallaxElement(options, this) );
}});
};})(jQuery);