(function() {
    var isOldIE = false;
    if ($.browser.msie) {
        if ($.browser.version <= 8) {
            isOldIE = true;
        }
    }

    // functions related to charts:
    var charts = {};

    // global function
    updateCharts = function(industry) {        
        $.getJSON('/index/stats', {'industry_id': industry}, function(data) {
            $.each(charts, function(name, chart) {chart.destroy()});

            // newCountryChart(toBarChart(data.countries));
            newJobsChart(toPieChart(data.jobs));
        })
    }

    function newCountryChart(countryStats) {
        return new Highcharts.Chart({
            chart: {
                renderTo: 'country-chart',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                defaultSeriesType: 'bar',
                backgroundColor: 'transparent'
            },
            credits: {enabled: false},
            title: {text: 'Leading Countries'},
            xAxis: {
                categories: [''],
                title: {text: ''}
            },
            yAxis: {
                min: 0,
                title: {
                    text: '',
                    align: 'high'
                }
            },
            tooltip: {
                formatter: function() {
                        return ''+
                                 this.series.name +': '+ this.y +' %';
                }
            },
            series: countryStats
        });
    }
    
    function newJobsChart(jobStats) {
        return new Highcharts.Chart({
            chart: {
                renderTo: 'job-chart',
                    height: 180,
                    spacingTop: 0,
                    spacingBottom: 0,
                    width: 320,
                    spacingRight: 0,
                    marginLeft: 0,
                    borderWidth: 0,
                    reflow: !1,
                    style: { fontFamily: "Arial", fontSize: "12px" },
                    backgroundColor: isOldIE ? '#fff' : 'transparent'
            },
            colors: ["#0081C6", "#4DA7D7", "#80C0E3", "#CCE6F4"],
            legend: {
                layout: "vertical",
                enabled: !0,
                align: "right",
                verticalAlign: "top",
                x: -60,
                y: isOldIE ? 45 : 35,
                borderWidth: 0,
                floating: !1,
                itemWidth: 100,
                style: { align: "right" },
                labelFormatter: function () {
                    return Math.round(this.y * 100) + '% ' + this.name;
                },
                itemHoverStyle: {
                    color: '#000000', fontFamily: "Arial", fontWeight: "bold"
                },
                itemStyle: {
                    color: '#0073ff', fontFamily: "Arial", fontWeight: "bold"
                }
            },
            credits: {enabled: false},
            title: {text: ''},
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                }
            },
            plotOptions: {
                pie: {
                    point: {
                        events: {
                            click: function (a) { var legendText = (isOldIE) ? $(a.currentTarget.legendItem.element).html().split(' ')[1] : a.currentTarget.legendItem.textStr.split(' ')[1]; window.location.href = searchUrl + legendText; },
                            legendItemClick: function (a) { a.preventDefault(); var legendText = (isOldIE) ? $(a.currentTarget.legendItem.element).html().split(' ')[1] : a.currentTarget.legendItem.textStr.split(' ')[1]; window.location.href = searchUrl + legendText; },
                            mouseOver: function (a) { (isOldIE) ? a.currentTarget.legendItem.element.style.color = "#000000" : $(a.currentTarget.legendItem.element).attr("style", "fill: #000000; font-weight: bold;"); },
                            mouseOut: function (a) { (isOldIE) ? a.currentTarget.legendItem.element.style.color = "#0073ff" : $(a.currentTarget.legendItem.element).attr("style", "fill: #0073ff; font-weight: bold;"); ; }
                        }
                    },
                    allowPointSelect: !1,
                    cursor: "pointer",
                    dataLabels: {
                        enabled: !1
                    },
                    showInLegend: !0,
                    size: 140,
                    center: ["85", "85"],
                    borderWidth: 0,
                    states: {
                        hover: { colorFill: "#333333", brightness: -0.7 }
                    },
                    shadow: !1
                }
            },
            series: jobStats
        });
    }

    function toPieChart(stats) {
        var series = [], total = 0, percent = null;
        $.each(stats.pairs, function(name, count) {
            total += Number(count);
        })
        $.each(stats.pairs, function(name, count) {
            percent = Math.round(100 * Number(count) / total) / 100;
            series.push([name, percent]);
        });
        return [{
            type: 'pie',
            data: series
        }];
    }

    function toBarChart(stats) {
        var series = [], total = 0, percent = null;
        $.each(stats.pairs, function(name, count) {
            total += Number(count);
        })
        $.each(stats.pairs, function(name, count) {
            percent = Math.round(100 * Number(count) / total);
            series.push({
                name: name, 
                data: [percent]
            });
        });
        return series;
    }
})();
