var tableHeadersFloatingPoint = 0;
var tableHeadersFloating = false;


function setTableHeaders()
{
    if ( $('#product-table-headers-controls').length > 0 )
    {
        $(window).scroll(function()
        {
            // Set table headers to float at the top of the page
            if ( !tableHeadersFloating )
            {
                if( $(window).scrollTop() > $('#product-table-headers-controls').position().top )
                {
                    tableHeadersFloatingPoint = $('#product-table-headers-controls').position().top;
                    $('#product-table-headers-controls').css('position', 'fixed');
                    $('#product-table-headers-controls').css('top', '0');
                    $('#product-table-headers-controls').css('zIndex', 20);
    
                    $('#product-table-headers-placeholder').show();
    
                    $('#product-table-headers-titles').css('position','fixed');
                    $('#product-table-headers-titles').css('zIndex', 1);
                    $('#product-table-headers-titles').css('top', '47px');
    
                    tableHeadersFloating = true;
                    synchronizeTableHeadersPosition();
                }
            }
            // Set table headers to their original position
            else if ( $(window).scrollTop() <= tableHeadersFloatingPoint )
            {
                $('#product-table-headers-placeholder').hide();
    
                $('#product-table-headers-controls').css('position', 'static');
                $('#product-table-headers-controls').css('zIndex', 0);
    
                $('#product-table-headers-titles').css('position', 'static');
                $('#product-table-headers-titles').css('zIndex', 0);
    
                tableHeadersFloating = false;
                synchronizeTableHeadersPosition();
            }
            // Refresh table headers position
            else if ( tableHeadersFloating )
            {
                 synchronizeTableHeadersPosition();
            }
        });
    }
}


function setTableTitlesWidth()
{
    // Set width only if table has rows
    if ( $('.list-content-table tbody tr').length > 0 )
    {
        $('.list-content-table thead').show();
        
        listTitleProductImageVisible = $('#listTitleProductImage').hasClass('hidden') ? false : true;
        
        
        // Set fake title table and real content table column widths from real content table columns
        $('.list-content-table tbody tr:first td').each(function(index)
        {
            /*console.log(index+ '   '+$(this).text());
            if ( listTitleProductImageVisible == false && index > 0)
            {
                //index = index+1
            }*/
            
            colWidth = $(this).width();
            $('#product-table-headers-titles table thead tr:first td:eq('+index+')').width(colWidth);
            $('.list-content-table tbody tr:eq(0) td:eq('+index+')').width(colWidth);
    
        });
    
        // Display fake title table
        $('#product-table-headers-titles').css('display', 'block');
    
        $('#product-table-headers-controls').width($('.list-content-table').width());
        $('#product-table-headers-titles table').width($('.list-content-table').width());
        $('.list-content-table').width($('.list-content-table').width());
        
        // Hide real content table head row
        $('.list-content-table thead').hide();
        $('.list-content-table').css('background', 'none');
    }
}


function synchronizeTableHeadersPosition()
{
    if ( tableHeadersFloating  )
    {
        $('#product-table-headers-controls').css('margin-left', '-' + $(window).scrollLeft() + 'px' );
        $('#product-table-headers-titles table').css('margin-left', '-' + $(window).scrollLeft() + 'px' );
    }
    else
    {
        $('#product-table-headers-controls').css('margin-left', '0px' );
        $('#product-table-headers-titles table').css('margin-left', '0px' );
    }
}


$(document).ready(function()
{
    setTableTitlesWidth();
    setTableHeaders();
});
