/*
    name : sparkLineMonthly
    file : jquery.sparkLineMonthly.js
    author : gregory tomlinson
    Dual licensed under the MIT and GPL licenses.
    ///////////////////////////
    ///////////////////////////        
    dependencies : jQuery 1.4.2, jquery.flot-0.6.js, jquery.cookie.js
    ///////////////////////////
    ///////////////////////////
    
*/

(function($) {
    
    $.fn.sparkLineMonthly = function( options ) {
        // extend the defaults settings
        var el = this, o = $.extend(true, defaults, options), flotBox, headline;
        
        if(el.length <= 0) return this;
        headline = $('<h2 style="display:none;" class="totalSparkLinePerformanceHeader">30 day click summary</h2>').appendTo(el);
        flotBox = $('<div style="display:none;" class="innerClickSummaryContainer"></div>').appendTo(el)
        


        setTimeout(function() {
                    connector(o.url, o.params, success, error);
        }, 600)

        //connector(o.url, o.params, success, error);
        return this;
        
        function success(jo) {
            if(!jo || !jo.data) return;
            /*
                TODO: send event this didn't display
            */
            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;
            }
                        
            series = {"data" : points, "label" : "30 days" }
            plot = $.plot( flotBox, [series], o.graphOptions )            
            flotBox.fadeIn('normal');
            headline.fadeIn('normal');
            
            el.bind('plothover', function(e, pos, item) {
                console.log(e,pos,item)
            }); 
            
            el.parent().fadeIn();                       
            
        }
       
        
        
        function error(jo) {
            // error
            console.log('error getting clicks summary', jo)
        }

    }
    
    
    var defaults = {
    
        url : '/data/clicks/summary',
        params : {
            days : '30'
        },
        graphOptions : {

            colors: ["#77C8FC"],
            xaxis: {
                mode: "time"
                //timeformat: ""
                //timeformat: "%y/%m/%d"

            },

            yaxis: { min: 0 },
            /* change in 0.6 */
            series : {
                bars: {
                    show: true,
                    // minTickSize: [1, "day"],
                    lineWidth: 0, // in pixels
                    barWidth: 43200, // in units of the x axis
                    //barWidth : 1,
                    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,
            type : 'POST',
            'url' : url,
            success: callback,
            'error' : error
        });
    }

})(jQuery);
