// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// When object is available, do function fn.
function when(obj, fn) {
  if (Object.isString(obj)) obj = /^[\w-]+$/.test(obj) ? $(obj) : $(document.body).down(obj);
  if (Object.isArray(obj) && !obj.length) return;
  if (obj) fn(obj);
}

function monitor_game(game_id) {
  new TipControl($(game_id).down('td.home'));
  new TipControl($(game_id).down('td.away'));
}

function monitor_table() {
  when('table.tips', function(table){
    $$('table.tips td.home').each(function(team){
      new TipControl(team);
    });
    $$('table.tips td.away').each(function(team){
      new TipControl(team);
    });
  });
}

document.observe('dom:loaded', function() {

  monitor_table();

  when('ul.rounds', function(list){
    new RoundControl(list);
  });

  when('game_home_team_id', function(element){
    new HomeTeamSelectControl(element);
  });

  when('div#flash-notice', function(element){
    new FlashControl(element);
  });
  when('div#flash-error', function(element){
    new FlashControl(element);
  });
  when('div#flash-warning', function(element){
    new FlashControl(element);
  });

});

var RoundControl = Class.create({
  initialize: function( element ) {
    if (Prototype.Browser.IE)
      $(element).
        observe('mouseover', this.onMouseOverRound.bindAsEventListener(this, 'addClassName')).
        observe('mouseout', this.onMouseOverRound.bindAsEventListener(this, 'removeClassName'));

    $(element).observe('click', this.onMouseClickRound.bindAsEventListener(this));
  },
  onMouseOverRound: function(event, method) {
    var event_round = event.findElement('li');
    if (event_round) event_round[method]('highlight');
  },
  onMouseClickRound: function(event) {
    var event_round = event.findElement('li');
    if (event_round) {
      var class_names = $(event_round).down('a').classNames();
      if( !class_names.include('new') && !class_names.include('all') ) {
        new Ajax.Request($(event_round).down('a').href, {
          onSuccess: function(transport){
            $$('ul.rounds li').each(function(list_item){$(list_item).removeClassName('selected')});
            $(event_round).addClassName('selected');
          },
          onFailure: function(transport){
            var growl = new Growl.Smoke;
            growl.show({
              title:  'Failure',
              text:   'We could not load the round for you.  Please try again.',
              image: '/images/football-icon.png',
              autohide: 2,
              animated: 0.75,
              opacity: 0.8
            });
          },
          onLoading: function(transport){
            var growl = new Growl.Smoke;
            growl.show({
              title:  'Loading',
              text:   'Loading ' + $(event_round).down('a').getAttribute('name') + '.  Please wait.',
              image: '/images/football-icon.png',
              autohide: 2,
              animated: 0.75,
              opacity: 0.8
            });
          }
        });
        event.stop();
      }
    }
  }
});

var FlashControl = Class.create({
  initialize: function( element ) {
    $(element).insert({bottom: new Element('span', {'id': 'close'})});
    $(element).down('span#close').observe('click', this.onClickClose.bindAsEventListener(this));
  },

  onClickClose: function( event ) {
    var element = event.findElement('div');
    if(element) {
      element.remove();
    }
  }
});


var TipControl = Class.create({
  initialize: function( element ) {
    if (Prototype.Browser.IE)
      $(element).
        observe('mouseover', this.onMouseOverTeam.bindAsEventListener(this, 'addClassName')).
        observe('mouseout', this.onMouseOverTeam.bindAsEventListener(this, 'removeClassName'));

    $(element).observe('click', this.onMouseClickTeam.bindAsEventListener(this));
  },
  onMouseOverTeam: function(event, method) {
    var team = event.findElement('td');
    if (team) team[method]('highlight');
  },
  onMouseClickTeam: function(event) {
    var team = event.findElement('td');
    if (team) {
      if ( !$(team).classNames().include('tip')){
        new Ajax.Request($(team).down('span.name a').href, {
          onSuccess: function(transport){
            var growl = new Growl.Smoke;
            growl.show({
              title:  'Tip Saved',
              text:   'Your tip has been saved.',
              image:  $(team).down('span.logo a img').src,
              autohide: 2,
              animated: 0.75,
              opacity: 0.8
            });
          },
          onFailure: function(transport){
            var growl = new Growl.Smoke;
            growl.show({
              title:  'Failure',
              text:   'We could not save you tip.  Please try again',
              image:  $(team).down('span.logo a img').src,
              autohide: 2,
              animated: 0.75,
              opacity: 0.8
            });
          },
          onLoading: function(transport){
            var growl = new Growl.Smoke;
            growl.show({
              title:  'Saving',
              text:   'We are saving your tip. Please Wait.',
              image:  $(team).down('span.logo a img').src,
              autohide: 2,
              animated: 0.75,
              opacity: 0.8
            });
          }
        });
      }
      event.stop();
    }
  }
});

var HomeTeamSelectControl = Class.create({
  initialize: function(element) {
    this.current_value = $(element).value;
    this.element_id = $(element).id;
    if(this.game_venue() == ""){
      this.change_venue(this.venue_for_value(this.current_value));
    }
    $(element).observe('change', this.onTeamChange.bindAsEventListener(this));
  },

  onTeamChange: function(event) {
    var select = event.findElement('select');
    if( select.value != this.current_value ){
      var game_venue = this.game_venue();
      if( game_venue == this.venue_for_value(this.current_value) )
        this.change_venue(this.venue_for_value(select.value));
      this.current_value = select.value;
    }
  },

  venue_for_value: function(value) {
    option = $(this.element_id).down('option[value=' + value + ']');
    if (option) {
      return option.getAttribute('venue');
    } else {
      return "";
    }
  },

  change_venue: function(value) {
    $('game_venue').value = value;
  },

  game_venue: function() {
    return $('game_venue').value;
  }

});
