/**
 * Player profile scripts
 *
 * @Henry Postulart
 * @1.0
 */

var topReader = null;
var topHandler = null;
var readers = new Array();
var oH = new Array();
var lboard = new Array();
var tournFolder = "";
var useOldStatData = true;
var currentYear = new Date().getFullYear();
var getPrevYear = currentYear - 1;
var ret = true, chk = 0;
	
var adjStatYear = function (elem) {
	var section = elem.getElementsByTagName('h2')[0];
	var oldstr = section.firstChild.nodeValue;
	var sub = 'Season (' + getPrevYear + ')';
	var newstr = oldstr.replace(/Season/i, sub );
	section.innerHTML = newstr;
}

function chkCurrentStatsYear(PID) {
	var available = false;
	var src = "/golf/static/xml/leaderboard/" + currentYear + "/playerstats/leaderboard_" + PID + ".xml";
	var itemfound = new Ajax.Request(src, {
		onSuccess: function(response){
			return available = true;
		},
		asynchronous : false
	});
	ret = available;
	return available;
}


function populatePlayers() {

  topReader = new xmlReader(-1, "moreDetails", 4);
  topHandler = window.setInterval(function() { waitForXML(-1, processTopPlayers); }, 100);

}

function populateStats(PID) {
	var t = (PID != '') ? PID : false;
	if (t != "false") playerCode = t.toLowerCase();

	useOldStatData = false;

	if (playerCode) {

		readers[0] = new xmlReader(playerCode, "details", 0);
		oH[0] = window.setInterval(function() { waitForXML(0, detailStats, playerCode,"profile-detail-box"); }, 100);

		readers[1] = new xmlReader(playerCode, "leader", 1);
		oH[1] = window.setInterval(function() { waitForXML(1, leaderStats, playerCode, "prboard-all-left"); }, 100);

		readers[2] = new xmlReader(playerCode, "season", 2);		
		oH[2] = window.setInterval(function() { waitForXML(2, seasonStats, playerCode,"prboard-all-right"); }, 100);

		/* this set of stats is combined from readers 1 & 2 */
		readers[3] = readers[2]
		oH[3] = window.setInterval(function() { waitForXML(3, summaryStats, playerCode, "prboard-all-right"); }, 100);
	}
}

function waitForXML(index, handler, PID, divID) {
	var r = null;
	var h = null;

	if (handler == summaryStats) {
		if (readers[1] && readers[1].xml && readers[2] && readers[2].xml) {
			window.clearInterval(oH[index]);
			oH[index] = new handler(readers, index, PID, divID);
			return;
		}

	} else if (handler == detailStats) {
		if (readers[0] && readers[0].xml && readers[1] && readers[1].xml) {
			window.clearInterval(oH[index]);
			oH[index] = new handler(readers, index, PID, divID);
			return;
		}
	} else  {
		if (index >= 0) r = readers[index];
		else r = topReader;
		if (r.xml) {
			if (index >= 0) {
				window.clearInterval(oH[index]);
				oH[index] = new handler(readers[index], index, PID, divID);
			} else {
				window.clearInterval(topHandler);
				topHandler = new handler(r);
			}
		}
	}
}

