var checkstatus_timer=null;
var running_timer=null;
var gameendmessage='本轮活动已经结束,你可以刷新页面来获取新一轮活动';
var customerevent='shake';//shake,click;
var shakeevent=new Shake({
	threshold: 15, // optional shake strength threshold
    timeout: 300 // 
});
var tips_len=THEME_INFO.themedata.tips.length;
                    
isbind=false;
$(function(){
    if(SHAKE_CONFIG.status!=3){
        if(joingame.code<0){
            _loading_toast._longshow(joingame.msg);
            return;
        }
    }
	if(SHAKE_CONFIG.status==1){
		checkstatus();
	}
	if(SHAKE_CONFIG.status==3){
		_loading_toast._longshow(gameendmessage);
	}
	if(SHAKE_CONFIG.status==2){
		startlisten();
	}
});

function checkstatus(){
	$.ajax({
		"url":"shake.do.php?action=getstatus",
		"type":"get",
		"dataType":"json",
		"success":function(json){
			if(json.code<0){
				_loading_toast._show(json.msg);
				return false;
			}
			if(json.code==1){
				SHAKE_CONFIG.status=json.status;
				if(json.status==1){
                    _loading_toast._show(json.msg);
                    checkstatus_timer=setTimeout(function(){checkstatus();},3000);
					return false;
				}else{
                    clearTimeout(checkstatus_timer);
                }
				if(json.status==2){
					startlisten();
				}
				if(json.status==3){
					endlisten();
					_loading_toast._longshow(json.msg);
					return false;
				}
			}
			
		}
	})
}
function playshakesound(){
	var shakeaudio = $('#ShakeAudio')[0];
	if(shakeaudio.paused || shakeaudio.ended){
		shakeaudio.play();
	}
}
//开始计数
var startrunning=false;
//上一次摇晃时间
var lasttime=new Date();
var currenttime=new Date();
var running=function(){
	playshakesound();
	lasttime=currenttime;
	if(startrunning==false){
		startrunning=true;
		uploaddata();
		
	}
	
}
//一个请求没有结束不进行第2个请求
var isrunning=false;
function uploaddata(){
	if(isrunning==true){
		return;
	}
	isrunning=true;
	currenttime=new Date();
	var timespan=currenttime-lasttime;
	console.log(timespan);
	if(timespan>3000){
		clearTimeout(running_timer);
		startrunning=false;
		isrunning=false;
		return;
	}
	$.ajax({
		"url":"shake.do.php?action=run",
		"data":{'userid':USERINFO.id},
		"type":"post",
		"dataType":"json",
		"success":function(json){
			if(json.code<=0){
				_loading_toast._show(json.msg);
				return false;
			}else{
				SHAKE_CONFIG.status=json.status;
				if(json.status==1){
					_loading_toast._show(json.msg);
					return false;
				}
				if(json.status==2){
					_loading_toast._show(THEME_INFO.themedata.tips[Math.floor(Math.random()*tips_len)]);
					running_timer=setTimeout(function(){
						uploaddata();
					},3000);
					return false;
				}
				if(json.status==3){
					_loading_toast._longshow(json.msg);
                    endlisten();
					return false;
				}
			}
		},
		"complete":function(){
			isrunning=false;
		}
	});
}

function startlisten(){
    if(isbind==false){
        shakeevent.start();	
        $(window).on(customerevent,running);
        isbind=true;
    }
	
};

function endlisten(){
	if(running_timer!=null){
		clearTimeout(running_timer);
	}
	startrunning=false;
    if(isbind==true){
        $(window).off(customerevent, running);
        shakeevent.stop();
        isbind=false;
    }
}