$(document).ready(function(){

    //timeToSec Helper function-------------------------------------------------
    var hmsToSec = function(hms){
        var time_array = Array(3);
        hms = hms.split(':');
        if (hms.length === 3){          //Assume standard hh:mm:ss
            time_array[0] = hms[0];
            time_array[1] = hms[1];
            time_array[2] = hms[2];
        }else if (hms.length === 2){    //Assume mm:ss
            time_array[0] = '00';
            time_array[1] = hms[0];
            time_array[2] = hms[1];
        }else if(hms.length === 1){     //Assume ss
            time_array[0] = '00';
            time_array[1] = '00';
            time_array[2] = hms[0];
        }
        var time_sec = parseInt(time_array[0]*60*60, 10) + parseInt(time_array[1]*60, 10) + parseInt(time_array[2], 10);
        return time_sec;
    }

    //secToHMS Helper function
    var secToHMS = function(sec){
        var hms = '';
        var h = parseInt(sec / 3600);
        var m = parseInt(sec / 60) % 60;
        var s = sec % 60;
        var h_str = '';
        var m_str = '';
        var s_str = '';

        if (h === 0){
            h_str = '';
        }else if (h < 10){
            h_str = '0' + h + ':';
        }else{
            h_str = h + ':';
        }

        if (m < 10){
            m_str = '0' + m + ':';
        }else{
            m_str = m + ':';
        }

        if (s < 10){
            s_str = '0' + s;
        }else{
            s_str = s;
        }

        hms = h_str + m_str + s_str;
        
        return hms;
    }
    //--------------------------------------------------------------------------

    //Sort the table
    $('.result_table_splits').each(function(){
        var current_table = '#' + $(this).attr('id');

        //Add the reset button
        var current_table_reset = $(current_table).attr('id') + '_reset_button';
        $('<input type="button" id="' + current_table_reset + '" value="Reset Sort" />').insertBefore($(current_table));

        //Add id to each tr allow a original ordering
        $(current_table).find('tbody tr').each(function(index){
            $(this).attr('id', index + current_table);
        });

        $('#' + current_table_reset).click(function(){
            var original_rows = $(current_table).find('tbody tr').get();
            original_rows.sort(function(a, b){
                var v1 = parseInt($(a).attr('id'),10);
                var v2 = parseInt($(b).attr('id'),10);
                if (v1 < v2){
                    return -1;
                }else if (v1 > v2){
                    return 1;
                }else{
                    return 0;
                }
            });
            $.each(original_rows, function(index, row){
                $(current_table).append(row);
            });
            $(current_table).find('th').removeClass('asc desc click_highlight');
            $(current_table).find('td').removeClass('click_highlight');
        });

        //Give each header sorting icons
        $(current_table).find('th.control_point').addClass('no_sort').each(function(index, value){
            $(this).hover(function(){
                $(this).addClass('header_hover');
            }, function(){
                $(this).removeClass('header_hover');
            });
            $(this).click(function(){
                $(this).siblings().removeClass('asc desc');
                var rows = $(current_table).find('tbody tr').get();
                var sort_direction;
                if ($(this).is('.asc')){
                    $(this).removeClass('asc').addClass('desc');
                    sort_direction = -1;//next sort will be desc
                }else{
                    $(this).removeClass('desc').addClass('asc');
                    sort_direction = 1;//next sort will be asc
                }
                rows.sort(function(a, b){
                    var v1 = $(a).children('td').eq(index + 3).text();  //Add 3 because sortable colums don't start at 0
                    var v2 = $(b).children('td').eq(index + 3).text();
                    //Test to see if the time is invalid (-----) and make to arbirtarily large number
                    v1 = (v1 === '-----') ? '99:99:99' : v1;
                    v2 = (v2 === '-----') ? '99:99:99' : v2;
                    //Sorting logic
                    if (v1 < v2){
                        return -sort_direction;
                    }else if (v1 > v2){
                        return sort_direction;
                    }else{
                        return 0;
                    }
                });
                $.each(rows, function(index, row){
                    $(current_table).find('tbody').append(row);
                });
            });
        });
    });
    //--------------------------------------------------------------------------

    //Add fastest split highlight
    $('.result_table_splits').each(function(){
        var current_table = '#' + $(this).attr('id');
        $(current_table).find('th.control_point').each(function(index, value){
            var leg_array = [];
            $(current_table).find('tbody tr').each(function(){
                leg_array.push($(this).children('td').eq(index + 3));//Add 3 due to leg not starting at col 0 in the table
            });
            leg_array.sort(function(a, b){
                var v1 = $(a).text();
                var v2 = $(b).text();
                //Test to see if the time is invalid (-----) and make to arbirtarily large number
                v1 = (v1 === '-----') ? '99:99:99' : v1;
                v2 = (v2 === '-----') ? '99:99:99' : v2;
                if (v1 < v2){
                    return -1;
                }else if (v1 > v2){
                    return 1;
                }else{
                    return 0;
                }
            });
            $(leg_array[0]).addClass('fastest_split_first');
            $(leg_array[1]).addClass('fastest_split_second');
            $(leg_array[2]).addClass('fastest_split_third');
        });
    });
});