﻿/******************************************************************************/
/******************************* MediaPlayer object ******************************/
/******************************************************************************/

// interface with the media player

MediaPlayer = function(playerEmbedId, onLoadFunc) {
  this.playerId = playerEmbedId;
  this.player=$f(this.playerId);
  this.type='V';
  this.isPlaying = false;
  flowplayer(playerEmbedId, "player/flowplayer-3.1.1.swf", {
    clip: {
     // url: 'content/Interne/audio/bip.mp3',
      autoPlay: true,
      autoBuffering: true,
      scaling: "fit",
      onBegin: function () { 
        //disable replay button for this playlist item 
        this.getPlugin('play').css({opacity: 0}); 
      },
      bufferLength: 1 //to ensure onBeforeFinish is handled even for very short media (< 3003 ms)
    },
    plugins: { 
      controls: (type == 'V') ? null : {url:'player/flowplayer.controls-3.1.5.swf'},        
      content: { 
        url:'player/flowplayer.content-3.1.0.swf', 
        height:150, //40
        backgroundColor: 'transparent', 
        backgroundGradient: 'none', 
        border: 0, 
        textDecoration: 'outline',
        html: '<p></p>',
        style: {  
          body: {  
            fontSize: 14,
            fontFamily: 'Arial', 
            textAlign: 'center',
            fontWeight: 'bold'
          }
        }
      },
      audio: {
        url:'player/flowplayer.audio-3.1.2.swf' 
      }
    },
    onBeforeFinish: function() {
	  //alert('beforeFinish');
      //if(scenario.prePlay == true) {
      //  scenario.prePlay = false;
      //  scenario.buildImagePlayer();
      //  }
      if(scenario.mediaPlayer.type == 'V') {
        scenario.endElement();
      }
    },
	onError: function(errorCode, errorMessage) {
	  alert('Le player DiapMe a rencontré un erreur.\n\n' + errorCode + ': ' + errorMessage);
	},
    onLoad: function() {
	  //alert('load');
	  if (typeof(onLoadFunc) == 'function') {
		onLoadFunc();
	  }
    }
  }); // end config 
}

MediaPlayer.prototype =
{
  play: function(media) {
    $f(this.playerId).play(media);
    this.isPlaying = true;
  },
  
  stop: function() {
    $f(this.playerId).stop();
    this.isPlaying = false;
  },
  
  pause: function() {
    $f(this.playerId).pause();
    this.isPlaying = false;
  },
  
  clear: function() {
    return true; // left here for compatibility with another player
  },
  
  show: function() {
    $f(this.playerId).getScreen().show();
  },
  
  unload: function() {
    $f(this.playerId).unload();
  },
  
  getState:function() {
    return $f(this.playerId).getState();
  },
  
  getScreen:function() {
    return $f(this.playerId).getScreen();
  },
  
  unloadMedia: function() {
    this.isPlaying = false;
  },
  
  resetScreen:function() { // blackens and reset screen properties
    if ($f(this.playerId).isLoaded()) {
    $f(this.playerId).getPlugin('content').height = 90;
    $f(this.playerId).getScreen().hide();
    $f(this.playerId).getScreen().css({
        width:'100%',
        height:'100%',
        top: '50%',
        left:'50%'
      });
    //$f(this.playerId).reset('screen');
    }
  },

  isPlaying:function() {
    return this.isPlaying;
  },
  
  isPaused: function() {
    return $f(this.playerId).isPaused();
  },
  
  getPlugin: function(str) {
    return $f(this.playerId).getPlugin(str);
  },
  
  isLoaded: function() {
    return $f(this.playerId).isLoaded();
  },
  
  resume : function() {
    $f(this.playerId).resume();
	this.isPlaying = true;
  },
  
  setVolume : function(v) {
    $f(this.playerId).setVolume(v);
  },
  
  getVolume : function() {
    return ($f(this.playerId).getVolume());
  },
  
  //-- Enter Transitions 
  
  transitionEnterFromLeft : function(delay) {
    this.getScreen().css({right:'150%'});
    this.getScreen().show();
    this.getScreen().animate({left:'50%'},delay);
   },
   
   transitionEnterFromRight : function(delay) {
    this.getScreen().css({left:'150%'});
    this.getScreen().show();
    this.getScreen().animate({left:'50%'},delay);  
   },
   
   transitionEnterFromTop : function(delay) {
    this.getScreen().css({bottom:'150%'});
    this.getScreen().show();
    this.getScreen().animate({top:'50%'},delay);
   },
   
   transitionEnterFromBottom : function(delay) {
    this.getScreen().css({top:'150%'});
    this.getScreen().show();
    this.getScreen().animate({top:'50%'},delay);
   },
   
   transitionGrowFromCenter: function(delay) {
    // not working yet
    this.getScreen().css({width:'1%',height:'1%',top:'50%',left:'50%'});
    this.getScreen().show();
    this.getScreen().animate({width:'100%',height:'100%'},delay);
   },
  
  //-- Transitions sortantes
  
  transitionExitToRight : function(delay) {
    this.getScreen().animate({left:'150%'},delay);
  },
  
  transitionExitToLeft : function(delay) {
    this.getScreen().animate({right:'150%'},delay);
  },
  
  transitionExitToBottom : function(delay) {
    this.getScreen().animate({top:'150%'},delay);
  },
  
  transitionExitToTop : function(delay) {
    this.getScreen().animate({bottom:'150%'},delay);
  },
  
  transitionExitShrinkToCenter : function(delay) {
    this.getScreen().animate({width:'1%',height:'1%'},delay);
  },
  
  fadeOut : function(duration,volume) {//recursive function that fades out the sound

    if (volume==1) {
      this.pause();
      $f(this.playerId).setVolume(100);
      scenario.end();
      return true;
    }
    
    else {
      $f(this.playerId).setVolume(volume);
      setTimeout("scenario.mediaPlayer.fadeOut("+duration+","+(volume-1)+")",duration/100);
    }
  
  },
  
  resizeScreen : function(aSize) {
    if (!(this.size) || this.size!=aSize) {
    
      if(aSize == "zero") {
      
        document.getElementById(this.playerId).setAttribute("style","width:0px;height:0px;");
        document.getElementById(this.playerId).style.cssText="width:0px;height:0px;";
      
      }

      if(aSize == "small") {

        document.getElementById(this.playerId).setAttribute("style","width:320px;height:240px;");
        document.getElementById(this.playerId).style.cssText="width:320px;height:240px;";
      }
      else if(aSize == "mid") {

        document.getElementById(this.playerId).setAttribute("style","width:640px;height:480px;");
        document.getElementById(this.playerId).style.cssText="width:640px;height:480px;";
      }
      
      else if(aSize == "big") {
        document.getElementById(this.playerId).setAttribute("style","width:800px;height:600px;");
        document.getElementById(this.playerId).style.cssText="width:800px;height:600px;";
      }
      
      this.size = aSize;
      return true;
      
        
    }
  }

}