if (typeof Class != "undefined") {
	var xmlReader = Class.create();
	xmlReader.prototype = {

		xml         : null,
		text        : null,
		indexHandle : null,
		rootPath    : "/golf/static/xml/leaderboard/",
		src         : null,
		year		: null,
		type        : null,

		initialize : function(PID, sType, num) {
			if(chk<1){
				this.year = (!chkCurrentStatsYear(PID)) ? getPrevYear : currentYear;
				chk++
			}
			else {
				this.year = (!ret) ? getPrevYear : currentYear;
			}
			this.type = sType.toLowerCase();
			try {
				switch (this.type) {
					case "details":
						this.rootPath += "playerbios/";
						this.src = this.rootPath + PID + ".xml";
						break;
					case "moredetails":
						this.rootPath += this.year + "/";
						this.rootPath += "stats/";
						this.src = this.rootPath + "r186_top5.xml";
						break;
					case "leader":
						this.rootPath += this.year + "/";
						this.rootPath += "playerstats/";
						this.src = this.rootPath + "leaderboard_" + PID + ".xml";
						break;
					case "season":
						this.rootPath += this.year + "/";
				 		this.rootPath += "playerstats/";
						this.src = this.rootPath + "stats_" + PID + ".xml";
						break;
					case "ranking":
						this.rootPath += this.year + "/";
				 		this.rootPath += "stats/";
						this.src = this.rootPath + "r_186" + ".xml";
						break;
					default: break;
				}
				this.indexHandle = num;
				this.getData();
			} catch(e) {}

		},

		getData: function () {
			if (this.src) {
				this.xmlHTTP = new Ajax.Request(this.src, {
					onSuccess: (this.handlerFunc).bindAsEventListener(this), 
					onFailure: (this.errFunc).bindAsEventListener(this), 
					method: "get" 
				});
			}
		},

		handlerFunc: function(t) {
			this.xml  = t.responseXML;
			this.text = t.responseText;
		},

		errFunc: function(t) {
   			// this content is stored in global.js so it is globally accessible
			alert(global404ErrorMessage);
		}
	};

	var getRanking = Class.create();
	getRanking.prototype = {
		pid : null,
		rank : null,
		initialize : function(pid) {
			this.pid = pid;
			this.rank = new xmlReader(playerCode, "ranking", 0)
		}
	}

	var processTopPlayers = Class.create();
	processTopPlayers.prototype = {
		initialize : function(reader) {
			this.reader = reader;
			this.xml    = this.reader.xml;
			this.max    = 4;
			this.rank   = new Array();

			try {
				this.players = $A(this.xml.getElementsByTagName("plr"));
				this.players.each( function(p, i) {
					if (i==this.max) throw $break;
					else {
						var PID = p.getAttribute("plrNum");
						var rankingNode = p.getElementsByTagName("curRank")[0];
						var rankingTie = safeAttrValue(rankingNode, "tied", "");
						var currentRank = safeNodeValue(rankingNode, "N/A")

						this.rank[i] = rankingTie + currentRank;
						readers[i] = new xmlReader(PID, "season", i);
						oH[i] = window.setInterval(function() { waitForXML(i, detailStats, PID,  "profile-detail-" + i); }, 100);
					}
				}.bind(this));
			} catch(e) {
				/*	
				var s  = "Error Encountered \n\n";
					s += "e.number is: " + (e.number & 0xFFFF) + "\n";
					s += "e.description is: " + e.description + "\n";
					s += "e.name is: " + e.name + "\n";
					s += "e.message is: " + e.message;
				alert(s);
				*/
			}
		}
	};

	var detailStats = Class.create();
	detailStats.prototype = {

		initialize : function(reader, index, PID, divID) {
			this.bio = 0;
			this.leader = 1;
			this.readers = readers;
			this.xml    = [this.readers[this.bio].xml, this.readers[this.leader].xml];
			this.sources = [oH[this.bio], oH[this.leader]];
			this.index  = index;
			this.PID    = PID;
			this.temp   = null;
			currentRanking = null;

			try {
				if (topHandler && typeof topHandler != typeof undefined) this.currentRanking = topHandler.rank[this.index];
				this.div = $(divID);
				this.heading = this.div.getElementsByTagName("h2")[0];
				this.imgDIV = document.getElementsByClassName("profile_box_section_left", this.div)[0];
				this.playerIMG = this.imgDIV.getElementsByTagName("img")[0];
				this.list = this.div.getElementsByTagName("ul")[0];
				this.buildData();
		   		this.buildUI();
			} catch(e) {
			}
		},

		buildData : function() {

			this.pInfo = new Object();
			this.data = new Object();

			this.data.img = new Object();
			this.data.img.src = "http://img.timeinc.net/golf/static/img/stats/players/" + this.PID + "_126x201.jpg";
			this.data.img.w   = "126";
			this.data.img.h   = "201";

			if (this.currentRanking) {		// Player Profiles Page

				this.pInfo.xml = readers[this.index].xml.getElementsByTagName("plr")[0];
				this.data.name = safeAttrValue(this.pInfo.xml, "plrName")
			
				this.statInfo = new Object();
				this.statInfo.standard = getNodeWithAttr(this.pInfo.xml, "statCat", "catName", "STANDARD STATS")
				this.statInfo.other = getNodeWithAttr(this.pInfo.xml, "statCat", "catName", "OTHER STATS")

				this.data.worldRanking = new Object();
				this.data.worldRanking.label = "World Ranking";
				this.data.worldRanking.value = this.currentRanking;

				this.data.fedexCupPoints = new Object();
				this.data.fedexCupPoints.node = getNodeWithAttr(this.statInfo.standard, "stat", "statID", "02394");
				this.data.fedexCupPoints.label = "Fedex Cup Points";
				this.data.fedexCupPoints.value = safeNodeValue(this.data.fedexCupPoints.node.getElementsByTagName("value")[0]);

				this.data.topTenFinishes = new Object();
				this.data.topTenFinishes.node = getNodeWithAttr(this.statInfo.other, "stat", "statID", "138");
				this.data.topTenFinishes.label = "Top Ten Finishes";
				this.data.topTenFinishes.value = safeNodeValue(this.data.topTenFinishes.node.getElementsByTagName("value")[0]);

				this.data.money = new Object();
				this.data.money.node = getNodeWithAttr(this.statInfo.standard, "stat", "statID", "109");
				this.data.money.label = "Money";
				this.data.money.value = safeNodeValue(this.data.money.node.getElementsByTagName("value")[0]);

				this.stats = [
					this.data.worldRanking,
					this.data.fedexCupPoints,
					this.data.topTenFinishes,
					this.data.money
				];

			} else {						// Player Details  Page

				this.pInfo.xml = this.xml[this.bio].getElementsByTagName("personalInfo")[0];
				this.pInfo.nameNode = this.pInfo.xml.getElementsByTagName("name")[0];
				var fName = this.pInfo.nameNode.getElementsByTagName("first")[0].firstChild.nodeValue;
				var mName = this.pInfo.nameNode.getElementsByTagName("middle")[0].firstChild.nodeValue;
				var lName = this.pInfo.nameNode.getElementsByTagName("last")[0].firstChild.nodeValue;
				this.pInfo.name = (fName) ? fName : "";
				this.pInfo.name += (mName) ? " " + mName : "";
				this.pInfo.name += (lName) ? " " + lName : "";

				this.data.name = this.pInfo.name;

				this.data.height = new Object();
				this.data.height.tagName = "height";
				this.data.height.label = "Height";
				this.data.height.value = safeNodeValue(this.pInfo.xml.getElementsByTagName(this.data.height.tagName)[0]);

				this.data.weight = new Object();
				this.data.weight.tagName = "weight";
				this.data.weight.label = "Weight";
				this.data.weight.value = safeNodeValue(this.pInfo.xml.getElementsByTagName(this.data.weight.tagName)[0]);

				this.data.born = new Object();
				this.data.born.tagName = "birthDate";
				this.data.born.label = "Born";
				this.data.born.value = safeNodeValue(this.pInfo.xml.getElementsByTagName(this.data.born.tagName)[0]);

				this.data.birthplace = new Object();
				this.data.birthplace.tagName = "birthPlace";
				this.data.birthplace.label = "Birthplace";
				this.data.birthplace.value = safeNodeValue(this.pInfo.xml.getElementsByTagName(this.data.birthplace.tagName)[0]);

				this.data.residence = new Object();
				this.data.residence.tagName = "residence";
				this.data.residence.label = "Residence";
				this.data.residence.value = safeNodeValue(this.pInfo.xml.getElementsByTagName(this.data.residence.tagName)[0]);

				this.data.turnedPro = new Object();
				this.data.turnedPro.tagName = "trndProYear";
				this.data.turnedPro.label = "Turned Pro";
				this.data.turnedPro.value = safeNodeValue(this.pInfo.xml.getElementsByTagName(this.data.turnedPro.tagName)[0]);

				this.data.scoreCardFolder = new Object();

				this.data.tourWins = new Object();
				this.data.tourWins.tagName = "TourWins";
				this.data.tourWins.label = "Tour Wins";
				this.data.tourWins.value = "";

				this.stats = [
					this.data.height,
					this.data.weight,
					this.data.born,
					this.data.birthplace,
					this.data.turnedPro,
/*					this.data.tourWins,
*/					this.data.residence
				];
			}
		},

		buildUI : function() {

			if(this.heading) {
				if (this.currentRanking) {		// Player Details Page
					var link = buildElem("a");
					link.href = "/golf/tours_news/playerdetails/0,28352,"+ this.PID +",00.html";
					link.innerHTML = this.data.name;
					this.heading.appendChild(link);
				} else {
					this.heading.innerHTML = this.data.name;
				}
			}
			with (this.playerIMG) {
				src = this.data.img.src;
				width = this.data.img.w;
				height = this.data.img.h;
				setAttribute("alt", this.data.name);
			}

			for (var i = 0; i < this.stats.length; i++) {
				var stat = this.stats[i];
				var span = buildElem("span", "psbold");
				span.appendChild(document.createTextNode(stat.label + ":"))
				span.style.paddingRight = "0.5em";
				var li = buildElem("li");
				var val = document.createTextNode(stat.value);
				li.appendChild(span);
				li.appendChild(val);
				this.list.appendChild(li);
			}

			if (!this.currentRanking) {		// Player Details Page

				/* Add spacer node and links */
				var li = buildElem("li");
				li.innerHTML = "&nbsp;";
				this.list.appendChild(li);

				var li = buildElem("li", "profile-detail-small-font");
				var a = buildElem("a", "profile-detail-link");
				a.href = "#";

				Event.observe (a, "click", function(e) {
					this.temp = window.setInterval(function() { this.testLeaderRow(); }.bind(this), 100);
					Event.stop(e);
				}.bind(this) );
				a.innerHTML = "Latest scorecard ";
				li.appendChild(a);

				li.appendChild(document.createTextNode(" | "));
				var a = buildElem("a", "profile-detail-link");
				a.href = "#";

				Event.observe (a, "click", function(e) {
					var el = Event.findElement(e, "a");
                                        var lbFolder = lboard[lboard.length-1];
					el.href = "/golf/tours_news/leaderboard/0,28360," + lbFolder + ",00.html?folder=" + lbFolder + "&PID=" + this.PID;
				}.bind(this) );

				a.innerHTML = "Latest leaderboard";
				li.appendChild(a);
				this.list.appendChild(li);
			}
		},

		testLeaderRow : function() {
			try {
					if (this.xml[this.bio] && this.xml[this.leader]) {
						window.clearInterval(this.temp);
						var w = 678;
						var h = 450;
						var t = parseInt((screen.availHeight - h) / 2);
						var l = parseInt((screen.availWidth - w) / 2);
						var name = oH[this.leader].data[oH[this.leader].data.length-1].name;
						var q = '?TFolder=' + lboard[lboard.length-1] + '&TName=' + name + '&PID=' + this.PID
						var options = 'height='+h+',width='+w+',top='+t+',left='+l
						this.popup = window.open('/golf/tours_news/playerdetails/popup/0,28794,,00.html'+q, 'scorecard', options);
					}
					Event.stop(e);
			} catch(e) {}
		}
	};

	var leaderStats = Class.create();
	leaderStats.prototype = {

	  initialize : function(reader, index, PID, divID) {
			this.reader = reader;
			this.xml    = this.reader.xml;
			this.index  = index;
			this.PID    = PID;
			this.year   = (!ret) ? getPrevYear : currentYear;;
			this.temp   = null;

			try {
				this.div = $(divID);
				this.table = this.div.getElementsByTagName("table")[0];
				this.thead = this.div.getElementsByTagName("thead")[0];
				this.tbody = this.table.getElementsByTagName("tbody")[0];
				this.buildData();
		   		this.buildUI();

			} catch(e) {
				/*
				var s  = "e is: " + e + "\n";
					s += "e.number is: " + (e.number & 0xFFFF) + "\n";
					s += "e.description is: " + e.description + "\n";
					s += "e.name is: " + e.name + "\n";
					s += "e.message is: " + e.message;

				alert(s);
				*/
			}
		},

		buildData : function() {
			if(!ret){adjStatYear(this.thead)}
			this.leaderboards = new Object();
			this.tournaments = $A(this.xml.getElementsByTagName("Tournament"));
			this.data = new Array();
			this.tournaments.each( function(t, i) {
				var o = new Object();
				o.name = safeAttrValue(t, "Name", "");
				o.position = safeAttrValue(t, "Finish", "");
				o.score = safeAttrValue(t, "Score", "");
				o.money = safeAttrValue(t, "Money", "");
				o.format = safeAttrValue(t, "Match", "");
				o.tourCode = t.getAttribute("TourCode");
				o.TID = t.getAttribute("TournId");
				o.lboard = o.tourCode.toLowerCase() + this.year + o.TID;
				if (o.name != "" && o.position != "" && o.score != "") {
					this.data.push(o);
					lboard.push(o.lboard);
				}
			}.bind(this) );

			this.tourWins = this.xml.getElementsByTagName("TourWins")[0];
			this.victories = parseInt(0 + this.tourWins.getAttribute("Champions") +
			                 this.tourWins.getAttribute("Nationwide") +
			                 this.tourWins.getAttribute("PGA"));
		},

		buildUI : function() {
			this.data.each ( function (t, i) {
				var odd = (i % 2 == 0) ? "even" : "odd";
				var tr = buildElem("tr", odd);
				tr.setAttribute("lboard", lboard[i])
				var td = buildElem("td", "psbold");
				td.innerHTML = t.name;
				tr.appendChild(td);
				var td = buildElem("td");
				td.innerHTML = t.position;
				tr.appendChild(td);
				var td = buildElem("td");
				td.innerHTML = t.score;
				tr.appendChild(td);
				var td = buildElem("td");
				td.innerHTML = t.money;
				tr.appendChild(td);
				var td = buildElem("td", "player");
				var a = buildElem("a");
				a.href="#";
				a.innerHTML = "Scorecard";
				Event.observe (a, "click", function(e) {
					var w = 678;
					var h = 450;
					var t = parseInt((screen.availHeight - h) / 2,10);
					var l = parseInt((screen.availWidth - w) / 2, 10);
					var q = '?TFolder=' + this.data[i].lboard + '&TName=' + this.data[i].name + '&PID=' + playerCode
					var options = 'height='+h+',width='+w+',top='+t+',left='+l
					this.popup = window.open('/golf/tours_news/playerdetails/popup/0,28794,,00.html'+q, 'scorecard', options);
					Event.stop(e);
				}.bind(this) );
				td.appendChild(a);
				tr.appendChild(td);
				this.tbody.appendChild(tr);
			}.bind(this) );
		}
	};

	var seasonStats = Class.create();
	seasonStats.prototype = {

	  initialize : function(reader, index, PID, divID) {
			this.reader = reader;
			this.xml    = this.reader.xml;
			this.text    = this.reader.text;
			this.index  = index;
			this.PID    = PID;

			try {
				this.div = $(divID);
				this.table = this.div.getElementsByTagName("table")[1];
				this.thead = this.div.getElementsByTagName("thead")[0];
				this.tbody = this.table.getElementsByTagName("tbody")[0];
				if (this.buildData()) this.buildUI();
			} catch(e) {
				/*
				var s  = "e is: " + e + "\n";
					s += "e.number is: " + (e.number & 0xFFFF) + "\n";
					s += "e.description is: " + e.description + "\n";
					s += "e.name is: " + e.name + "\n";
					s += "e.message is: " + e.message;
				alert(s);
				*/
			}
		},

		buildData : function() {
			this.pInfo = new Object();

			try {
				if(useOldStatData)
				{
					this.pInfo.PGA = this.xml.getElementsByTagName("PGA")[0];
					this.pInfo.Nationwide = this.xml.getElementsByTagName("Nationwide")[0];
					this.pInfo.Champions = this.xml.getElementsByTagName("Champions")[0];
					this.pInfo.testStats  = [this.pInfo.PGA.getElementsByTagName("Stat"),
											 this.pInfo.Nationwide.getElementsByTagName("Stat"),
											 this.pInfo.Champions.getElementsByTagName("Stat")]

					  if (this.pInfo.testStats[0].length) this.tour = this.pInfo.PGA;
					  else if (this.pInfo.testStats[1].length) this.tour = this.pInfo.Nationwide;
					  else if (this.pInfo.testStats[2].length) this.tour = this.pInfo.Champions;
				}
			else
				{
				  // not sure what to do with PGA/Nationwide.Champions
					this.tour =  this.xml.getElementsByTagName("tour")[0];
					this.minRndsMet = this.tour.getAttribute("minRndsMet");

					var year = this.xml.getElementsByTagName("year")[0];
					if(year)
					{
						this.disclaimer = year.getAttribute("disclaimer");
					}
			}

			this.data = new Object();

			this.data.scoring = new Object();
			if (this.tour == this.pInfo.PGA)
			{
				this.buildStat(this.data.scoring, 120, "Scoring Average");
			}
			else
			{
				this.buildStat(this.data.scoring, 108, "Scoring Average");
			}

			this.data.GIR = new Object();
			this.buildStat(this.data.GIR, 103, "Greens In Regulation");

			this.data.drivingDistance = new Object();
			this.buildStat(this.data.drivingDistance, 101, "Driving Distance");

			this.data.scrambling = new Object();
			this.buildStat(this.data.scrambling, 130, "Scrambling");

			this.data.drivingAccuracy = new Object();
			this.buildStat(this.data.drivingAccuracy, 102, "Driving Accuracy");

			this.data.puttingAverage = new Object();
			this.buildStat(this.data.puttingAverage, 104, "Putting Average");

			this.data.totalDriving = new Object();
			this.buildStat(this.data.totalDriving, 129, "Total Driving");

			this.data.puttsPerRound = new Object();
			this.buildStat(this.data.puttsPerRound, 119, "Putts Per Round");

			this.data.ballStriking = new Object();
			this.buildStat(this.data.ballStriking, 158, "Ball Striking");

			this.data.bounceBack = new Object();
			this.buildStat(this.data.bounceBack, 160, "Bounce Back");

			this.data.sandSaves = new Object();
			this.buildStat(this.data.sandSaves, 111, "Sand Saves");

			this.data.consecutiveCuts = new Object();
			this.buildStat(this.data.consecutiveCuts, 122, "Consecutive Cuts");

			// These stats are used in Summary object only

			this.data.moneyListValue = new Object();
			this.buildStat(this.data.moneyListValue, 109, "Money List");

			this.data.topTenFinishes = new Object();
			this.buildStat(this.data.topTenFinishes, 138, "Top 10 Finishes");

			this.data.worldRanking = new Object();
			this.buildStat(this.data.worldRanking, 186, "World Ranking");

			// Money List Rank is different because it gets the value from the rank
			this.data.moneyListRank = new Object();
			this.data.moneyListRank.statID = 109;
			this.data.moneyListRank.label = "Money List Rank";
			this.data.moneyListRank.value = safeNodeValue(this.data.moneyListValue.node.getElementsByTagName("rank")[0]);
			this.data.moneyListRank.rank = this.data.moneyListRank.value;

			this.stats = [
				this.data.scoring,
				this.data.GIR,
				this.data.drivingDistance,
				this.data.scrambling,
				this.data.drivingAccuracy,
				this.data.puttingAverage,
				this.data.totalDriving,
				this.data.puttsPerRound,
				this.data.ballStriking,
				this.data.bounceBack,
				this.data.sandSaves,
				this.data.consecutiveCuts
			];
			return true;

		} catch(e) { return false; }

	},

	  buildStat : function(statObj, id, text) {

		    statObj.statID = id;

			if(useOldStatData)
			{
			    statObj.node = getNodeWithAttr(this.tour, "Stat", "statID", id);
				if (statObj.node) {
				    statObj.label = safeNodeValue(statObj.node.getElementsByTagName("statName")[0]);
					statObj.value = safeNodeValue(statObj.node.getElementsByTagName("StatValue")[0]);
					statObj.rank = safeNodeValue(statObj.node.getElementsByTagName("curRank")[0]);

			  	} else {
				    statObj.label = text;
					statObj.value = "N/A";
					statObj.rank = "N/A";
				}

			} else {

			    statObj.node = getNodeWithAttr(this.tour, "stat", "statID", id);
				if (statObj.node) {
  	          	    statObj.label = safeNodeValue(statObj.node.getElementsByTagName("name")[0]);
					statObj.value = safeNodeValue(statObj.node.getElementsByTagName("value")[0]);
					if(this.minRndsMet == "N")
					{
						statObj.rank = safeNodeValue(statObj.node.getElementsByTagName("projRank")[0]);
					} else {
					    statObj.rank = safeNodeValue(statObj.node.getElementsByTagName("rank")[0]);
					}


				} else {
				    statObj.label = text;
					statObj.value = "N/A";
					statObj.rank = "N/A";
				}
			}

	  },


		buildUI : function() {
			if (this.minRndsMet == "N") {
				var cell = $("disclaimer");
				cell.innerHTML = this.disclaimer;
			}
			this.stats.each ( function (stat, i) {
				var odd = (i % 2 == 0) ? "even" : "odd";
				var tr = buildElem("tr", odd);
				var td = buildElem("td", "psbold");
				td.innerHTML = stat.label;
				tr.appendChild(td);
				var td = buildElem("td");
				td.innerHTML = stat.value;
				tr.appendChild(td);
				var td = buildElem("td");
				td.innerHTML = stat.rank;
				tr.appendChild(td);
				this.tbody.appendChild(tr);
			}.bind(this) );
		}
	};
	
	var summaryStats = Class.create();
	summaryStats.prototype = {

	  initialize : function(readers, index, PID, divID) {
			this.readers = readers;
			this.leader = 1;
			this.statistics = 2;
			this.xml    = [this.readers[this.leader].xml, this.readers[this.statistics].xml];
			this.sources = [oH[this.leader], oH[this.statistics]];
			this.index  = index;
			this.PID    = PID;

			try {
				this.div = $(divID);
				this.table = this.div.getElementsByTagName("table")[0];
				this.thead = this.table.getElementsByTagName("thead")[0];
				this.tbody = this.table.getElementsByTagName("tbody")[0];
				this.buildData();
			   	this.buildUI();
			} catch(e) {
				/*		
				var s  = "e is: " + e + "\n";
					s += "e.number is: " + (e.number & 0xFFFF) + "\n";
					s += "e.description is: " + e.description + "\n";
					s += "e.name is: " + e.name + "\n";
					s += "e.message is: " + e.message;
				alert(s);
				*/
			}
		},

		buildData : function() {
			if(!ret){adjStatYear(this.thead)}
			this.data = new Object();
			this.data.victories = new Object()
			this.data.victories.label = "Victories";
			this.data.victories.value = oH[this.leader].victories;
			this.data.moneyListValue = oH[this.statistics].data.moneyListValue;
			this.data.moneyListRank = oH[this.statistics].data.moneyListRank;
			this.data.topTenFinishes = oH[this.statistics].data.topTenFinishes;
			this.data.worldRanking = oH[this.statistics].data.worldRanking;

			this.stats = [
				this.data.victories,
				this.data.moneyListValue,
				this.data.moneyListRank,
				this.data.topTenFinishes,
			//	this.data.worldRanking
			];
		},

		buildUI : function() {
			this.stats.each ( function (stat, i) {
				var odd = (i % 2 == 0) ? "even" : "odd";
				var tr = buildElem("tr", odd);
				var td = buildElem("td", "psbold");
				td.innerHTML = stat.label;
				tr.appendChild(td);
				var td = buildElem("td", "total");
				td.innerHTML = stat.value;
				tr.appendChild(td);
				this.tbody.appendChild(tr);
			}.bind(this) );
		}
	};
}
