$(document).ready(function()
{
    /* Add a trade detail item. */
    $(document).on('click', '.addDetail', function()
    {
        if($('#hiddenDetail').length)
        {
            $(this).parents('tr').after($('#hiddenDetail').html().replace(/key/g, v.key));
            $(this).parents('tr').next().find("[name*='handlers']").chosen({no_results_text: '', placeholder_text:' ', disable_search_threshold: 1, search_contains: true, width: '100%'});
            $(this).parents('tr').next().find("[name*='category']").chosen({no_results_text: '', placeholder_text:' ', disable_search_threshold: 1, search_contains: true, width: '100%'});
        }

        v.key ++;
    });

    /* Remove a trade detail item. */
    $(document).on('click', '.delDetail', function()
    {
        if($('#hiddenDetail').length)
        {
            if($('#detailList > table tbody tr').size() > 1)
            {
                $(this).parents('tr').remove();
            }
            else
            {
                $(this).parents('tr').find('input,select').val('');
                $(this).parents('tr').find('select').trigger('chosen:updated');
            }
        }
    });

    $(document).on('click', '.trade-toggle', function(e)
    {
        var $toggle = $(this);
        var id = $(this).data('id');
        var isCollapsed = $toggle.toggleClass('collapsed').hasClass('collapsed');
        $toggle.closest('[data-ride="table"]').find('tr.parent-' + id).toggle(!isCollapsed);

        e.stopPropagation();
        e.preventDefault();
    });

    $('#submit').click(function()
    {
        var tradeChecked = false;

        $('[name*=tradeIDList]').each(function()
        {
            if($(this).prop('checked')) tradeChecked = true;
        })

        return tradeChecked;
    });

    if(v.treeview == '')
    {
        $('.tree').tree('expandSelect', $('a[href*=' + v.currentYear + ']').parents('li.has-list'));
    }

    $('.main-table').table(
    {
        statisticCreator: function(table)
        {
            var $checkedRows = table.getTable().find('tbody>tr.checked');
            var checkedTotal = $checkedRows.length;
            if(!checkedTotal) return;

            selectedMoney = new Array();
            $.each(v.currencyList, function(index, currency)
            {
                selectedMoney[index] = 0;
                $('[name*=tradeIDList]').each(function()
                {
                    if($(this).prop('checked'))
                    {
                        if($(this).parents('tr').data('currency') == index)
                        {
                            currentMoney = parseFloat($(this).parents('tr').data('money'));
                            selectedMoney[index] = selectedMoney[index] + currentMoney;
                        }
                    }
                });
            });

            var content = v.checkedSummary.replace('%total%', checkedTotal);
            for(currency in selectedMoney)
            {
                money = selectedMoney[currency];
                money = money > 10000 ? Math.round(money / 10000 * 100) / 100 + v.unit : Math.round(money * 100) / 100;
                if(money)
                {
                    content += v.currencyList[currency] + v.modeLabel + money + v.semicolon;
                }
            }

            return content;
        }
    });
});
