
var timer_on;
var tmx;
var clk=14;

function timedCount(){
var txt;
if(clk<11){
txt=clk;
}
	
		$("#timeclock").html(txt);
	clk=clk-1;
	if(clk<0){
		get_ans("NULL");
	}else{
		tmx=setTimeout('timedCount()',1000);
	}
}
function doTimer(){
clk=14;
	if(!timer_on){
		timer_on=1;
		timedCount();
		}

}
function stopCount(){
	clearTimeout(tmx);
	timer_on=0;
	
}

function scoreObject(){
var correct=0;
var incorrect=0;
var questNum=0;
var cat;
var ans;
var curr_round=0;
var catName=new Array();
var cats=new Array();
var used_cat=new Array();
	this.resetGame=function(){
	
		$("#score_mid").html("");	
	$("#score_left").html("");	
	$("#score_right").html("");
	$("#category").html("");	
	}
	this.getRound=function(){
		return curr_round;
	}
	this.setRound=function(){
		curr_round++;
	
	}
	this.showRound=function(){
		var currR=curr_round+1;
		var txt=currR;
			$("#score_mid").html(txt);	
	}
	this.showCategory=function(){
		var i;
		var currC;
		for(i=0;i<6;i++){
			if(cat==cats[i]){currC=catName[i];}
		}
		var txt="<p class=\"score_font\">CATEGORY: "+currC+"</p>";
			$("#category").html(txt);	
	}
	this.clearCatagory=function(){
		$("#category").html("");	
	
	}
	this.clearUsedCat=function(){
		used_cat.length=0;
	}
	this.setUsedCat=function(c_in){
		used_cat.push(c_in);
	}
	this.findCategory=function(search_cat){
		var catX=0;
		var cLength=used_cat.length;
		for(m=0;m<cLength;m++){
		if(used_cat[m]==search_cat){catX=1;}
		}
		return catX;		
	}
	
	this.clearCatName=function(){
		catName.length=0;
	}
	this.setCatName=function(cn){
		catName.push(cn);
	}
	this.getCatName=function(cnum){
		return catName[cnum];
	}
	
	this.clearCats=function(){
		cats.length=0;	
	}	
	
	this.getCategory=function(x){
		return cats[x];
	}
	this.setCategory=function(c_in){
		cats.push(c_in);
	}
	
	this.setCorrect=function(){
		correct++;
	}
	this.setIncorrect=function(){
		incorrect++;
	}
	this.showCorrect=function(){
		return correct;
	}
	this.showIncorrect=function(){
		return incorrect;
	}
	this.beginGame=function(){
		correct=0;
		incorrect=0;
		questNum=0;
		curr_round=0;
		questNum=0;
		used_cat.length=0;
		catName.length=0;
		cats.length=0;
	}
	this.resetQuest=function(){
		questNum=0;
	}
	this.getQuest=function(){
		return questNum;
	}
	this.setQuest=function(){
		questNum++;
	}
	this.updScore=function(){
	
		
		var txt=correct;	
		
		$("#score_left").html(txt);	
	
	
		
	}
	this.setCat=function(cat_in){
		cat=cat_in;
	}
	this.getCat=function(){
		return cat;
	}
	this.setAnswer=function(ans_in){
		ans=ans_in;
	}
	this.getAnswer=function(){
		return ans;
	}
	this.updQuest=function(){
		var qn=questNum+1;
	
			
		var txt=qn;
		
		$("#score_right").html(txt);
	}
}



function start_game(cat_in){
score.showRound();
score.updScore();
score.updQuest();
score.setCat(cat_in);
score.setUsedCat(cat_in);

get_question();

}

function get_ans(choice){
stopCount();
var txt;
var ans;
var q=score.getQuest();
var a=score.getAnswer();
var sc=0;

if(choice==a){
	score.setCorrect();
	sc=1;

}else{
	score.setIncorrect();
	
}
ans=data_obj.q[q].ans;
score.updScore();
ans=decodeURIComponent(ans);
$("#timeclock").html("");
if(choice=="NULL"){
	txt="<p class\"score_font\">Sorry, Times up, the Answer was "+ans+"</p>";
}else{
	if (sc==1){txt="<p class=\"score_font\">Correct, the answer is "+ans+"</p>";}else{txt="<p class=\"score_font\">Wrong, the answer is "+ans+"</p>";}
}
x=score.getQuest();
if(x==9){
	$("#category").html("");	
	score.setRound();
	if(score.getRound()==5){
	// end game
	txt="<p class=\"score_font\">Game Over</p><br/><p class=\"score_font\">Here are your totals:</p><br/>";
	txt=txt+"<p class=\"score_font\">Questions you got correct: "+score.showCorrect()+"</p><br/>";
	txt=txt+"<p class=\"score_font\">Questions answered incorrectly: "+score.showIncorrect()+"</p><br/>";
	txt=txt+"<p class=\"score_font\">Thanks For Playing!</p>";
	
	$("#game_text").html(txt);
	
	
	var t=setTimeout("new_game();",5000);
	
	
	}else{

		$("#game_text").html(txt);
		var t=setTimeout("next_round();",2000);

	
	}
}else{	
	score.updScore();
	var t=setTimeout("show_question()",2000);
		score.setQuest();
	$("#game_text").html(txt);	
		
}
}
function new_game(){
score.resetGame();
	$("#game_text").fadeOut(2000,function(){
		$("#ad_screen").animate({height:"435px"},5000,function(){
			
			
				$(".intro_text").show();
		
		});	
	       
	});	
}
function next_round(){
var r=score.getRound();
var q;
var catNo;
var rsp;
var i;
var z;
var txt;

score.resetQuest();
r++;
txt="<p>Round "+r+"</p>";
q=6-r;
if(r==1){
	txt=txt+"<p>You start with 5 categories, 10 Questions each</p><p>choose the first category below</p>";
	}else{
	
	txt=txt+"<p>You have "+q+" more";
		if(r==5){txt=txt+" category</p>";}else{txt=txt+" categories<p>";	}
}

// list categories
for (i=0;i<5;i++){
	catNo=score.getCategory(i);	
	
	z=score.findCategory(catNo);
	if(z!=1){
	txt=txt+"<p class=\"catclass\" onclick=\"start_game('"+catNo+"')\">"+score.getCatName(i)+"</p><br />";
	
	
	
	}
}
	
$("#game_text").html(txt);


}

