/* krpanoJS 1.0.8.14 gyro plugin (build 2011-04-04) 
for devices with Gyro sensor (iPhone4 and iPad2 with iOS4.2+)
by Aldo Hoeben / fieldOfView.com
contributions by 
        Sjeiti / ronvalstar.nl
        Klaus / krpano.com
        
http://fieldofview.github.com/krpano_fovplugins/gyro/plugin.html
This software can be used free of charge and the source code is available under a Creative Commons Attribution license:
        http://creativecommons.org/licenses/by/3.0/     
*/
var krpanoplugin=function(){function v(){if(t===undefined)g=true;else if(t&&!g){window.addEventListener("deviceorientation",C,true);d.control.layer.addEventListener("touchstart",D,true);d.control.layer.addEventListener("touchend",w,true);d.control.layer.addEventListener("touchcancel",w,true);g=true;y=-window.orientation;o=k=h=0}else g=false}function x(){if(t&&g){window.removeEventListener("deviceorientation",C);d.control.layer.removeEventListener("touchstart",D);d.control.layer.removeEventListener("touchend",
w);d.control.layer.removeEventListener("touchcancel",w)}g=false}function F(){g?x():v()}function z(){t=true;window.removeEventListener("deviceorientation",z);if(g){g=false;v()}}function D(){A=true}function w(){A=false}function C(f){if(!A&&g){var u,e=Object({yaw:f.alpha*p,pitch:f.beta*p,roll:f.gamma*p}),a,c,b;a=Math.cos(e.yaw);c=Math.sin(e.yaw);b=Math.cos(e.pitch);var i=Math.sin(e.pitch),l=Math.cos(e.roll);e=Math.sin(e.roll);matrix=Array(c*e-a*i*l,-a*b,a*i*e+c*l,b*l,-i,-b*e,c*i*l+a*e,c*b,-c*i*e+a*l);
if(matrix[3]>0.9999){a=Math.atan2(matrix[2],matrix[8]);b=Math.PI/2;c=0}else if(matrix[3]<-0.9999){a=Math.atan2(matrix[2],matrix[8]);b=-Math.PI/2;c=0}else{a=Math.atan2(-matrix[6],matrix[0]);c=Math.atan2(-matrix[5],matrix[4]);b=Math.asin(matrix[3])}u=Object({yaw:a,pitch:b,roll:c});a=q(u.yaw/p);c=u.pitch/p;b=a;i=d.view.hlookat;l=d.view.vlookat;e=d.view.camroll;var G=i-k,E=l-o;if(B)n=q(180+Number(window.orientation)-u.roll/p);if(Math.abs(c)>70){b=f.alpha;switch(window.orientation){case 0:if(c>0)b+=180;
break;case 90:b+=90;break;case -90:b+=-90;break;case 180:if(c<0)b+=180;break}b=q(b);if(Math.abs(b-a)>180)b+=b<a?360:-360;f=Math.min(1,(Math.abs(c)-70)/10);a=a*(1-f)+b*f;n*=1-f}y+=G;h+=E;if(Math.abs(c+h)>90)h=c+h>0?90-c:-90-c;k=q(-a-180+y);o=Math.max(Math.min(c+h,90),-90);if(Math.abs(k-i)>180)i+=k>i?360:-360;k=(1-m)*k+m*i;o=(1-m)*o+m*l;if(Math.abs(n-e)>180)e+=n>e?360:-360;n=(1-m)*n+m*e;d.view.hlookat=q(k);d.view.vlookat=o;d.view.camroll=q(n);if(h!=0&&r>0)if(E==0)if(r==1)s=h=0;else{s=1-(1-s)*d.control.touchfriction;
h*=1-Math.pow(r,2)*s;if(Math.abs(h)<0.1)s=h=0}else s=0}}function q(f){f%=360;return f<=180?f:f-360}var d=null,j=null,t,g=false,r=0,B=false,m=0.5,A=false,y=0,h=0,k=0,o=0,n=0,s=0,p=Math.PI/180;this.registerplugin=function(f,u,e){d=f;j=e;if(d.version<"1.0.8.14"||d.build<"2011-03-30")d.trace(3,"gyro plugin - too old krpano version (min. 1.0.8.14)");else{window.DeviceOrientationEvent&&window.addEventListener("deviceorientation",z);j.registerattribute("available",false,function(){},function(){return t});
j.registerattribute("enabled",true,function(a){String("yesontrue1").indexOf(String(a).toLowerCase())>=0?v():x()},function(){return g});j.registerattribute("velastic",0,function(a){r=Math.max(Math.min(Number(a),1),0);d.trace(0,1-Math.pow(r,2))},function(){return r});j.registerattribute("camroll",false,function(a){B=String("yesontrue1").indexOf(String(a).toLowerCase())>=0},function(){return B});j.registerattribute("friction",0.5,function(a){m=Math.max(Math.min(Number(a),1),0)},function(){return m});
j.enable=v;j.disable=x;j.toggle=F}};this.unloadplugin=function(){window.removeEventListener("deviceorientation",z);x();d=j=null}};

