/*
    name : sparkLineWeekly
    file : jquery.sparkLineWeekly.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.commifyNumber.js
    ///////////////////////////
    ///////////////////////////
    
*/

(function($) {
    
    $.fn.sparkLineWeekly = function( options ) {
        // extend the defaults settings
        var el = this, o = $.extend(true, defaults, options), flotBox;
        
        flotBox = $('<div style="display:none;" class="innerWeeklyClickSummaryContainer"></div>').appendTo(el);
        // TODO:
        // integrate the click summary with the weekly sparkline for basic flot code?
        el.bind('click', function(){
            document.location.href = "/a/summary"
        })
        setTimeout(function() {
            connector(o.url, o.params, success, error);
        }, 1500)        
        
        return this;
        
        function success(jo) {
            if(!jo || !jo.data) {
                console.log('ERRPR: sparkline not returning data correctly')
                return;
            }
            
            var series, i=0, r, points=[], totalClicks = 0, 
                clicks = jo.data.clicks;
            
            for(; i<clicks.length; i++) {
                r = clicks[i];
                points.push( [ r.ts, r.clicks ] );
                totalClicks += r.clicks;
            }

            if(totalClicks <= 0 ) return;
            el.slideDown();
            series = {"data" : points, "label" : "7 days" };
            plot = $.plot( flotBox, [series], o.graphOptions )            

            flotBox.fadeIn('normal');

            // todo: commify #
            $('<div class="weeklySparkLineDescText"></div>').html( $.commifyNumber(totalClicks) + ' clicks this week').appendTo(el)
         
        }

        function error() {
            console.log('error getting weekly spark lines >> please phone home')
        }
        
    }
    
    
    var defaults = {
        url : '/data/clicks/summary',
        params : {
            days : '7'
        },
        graphOptions : {

            colors: ["#77C8FC"],
            xaxis: {
                mode: "time"
            },

            yaxis: { min: 0 },
            /* change in 0.6 */
            series : {
                bars: {
                    show: true,
                    // minTickSize: [1, "day"],
                    lineWidth: 0, // in pixels
                    barWidth: 54600, // in units of the x axis
                    fill: true,
                    fillColor: "#77C8FC"
                }
            },
            legend: {
                show: false
            },
            grid: {hoverable: false, clickable: true, borderWidth: 0, show:false}                
        }
        
    }, $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);