function start_page(){
 var i;
$(".intro_text").hide();
score.beginGame();
	$("#game_text").html("");
$("#game_text").show();

$(document).ready(function(){
$.ajax({
			type:'POST',
			url:'lib/category.php',
		
			dataType:"JSON",
			success:function(result){	
			for(i=0;i<5;i++){
				score.setCategory(result.cat[i].no);
				score.setCatName(decodeURIComponent(result.cat[i].name));
			}		
	$("#ad_screen").animate({height:"0px"},4000,function(){
	next_round();	

});
					
			}})
})
}

function randOrd(){
return(Math.round(Math.random()))-0.5;
}

function get_question(){

var cat_in;
$(document).ready(function(){
cat_in=score.getCat();
$.ajax({
	
			type:'POST',
			url:'lib/short_game.php',
			data:({cat:cat_in}),
			dataType:"JSON",
			success:function(result){
			 data_obj=result;	
				
			 for (var i=0;i<10;i++){
				data_obj.q[i].oquest=decodeURIComponent(data_obj.q[i].oquest);
				data_obj.q[i].oquest2=decodeURIComponent(data_obj.q[i].oquest2);	
				data_obj.q[i].quest=decodeURIComponent(data_obj.q[i].quest);
				data_obj.q[i].pic=decodeURIComponent(data_obj.q[i].pic);
				data_obj.q[i].expl=decodeURIComponent(data_obj.q[i].expl);
				data_obj.q[i].ans=decodeURIComponent(data_obj.q[i].ans);
				data_obj.q[i].w0=decodeURIComponent(data_obj.q[i].w0);
				data_obj.q[i].w1=decodeURIComponent(data_obj.q[i].w1);
				data_obj.q[i].w2=decodeURIComponent(data_obj.q[i].w2);
				
			 }
			
			
			
			
			
			show_question();
		}})
})

}
function show_question(){

			
			var oqtext;
			var qtext;
			var oqtext2;
			var pic;
			var i;
			var ans=new Array();
			var txt="";
			var sw=score.getQuest();
			score.updQuest();
			
				txt="<p>";
			oqtext=data_obj.q[sw].oquest;
			qtext=data_obj.q[sw].quest;
			oqtext2=data_obj.q[sw].oquest2;
			
			pic=data_obj.q[sw].pic;
			ans[0]=data_obj.q[sw].ans;
			ans[1]=data_obj.q[sw].w0;
			ans[2]=data_obj.q[sw].w1;
			ans[3]=data_obj.q[sw].w2;
				
			if(oqtext !="NULL") txt=txt+oqtext;
			if(qtext !="NULL") txt=txt+" "+qtext;
			if(oqtext2 !="NULL") txt=txt+" "+oqtext2;		
			if(pic !="NULL") {
				pic="game_images/"+pic;
			txt=txt+"</p><img src='"+pic+"'><br /><br />";			
			}else{
				txt=txt+"</p>" ;
			}
			
			
			 ans.sort(randOrd);
			 for(i=0;i<4;i++){
			
				if(ans[i]==data_obj.q[sw].ans) score.setAnswer(i);
			 }			
			txt=txt+"<p class=\"ansclass\" id=\"ans0\" onclick=\"get_ans('"+0+"')\">"+ans[0]+"</p><br />";
			txt=txt+"<p class=\"ansclass\" id=\"ans1\" onclick=\"get_ans('"+1+"')\">"+ans[1]+"</p><br />";
			txt=txt+"<p class=\"ansclass\" id=\"ans2\" onclick=\"get_ans('"+2+"')\">"+ans[2]+"</p><br />";
			txt=txt+"<p class=\"ansclass\" id=\"ans3\" onclick=\"get_ans('"+3+"')\">"+ans[3]+"</p>";
			if(sw==0)score.showCategory();
			$("#game_text").html(txt);	
				doTimer();

}



