$(document).ready(function() {

    stateReportChart = function(id, crit)
    {
        swfobject.embedSWF(
            '/swf/open-flash-chart.swf',
            id,
            '470',
            '340',
            '9.0.0',
            '/swf/expressInstall.swf',
            {
                "data-file" : "/api/state_report_chart.php?crit=" + crit,
                "loading"   : "Trwa pobieranie danych.\nProszę poczekać."
            }
        );

        if (true == exists('#' + id + '-text'))
        {
            $.getJSON('/api/state_report_chart.php?crit=' + crit, function(data) {
                /*
                $.each(data.elements[0].values, function(i, item) {
                    html = html + '<b>' + item.tip.split('<br>')[0]+'</b>:' + item.tip.split('<br>')[1] + '<br />';
                });*/
                if (0 < data.elements[0].values.length)
                {
                    html = '<table>';
                    $.each(data.elements[0].values, function(i, item) {
                        html = html + '<tr><th style="color: ' + data.elements[0].colours[i] + '; padding: 0 2em 0 0; line-height: 1.5em;">' + item.tip.split('<br>')[0] + '</th><td>' + item.tip.split('<br>')[1] + '</td></tr>';
                    });
                    html = html + '</table>';
                    $('#' + id + '-text').html(html);
                }
            });
        }
    }

    if (true == exists('#PopulationDoctrine'))
    {
        stateReportChart('PopulationDoctrine', 'byDoctrines');
    }

    if (true == exists('#PopulationParties'))
    {
        stateReportChart('PopulationParties', 'byParties');
    }

    if (true == exists('#PopulationGenders'))
    {
        stateReportChart('PopulationGenders', 'byGenders');
    }

    if (true == exists('#PopulationProfessions'))
    {
        stateReportChart('PopulationProfessions', 'byProfessions');
    }

    if (true == exists('#PopulationDistricts'))
    {
        stateReportChart('PopulationDistricts', 'byDistricts');
    }

    if (true == exists('#ParliamentChart'))
    {
        swfobject.embedSWF(
            '/swf/open-flash-chart.swf',
            'ParliamentChart',
            '470',
            '340',
            '9.0.0',
            '/swf/expressInstall.swf',
            {
                "data-file" : "/api/parliament_chart.php?crit=byParty",
                "loading"   : "Trwa pobieranie danych.\nProszę poczekać."
            }
        );

        chartCrit = function(crit)
        {
            swfobject.embedSWF(
                '/swf/open-flash-chart.swf',
                'ParliamentChart',
                '470',
                '340',
                '9.0.0',
                '/swf/expressInstall.swf',
                {
                    "data-file" : "/api/parliament_chart.php?crit=" + crit,
                    "loading"   : "Trwa pobieranie danych.\nProszę poczekać."
                }
            );
        }

        if (true == exists('#ParliamentMembersSelect'))
        {
            getParliamentMembers = function () {
                if ($('#ParliamentMembersSelect option:selected').val() != -1) {
                $.ajax({
                    url: '/api/parliament_members.php?p=' + $('#ParliamentMembersSelect option:selected').val(),
                    type: 'GET',
                    dataType: 'html',
                    timeout: 40000,
                    beforeSend: function ()
                    {
                        $('#ParliamentMembersResult').empty();
                        $('#ParliamentMembersResult').html('<div id="lwEditorMsg" class="lwEditorMsgGreen">Trwa pobieranie danych. Proszę czekać…</div>');
                        $('#lwEditorMsg').fadeIn('medium');
                    },
                    error: function()
                    {
                        $('#ParliamentMembersResult').html('<div id="lwEditorMsg" class="lwEditorMsgRed">Wystąpił błąd. Proszę spróbować ponownie.</div>');
                        $('#lwEditorMsg').fadeIn('medium');
                        $("#ParliamentMembersSelect")[0].selectedIndex = 0;
                    },
                    success: function(html)
                    {
                        $('#lwEditorMsg').fadeOut(
                            'medium',
                            function()
                            {
                                $('#ParliamentMembersResult').html(html);
                            }
                        );
                    }
                });
                }
            }

            $('#ParliamentMembersSelect').change(function() {
                getParliamentMembers();
            });


            getParliamentMembers();


        }
    }

    if (true == exists('#GrowthRateRecentChart'))
    {
        swfobject.embedSWF(
            '/swf/open-flash-chart.swf',
            'GrowthRateRecentChart',
            '470',
            '250',
            '9.0.0',
            '/swf/expressInstall.swf',
            {
                "data-file" : "/api/growthrate_recent_chart.php",
                "loading"   : "Trwa pobieranie danych.\nProszę poczekać."
            }
        );
    }

    if (true == exists('#GrowthRateHistoryChart'))
    {
        swfobject.embedSWF(
            '/swf/open-flash-chart.swf',
            'GrowthRateHistoryChart',
            '470',
            '250',
            '9.0.0',
            '/swf/expressInstall.swf',
            {
                "data-file" : "/api/growthrate_history_chart.php",
                "loading"   : "Trwa pobieranie danych.\nProszę poczekać."
            }
        );
    }

    chartHistory = function(p)
    {
        swfobject.embedSWF(
            '/swf/open-flash-chart.swf',
            'GrowthRateHistoryChart',
            '470',
            '250',
            '9.0.0',
            '/swf/expressInstall.swf',
            {
                "data-file" : "/api/growthrate_history_chart.php?page=" + p,
                "loading"   : "Trwa pobieranie danych.\nProszę poczekać."
            }
        );
    }
});
