(function($) {
  
  // Private class variable
  var options;
  var tournaments;
  var current_tournaments;
  
  $.fn.tournamentSchedule = function(opts) {  
    
    options = opts || {};
    
    this.each(function() {
      var $this = $(this);
      
      typeList($this.find('.typeList')[0]);
      gamesTable($this.find('.gamesTable')[0]);
      detailsPanel($this.find('.detailsPanel')[0]);      
    });
    
    $.getJSON(options.url, function(response, statusText) {
      $(document).trigger("tournament:data:loaded", {tournaments: response});
    });
    
    // Private instance methods
    
    function typeList(el) {
      /* 
       * listen to tournament:data:loaded event and populate the list
       * when an item is clicked, fire tournament:type:selected
       */       
       $(document).bind("tournament:data:loaded", function(e, data, value) {
         var game_types = $.map(data.tournaments, function(tournament, index) {
           return tournament.game_type;
         });

         var template = "<ul>";
         template += '<li class="selected"><a href="#">All</a></li>';

         $.each(game_types.unique(), function() {
           template += '<li><a href="#">' + this + '</a></li>';
         });

         template += "</ul>";

         $(el).html(template);

         $('a', el).bind('click', function() {
           $(document).trigger("tournament:type:selected", $(this).text());
           $('li.selected', el).removeClass("selected");
           $(this).parent().addClass("selected");
           return false;
         });
       });
    };

    function gamesTable(el) {
      /* 
       * listen to tournament:type:selected event and populate the table
       * when an item is clicked, fire tournament:game:selected
       */      
      $(document).bind("tournament:data:loaded", function(e, data, value) {
        tournaments = data.tournaments;
        current_tournaments = tournaments;
        $(el).html(renderTable(current_tournaments));
      });
      
      $(document).bind("tournament:type:selected", function(e, data, value) {
        if(data == "All") {
          current_tournaments = tournaments;
        }
        else {
          current_tournaments = $.grep(tournaments, function(item, index) {
            return item.game_type == data;
          });
        }
        $(el).html(renderTable(current_tournaments));
      });

      $('tr', el).live("click", function() {
        $(document).trigger("tournament:game:selected", $(this).parent().children().index(this));
        $('tr.selected', el).removeClass("selected");
        $(this).addClass("selected");
        return false;
      });
    };

    function detailsPanel(el) {
      /* 
       * listen to tournament:type:selected event and clear the panel
       * listen to tournament:game:selected event and populate the panel
       */
      $(document).bind("tournament:data:loaded", function(e, data, value) {
        tournaments = data.tournaments;
        current_tournaments = tournaments;
      });

      $(document).bind("tournament:game:selected", function(e, data, value) {
        $('#tournament_schedule .detailsPanel #details').html("<strong>Details:</strong><br/>" + current_tournaments[data].name);
        $('#tournament_schedule .detailsPanel #start_date').html("<strong>Start Date:</strong><br/>" + current_tournaments[data].start_date);
        $('#tournament_schedule .detailsPanel #reg_start').html("<strong>Reg Start:</strong><br/>" + current_tournaments[data].registration_start_date);
        $('#tournament_schedule .detailsPanel #reg_end').html("<strong>Reg End:</strong><br/>" + current_tournaments[data].registration_end_date);
        $('#tournament_schedule .detailsPanel #rebuy').html("<strong>Rebuy:</strong><br/>" + (current_tournaments[data].rebuy_allowed == 1 ? 'yes' : 'no'));
        $('#tournament_schedule .detailsPanel #max_players').html("<strong>Max Players:</strong><br/>" + current_tournaments[data].max_players);
        $('#tournament_schedule .detailsPanel #registered_players').html("<strong>Registered Players:</strong><br/>" + current_tournaments[data].reservation_count);
      });      
    };
    
    function renderTable(data) {
      var template = "<table>";
      template += "<tbody>";
      
      $.each(data, function(index, value) {
        css_class = index % 2 == 0 ? 'light' : 'dark';
        var new_date = this.start_date.substr(0, this.start_date.length - 3);

        template += "<tr class=\"" + css_class + "\">";
        template += "<td class=\"colName\">" + this.name + "</td>";
        template += "<td class=\"colType\">" + this.game_type + "</a></td>";
        template += "<td class=\"colBuyIn\">$" + this.buyin + "+$" + this.fee + "</td>";
        template += "<td class=\"colDate\">" + new_date + "</td>";
        template += "</tr>";          
      });
      
      template += "</tbody>";
      template += "</table>";
      
      return template;    
    }
    
    return this;
  };
    
})(jQuery);