//////////////////////////////////////////////////////////////
/////////////////// 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 = {
			"teamRs"			: "getTeamRs",
			"teamSt"			: "getTeamSt",
			"teamWc"			: "getTeamWc",
			"teamDl"			: "getTeamDl",
			"wins"				: "getWins",
			"losses"			: "getLosses",
			"percentage"		: "getPercentage",
			"gamesback"			: "getGamesBack",
			"elim"				: "getElim",
			"last10"			: "getLast10",
			"streak"			: "getStreak",
			"lastGame"			: "getLastGame",
			"nextGame"			: "getNextGame"
		} 
}


/* 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
	else alert("No method associated with fieldId: " + fieldId + " in GridManager.fieldMethods");
}

/* 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;
}

/* show grid containing the passed items, within the passed index range */
GridManager.prototype.appendGridBody = 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.tbodyElem = document.createElement("tbody");
		var tr;
		var td;
		for (var i=startAt; i<(startAt+totalItemsToShow); i++){
			tr = document.createElement("tr");
			tr.style.verticalAlign = "top";
			tr.className = this.rowClass;
			for (var j=0; j<this.fields.length; j++){
				td = document.createElement("td");
				for (var k=0; k<this.fields[j].attributes.length; k++){  // for each attribute...
					eval("td." + this.fields[j].attributes[k].name + " = '" + this.fields[j].attributes[k].value + "'");
				}
				td.innerHTML = this.getFieldMethod(this.fields[j].name, this.dataRows[i]);   // since we may get HTML from data files				
				tr.appendChild(td);			
			}
			this.tbodyElem.appendChild(tr);
			this.updateRowClass();
		}
		this.container.appendChild(this.tbodyElem);
		if (this.paginate) {
			this.showPrevNextNav(startAt,i);
		}
	} else {
		this.appendError();
	}
}

/* turn on/off title rows according to game_status. if the PRE/POST array contains even 1 game 
in P/F mode, leave it on. otherwise, turn it off. */
GridManager.prototype.hideGameContent = function(){
	var nogames = 0;
	for (var i=0; i<this.dataRows.length; i++){
		if (this.dataRows[i] != "nogames") nogames++;
	}
	if (nogames > 0) 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;
	
	//alert(this.selfName + "\n totalitems: " + totalItemsToShow + "\n containerexists: " + this.containerExists() + "\n dataRows: " + this.dataRows.length);
	
	if (totalItemsToShow > 0 && this.containerExists()){

		var output = "<table width='100%' cellpadding='3' cellspacing='0' border='0' class='dataTable'>\n";
		output += "<thead>\n";
			output += "<tr class='dataTitleRow'>\n";
			for (var c=0; c<this.cols.length; c++){
				output += "<td ";
				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 + "</td>\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";
	
		if (this.hideGameContent()) this.container.innerHTML = ""; // IE. grrr.
		else this.container.innerHTML = output;
		
		if (this.paginate) {
			this.showPrevNextNav(startAt,i);
		} 
	} else {
		this.appendErrorAsHTML();  
	}
}


/* update main grid based on the prev/next-page request (handles pagination) */
GridManager.prototype.updateGrid = function(direction){
	this.removeGridBody();
	if (direction == "prev"){
		this.startEventsAt -= this.increment; 
		if (this.startEventsAt < 0) this.startEventsAt = 0;
	}
	else if (direction == "next"){
		this.startEventsAt += this.increment;
		if (this.startEventsAt > this.dataRows.length){ // there are more events in array
			this.startEventsAt -= this.increment;
		}
	}
	else return false;
	this.addGridAsHTML(this.startEventsAt,this.increment);
}


/* 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
}

/* show error (no data to display) */
GridManager.prototype.appendError = function(){
	var errorText = document.createTextNode("No multimedia events are available.");
	this.tbodyElem = document.createElement("tbody");	
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.appendChild(errorText);
	td.colSpan = (this.fields.length == 0)?"99":this.fields.length;
	td.className = "error";
	tr.appendChild(td);
	this.tbodyElem.appendChild(tr);
	this.container.appendChild(this.tbodyElem);	
	this.reset();
}

/* show error (no data to display) by updating the innerHTML of the container div */
GridManager.prototype.appendErrorAsHTML = function(){
	if (this.container == "" || typeof (this.container) == "undefined") {
		alert("Container for " + this.selfName + " doesn't exist.");
		return false;
	} 
	var errorTxt = "Data is currently unavailable.";
	var output = "<table width='100%' cellpadding='2' cellspacing='1' border='0' class='mediaGrid' id='mediaGrid'><tr><td colspan='99' class='error'>" + errorTxt + "</td></tr></table>";
	this.container.innerHTML = output;
	
	this.reset();
}

/* show loading message while data is requested and prepared for display */
GridManager.prototype.showLoading = function(){
	this.container.innerHTML = "<div class='gridMessage' style='border:1px solid #999; text-align:center; padding:10px'>Loading...</div>";
}

/* show passed message in grid */
GridManager.prototype.showMessage = function(message){
	this.container.innerHTML = "<div class='gridMessage'>" + message + "</div>";
}

GridManager.prototype.showLoadingAsHTML = function(){
	this.tbodyElem = document.createElement("tbody");	
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	td.appendChild(document.createTextNode("Loading..."));
	td.className = "loadingMsg";
	tr.appendChild(td);	
	this.tbodyElem.appendChild(tr);
	this.container.appendChild(this.tbodyElem);	
}




//////////////////////////////////////////////////////////////
////////////////// 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;
}