;(function (window, document, $, bam) {
  

  /**
   * PostseasonScoreboard
   * If called without the new keyword, use a singleton instance.
   */
  var instance;


  function PostseasonScoreboard (options) {
    if (!(this instanceof PostseasonScoreboard)) {
      if (!instance) {
        instance = new PostseasonScoreboard();
      }
      return instance;
    }
    
     this.options = $.extend({
      i18n: 'en'
    }, options);
  }
  
  function sanitize (str) {
    return unescape(str || '');
  }
  
  // Add $.loadable behavior to PostseasonScoreboard
  $.loadable(PostseasonScoreboard, {
    dataType: 'xml',
    cache:    !bam.env.host.isDev, // bust cache on dev
    dataFilter: function (xml) {
      return $('scoreboard module', xml).map(function () {

        var module       = $(this),
            game         = module.find('game'),
            awayPitcher  = module.find('awayPitcher'), 
            homePitcher  = module.find('homePitcher'),
            hilites      = module.find('hilites'),
            alert        = module.find('alert'),
            message      = module.find('message');
        
        return {
          id:              module.attr('id'),
          series:          module.attr('series'),
          round:           module.attr('round'),
          status_override: module.attr('status_override'),
          game: {          
            number:        game.attr('number'),
            id:            game.attr('id')
          },               
          away_pitcher: {  
            link:          awayPitcher.text()
          },               
          home_pitcher: {  
            link:          homePitcher.text()
          },               
          hilites: {       
            blurb:         sanitize(hilites.attr('blurb')),
            link:          hilites.text()
          },               
          alert: {         
            blurb:         sanitize(alert.attr('blurb')),
            link:          alert.text()
          },               
          message: {       
            enabled:       message.attr('enabled'),
            date:          message.attr('date'),
            time:          message.attr('time'),
            away:          message.attr('away'),
            home:          message.attr('home'),
            blurb:         sanitize(message.attr('blurb')),
            link:          message.text()
          }
        };

      }).get();
    }
  });


  // Overload $.loadable load method with a custom load method that accepts
  // a date as an optional parameter to configure the request URL
  PostseasonScoreboard.prototype.load = (function (load) {
    return function (date) {

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

      var year = date.getFullYear(),
          url,
          /*
          en  = '/shared/components/ps/y' + year + '/xml/ps_scoreboard.xml',
          es = '/shared/components/ps/y' + year + '/xml/ps_scoreboard_es.xml';
          */
          
          en  = '/shared/components/ps/y2011/xml/ps_scoreboard.xml',
          es = '/shared/components/ps/y2011/xml/ps_scoreboard_es.xml';
          

         if (this.options.i18n === 'es') {
            url = es;
         }else{
            url = en;
         }
         
      return load.call(this, url);

    };    
  })(PostseasonScoreboard.prototype.load);
  
  
  // Add PostseasonScoreboard to bam.services namespace
  bam.namespace('services').PostseasonScoreboard = PostseasonScoreboard;


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