/*
    name : pieChartReferrersMonthly
    file : jquery.pieChartReferrersMonthly.js
    author : gregory tomlinson
    copyright: (c) 2010 bit.ly
    Dual licensed under the MIT and GPL licenses.
    ///////////////////////////
    ///////////////////////////        
    dependencies : jQuery 1.4.2, jquery.flot-0.6.js, jquery.flot.pie-1.0.js
    ///////////////////////////
    ///////////////////////////
    Events: pieChartFailedToLoad
    
*/

(function($) {
    
    $.fn.pieChartReferrersMonthly = function( options ) {
        // extend the defaults settings
        var el = this, o = $.extend(true, defaults, options), flotPieBox;
        
        connector(o.url, o.params, success, error);
        flotPieBox = $('<div class="referrersMetricsSummaryContainerInner"></div>').appendTo(el);
        
        return this;
        
        function success(jo) {

            if(!jo || !jo.data || jo.data.referrers.length <=0 )  {
                console.log(jo, "pice chart referrers has no data");
                el.trigger('pieChartFailedToLoad')
                return;
            }
            var clicks_by_domain = [], d, referrers = jo.data.referrers, regex_pattern = /^https{0,1}:\/\/([^\/]+)\/(.*)$/i, matches = [], domains = {};
            if(referrers.length > 0 ) el.parent().fadeIn();
            for(var i=0; i<referrers.length; i++) {
                
                matches = referrers[i].referrer.match(regex_pattern);
                if(matches && matches[1]) {
                    d = matches[1];
                    if( domains[ d ] ) domains[ d ] += referrers[i].clicks;
                    else domains[ d ] = referrers[i].clicks;
                }
            }
            
            for(var domain in domains) {
                clicks_by_domain.push( {'data' : domains[domain] } )
            }
            
            console.log(domains)
            
            $.plot(flotPieBox, clicks_by_domain, {
                colors: ["#84ba5b", "#afd8f8", "#e1974c", "#7293cb", "#d35e60", "#959799", "#ccc274", "#8965a6"],
                series: {
                    pie: { 
                        // gradient: {show:true, colors:["#aaaaaa"]},
                        show: true, 
                        showLabel: true,
                        radius: 1,
                        stroke: { width:0 },
                        label: {
                            show: false,
                            radius: 3/4 
                            //                             formatter: function(label, series){
                            //                                 var p = Math.round(series.percent);
                            //                                 if (p > 15) {
                            //                                     return "<div style='font-size:8pt;text-align:center;padding:2px;color:#000;'>" + label+'<br/>'+p+'%' + "</div>";
                            //                                 } else {
                            //                                     return '';
                            //                                 }
                            //                             }
                        }                       
                        
                    }
                },
                grid: {hoverable: false, clickable: true},
                legend: { show: false }
            });            
            
            
        }
        
        function error() {

        }   

    }
    
    
    var defaults = {
    
        url : '/data/referrers/summary',
        params : {
            days : '30'
        }
        
    }, $bod;
    

    
    function connector(url, params, callback, error) {
        var str = $.param( params );
        $.ajax({
            dataType: 'json',
            data : str,
            traditional : true,
            type : 'POST',
            'url' : url,
            success: callback,
            'error':error            
        });
    }

})(jQuery);
