(function (window, $, bam) {

    var defaultConfig = {
        polling: false
    };

    /** 
     * Pads digits with zeroes. Converts number to string
     * if necessary.
     *
     * @private
     *
     * @param {Number|String} val  number to pad
     * @param {Number} len  lenght of string
     *
     * @return {String}
     */
    function addLeadingZeros(val, len) {
        val += '';
        while (val.length < (len || 2)) {
            val = '0' + val;
        }
        return val;
    }

    function MediaGrid(config) {

        if (!(this instanceof MediaGrid)) {
            if (!instance) {
                instance = new MediaGrid(config);
            }
            return instance;
        }

        this.config = $.extend({}, defaultConfig, config || {});

    }

    MediaGrid.prototype = {
        /**
         * Placeholder proxy function that handles the 
         */
        _loaderProxy: function() {},

        /**
         * Initiates polling behavior. The polling interval is defined in the 
         * as an attribute within the datafile.
         */
        start: function() {
            var dataFilter;

            // throw an error if the polling flag is not set to true
            if ( ! this.config.polling) {
                throw new Error("MediaGrid instance config.polling is false");
            }

            // read the the wait attribute and determine when to make the next request
            // for the datafile
            this._loaderProxy = $.proxy(function(data) {
                this.config.pollingTimeout = ~~data.pitches.wait;

                setTimeout($.proxy(this.load, this), this.config.pollingTimeout * 1000);
            }, this);

            this.onLoadSuccess($.eventProxy(this._loaderProxy));

            // initial load of datafile
            this.load();
        },

        /**
         * Terminates polling behavior
         */
        stop: function() {
            this.unbind("onLoadSuccess", this._loaderProxy);
        }
    };

    // add loadable behavior
    $.loadable(MediaGrid.prototype, {
        dataType: "json" 
    });

    // To provide a cleaner API, let's overload the loadableConfig method 
    // to accept a date argument. Inspired by Furf's MasterScoreboard data provider
    MediaGrid.prototype.loadableConfig = (function(loadableConfig) {
        return function(date) {
            var url;

            // If undefined, set date current date/time Eastern and display
            // previous day's scoreboard until 11am ET (or specified time)
            if ( ! date) {
                date = new Date();
            } else {
                date = $.ensureDate(date);
            }

            url = '/gen/hb/video/' + date.getFullYear() + '/' + addLeadingZeros(date.getMonth() + 1) + '/' + addLeadingZeros(date.getDate()) + '/grid.json';

            return loadableConfig.call(this, url);

        };
    }(MediaGrid.prototype.loadableConfig));

    // expose the FastPitch class under the bam.services namespace
    bam.namespace("services").MediaGrid = MediaGrid;


}(this, this.jQuery, this.bam));


