/* krpanoJS 1.0.8.15 gyro plugin (build 2012-10-05) 
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 x(){B=false;p=null;if(v===undefined)i=true;else if(v&&!i){window.addEventListener("deviceorientation",I,true);c.control.layer.addEventListener("touchstart",J,true);c.control.layer.addEventListener("touchend",y,true);c.control.layer.addEventListener("touchcancel",y,true);i=true;C=-(q?top.orientation:window.orientation);r=l=h=0;m=c.view.camroll}else i=false}function z(){if(v&&i){window.removeEventListener("deviceorientation",I,true);c.control.layer.removeEventListener("touchstart",
J);c.control.layer.removeEventListener("touchend",y);c.control.layer.removeEventListener("touchcancel",y)}A&&c.call("tween(view.camroll,0)");i=false}function L(){i?z():x()}function D(){window.removeEventListener("deviceorientation",D,false);if(!c.isdesktop){v=true;if(i){i=false;x()}g.onavailable!=null&&c.call(g.onavailable,g)}}function J(){E=true}function y(){E=false}function I(f){if(!E&&i){var F=q?top.orientation:window.orientation,k,e={yaw:f.alpha*s,pitch:f.beta*s,roll:f.gamma*s},b,d,a;b=Math.cos(e.yaw);
d=Math.sin(e.yaw);a=Math.cos(e.pitch);var j=Math.sin(e.pitch),n=Math.cos(e.roll);e=Math.sin(e.roll);a=[d*e-b*j*n,-b*a,b*j*e+d*n,a*n,-j,-a*e,d*j*n+b*e,d*a,-d*j*e+b*n];if(a[3]>0.9999){b=Math.atan2(a[2],a[8]);a=Math.PI/2;d=0}else if(a[3]<-0.9999){b=Math.atan2(a[2],a[8]);a=-Math.PI/2;d=0}else{b=Math.atan2(-a[6],a[0]);d=Math.atan2(-a[5],a[4]);a=Math.asin(a[3])}k={yaw:b,pitch:a,roll:d};b=t(k.yaw/s);d=k.pitch/s;a=b;j=c.view.hlookat;n=c.view.vlookat;e=c.view.camroll;var M=j-l,K=n-r;if(B){if(A)m=t(180+Number(F)-
k.roll/s);if(Math.abs(d)>70){a=f.alpha;switch(F){case 0:if(d>0)a+=180;break;case 90:a+=90;break;case -90:a+=-90;break;case 180:if(d<0)a+=180;break}a=t(a);if(Math.abs(a-b)>180)a+=a<b?360:-360;f=Math.min(1,(Math.abs(d)-70)/10);b=b*(1-f)+a*f;m*=1-f}C+=M;h+=K;if(Math.abs(d+h)>90)h=d+h>0?90-d:-90-d;l=t(-b-180+C);r=Math.max(Math.min(d+h,90),-90);if(Math.abs(l-j)>180)j+=l>j?360:-360;l=(1-o)*l+o*j;r=(1-o)*r+o*n;if(Math.abs(m-e)>180)e+=m>e?360:-360;m=(1-o)*m+o*e;c.view.hlookat=t(l);c.view.vlookat=r;c.view.camroll=
t(m);if(h!=0&&w>0)if(K==0)if(w==1)u=h=0;else{u=1-(1-u)*c.control.touchfriction;h*=1-Math.pow(w,2)*u;if(Math.abs(h)<0.1)u=h=0}else u=0}else if(p==null)p=k;else if(k.yaw!=p.yaw||k.pitch!=p.pitch||k.roll!=p.roll){p=null;B=true;if(G)h=-d}}}function t(f){f%=360;return f<=180?f:f-360}function H(f){return String("yesontrue1").indexOf(String(f).toLowerCase())>=0}var c=null,g=null,q=false,v,i=false,w=0,G=false,A=false,o=0.5,E=false,B=false,p=null,C=0,h=0,l=0,r=0,m=0,u=0,s=Math.PI/180;this.registerplugin=function(f,
F,k){c=f;g=k;if(c.build.charAt(0)!="%"&&(c.version<"1.0.8.14"||c.build<"2011-03-30"))c.trace(3,"gyro plugin - too old krpano version (min. 1.0.8.14)");else{q=c._have_top_access;if(q===undefined){q=false;try{if(top&&top.document)q=true}catch(e){}}window.DeviceOrientationEvent&&window.addEventListener("deviceorientation",D,false);g.registerattribute("available",false,function(){},function(){return v});g.registerattribute("enabled",true,function(b){H(b)?x():z()},function(){return i});g.registerattribute("velastic",
0,function(b){w=Math.max(Math.min(Number(b),1),0)},function(){return w});g.registerattribute("vrelative",false,function(b){G=H(b)},function(){return G});g.registerattribute("camroll",false,function(b){A=H(b)},function(){return A});g.registerattribute("friction",0.5,function(b){o=Math.max(Math.min(Number(b),1),0)},function(){return o});g.registerattribute("onavailable",null);g.enable=x;g.disable=z;g.toggle=L}};this.unloadplugin=function(){window.removeEventListener("deviceorientation",D,false);z();
c=g=null}};
