//////////////////////////////////////////////////////////////
/////////////////// GridManager Object ///////////////////////
//////////////////////////////////////////////////////////////

GridManager.prototype = new DataProcessor();  // extend DataProcessor object
GridManager.prototype.constructor = GridManager;

function GridManager(selfName, containerId){
	this.selfName = selfName;  // for references to self
	this.dataRows = new Array();

	this.container = document.getElementById(containerId);  // element that we're appending or innerHTML-ing the grid to
	this.tbodyElem;
		
	this.cols = new Array();	// array of GridFields
	this.fields = new Array();  // array of GridFields	
	this.rowClass = "trA";
		
	/* associate field identifiers with DataProccessor methods */
		this.fieldMethods = {
			"team"				: "getTeam",
			"wins"				: "getWins",
			"losses"			: "getLosses",
			"percentage"		: "getPercentage",
			"gamesBack"			: "getGamesBack"
		} 
}


/* add grid column */
GridManager.prototype.addCol = function(fieldName,attributes){
	var newField = new GridField();
	newField.name = fieldName;
	if (attributes){
		for (var i=0; i<attributes.length; i++){
			for (var key in attributes[i]){
				newField.addAttribute(key,attributes[i][key]);
			}
		}
	}
	this.cols.push(newField);
}

/* add grid data field */
GridManager.prototype.addField = function(fieldName,attributes){
	var newField = new GridField();
	newField.name = fieldName;
	if (attributes){
		for (var i=0; i<attributes.length; i++){
			for (var key in attributes[i]){
				newField.addAttribute(key,attributes[i][key]);
			}
		}
	}
	this.fields.push(newField);
}

/* get method mapped to passed field id */ 
GridManager.prototype.getFieldMethod = function(fieldId, dataRow){
	if (this.fieldMethods[fieldId]) return eval(this.selfName + "." + this.fieldMethods[fieldId] + "(dataRow)"); // GridManagerSt.home_full
}

/* toggle row class (used to displaying alternating row colors) */
GridManager.prototype.updateRowClass = function(){
	this.rowClass = (this.rowClass == "trA") ? "trB" : "trA";	
}

GridManager.prototype.containerExists = function(){
	if (this.container == null || typeof (this.container) == "undefined") {
		alert("Container for " + this.selfName + " doesn't exist.");
		return false;
	}
	return true;
}

GridManager.prototype.dataRowExists = function(){
	if (this.getFieldMethod(this.fields.name, this.dataRows) == "undefined") {
		alert("data doesn't exist");
		return false;
	}
	return true;
}


/* draw game table by writing innerHTML of container DIV.  this method prevents crashes in IE that
were cased by the DOM method of creating/populating the table used in method: appendGridBody */
GridManager.prototype.addGridAsHTML = function(startAt,totalItemsToShow){
	if (typeof(startAt) == "undefined" || startAt > this.dataRows.length) startAt = 0;
	if (typeof(totalItemsToShow) == "undefined") totalItemsToShow = this.dataRows.length;
	else if (startAt+totalItemsToShow > this.dataRows.length) totalItemsToShow = this.dataRows.length-startAt;
	
	if (totalItemsToShow > 0 && this.containerExists()){

		var output = "<table width='100%' cellpadding='0' cellspacing='0' border='0' class='dataTable'>\n";
		output += "<thead>\n";
			output += "<tr>\n";
			for (var c=0; c<this.cols.length; c++){
				output += "<th ";
				for (var a=0; a<this.cols[c].attributes.length; a++){  // for each attribute...
					output += this.cols[c].attributes[a].name + "='" + this.cols[c].attributes[a].value + "' ";
				}
				output += ">" + this.cols[c].name + "</th>\n";  
			}
			output += "</tr>\n"; 
		output += "</thead>\n";
		output += "<tbody>\n";
		
		for (var i=startAt; i<(startAt+totalItemsToShow); i++){
			if (this.dataRows[i] == "nogames") {
				// do nothing
			}
			else {
				output += "<tr class='" + this.rowClass + "' valign='top'>\n";
				for (var j=0; j<this.fields.length; j++){
					output += "<td "; 
					for (var a=0; a<this.fields[j].attributes.length; a++){  // for each attribute...
						output += this.fields[j].attributes[a].name + "='" + this.fields[j].attributes[a].value + "' ";
					}
					output +=	">" + this.getFieldMethod(this.fields[j].name, this.dataRows[i]) + "</td>\n";
	
				}
				output += "</tr>\n";
				this.updateRowClass();
			}					
		}
		 
		output += "</tbody>\n";
		output += "</table>\n";
	
		this.container.innerHTML = output;
	}
}

/* reset grid display properties */
GridManager.prototype.reset = function(){
	this.rowClass = "trA";		// used for alternating TR colors
//	this.cols = new Array(); // reset grid columns
//	this.fields = new Array();  // reset grid field identifiers
}

//////////////////////////////////////////////////////////////
////////////////// Supporting Sub-Objects ////////////////////
//////////////////////////////////////////////////////////////

function GridField(){
	this.name;
	this.attributes = new Array();  // array of GridFieldAttribute objects
}

GridField.prototype.addAttribute = function(name,value){
	var attribute = new GridFieldAttribute(name,value);
	this.attributes.push(attribute);
}

function GridFieldAttribute(name, value){
	this.name  = name;
	this.value = value;
}
