﻿// depends: global.js

MediaPlayerController = function() {
  var pThis = this;
  $("#mediaPlayerTabNavWrapper > ul").tabs();   
  $('ul#mediaPlayerTabNav a').click(function() {    
    var anchor = this;
    pThis.tabClickHandler(anchor);
  });  
  this.emptyBufferHandler = function(flowplayer) { MediaPlayerController.prototype.emptyBufferHandler(pThis, flowplayer); } 
  this.fullBufferHandler = function() { MediaPlayerController.prototype.fullBufferHandler(pThis); }
};

MediaPlayerController.prototype = {
  webcastID : 0,
  ppName : '_',
  playingLive : false,
  livestreamURL : '_',
  streamServerURL : '_',
  isFlashPlayer : false,  
  flashArchiveSource: '_',                            
  streamInLimbo : false,  // when livestream is interrupted, but no "official" word from server
  
  // event handlers
  onWebcastEnded : function(){},
  onViewingLivecast : function(){},
  
  emptyBufferHandler : function(pThis, flowplayer) {
    pThis.streamInLimbo = true;
    setTimeout(function() { pThis.limboState(pThis, flowplayer); }, 1000); // boost this number if flash stream is intermittent
  },
  
  fullBufferHandler : function(pThis) {
    pThis.streamInLimbo = false;
  },
  
  limboState: function(pThis, flowplayer) {
    if(!pThis.streamInLimbo) return; //freedom!
    if(!pThis.isLive()) {    
      flowplayer.stop()
      pThis.streamInLimbo = false;
    }
    else setTimeout(function() { pThis.limboState(pThis, flowplayer); }, 1000);
  },
  
  tabClickHandler : function(anchor, supressPlayerChange) {
    // manually setting the CSS class
    // because of jQuery tab library
    // doesn't handle multiple groups of tabs
    // on the same page
    var curChildIndex = $(anchor).parent().prevAll().length + 1; // 1-based index
    $(anchor).parent().parent().children('.current').removeClass('current');
    $(anchor).parent().addClass('current');    
    if(!supressPlayerChange) this.changePlayer(curChildIndex);
//    return false;
  },
  
  // is there a livecast available from the current user?
  isLive : function() {
    return(this.webcastID && this.webcastID > 0);
  },
  
  setLivecast : function(webcastID) {
    if(!webcastID && this.isLive()) {
      // state change
      if(this.playingLive) this.onWebcastEnded();
    }
    this.webcastID = webcastID;
  },
  
  setInitialTab : function(player) {
    var i=0;
    if(player=='WM') i = 0;
    else if(player=='QT') i = 1;
    else if(player=='Flash') i = 2;

    // fake a tab click
    $("#mediaPlayerTabNavWrapper > ul").tabs('select', i);
    var anchor = $("a[href='#tab"+i+"']")[0];
    supressPlayerChange = true;    
    this.tabClickHandler(anchor, supressPlayerChange);
    
    if(IsMacAndNoWindowsMediaPlugin()) { // global.js
      $('a[href=#tab0]').parent().hide();
    }
  },
  
  changePlayer : function(idx) {
   switch(idx) {
    case 1: this.isFlashPlayer = false; MediaPlayerControl.changePlayer('WM'); break;
    case 2: this.isFlashPlayer = false; MediaPlayerControl.changePlayer('QT'); break;
    case 3: this.isFlashPlayer = true;
      if(this.playingLive) {
        if(this.webcastID) {
          MediaPlayerControl.showMediaPlayer('Flowplayer', this.webcastID+'.sdp');
        }
        else {
          // live webcast ended, play default
          MediaPlayerControl.showMediaPlayer('Flash', this.flashArchiveSource+'/dflt/default.flv');
        }
      }
      else MediaPlayerControl.showMediaPlayer('Flash', this.getFlashURL(this.ppName));
      break;
    }
  },
  
  getFlashURL: function(strGuid) {
    if(strGuid) return(this.flashArchiveSource + "/" + strGuid.substr(0,2) + '/' + strGuid.substr(2,2) + '/' + strGuid + '.flv');    
    return "__none";
  },
  
  playLivestream : function(seqNum) {
    if(this.isLive()) {
      this.playingLive = true;
      if(this.isFlashPlayer) {
        MediaPlayerControl.showMediaPlayer('Flowplayer', this.webcastID+'.sdp');
      }
      else {
        var URL = this.livestreamURL.replace(/<%=seqNum%>/, seqNum);
        MediaPlayerControl.play(URL);
      }
      this.onViewingLivecast();
    }
  },
  
  playArchive : function(mediaID, ppName) {
    this.ppName = ppName;
    var changeToFlashPlayer = (this.isFlashPlayer && this.playingLive);      
    if(changeToFlashPlayer || this.isFlashPlayer) {
      MediaPlayerControl.currentPlayer = 'Flash';
    }
    this.playingLive = false;
    var URL = this.streamServerURL
              .replace(/<%=mediaID%>/, mediaID)
              .replace(/<%=seqNum%>/, 0); // omit the seqNum to enable browser caching
    MediaPlayerControl.play(URL, this.getFlashURL(ppName));
  }
  
};