var CelebrityGame = function() {
	var conn;
	var xOffset=192;
	var yOffset=134;
	return {
		init : function(){
			Ext.get("prev-3").on("click",function(e){this.prevPage("3");},this);
			Ext.get("next-3").on("click",function(e){this.nextPage("3");},this);
			Ext.get("last-3").on("click",function(e){this.lastPage("3");},this);
			Ext.get("first-3").on("click",function(e){this.firstPage("3");},this);
			Ext.get("prev-4").on("click",function(e){this.prevPage("4");},this);
			Ext.get("next-4").on("click",function(e){this.nextPage("4");},this);
			Ext.get("last-4").on("click",function(e){this.lastPage("4");},this);
			Ext.get("first-4").on("click",function(e){this.firstPage("4");},this);
			Ext.get("prev-6").on("click",function(e){this.prevPage("6");},this);
			Ext.get("next-6").on("click",function(e){this.nextPage("6");},this);
			Ext.get("first-6").on("click",function(e){this.firstPage("6");},this);
			Ext.get("last-6").on("click",function(e){this.lastPage("6");},this);
			Ext.get("prev-7").on("click",function(e){this.prevPage("7");},this);
			Ext.get("next-7").on("click",function(e){this.nextPage("7");},this);
			Ext.get("last-7").on("click",function(e){this.lastPage("7");},this);
			Ext.get("first-7").on("click",function(e){this.firstPage("7");},this);
			conn=new Ext.data.Connection();
			
			var loading3=Ext.get("loading_3");
			loading3.setY(loading3.getY()+yOffset);
			var loading4=Ext.get("loading_4");
			loading4.setY(loading4.getY()+yOffset);
			var loading6=Ext.get("loading_6");
			loading6.setY(loading6.getY()+yOffset);
			var loading7=Ext.get("loading_7");
			loading7.setY(loading7.getY()+yOffset);
			
			document.getElementById("p_3").value=0;
			document.getElementById("p_4").value=0;
			document.getElementById("p_6").value=0;
			document.getElementById("p_7").value=0;
			
			if(document.getElementById("rc-3")&&document.getElementById("rc-3").value!="1")
				Ext.get("ch-3").show();
			if(document.getElementById("rc-4")&&document.getElementById("rc-4").value!="1")
				Ext.get("ch-4").show();
			if(document.getElementById("rc-6")&&document.getElementById("rc-6").value!="1")
				Ext.get("ch-6").show();
			if(document.getElementById("rc-7")&&document.getElementById("rc-7").value!="1")
				Ext.get("ch-7").show();
		},
		prevPage : function(id){
			var p=document.getElementById("p_"+id);
			var curr=parseInt(p.value);
			if(curr>0){
				--curr;
				p.value=curr;
				this.updateScores(curr,id,"t");
			}
		},
		nextPage : function(id){
			var p=document.getElementById("p_"+id);
			var curr=parseInt(p.value);
			var maxPages=parseInt(document.getElementById("rc-"+id).value);
			if(curr<maxPages-1){
				++curr;
				p.value=curr;
				this.updateScores(curr,id,"b");
			}
		},
		firstPage : function(id){
			var p=document.getElementById("p_"+id);
			var curr=parseInt(p.value);
			if(curr>0){
				curr=0;
				p.value=curr;
				this.updateScores(curr,id,"t");
			}
		},
		lastPage : function(id){
			var p=document.getElementById("p_"+id);
			var curr=parseInt(p.value);
			var maxPages=parseInt(document.getElementById("rc-"+id).value);
			if(curr<maxPages-1){
				curr=maxPages-1;
				p.value=curr;
				this.updateScores(curr,id,"b");
			}
		},
		updateScores : function(pageNum,categoryId,dir){			
			var loading=Ext.get("loading_"+categoryId);
			var el=Ext.get("s_"+categoryId);
			el.update("");
			el.hide();
			loading.show();
			conn.request({
				url: "/ajax/celebrity-game/get_records.aspx?cid="+categoryId+"&p="+pageNum+"&rand="+Math.random(), 
				method: "get", scope: this,
				callback: function(options, bSuccess, response) {
					try {
						loading.hide();
						el.update(response.responseText);
						el.slideIn(dir,{duration:0.25,easing:"easeOut"});
					}
					catch(e) {
						if(confirm("Error loading scores. Try again?"))
							this.updateScores(pageNum,categoryId,dir);
					}
				}
			});
		}
};}();
Ext.onReady(CelebrityGame.init,CelebrityGame,true);