$(
    function() {
        $.ajax(
            {
                url: "/admin/ajax/analytics",
                type: "POST",
                success: function(data) {
                    // Replacing top data
                    $('#sessions')
                        .animateNumber({number: data.sessions}, 2000);
                    $('#users')
                        .animateNumber({number: data.users}, 2000);
                    $('#views')
                        .animateNumber({number: data.views}, 2000);
                    $('#newusers')
                        .animateNumber({number: data.newusers}, 2000);
                    // Building main chart
                    var chart = AmCharts.makeChart(
                        "chartdiv", {
                            "type": "serial",
                            "startDuration": 1,
                            "startEffect": "elastic",
                            'dataProvider': data.plot,
                            'categoryField': 'day',
                            'categoryAxis': {
                                'labelFrequency': 5
                            },
                            'graphs': [
                                {
                                    'balloon': {
                                        "drop": true,
                                        "adjustBorderColor": false,
                                        "color": "#ffffff"
                                    },
                                    "bullet": "round",
                                    "bulletBorderAlpha": 1,
                                    "bulletColor": "#FFFFFF",
                                    "bulletSize": 5,
                                    "hideBulletsCount": 50,
                                    "lineThickness": 2,
                                    "title": "red line",
                                    "useLineColorForBulletBorder": true,
                                    "valueField": "users",
                                    "balloonText": "<span style='font-size:18px;'>Users: [[users]]</span>",
                                    "lineColor": '#ff8800'
                                },
                                {
                                    'balloon': {
                                        "drop": true,
                                        "adjustBorderColor": false,
                                        "color": "#ffffff"
                                    },
                                    "bullet": "round",
                                    "bulletBorderAlpha": 1,
                                    "bulletColor": "#FFFFFF",
                                    "bulletSize": 5,
                                    "hideBulletsCount": 50,
                                    "lineThickness": 2,
                                    "title": "red line",
                                    "useLineColorForBulletBorder": true,
                                    "valueField": "sessions",
                                    "balloonText": "<span style='font-size:18px;'>Sessions: [[sessions]]</span>",
                                    "lineColor": '#652e8f'
                                }
                            ],
                            'chartCursor': {
                                "pan": true,
                                "valueLineEnabled": true,
                                "valueLineBalloonEnabled": true,
                                "cursorAlpha": 1,
                                "cursorColor": "#46b749",
                                "limitToGraph": "g1",
                                "valueLineAlpha": 0.2,
                                "valueZoomable": true,

                            }
                        }
                    );

                    // Building pie chart
                    var pie = AmCharts.makeChart(
                        "piediv", {
                            "radius": 130,
                            "balloonText": "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>",
                            "type": "pie",
                            "color": "#fff",
                            "labelsEnabled" : false,
                            'dataProvider': data.pie,
                            "titleField": "category",
                            "valueField": "column-1",
                            'colors': [
                                "#652e8f",
                                "#ff8800"
                            ]
                        }
                    );
                    // Replacing table stabs
                    $('#browsers')
                        .replaceWith(data.browsers);
                    $('#cities')
                        .replaceWith(data.cityes);
                    $('#countries')
                        .replaceWith(data.countries);
                    $('.yt-loader')
                        .remove();
                }
            }
        )
    }
);

