/**
 * Defines a media player component for Colibri.
 * This component handles default flash streaming, 
 * flumotion streaming, highwinds streaming and 
 * Windows Media local/streaming.
 * 
 * @version 2.0
 * @package Colibri.player
 * @author  Momindum
 */

if(Colibri)(function(Colibri){
    
Colibri.player = function(options) {
	
	if(options === undefined)
	  return;
	
	player = null;
	if(options.playerOptions.vType == "wmv")
	  player = new Colibri.player.wmv(options);
	else if(options.playerOptions.vType == "flash")
    player = new Colibri.player.flash(options);
  else if(options.playerOptions.vType == "flumotion")
    player = new Colibri.player.flumotion(options);
  else if(options.playerOptions.vType == "highwinds")
    player = new Colibri.player.highwinds(options);
	
	return player;
};

/**
 * Defines base player prototype
 */
$.extend(Colibri.player, {
  isPlaying: false,
  isMute: false,
  init: function(options) { },
  setTimecode: function(timecode) {	},
  sync: function(timecode) { },
  mute: function () {},
  
  /**
   * Gets or sets audio volume.
   * @param v A positive integer in range of 0-100 (Optionnal)
   * @return Return the value of the volume
   */
  volume: function (v) {},
  
  /**
   * Toggles fullscreen mode.
   */
  fullScreen: function () {},
  
  /**
   * Show the player.
   * Put display logic here.
   */
  show: function() {}
  
});

/**
 * Defines common flash player prototype
 */
Colibri.player.flash = function(options) {
  return this.init(options);
}
Colibri.player.flash.prototype = new Colibri.player;
jQuery.extend(Colibri.player.flash.prototype, {
  
  init: function (options) {
    
    if(options === undefined)
      return;
  
    isPlaying = true;
   
    $('#media').media({
      width: options.playerOptions.vWidth,
      height:options.playerOptions.vHeight,
       autoplay:  true,
       name: 'mediaPlayer',
       id:   'mediaPlayer',
       src:  options.playerOptions.vRelativeUrl + '/colibri/assets/mediaPlayer.swf',
       attrs:     { id: 'mediaPlayer', name: 'mediaPlayer', allowfullscreen: 'true'},  // object/embed attrs 
       params:    { id: 'mediaPlayer', name: 'mediaPlayer', allowfullscreen: 'true'}, // object params/embed attrs 
       flashvars: { videoURL : options.playerOptions.vURL, autoPlay: true, language: 'fr', timecode: theColibriOptions.initialTimecode}
    });
 },
 
 setTimecode: function (timecode) {
   this.getFlashMovie('mediaPlayer').setCurrentTime(Math.floor(timecode / 1000));
   this.getFlashMovie('mediaPlayer').doPlay();
 },

 playPause: function () {
   if(this.isPlaying)
     this.getFlashMovie('mediaPlayer').doPause();
   else
     this.getFlashMovie('mediaPlayer').doPlay();
 },
 
 getFlashMovie: function(movieName) {
   var isIE = navigator.appName.indexOf("Microsoft") != -1;
   return (isIE) ? window[movieName] : document[movieName];
 },
 
 sync: function(timecode) { },
 mute: function () {},
 volume: function (v) {},
 fullScreen: function () {},
 show: function() {}
 
});

/**
 * Defines flumotion flash player prorotype
 */
Colibri.player.flumotion = function(options) {
  return this.init(options);
}
Colibri.player.flumotion.prototype = new Colibri.player.flash;
//jQuery.extend(Colibri.player.flumotion.prototype, {
  //no differences between common flash player and flumotion player
//});


/**
 * Defines highwinds flash player prorotype
 */
Colibri.player.highwinds = function(options) {
  return this.init(options);
}
Colibri.player.highwinds.prototype = new Colibri.player.flash;
jQuery.extend(Colibri.player.highwinds.prototype, {
  
  /**
   * Override parent constructor to load a different swf
   */
  init: function (options) {

    if(options === undefined)
      return;
  
    isPlaying = true;
  
    $('#media').media({
      width: options.playerOptions.vWidth,
      height:options.playerOptions.vHeight,
      autoplay:  true,
      name: 'mediaPlayer',
      id:   'mediaPlayer',
      src:  options.playerOptions.vRelativeUrl + '/colibri/assets/mediaPlayer-highwinds.swf',
      attrs:     { id: 'mediaPlayer', name: 'mediaPlayer', allowfullscreen: 'true'},  // object/embed attrs 
      params:    { id: 'mediaPlayer', name: 'mediaPlayer', allowfullscreen: 'true'}, // object params/embed attrs 
      flashvars: { videoURL : options.playerOptions.vURL, autoPlay: true, language: 'fr', timecode: theColibriOptions.initialTimecode}
    });
  },
  setTimecode: function (timecode) {
    this.getFlashMovie('mediaPlayer').setCurrentTime(Math.floor(timecode / 1000));
    this.getFlashMovie('mediaPlayer').doPlay();
  },

  playPause: function () {
    if(this.isPlaying)
      this.getFlashMovie('mediaPlayer').doPause();
    else
      this.getFlashMovie('mediaPlayer').doPlay();
  },
  
  getFlashMovie: function(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
  },
  sync: function(timecode) { },
  mute: function () {},
  volume: function (v) {},
  fullScreen: function () {},
  show: function() {}
});

/**
 * Defines Windows Media player prototype
 */
Colibri.player.wmv = function(options) {
  return this.init(options);
}
Colibri.player.wmv.prototype = new Colibri.player;
jQuery.extend(Colibri.player.wmv.prototype, {
  
  init: function (options) {
  
    if(options === undefined)
      return;
  
    this.isPlaying = true;
    
    $('#media').media({
        width: options.playerOptions.vWidth,
        height:options.playerOptions.vHeight-42,
        autoplay:  this.isPlaying,
        src:       options.playerOptions.vURL,
        attrs:     { showcontrols: false, name: 'mediaPlayer', id: 'mediaPlayer'},  // object/embed attrs 
        params:    { uimode: 'none', showcontrols: false, name: 'mediaPlayer', id: 'mediaPlayer'} // object params/embed attrs  
    });
    
    //wait the DOM document is ready before replacing controls
    $(this.show());
    //eventual seek an initial timecode
    if(theColibriOptions.initialTimecode && theColibriOptions.initialTimecode != 0)
    {
      setTimeout('theColibri.components.player.setTimecode(theColibriOptions.initialTimecode)', 1000)
    }
  },
  
  setTimecode: function (timecode) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var objMovie = (isIE) ? window['mediaPlayer'] : document['mediaPlayer'];
    objMovie.controls.currentPosition = Math.floor(timecode / 1000);
    objMovie.controls.play();
  
  },
  
  sync: function (timecode) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var objMovie = (isIE) ? window['mediaPlayer'] : document['mediaPlayer'];
    if(objMovie.currentMedia) {
      theColibri.vDuration = objMovie.currentMedia.duration;
      theColibri.vPosition = objMovie.controls.currentPosition; 
    }
  
  },
  
  playPause: function () {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var objMovie = (isIE) ? window['mediaPlayer'] : document['mediaPlayer'];
    if(this.isPlaying == false) {
      objMovie.controls.play();
    } else {
      objMovie.controls.pause();
    }
    
    this.isPlaying = !this.isPlaying;
  },
  
  
  mute: function () {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var objMovie = (isIE) ? window['mediaPlayer'] : document['mediaPlayer'];
    objMovie.settings.mute=!objMovie.settings.mute;
    this.isMute = objMovie.settings.mute;
  },
  
  /**
   * Gets or sets audio volume.
   * @param v A positive integer in range of 0-100 (Optionnal)
   * @return Return the value of the volume
   */
  volume: function (v) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var objMovie = (isIE) ? window['mediaPlayer'] : document['mediaPlayer'];
    
    if(objMovie.settings !== undefined) {
      //a value has been specified in parameters, set volume
      if(v !== undefined)
        objMovie.settings.volume=v;
      
      return objMovie.settings.volume;
    } else {
      return 100;
    }
      
  },
  
  /**
   * Toggles fullscreen mode.
   */
  fullScreen: function () {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var objMovie = (isIE) ? window['mediaPlayer'] : document['mediaPlayer'];
    objMovie.fullScreen=!objMovie.fullScreen;
  },
  
  /**
   * Show the player.
   * Put display logic here.
   */
  show: function() {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    var objMovie = (isIE) ? window['mediaPlayer'] : document['mediaPlayer'];
    objMovie.uiMode="none";
    $('#mediaControls').show();
  }
});

})(Colibri);

//--------------------------------------------------------------------

/**
 * Flash to Javascript functions
 */
function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
}/*
function isPlaying(isPlaying) {
  theColibri.isPlaying = isPlaying;
}*/
function getDuration(duration) {
  theColibri.vDuration = duration;
}
function getCurrentTime(currentTime) {
  theColibri.vPosition = currentTime;
}
function setCurrentTime(time) {
  getFlashMovie('mediaPlayer').setCurrentTime(time);
}
function playPause(Play) {
  Play ? getFlashMovie('mediaPlayer').doPlay() : getFlashMovie('mediaPlayer').doPause();
}
