/*
 * Betfair Calendar javascript functions.
 */

var Calendar = function() {

  // Reference to betfair.com window opened from links on this site.
  var betfairWindow      = null;
  var betfairRadioWindow = null;
  var betfairTVWindow    = null;

  // Add event handler to navigate between localized versions of the site
  // when the user selects a new language from the locales drop down menu.
  var addSelectLocaleHandler = function() {
    $('form#select_locale select').change(function() {
      document.location.pathname = this.value;
    });
  };

  // Add event handler to navigate between tv listings filtered to a
  // specific channel.
  var addSelectChannelHandler = function() {
    $('form#select_channel select').change(function() {
      document.location.pathname = this.value;
    });
  };

  var addBetfairLinkHandlers = function() {
    $('a.betfair_link').click(function() {
      if (betfairWindow && !betfairWindow.closed) {
        betfairWindow.location.href = this.href;
      } else {
        betfairWindow = window.open(this.href, 'betfair_window');
      }

      if (window.focus) { betfairWindow.focus(); }
      return false;
    });
  };

  var addSearchBoxHandler = function() {
    var searchBox         = $('input#search_box'),
        searchTranslation = $('span#search_translation');

    searchBox.addClass('search_box_effect');
    searchBox.val(searchTranslation.text());

    var callback = function() {
      searchBox.val('');
      searchBox.removeClass('search_box_effect');
    };

    searchBox.select(callback);
    searchBox.click(callback);
  };

  var addPrintPageHandler = function() {
    var printPage = $('a#print_page');

    printPage.click(function() { window.print(); });
  };

  // Note that the assumption is that the grandparent element of the submit
  // button image is the form to be submitted.
  var addSubmitButtonHandlers = function() {
    $('img.submit_button').click(function() {
      $(this).parent().parent('form').submit();
    });
  };

  var addRaceMeetingHandlers = function() {
    $('a.toggle_race_meeting').click(function() {
      var raceList = $(this).next('ul.race_list');

      if (raceList.is(':visible')) {
        $(this).removeClass('close_race_meeting').addClass('open_race_meeting');
        raceList.hide();
      } else {
        $(this).removeClass('open_race_meeting').addClass('close_race_meeting');
        raceList.show();
      }

      return false;
    });
  };

  var addRadioLinkHandlers = function() {
    $('a.radio_link').click(function() {
      if (betfairRadioWindow && !betfairRadioWindow.closed) {
        betfairRadioWindow.location.href = this.href;
      } else {
        betfairRadioWindow = window.open(this.href, 'betfair_radio_window', 'height=412,width=320');
      }

      if (window.focus) { betfairRadioWindow.focus(); }

      return false;
    });
  };

  var addTVLinkHandlers = function() {
    $('a.tv_link').click(function() {
      if (betfairTVWindow && !betfairTVWindow.closed) {
        betfairTVWindow.location.href = this.href;
      } else {
        betfairTVWindow = window.open(this.href, 'betfair_tv_window');
      }

      if (window.focus) { betfairTVWindow.focus(); }

      return false;
    });
  };

  var eventTypeMenu = {
    cookieName: 'event_type_menu',

    selectors: {
      eventTypeHeaders: 'div.event_type_header',
      eventTypeLists:   'ul.event_types'
    },

    init: function() {
      this.eventTypeHeaders = $(this.selectors.eventTypeHeaders);
      this.eventTypeLists   = $(this.selectors.eventTypeLists);

      this.addHandlers();
      this.displaySelectedMenu();
    },

    addHandlers: function() {
      var that = this;

      $(this.selectors.eventTypeHeaders).click(function() {
        var eventTypes = $(this).next(that.selectors.eventTypeLists);

        if (eventTypes.not(':visible')) {
          that.eventTypeLists.hide();
          eventTypes.show();

          $.cookie(that.cookieName, eventTypes.attr('id'), {path: '/', expires: 7});
        }
      });
    },

    displaySelectedMenu: function() {
      switch ($.cookie(this.cookieName)) {
        case 'popular_event_types':
          this.eventTypeHeaders.filter(':first').click();
          break;
        case 'all_event_types':
          this.eventTypeHeaders.filter(':last').click();
          break;
      }
    }
  };

  return {

    // ------------------
    // Exposed functions:
    // ------------------

    init: function() {
      eventTypeMenu.init();
      addSelectLocaleHandler();
      addSelectChannelHandler();
      addPrintPageHandler();
      addSearchBoxHandler();
      addSubmitButtonHandlers();
      addRaceMeetingHandlers();
      addBetfairLinkHandlers();
      addRadioLinkHandlers();
      addTVLinkHandlers();
    }
  };
}();
