(function ($) {
	var innov8scores = {
		numScoresToShow: 100
		// Loads data and runs callback
		, getData: function (o) {
			$.getScript(o.url);
		}
		// Sorts object array based on the score property, descending order.
		, sortObjDescScore: function (sortMe) {
			for (i = 0; i < sortMe.length; i++) {
				tmp = i;
				for (j = i+1; j < sortMe.length; j++) {
					if (sortMe[j].score > sortMe[tmp].score) {
						tmp = j;
					}
				}
				tmp2 = sortMe[tmp];
				sortMe[tmp] = sortMe[i];
				sortMe[i] = tmp2;
			}
		}	
		// HTML output for each row
		, createScoreRow: function (i, thisObj) {
			//var itemFlag = '<p style="padding-top: 0px; padding-bottom: 0px;"><img src="flags/' + thisObj.flag.toLowerCase() + '" width="20" height="13" alt="" /></p>';
			// Show flag or not, based on prop we set on object declaration.
			//var flagHtml = innov8scores.showFlags == 1 ? itemFlag : "";
			var scorePos = Number(i)+1;
			var rowHtml = '<div style="clear: both; padding-top: 3px;">'
					+ '<div style="float: left;width: 28px; text-align: right;"><p style="padding-top: 0px; padding-bottom: 0px;">' + scorePos + '.</p></div>'
					//+ '<div style="float: left;">' + flagHtml + '</div>'
					+ '<div style="float: left;"><p style="padding-top: 0px; padding-bottom: 0px;">(' + thisObj.flag.substring(0,2) + ')&nbsp; ' + thisObj.initials + '</p></div>'
					+ '<div style="text-align: right;"><p style="padding-top: 0px; padding-bottom: 0px;">' + thisObj.score + '</p></div>'
				+ '</div>';
			return rowHtml;
		}
		// The JSON callback function - builds full score array for each game, and builds top 100
		, createScores: function (data) {
			// Set paths to each game's scores array
			for (i in data) {
				if (data[i].game == "supply chain") {
					scArr = data[i].scores;
				}
				else if (data[i].game == "traffic") {
					trArr = data[i].scores;
				}
				else if (data[i].game == "customer service") {
					csArr = data[i].scores;
				}
			}
			
			// Sort each array in descending order
			innov8scores.sortObjDescScore(scArr);
			innov8scores.sortObjDescScore(trArr);
			innov8scores.sortObjDescScore(csArr);
			
			// Create each game's object: where to output and the sorted array of scores to use
			innov8scores.scObj = {
				"conId": "scBody"
				, "scores" : scArr
			}
			innov8scores.trObj = {
				"conId": "trBody"
				, "scores" : trArr
			}
			innov8scores.csObj = {
				"conId": "csBody"
				, "scores" : csArr
			}
			
			// Build the initial set of scores: top 100. Wait till DOM's ready b/c this is onload
			var min = 0, max = innov8scores.numScoresToShow;
			$(function(){
				innov8scores.generateScoreRows(innov8scores.scObj, min, max);
				innov8scores.generateScoreRows(innov8scores.trObj, min, max);
				innov8scores.generateScoreRows(innov8scores.csObj, min, max);
			});
		}
		, generateScoreRows: function (gameScoresObj, min, max) {
			var thisHtml = ""
			, prevButton = '<div style="float: left;"><p>[ <a class="' + gameScoresObj.conId + 'PrevLink" href="#">Previous</a> ]</p></div>'
			, nextButton = '<div style="float: right;"><p>[ <a class="' + gameScoresObj.conId + 'NextLink" href="#">Next</a> ]</p></div>';
			
			// Trim to the specified min-max # scores.
			var slicedArr = gameScoresObj.scores.slice(min,max);
			
			// Build a row for each score in the sliced array
			for (i in slicedArr) thisHtml += innov8scores.createScoreRow(Number(i)+min, slicedArr[i]);
			
			// If there's more scores than the max that we're showing, generate a 'next' link
			var nextMin = min+innov8scores.numScoresToShow;
			var nextMax = max+innov8scores.numScoresToShow;
			var prevMin = min-innov8scores.numScoresToShow;
			var prevMax = max-innov8scores.numScoresToShow;
			
			// If we're not showing the first set of scoers, generate a 'prev' link
			var showPrev = min != 0 ? prevButton : "";
			var showNext = gameScoresObj.scores.length > max ? nextButton : "";
			
			prevNextButtons = '<div>' + showPrev + showNext + '</div>';
			prevNextButtons = showPrev != "" || showNext != "" ? prevNextButtons : "" ;
			
			// Output HTML score rows for this game
			$("#" + gameScoresObj.conId).html(prevNextButtons + thisHtml + prevNextButtons);
			
			// Bind the prev/next buttons for this column
			$("." + gameScoresObj.conId + "PrevLink").click(function(){
				innov8scores.generateScoreRows(gameScoresObj, prevMin, prevMax);
				return false;
			});
			$("." + gameScoresObj.conId + "NextLink").click(function(){
				innov8scores.generateScoreRows(gameScoresObj, nextMin, nextMax);
				return false;
			});
			
		}
	}
	// END namespace
	
	// Bind namespace
	window.innov8scores = innov8scores;
	// Set flags on/off then get the data and JSON callback generates the listing.
	innov8scores.showFlags = 0;
	innov8scores.getData({
		"url" : "http://www.centerline.net/INNOV8/rss/Gateway/GW_Scores.php?action=getjson"
	});
	
})(jQuery);

	