var pickem = new function() {
	this.ajax = new Ajax();
	this.selections = new Array();
	this.previousClassname;
	this.userscores = new Array();
	this.specialscores = new Array();
	this.num_per_page = 25;
	this.special = 0;
	this.callMade = new Array();
	this.boards = new Array();
	this.picks = new Array();
	
	this.getLeaderboard = function(cp_id) {
		var found = false;
		for(var i = 0; i < pickem.callMade.length; i++) {
			if(pickem.callMade[i] == cp_id) found = true;
		}
		if(!found) { 
			pickem.callMade.push(cp_id);
			pickem.ajax.setMimetype="text/xml";
			pickem.ajax.responseFormat="xml";
			pickem.ajax.doGet("/includes/pickem/src/index.php?xaction=get_leaderboard&cp_id="+cp_id, pickem.finishScores, "xml");
		} 
	};
	
	this.finishScores = function(ajax_xml) {
		var xmlDom=ajax_xml;
		var root=xmlDom.documentElement;
		
		var place = 0;
		var curr_right = -1;
		var mod = 1;
		
		var cp_id = root.getAttribute("cp_id");
		
		pickem.userscores[cp_id] = new Array();
		pickem.specialscores[cp_id] = new Array();
		
		for(var i = 0; i < root.childNodes.length; i++) {
			if(root.childNodes[i].nodeType == 1) {
				if(Math.ceil(root.childNodes[i].getAttribute("right")) < curr_right || curr_right == -1) {
					place+=mod;
					curr_right = Math.ceil(root.childNodes[i].getAttribute("right"));
					mod = 1; 
				} else mod++;
				
				var thisUserteam = {"team_id":root.childNodes[i].getAttribute("id"),
					 "place":place,
					 "name":root.childNodes[i].firstChild.nodeValue,
					 "user":root.childNodes[i].getAttribute("userid"),
					 "right":root.childNodes[i].getAttribute("right"),
					 "wrong":root.childNodes[i].getAttribute("wrong"),
					 "push":root.childNodes[i].getAttribute("push"),
					 "pct":root.childNodes[i].getAttribute("pct"),
					 "special":root.childNodes[i].getAttribute("special")
					};
			
				if(root.childNodes[i].getAttribute("special") == "1") {
					pickem.specialscores[cp_id].push(thisUserteam);
				}
				pickem.userscores[cp_id].push(thisUserteam);
			}
		}
		pickem.publishBoards();
	};

	this.registerBoard = function(cp_id, special) {
		pickem.boards.push([cp_id,special]);
	};
	
	this.publishBoards = function() {
		for(var i = 0; i < pickem.boards.length; i++) {
			pickem.leaderBoard(pickem.boards[i][0], 1, pickem.boards[i][1]); 
		}
	};

	this.leaderBoard = function(cp_id, page_num, special) {
		var teamArray = special ? pickem.specialscores[cp_id] : pickem.userscores[cp_id];
		
		if(teamArray.length > 0) {
			num_pages = Math.ceil(teamArray.length/pickem.num_per_page);
		} else {
			num_pages = 1;
		}
		
		if(page_num > num_pages) page_num = 1;
		
		var pagination = "";
		
		if(num_pages > 1) {
			var pagination = "<tr><td colspan=\"3\">Select Page: &nbsp; ";
			pagination += "<select onchange=\"pickem.leaderBoard("+cp_id+", this.value, pickem.special)\">";
			for(var i = 1; i <= num_pages; i++) {
				pagination += "<option value=\""+i+"\""+(i==page_num?" selected=\"selected\"":"")+">"+i+"</option>";
			}
			pagination += "</select></td></tr>";
		}
		
		
		var new_html = "<table cellspacing=\"1\" class=\"pickem_scores_table\"><tr>";
		new_html += "\n\t<th>#</th>";
		new_html += "\n\t<th>Team Name</th>";
		new_html += "\n\t<th>Record</th>";
		new_html += "\n</tr>";
		new_html += pagination;
		
		for(var i = (page_num-1)*pickem.num_per_page; i < (page_num)*pickem.num_per_page && i < teamArray.length; i++) {
			var cName = "regular";
			if(teamArray[i]["user"] == pickem.reg_user_id) 
				cName = "highlighted";
			else if(teamArray[i]["special"] == "1") 
				cName = "special";
				
			new_html += "<tr class=\""+cName+"\">";
			new_html += "<td class=\"place\">"+teamArray[i]["place"]+".</td>";
			new_html += "<td class=\"teamname\"><span>"+teamArray[i]["name"]+"</span></td>";
			new_html += "<td class=\"record\">"+(teamArray[i]["right"]+
					"-"+
					teamArray[i]["wrong"]+
					(teamArray[i]["push"]
						? "-"+teamArray[i]["push"]
						: "")
					+" ("+teamArray[i]["pct"]+")");
			new_html += "</tr>";
		}
		
		var c = document.getElementById("pickem_"+(special?"special_":"")+"scores_table");
		new_html += pagination + "</table>";
		c.innerHTML = new_html;
	};
	
	this.mOver = function(tr, status) {
		tr.className = status>0 ? "over" : "selector";
	};
	
	this.recordPick = function(tr, game_id, team_id, other_team) {
		pickem.picks.push(game_id+":"+team_id);
		 
	 // figure out if one is already selected
	 	document.getElementById("game_"+game_id+"_"+team_id+"_selection").className = "selected";
	 	document.getElementById("game_"+game_id+"_"+other_team+"_selection").className = "unselected";
		
	 // then make 
/*	save this until the end when they click save picks	
 * this.ajax.setMimetype="text/xml";
		this.ajax.responseFormat="xml";
		this.ajax.doGet("/includes/pickem/src/index.php?xaction=make_pick&game_id="+game_id+"&team_id="+team_id, this.finishPick, "xml");
	*/	
	};
	
	this.finishPick = function(ajax_xml) {
		var xmlDom=ajax_xml;
		var root=xmlDom.documentElement;
	};
	
	this.savePicks = function() {
		var q_string = "";
		for(i=0; i<pickem.picks.length; i++) {
			var info = pickem.picks[i].split(":");
			q_string += "&picks["+info[0]+"]="+info[1];
		}
		
		this.ajax.setMimetype="text/html";
		this.ajax.responseFormat="text";
		this.ajax.doGet("/includes/pickem/src/index.php?xaction=save+picks"+q_string, this.finishSave, "text");
	};
	
	this.finishSave = function(ajax_text) {
		if(ajax_text == "success") {
			var c = document.getElementById("pickem_save_button");
			c.disabled=true;
			alert("Picks Saved!");
			c.value='Picks Saved';
		} else {
			alert("There was an error, please try again");
		}
	};
}