jQuery(function($){
    
    $('td.chart div.expander a').click(function(){
        $('td.chart div.hideable').slideToggle();
        $(this).hide().siblings('a').show();
    });
    
    $('.chart table.data .legend').prepend( $('<div />').addClass('dot') );
    
    // Build the business avg data graph    
    (function(){
        // Get the colours dynamically from the CSS
        var currentDestAvgColor = $("th.legend.current div.dot").css("background-color");
        
        var shadowSizePix = 0;
        var currentDestVisitsSeries = {
            color: currentDestAvgColor,
            data: visits,
            label: "Destination Avg.",
            shadowSize: 0
        };
        
        // We'll have to build the legend ourselves. Setting show=false for now.        
        var legendOptions = {   show: false, container: $("#businessReviewLegend")};
                                
        var xMin = (function(){
            var aYearAgo = (new Date()).setDate( (new Date()).getDate()-365 );
            if (visits[0]) return visits[0][0] > aYearAgo ? visits[0][0] : aYearAgo;
            else return null;
        })();
        var options = { legend: legendOptions,
                        yaxis: { min: 0, tickDecimals: 0 },
                        xaxis: {
                            mode: "time",
                            min: xMin,
                            minTickSize: [1, "day"]
                        },
                        grid: { color: "#999999", borderWidth: 1} };
        
        if ($("td.chart div.graph").length > 0)
            $.plot($("td.chart div.graph"), [currentDestVisitsSeries], options);
    })();

});

