﻿var LengthPerTest = 100; //Time in ms
var Scores = [];

function RunTests() {
    var TotalValue = 0;
    var output = '<table id="TestOutputTable" class="datatable" style="width: 100%;">';
    output += '<thead><td>&nbsp;</td> <td>Test Description</td> <td>Score</td></thead>';
    for (i = 0; i < 10; i++) {
        var trclass;
        if (i % 2 == 0) { trclass = 'dataTableEven'; } else { trclass = 'dataTableOdd'; }
        BenchTests.Tests[i].Value = Math.round(BenchTests.Tests[i].Run() * BenchTests.Tests[i].Coeff * 10) / 10;
        TotalValue += BenchTests.Tests[i].Value;
        output += '<tr class=' + trclass + '>';
        output += '<td>' + (i + 1) + '. </td>';
        output += '<td>';
        output += BenchTests.Tests[i].Name + ' <span class="subtle"><a href="#" id="alink_' + BenchTests.Tests[i].id + '" title="' + BenchTests.Tests[i].Description + '" onclick="return false;">[?]</a></span>';
        output += '</td>';
        output += '<td style="text-align: right;">' + BenchTests.Tests[i].Value + '</td>';
        output += '</tr>';
    }
    TotalValue = Math.round(TotalValue);
    Scores.push(TotalValue);

    var AvgValue = 0;
    for (i = 0; i < Scores.length; i++) {
        AvgValue += Scores[i];
    }
    AvgValue = Math.round(AvgValue / Scores.length);

    var StdDev = 0;
    if (Scores.length > 1) {
        for (i = 0; i < Scores.length; i++) {
            StdDev += Math.pow((Scores[i] - AvgValue), 2);
        }
        StdDev = Math.round(Math.sqrt(StdDev / (Scores.length - 1)));
    }
    else {
        StdDev = 0;
    }

    output += '<tfoot><td colspan="2">Total Score: </td> <td style="text-align: right;">' + TotalValue + '</td></tfoot>';
    output += '</table>';

    $("#TestOutputReminder").remove();
    $('#TestOutput').html(output);

    var output = '';
    output += '<strong>Average Score: </strong>' + AvgValue + ' ± ' + StdDev + '<br />';
    output += '<strong>Last Score: </strong>' + TotalValue + '<br />';
    output += '<strong>Test iterations: </strong>' + Scores.length + '<br />';
    $('#AvgScoring').html(output);

    try {
        var aoReportAvg = new AjaxObject101();
        aoReportAvg.funcResponse = AjaxTextResponse;
        aoReportAvg.funcWait = AjaxWaiting;
        aoReportAvg.sndReq('post', 'AjaxHandler.aspx', 'action=record&score=' + TotalValue);

        var aoGraph = new AjaxObject101();
        aoGraph.funcResponse = AjaxGraphicalResponse;
        aoGraph.sndReq('post', 'AjaxHandler.aspx', 'action=graph&score=' + TotalValue + '&AvgScore=' + AvgValue + '&StdDev=' + StdDev); 
    }
    catch (ex) { //Logging was attempted, but failed
        $('#AjaxResponse').text("An attempt was made to compare your results agains the community; however, JSBenchmark was unable to ascertain your browser characteristics and successfully communicate with the community results.");
    }
    
    $("a").easyTooltip();
}

function AjaxGraphicalResponse(json) {
    $("#flotGraph").fadeOut("fast");
    $("#flotGraph").fadeIn("slow", function() { eval(json); });
}

function AjaxWaiting() {
    $('#AjaxResponse').html("Generating comparison...");
}

function AjaxTextResponse(json) {
    try {
        var obj = eval("(" + json + ")");
        $('#AjaxResponse').html("Your computer scored: <ul style=\"list-style-type: square;\"> <li>Faster than <u>" + obj.PercentileEntireCommunity + "%</u> of all computers that ran this test</li> <li>Faster than <u>" + obj.PercentileBrowserAndOS + "%</u> of computers running " + obj.BrowserAndOS + "</li> </ul> Remember - the HIGHER your score, the better.");
        $('#ResponseHolderWithAd').show("fast");

        if (obj.LoggedIn.toLowerCase() == 'false') {
            $('#AjaxResponseLogin').html('<a href="Login.aspx">Login to save your results</a>');
        }
        else {
            $('#AjaxResponseLogin').html('<a href="MyProfile.aspx">View Your Benchmark History</a>');
        }
    }
    catch (ex) {
        $('#AjaxResponse').text("An attempt was made to compare your results agains the community; however, JSBenchmark was unable to ascertain your browser characteristics and successfully communicate with the community results.");           
    } 
}

$(document).ready(function() {
    var output = '<table id="TestOutputTable" class="datatable" style="width: 100%;">';
    output += '<thead><td>&nbsp;</td> <td>Test Description</td> <td>Score</td></thead>';
    for (i = 0; i < 10; i++) {
        var trclass;
        if (i % 2 == 0) { trclass = 'dataTableEven'; } else { trclass = 'dataTableOdd'; }
        output += '<tr class=' + trclass + '><td>' + (i + 1) + '. </td> <td>' + BenchTests.Tests[i].Name + '</td> <td style="text-align: right;">0</td></tr>';
    }
    output += '<tfoot><td colspan="2">Total Score: </td> <td style="text-align: right;">' + '0' + '</td></tfoot>';
    output += '</table>';
    $('#TestOutput').html(output);

    var pos = $('#TestOutput').offset();
    var width = $('#TestOutput').width();
    var height = $('#TestOutput').height();

    $('#TestOutputReminder').css({ "left": pos.left + "px", "top": pos.top + "px", "width": 650 + "px", "height": height + "px" });
    $('#TestOutputReminder').html('<div id="TestOutputReminderText">Click \'Run Benchmark\' to begin</div>');
    $('#TestOutputReminderText').css({ "margin-top": height * 0.4 + "px" });

    //$("a").easyTooltip();
});