var DIVISION_IDX = 4;
var divisionsToRowsMap = new HashMap();

function populateDivisionsToRowsMap(data) {
    for (var i = 0; i<data.length; i++) {
        var row = data[i];
        var division = row[DIVISION_IDX];
        if (divisionsToRowsMap.get(division) == null) {
            divisionsToRowsMap.put(division, []);
        }
        var rows = divisionsToRowsMap.get(division);
        rows[rows.length] = row;
    }
}

// write the standings table in html for interim pages,
// and club/league home pages in a mini-format
//
// format is currently 'wide' or not.  wide format used on layout #3
function getStandingsRowsHTML(raw, format) {

    // special characters to flag playoff spots
    var FLAG_FIRSTWINNER   = "*";
    var FLAG_FIRSTWILDCARD = "$";
	var FLAG_CLINCH         = "@";
	var FLAG_DIVWINNER      = "#";
	var FLAG_WILDCARDWINNER = "&";

    // set colors for alternating rows. 
    // Should be abstracted to a css class so it can be themed per club/league
    var oddColorHex  = "DBE0DE";
    var evenColorHex = "FFFFFF";

    var headerStyle = "font-weight: bold;";

    var standingsRaw = copyArray(raw);
    var standingsDataExists = (standingsRaw!=null);

    var s = "";

    var tableWidth = 180;
    if (format == "wide") { tableWidth = 360; }
    if (format == "wide_multi") { tableWidth = 360; }
    s += "<table class=\"standingsModule\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"" + tableWidth + "\">";

    if (!standingsDataExists) {
        
        s += "<tr class=\"standingsHeader\">";
        s += "<td style=\"" + headerStyle + " \" colspan=\"7\"  ><b>&nbsp;&nbsp;&nbsp;No standings yet.</b></td>";
        s += "</tr>";

    } else {

        populateDivisionsToRowsMap(standingsRaw);
        var divisions = divisionsToRowsMap.keys();
        var showKey = { firstwinner: false, firstwildcard: false, clinch: false, divwinner: false, wildcardwinner: false};
        
        if (format == "wide") {
            s += "<tr><td colspan=\"7\" style=\"padding: 0px;\" ><table class=\"wideStandings\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 358px;\"><tr>";
        }

        for (var d = 0; d<divisions.length; d++) {
            var division = divisions[d];
            var isLastDivision = (d==divisions.length-1);
            
            if (format == "wide") {
                s += "<td style=\"vertical-align: top; width: 50%; padding: 0px;\">";
                s += "<table class=\"division\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 100%;"
                if (isLastDivision) {
                    s += " border-left: 1px solid black; ";
                } 
                s += "\">"
            }

            s += "<tr class=\"standingsHeader\">";
            s += "<td style=\"" + headerStyle + " \" ></td>";
            s += "<td style=\"" + headerStyle + " text-align: left;\">" + division + "</td>";
            s += "<td style=\"" + headerStyle + " \" >&nbsp;</td>";
            s += "<td style=\"" + headerStyle + " \" >W</td>";
            s += "<td style=\"" + headerStyle + " \" >L</td>";
            s += "<td style=\"" + headerStyle + " \" >PCT</td>";
            s += "<td style=\"" + headerStyle + " \" >GB</td>";
            s += "</tr>";

            var rows = divisionsToRowsMap.get(division);
            var isEven = true;

            for (var r = 0; r<rows.length; r++) {
                var cellColorHex = (isEven?evenColorHex:oddColorHex);
                var row = rows[r];

                s += "<tr>";

                // if the standings have markers for division
                // winner/wild card, add 'em or leave empty
                s += "<td style=\"background-color:#" + cellColorHex + "; \">";
/*
                if (row[2] == "*") { 
                    s += " title=\"1st half division winner\">";
                } else if (row[2] == "$") { 
                    s += " title=\"1st half wild card winner\">";
                } 
		         else if (row[2] == "@") { 
            		s += " title=\"clinched playoff spot\">";
         		}
	 	        else if (row[2] == "#") { 
	           		s += " title=\"division winner\">";
		        }
	 	        else if (row[2] == "&") { 
	           		s += " title=\"wild card winner\">";
		        }
*/
                s += row[2];
                s += "</td>";

                // if we see a flag marking a winner, then set a flag
                // to show the key at the bottom of the table.
                if(row[2]) {
                    if (row[2].indexOf(FLAG_FIRSTWINNER) > -1) { 
                        showKey.firstwinner = true;
                    } 
                    if (row[2].indexOf(FLAG_FIRSTWILDCARD) > -1) { 
                        showKey.firstwildcard = true;
                    } 
                    if (row[2].indexOf(FLAG_CLINCH) > -1) { 
                        showKey.clinch = true;
                    } 
		            if (row[2].indexOf(FLAG_DIVWINNER) > -1) { 
        		        showKey.divwinner = true;
		            }
        		    if (row[2].indexOf(FLAG_WILDCARDWINNER) > -1) { 
		                showKey.wildcardwinner = true;
        		    }	
                }

					s += "<td style=\"background-color:#" + cellColorHex + "; text-align: left;\">";

					var _club_name = row[3];
					_club_name= _club_name. replace("Winston-Salem", "Winston- Salem");
					if ("t" + row[1] == ((cid)?cid:sid)) _club_name = "<span class=\"highlightTeam\">" + _club_name + "</span>";

                // fornmat and draw the team name.  if the name is the
                // current team, then mark it for highlighting
					if (properties) {
						var clubid = "t" + row[1];
						if (properties.clubs[clubid] && properties.clubs[clubid].hidden == "0") { 
							 s += "<a href=\"/clubs/index.jsp?cid=t" + row[1] + "\">" + _club_name + "</a>";
						} else {
							 s += _club_name;
						}
					}

                s += "</td>";

                // draw W, L, PCT, and GB
                s += "<td style=\"background-color:#" + cellColorHex + ";\"></td>";  // unnecessary?
                s += "<td style=\"background-color:#" + cellColorHex + ";\">" + row[5] + "</td>";
                s += "<td style=\"background-color:#" + cellColorHex + ";\">" + row[6] + "</td>";
                s += "<td style=\"background-color:#" + cellColorHex + ";\">" + row[7] + "</td>";
                s += "<td style=\"background-color:#" + cellColorHex + ";\">" + row[8] + "</td>";
                s += "</tr>";
                isEven = (!isEven);
            }

            if (isLastDivision) {
                // add winner key to bottom of standings tables
                // NOTE: update to key off of first half / second half status
                s += "<tr><td colspan=\"7\" style=\"background-color:#" + (isEven?evenColorHex:oddColorHex) + "; \" >";

                // if we're showing the last division, add the key as necessary.
                // in wide format, its just added after the whole table.
                if ((showKey.firstwinner || showKey.firstwildcard || showKey.clinch || showKey.divwinner || showKey.wildcardwinners) && (format != "wide")) {
                    s += "<div class=\"winnerKey\" style=\"font-size: 9px; text-align: left;\">";
                    if (showKey.firstwinner) { 
                        s += "&nbsp;&nbsp;&nbsp;" + FLAG_FIRSTWINNER + " - 1st half division winner";
                    }
                    if (showKey.firstwildcard) { 
                        s += "<br />&nbsp;&nbsp;&nbsp;" + FLAG_FIRSTWILDCARD + " - 1st half wild card winner";
                    }
                    if (showKey.clinch) { 
                        s += "<br />&nbsp;&nbsp;&nbsp;" + FLAG_CLINCH + " - clinched playoff spot";
                    }
			        if (showKey.divwinner) { 
			            s += "<br />&nbsp;&nbsp;&nbsp;" + FLAG_DIVWINNER + " - division winner  ";
			        }
			        if (showKey.wildcardwinner) { 
            			s += "<br />&nbsp;&nbsp;&nbsp;" + FLAG_WILDCARDWINNER + " - wild card winner  ";
 			        }
                    s += "</div>";
                }

                s += "</td></tr>";
            }

            if (format == "wide") {
                s += "</table></td>";
            }
        }

        if (format == "wide") {
            if (showKey.firstwinner || showKey.firstwildcard || showKey.clinch || showKey.divwinner || showKey.wildcardwinners) {
                s += "<tr><td colspan=\"7\"><span class=\"winnerKey\" style=\"font-size: 9px;\">";
                if (showKey.firstwinner) { 
                    s += "&nbsp;&nbsp;&nbsp;" + FLAG_FIRSTWINNER + " - 1st half division winner";
                }
                if (showKey.firstwildcard) { 
                    s += "&nbsp;&nbsp;&nbsp;" + FLAG_FIRSTWILDCARD + " - 1st half wild card winner";
                }
                if (showKey.clinch) { 
                    s += "&nbsp;&nbsp;&nbsp;" + FLAG_CLINCH + " - clinched playoff spot";
                }
			    if (showKey.divwinner) { 
			        s += "&nbsp;&nbsp;&nbsp;" + FLAG_DIVWINNER + " - division winner  ";
			    }
			    if (showKey.wildcardwinner) { 
            		s += "&nbsp;&nbsp;&nbsp;" + FLAG_WILDCARDWINNER + " - wild card winner  ";
 			    }
                s += "</span></td></tr>";
            }
            s += "</tr></table></td></tr>";
        }

    }

    s += "</table>";

    // return the html for rendering
    return s;
}
