/**
 * 问题页面的下一步
 */
$(document).ready(function(){
	$('#Reset').click(function(){
		var url = 'http://gongyi.hudong.com/tree/';
		var hdforestplanobj = getCookie('Hdforestplan');
		var questionidarray = hdforestplanobj.split(',');
		var hdforestplanCurPosobj = getCookie('HdforestplanCurPos');
		var hdforestplanCurPosNum = parseInt(hdforestplanCurPosobj);
		var hdforestplananswer='Hdforestplananswer'+(parseInt(questionidarray[hdforestplanCurPosNum])+1);
		var hdforestplananswerobj = SetCookie(hdforestplananswer,$("input[name=result][checked]").val());
		hdforestplanCurPosNum = hdforestplanCurPosNum + 1;
		SetCookie('HdforestplanCurPos',hdforestplanCurPosNum);
		if(hdforestplanCurPosNum!=10){
			if((parseInt(questionidarray[hdforestplanCurPosNum])+1)<10){
				window.location = url+'0'+(parseInt(questionidarray[hdforestplanCurPosNum])+1)+'.html';
			}else{
				window.location = url+(parseInt(questionidarray[hdforestplanCurPosNum])+1)+'.html';
			}
		}else{
			window.location = 'http://gongyi.hudong.com/tree/result.html';
		}
		return false;
	});
});

/**
 * 用于开始页面
 */
$(document).ready(function(){
	$('#startquestion').click(function(){
		initcookies(false,null);
		var hdforestplanobj = getCookie('Hdforestplan');
		var questionidarray = hdforestplanobj.split(',');
		var url = 'http://gongyi.hudong.com/tree/';
		if((parseInt(questionidarray[0])+1)<10){
			window.location = url+'0'+(parseInt(questionidarray[0])+1)+'.html';
		}else{
			window.location = url+(parseInt(questionidarray[0])+1)+'.html';
		}
		return false;
	});
});

/**
 * 进入页面的加载
 */
$(document).ready(function(){
	var url = 'http://tree.hudong.com/';
	var urlbackup = 'http://gongyi.hudong.com/tree/index.html';
	var urlbackup1 = 'http://gongyi.hudong.com/tree/';
	var cururl = window.location.toString();
	if(cururl == url || cururl==urlbackup || cururl==urlbackup1){
		deleteForestPlanCookies();
	}else{
		if(getCookie('Hdforestplan') != null){
			var HdforestplanCurPosobj = getCookie('HdforestplanCurPos');
			if(HdforestplanCurPosobj != null){
				var HdforestplanCurPosNum = parseInt(HdforestplanCurPosobj);//当前位置
				//获取当前题号从cookies中
				var questionidarray = getCookie('Hdforestplan').split(',');
				if((parseInt(questionidarray[HdforestplanCurPosNum])+1) != getQuestionIDFromURL(cururl)){
					deleteForestPlanCookies();
					var questionID = getQuestionIDFromURL(cururl);
					initcookies(true,questionID);			
				}
			}else{
				deleteForestPlanCookies();
				var questionID = getQuestionIDFromURL(cururl);
				initcookies(true,questionID);
			}
		}else{
			deleteForestPlanCookies();
			var questionID = getQuestionIDFromURL(cururl);
			initcookies(true,questionID);
		}
		var leftnum=9-parseInt(getCookie('HdforestplanCurPos'));
		if(leftnum==0){
			$('#leftquestionnum').append('点击下一步查看您的减碳结果');
		}else{
			$('#leftquestionnum').append('还有'+leftnum+'步，请耐心参与');
		}
		$('#Reset').show();
	}
});

/**
 * 获得当前页面的题号（通过URL）
 */
function getQuestionIDFromURL(cururl){
	var startexp = new RegExp('tree/','g');
	var startpos = cururl.search(startexp)+5;
	var endexp = new RegExp(".html","g"); // 创建正则表达式对象。 
	var endpos = cururl.search(endexp); // 在字符串 s 中查找匹配。
	var questionIDStr=cururl.substring(startpos,endpos); //根据url判断现在的题号
	return Number(questionIDStr);
}

/**
 * 清空森林计划的cookies
 */
function deleteForestPlanCookies(){
	if(getCookie('Hdforestplan') != null){
		delCookie('Hdforestplan');
	}
	if(getCookie('HdforestplanCurPos') != null){
		delCookie('HdforestplanCurPos');
	}
	var Hdforestplananswer='';
	for(var i=1; i<11; i++)
	{
		Hdforestplananswer='Hdforestplananswer'+i;
		if(getCookie(Hdforestplananswer) != null){
			delCookie(Hdforestplananswer);
		}
	}
}

/**
 * 初始化cookies。
 */
function initcookies(flag,questionID){
	var questionidarray=rands(10,10);
	if(flag==true){
		//随机数组第一个
		var firstnum=questionidarray[0];
		for(var i=1;i<10;i++)
		{
			if(questionidarray[i]==questionID-1){
				questionidarray[0]=questionID-1;
				questionidarray[i]=firstnum;
				break;
			}
		}
	}else{
		//不需要判断该数组
	}
	SetCookie('Hdforestplan',questionidarray);
	SetCookie('HdforestplanCurPos','0');
	var Hdforestplananswer='';
	for(var i=1; i<11; i++)
	{
		Hdforestplananswer='Hdforestplananswer'+i;
		SetCookie(Hdforestplananswer,'0');
	}
}

/**
 * 写cookies函数
 * 两个参数，一个是cookie的名子，一个是值
 */
function SetCookie(name,value){
	var Days = 30; //此 cookie 将被保存 30 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString()+";path=/;domain=.hudong.com;";
}
/**
 * 读取cookies函数
 */
function getCookie(name){
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null) return unescape(arr[2]); return null;
}
/**
 * 删除cookie
 */
function delCookie(name){
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString()+";path=/;domain=.hudong.com;";
}

var rmArrayElement=function(i,arr){return arr.slice(0,i).concat(arr.slice(i+1));};//返回移除了第i个数组的新数组
var rand=function(n){return Math.floor(Math.random()*(parseInt(n) || 1));};//返回小于n的整数随机数
var rands=function(n,c){//返回小于n的c个不重复的整数随机数
	var i=0,
	_reArr=[],//结果
	c=(typeof(parseInt(c))=="number" && c>1 && c<=n)?c:1,//随机数个数修正
	_tempRand=null,//所抓取的随机序号
	_tempArr=[];//临时数组
	for(i=0;i<n;i++){_tempArr=_tempArr.concat([i]);}//生成临时数组，此处有待改善:P
	for(i=0;i<c;i++){//循环不大于c次
		_tempRand=rand(_tempArr.length);//得到随机序号
		_reArr=_reArr.concat([_tempArr[_tempRand]]);//从临时数组中抓1个随机数
		_tempArr=rmArrayElement(_tempRand,_tempArr);//在临时数组中删除已抓取的数组
	}
	return _reArr;//返回最终结果
};
