/**
 * SongRouter
 * a Javascript library for routing song play requests to either an on-page
 * player object or to a popup window player.
 *
 * by Aaron Longwell
 */ 
var SongRouter = {

  popupPlayerUrl: 'popup.html',
  popupWindow: null,


  /**
   * --------------------------------------------------------------------------
   * ->> openPopup <<----------------------------------------------------------
   * --------------------------------------------------------------------------
   * Opens the popup window and sets the users preference so that songs play
   * in the popup. Does not start playing any songs in the popup... simply
   * opens it unintialized.
   */
  openPopup: function() {
    this.playInPopup("");
  },


  /**
   * --------------------------------------------------------------------------
   * ->> forgetPopup <<--------------------------------------------------------
   * --------------------------------------------------------------------------
   * Closes the popup window if it's open and resets the user's preference so
   * that future songs play in the on-page player.
   */
  forgetPopup: function() {
    $.cookie('preferPopupPlayer', null);
    if(this.popupExists()) {
      this.popupWindow.close();
    }
  },


  /**
   * -------------------------------------------------------------------------
   * ->> play <<--------------------------------------------------------------
   * -------------------------------------------------------------------------
   * The automatic song play function. Call this function for the vast
   * majority of requests to play a song.
   *
   * Sends a song to the on-page player unless the user has indicated a
   * preference for the popup player, in which case it open the popup player
   * to play the song.
   */
  play: function(song) {
    if($.cookie('preferPopupPlayer') == 1) {
      this.playInPopup(song);
    } else {
      this.playOnPage(song);
    }
  },


  /* --------------------------------------------------------------------------
   * ->> playOnPage <<---------------------------------------------------------
   * --------------------------------------------------------------------------
   * Plays the submitted song in the player that exists on this page. Also
   * updates the users preference to send future calls to SongRouter.play() to
   * the on-page player as well.
   */
  playOnPage: function(song) {
    var jbPlayer = document.getElementById("jbPlayer");
    $.cookie('preferPopupPlayer', null);
    jbPlayer.loadFlashSong(song);
  },


  /**
   * -------------------------------------------------------------------------
   * ->>> playInPopup <<<-----------------------------------------------------
   * -------------------------------------------------------------------------
   * Opens a popup window to the popup window URL, and includes a ?song=
   * paramter on the query string so that the popup player can figure out
   * which song was requested and play it.
   *
   * Calling this function updates the users preference cookie so that future
   * calls to SongRouter.play() will result in popup player plays.
   */
  playInPopup: function(song) {
    $.cookie('preferPopupPlayer', 1);
    this.popupWindow = window.open(this.popupPlayerUrl + '?song=' + song, 'SongRouterPopup', 'width=500,height=500');
    this.popupWindow.focus();
  },


  /**
   * ---------------------------------------------------------------------------
   * ->> popupExists <<---------------------------------------------------------
   * ---------------------------------------------------------------------------
   * Simple utility function that returns the state of the popup window.
   */
  popupExists: function() {
    return (typeof this.popupWindow != "undefined" && this.popupWindow != null);
  }

};
