$(document).ready(function()
{
    $("#menu .nav a[href*=" + config.currentMethod + "]").parent().addClass('active');
    $("#menu .nav a[href*=invoiceStatistic]").parent().removeClass('active');
    $('#menu .nav a[href*=export2Excel]').addClass('iframe');

    var labels   = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
    var datasets = [];

    var datasetHas     = {label: $('#lineChart').find('thead .chart-label-has').text(), color: 'green', data: []};
    var datasetNo      = {label: $('#lineChart').find('thead .chart-label-no').text(), color: 'red', data: []};
    var datasetInvoice = {label: $('#lineChart').find('thead .chart-label-invoice').text(), color: 'yellow', data: []};
    
    $('#lineChart').find('.chart-color-dot-has').css('color', 'green');
    $('#lineChart').find('.chart-color-dot-no').css('color', 'red');
    $('#lineChart').find('.chart-color-dot-invoice').css('color', 'yellow');
    
    var chartLabels = [];
    $('#lineChart').find('tbody .chart-label').each(function(){ chartLabels.push($(this).text()); })

    $.each(labels, function(key, value)
    {
        if($.inArray(value, chartLabels) != -1)
        {
            $('#lineChart').find('tbody .chart-value-has').each(function()
            {
                if($(this).parent('tr').find('.chart-label').text() == value)
                {
                    datasetHas.data.push(parseFloat($(this).text()));
                }
            })

            $('#lineChart').find('tbody .chart-value-no').each(function()
            {
                if($(this).parent('tr').find('.chart-label').text() == value)
                {
                    datasetNo.data.push(parseFloat($(this).text()));
                }
            })

            $('#lineChart').find('tbody .chart-value-invoice').each(function()
            {
                if($(this).parent('tr').find('.chart-label').text() == value)
                {
                    datasetInvoice.data.push(parseFloat($(this).text()));
                }
            })
        }
        else
        {
            datasetHas.data.push(parseFloat(0));
            datasetNo.data.push(parseFloat(0));
            datasetInvoice.data.push(parseFloat(0));
        }
    })
    
    var data = {labels: labels, datasets: [datasetHas, datasetNo, datasetInvoice]};
    
    var options = {multiTooltipTemplate: "<%= datasetLabel %> <%= value %>"};
    chart = $('#chart-invoice').lineChart(data, options);
    
    $('#currency').change(function()
    {
       var selectYear     = $('#year option:selected').text();
       var selectCurrency = $('#currency').val();
       location.href = createLink('trade', 'report', "date=" + selectYear + "&currency=" + selectCurrency);
    })

    if($('td.invoice > .table-wrapper').height() < $('td.invoice > .chart-wrapper').height())
    {   
        $('td.invoice > .table-wrapper').height($('td.invoice > .chart-wrapper').outerHeight() - 2); 
    }   

    initTableHeader();
})
