(function($){var maxfps=25,delay=1/maxfps*1000,lastRender=new Date().getTime(),layers=[],docWidth=$(window).width(),docHeight=$(window).height(),motionEnabled=false,motionMax=1,motionAllowance=.05,movementCycles=0,motionData={"xArray":[0,0,0,0,0],"yArray":[0,0,0,0,0],"xMotion":0,"yMotion":0}
$(window).resize(function(){docWidth=$(window).width()
docHeight=$(window).height()})
$.fn.plaxify=function(params){return this.each(function(){var layerExistsAt=-1
var layer={"xRange":$(this).data('xrange')||0,"yRange":$(this).data('yrange')||0,"invert":$(this).data('invert')||false}
for(var i=0;i<layers.length;i++){if($(this).attr('id')==layers[i].obj.attr('id')){layerExistsAt=i}}
for(var param in params){if(layer[param]==0){layer[param]=params[param]}}
layer.obj=$(this)
layer.startX=this.offsetLeft
layer.startY=this.offsetTop
if(layer.invert==false){layer.startX-=Math.floor(layer.xRange/2)
layer.startY-=Math.floor(layer.yRange/2)}else{layer.startX+=Math.floor(layer.xRange/2)
layer.startY+=Math.floor(layer.yRange/2)}
if(layerExistsAt>=0){layers.splice(layerExistsAt,1,layer)}else{layers.push(layer)}})}
function getMin(arr){return Math.min.apply({},arr)}
function getMax(arr){return Math.max.apply({},arr)}
function moveable(){return window.DeviceMotionEvent!=undefined}
function detectMotion(e){if(new Date().getTime()<lastRender+ delay)return
if(moveable()){var accel=e.accelerationIncludingGravity,x=accel.x,y=accel.y
if(motionData.xArray.length>=5){motionData.xArray.shift()}
if(motionData.yArray.length>=5){motionData.yArray.shift()}
motionData.xArray.push(x)
motionData.yArray.push(y)
motionData.xMotion=Math.round((getMax(motionData.xArray)- getMin(motionData.xArray))*1000)/1000
motionData.yMotion=Math.round((getMax(motionData.yArray)- getMin(motionData.yArray))*1000)/1000
if((motionData.xMotion>1.5||motionData.yMotion>1.5)){if(motionMax!=10){motionMax=10}}
if(motionData.xMotion>motionAllowance||motionData.yMotion>motionAllowance){movementCycles++;}else{movementCycles=0;}
if(movementCycles>=5){motionEnabled=true
$(document).unbind('mousemove.plax')
$(window).bind('devicemotion',plaxifier(e))}else{motionEnabled=false
$(window).unbind('devicemotion')
$(document).bind('mousemove.plax',function(e){plaxifier(e)})}}}
function plaxifier(e){if(new Date().getTime()<lastRender+ delay)return
lastRender=new Date().getTime()
var x=e.pageX,y=e.pageY
if(motionEnabled==true){var i=window.orientation?(window.orientation+ 180)%360/90:2,accel=e.accelerationIncludingGravity,tmp_x=i%2==0?-accel.x:accel.y,tmp_y=i%2==0?accel.y:accel.x
x=i>=2?tmp_x:-tmp_x
y=i>=2?tmp_y:-tmp_y
x=(x+motionMax)/2
y=(y+motionMax)/2
if(x<0){x=0}else if(x>motionMax){x=motionMax}
if(y<0){y=0}else if(y>motionMax){y=motionMax}}
var hRatio=x/((motionEnabled==true)?motionMax:docWidth),vRatio=y/((motionEnabled==true)?motionMax:docHeight),layer,i
for(i=layers.length;i--;){layer=layers[i]
if(layer.invert!=true){layer.obj.css('left',layer.startX+(layer.xRange*hRatio)).css('top',layer.startY+(layer.yRange*vRatio))}else{layer.obj.css('left',layer.startX-(layer.xRange*hRatio)).css('top',layer.startY-(layer.yRange*vRatio))}}}
$.plax={enable:function(){$(document).bind('mousemove.plax',function(e){plaxifier(e)})
if(moveable()){window.ondevicemotion=function(e){detectMotion(e)}}},disable:function(){$(document).unbind('mousemove.plax')
window.ondevicemotion=undefined}}
if(typeof ender!=='undefined'){$.ender($.fn,true)}})(function(){return typeof jQuery!=='undefined'?jQuery:ender}())
