jQuery( function($) {
    
    $('.box.charting div.expander a').click(function(){
        $('.box.charting div.hideable').slideToggle();
        $(this).hide().siblings('a').show();
    });

    // Do anything you want to do on document ready
    $(document).ready(function(){
        var oriH = $('.description').height();
        var scrH = $('.description').attr('scrollHeight');
        if ( oriH < scrH ) {
            $('.summary .expander a').click(function(){
                $('.summary .expander a').toggle();
            });
            $('.summary .expander a.expand').click(function(){
                $('.summary .description').animate({ height: scrH },1000);
            });
            $('.summary .expander a.collapse').click(function(){
                $('.summary .description').animate({ height: oriH },1000);
            });
        } else {
            $('.summary .expander a').hide();
        }
        $('.box.charting table.data .legend').prepend( $('<div />').addClass('dot') );
        buildBusinessReviewChart();
     });
     
    $('.photo-gallery:not(.empty) .thumbnails .thumb a').click( function( event ) {
        event.preventDefault();
        
        $('.photo-gallery .main-photo').addClass('wait');
        
        $.getJSON( $(this).attr('href'), function(json){
        
            var preloader = new Image();
            preloader.onload = function() {
                
                var title = '';
                if ( json.caption != '' ) {
                    
                    title = json.caption;
                    if ( json.credit != '' )
                        title = json.caption+'('+json.credit+')';
                } else if ( json.credit != '' )
                    title = json.credit;
                
                $('.photo-gallery .main-photo img').attr( 'src', json.src ).attr('title', title).attr('alt', title)
                    .each( function(i, item) {
                        $(this).css('margin-top',0).filter( function(i) {
                            return $(this).height() < $('.photo-gallery .main-photo').height();
                        }).css( 'margin-top', ( $('.photo-gallery .main-photo').height() - $(this).height() ) / 2 +'px' );
                    })
                    .siblings('.caption').html( json.caption )
                    .siblings('.credit' ).html( json.credit  )
                    .parents('.main-photo').removeClass('wait');
                preloader.onload = null;
                preloader = null;
            };
            preloader.src = json.src;
        });
    });
    
    // Build the business avg data graph
    function buildBusinessReviewChart()
    {
    // Get the colours dynamically from the CSS
        var visitsColor  = $("th.legend.business div.dot").css("background-color");
        
        var shadowSizePix = 0;
        var businessVisitsSeries = {
            color: visitsColor,
            data: visits,
            label: "Business Avg.",
            shadowSize: shadowSizePix
        };
        
        // 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: { tickDecimals: 0 },
            xaxis: {
                mode: "time",
                min: xMin,
                minTickSize: [1, "day"]
            },
            grid: { color: "#999999", borderWidth: 1 }
        };
        
        if ($("div#chartDiv div.graph").length > 0)
            $.plot($("div#chartDiv div.graph"), [businessVisitsSeries], options);
    };

});

