/*extern addEvent, scores_data, setup_lsmatrix, gauss_jordan_solve, display_strengths, display_predictions, display_matrix_as_table */


// -----------------------------------------------------------------------------------
function get_os(matrix) {   // Retrieve the offensive strength numbers from the
                            // top half of the last column of the matrix
                            // Use only 1 digit to the right of the decimal place
    var ml = matrix.length;
    var os = [];
    for (var i=0; i<ml/2; i++) {
        os.push(matrix[i][ml]);
    }
    return os;
}  // End of get_os(matrix)



// -----------------------------------------------------------------------------------
function get_ds(matrix) {   // Retrieve the defensive strength numbers from the
                            // bottom half of the last column of the matrix
                            // Use only 1 digit to the right of the decimal place
    var ml = matrix.length;
    var ds = [];
    for (var i=ml/2; i<ml; i++) {
        ds.push(matrix[i][ml]);
    }
    return ds;
}  // End of get_ds(matrix)


// -----------------------------------------------------------------------------------
function normalize_strengths(off, def) {   // Normalize the strengths so the max overall strength is 100

    var len = off.length;
    var i, maxone, maxhalf, thisone, bump;
    maxone = (off[0] - 0) + (def[0] - 0);
    
    // alert("len = " + len + "\n");
    for (i=1; i<len; i++) {
        thisone = (off[i] - 0) + (def[i] - 0);
        if (maxone < thisone ) maxone = thisone;
    }
    
    bump = (100 - maxone)/2;
    for (i=0; i<len; i++) {
        off[i] = bump + (off[i] - 0);
        off[i] = off[i].toFixed(1);
        def[i] = bump + (def[i] - 0);
        def[i] = def[i].toFixed(1);
    }



}  // End of normalize_strengths(off, def)





// -----------------------------------------------------------------------------------
function make_strength_tables() {           // Set up and solve linear equations to find the 
                                            // offensive and defensive strengths, and display
                                            // them in various tables.
    var scores2006 = scores_data.getdata(); // Retrieve the scores of all games played so far
    var teamnames = scores_data.getnames(); // Retrieve the names of the teams
    
    var full = 1;
    var future = 2;
    var past = 3; 

    var i, j;
    var numd = scores2006.length - 2;       // Ignore the two non-district games
     
    var mrow = setup_lsmatrix(scores2006, numd);    // Set up linear equations that represent the 
                                                    // least-squares approximation to the set of 
                                                    // offensive and defensive strengths of all the
                                                    // teams whose games are captured in the scores_data matrix.
                                                    // Leave off the two non-district teams, which are at
                                                    // the far right and bottom of the matrix of scores
             
                                            
    /***************************
    // Display the linear equations matrix, mrow, as a table, for debugging purposes.
    var rowlabels = [];
    var columnlabels = [];
    var tl = teamnames.length-2;
    columnlabels[0] = "Team";
    for (i=0;i<tl; i++) {columnlabels[i+1]=teamnames[i].substr(0,4); columnlabels[tl+i+1]=teamnames[i].substr(0,4);}
    columnlabels[2*tl+1] = "Sum";
    for (i=0;i<tl; i++) {rowlabels[i]=teamnames[i].substr(0,4); rowlabels[tl+i]=teamnames[i].substr(0,4);}
    var attribm = [];

    for (i=0;i<2*numd; i++) {
        attribm[i] = [];
        for (j=0; j<2*numd+1; j++) {
            attribm[i][j] = " ";
        }  
    }
                           
    // display_matrix_as_table(mrow, attribm, "Linear equations matrix", rowlabels, columnlabels, "600", 0, "lsmatrix_table");
    
    *******************/
    
    
    var inv = gauss_jordan_solve(mrow);   // Solve the linear equations represented by mrow
    
    
    /***************************
    // Display the linear equations matrix after inversion, as a table, for debugging purposes.
    // columnlabels[2*tl] = "Strengths";    
    // display_matrix_as_table(inv, attribm, "Inverted matrix", rowlabels, columnlabels, "600", 0, "inverted_matrix");
    *******************/    
    
    
    var ostrengths = get_os(inv);   // Retrieve the offensive strengths from the solution matrix
    var dstrengths = get_ds(inv);   // Retrieve the defensive strengths from the solution matrix
    
    normalize_strengths(ostrengths, dstrengths);  // Normalize the strengths so the max overall strength is 100

    // Display the strengths:
    display_strengths(ostrengths, dstrengths, teamnames, "strengths_table");  

    
    // Display the full predictions:
    display_predictions(ostrengths, dstrengths, teamnames, "district_predictions_table_full", scores2006, full);
    
        // Display only predictions for past games, colored to show which ones predicted the correct winner:
    display_predictions(ostrengths, dstrengths, teamnames, "district_predictions_table_past", scores2006, past);
    
    // Display only the predictions for games that haven't yet been played:
    display_predictions(ostrengths, dstrengths, teamnames, "district_predictions_table_future", scores2006, future);
    
}     // End of make_strength_tables()



addEvent(window, "load", make_strength_tables);
