/**
 * Created by huanggang on 16/9/9.
 */
var shake=3500,
    last_update=0,
    count=0,
    x=y=z=last_x=last_y=last_z=0;


function init(){
    if(window.DeviceMotionEvent){
        window.addEventListener("devicemotion",deviceMotionHandler,false);
    }else{
        alert("本设备不支持摇一摇功能");
    }
    function deviceMotionHandler(eventData){
        var acceleration = eventData.accelerationIncludingGravity,
            currTime=new Date().valueOf(),
            diffTime=currTime-last_update;

        if(diffTime>100){
            last_update=currTime;
            x=acceleration.x;
            y=acceleration.y;
            z=acceleration.z;
            var speed=Math.abs(x+y+z-last_x-last_y-last_z)/diffTime*10000;
            if(speed>shake){
                count++;
                shake_app();
            }
            last_x = x;
            last_y = y;
            last_z = z;
        }
    }

}




function shake_app(){
    var shake_over=false;
    if(shake_over==false) {
        $('.results_box').removeClass('fadeIn');
    }
    function shake(){
        var Dmp3=document.getElementById('shake_sound');
        Dmp3.play();
        $('.shake_top_box').addClass('slider-top');
        $('.shake_bottom_box').addClass('slider-bottom');
        $('.top_border').addClass('border_opacity');
        $('.bottom_border').addClass('border_opacity');
        shake_over=true;
        setTimeout(function(){
            $('.shake_top_box').removeClass('slider-top');
            $('.shake_bottom_box').removeClass('slider-bottom');
            $('.top_border').removeClass('border_opacity');
            $('.bottom_border').removeClass('border_opacity');
        },1200);
        loading();
    }
   function loading(){
       if(shake_over==true){
           $('.loading_icon').css('display','block');
           setTimeout(function(){
               $('.loading_icon').css('display','none');
               var Smp3=document.getElementById('shake_success');
               Smp3.play();
               $('.results_box').addClass('fadeIn');
           },2000);
       }
       else {
           return false;
       }
   }
   shake();
}




init();