/*
   pad.ma player.js
    >requries: jquery.js

   Player prototype, using HTML5 Video, liboggplay or VLC

   functions:
    var element = createVideoElement(id, url, still_url, width, height);
    $(document).append(video);
    player.play();
    player.pause();
    player.remove();
*/

//  ----------------------------------------------------------------------------
//  load Player
//  ----------------------------------------------------------------------------
var playerID='player';

$(document).ready(function() {
  selectedPlayer = detectPlayer();

});

$(window).unload(function () { 
    $('video').each(function() { 
      if(this.pause)
        this.pause();
    });
} );

function createVideoElement(id, url, still_url, width, height) {
  if (selectedPlayer=='video') {  
    player = new VideoPlayer();
  } else if (selectedPlayer=='oggplay') {
    player = new OggPlayPlayer();
  } else if (selectedPlayer=='totem') {
    player = new TotemPlayer();
  } else if (selectedPlayer=='vlc') {
    player = new VLCPlayer();
  } else if (selectedPlayer=='cortado') {
    player = new CortadoPlayer();
  } else if (selectedPlayer=='msie_cortado') {
    player = new MSCortadoPlayer();
  } else if (selectedPlayer=='ogg') {
    player = new OggPlayer();
  } else {
    player = new Player();
  }
  element = player.init(id, url, still_url, width, height);
  return element;
}

//  ----------------------------------------------------------------------------
//  Player prototype
//  ----------------------------------------------------------------------------
function Player() {
  this.supportsOverlay = false;
  this.muted = false;
}
Player.prototype.init = function(id, url, still_url, width, height) {
  this.player = $('<div>You need a Browser with <a href="http://www.whatwg.org/specs/web-apps/current-work/#video">Video Tag</a> or <a href="http://java.com/download">Java</a> support.</div>');
  this.player.css('width', width+'px');
  this.player.css('height', height/2);
  this.player.css('text-align', 'center');
  this.player.css('padding-top', height/2);
  return this.player;
}
Player.prototype.play = function() { }
Player.prototype.pause = function() { }
Player.prototype.remove = function() {
  this.pause();
  $(player.player).remove();
}

//  ----------------------------------------------------------------------------
//  <video> HTML5 Player 
//  ----------------------------------------------------------------------------
function VideoPlayer() {
  this.supportsOverlay = true;
}
VideoPlayer.prototype = new Player();
VideoPlayer.prototype.init = function(id, url, still_url, width, height) {
  this.link = url;
  this.player = document.createElement('video');
  this.player.setAttribute('src', url);
  this.player.setAttribute('autoplay', true);
  this.player.setAttribute('controls', true);
  this.player.id = id;
  this.player.width = width;
  this.player.height = height;
  setTimeout("player.play();", 500);
  return this.player;
}
VideoPlayer.prototype.set = function(pos) {
  this.pause();
  this.player.src = this.url(pos);
  this.play();
}
VideoPlayer.prototype.get = function() {
  try {
    return parseInt(this.player.currentTime * 1000);
  } catch(err) { }
  return -1;
}
VideoPlayer.prototype.play = function() {
  this.player.play();
}
VideoPlayer.prototype.pause = function() {
  this.player.pause();
}
VideoPlayer.prototype.mute = function(pos) {
  this.player.muted = true;
  this.muted = true;
}
VideoPlayer.prototype.unmute = function(pos) {
  this.player.muted = false;
  this.muted = false;
}

//  ----------------------------------------------------------------------------
//  VLC Player 
//  ----------------------------------------------------------------------------
function VLCPlayer() {
}
VLCPlayer.prototype = new Player();
VLCPlayer.prototype.init = function(id, url, still_url, width, height) {
  this.player = document.createElement('object');
  this.player.type = 'application/x-vlc-plugin';
  this.player.setAttribute('target', url);
  this.player.setAttribute('version', 'VideoLAN.VLCPlugin.2');
  this.player.setAttribute('autoplay', 'true');
  this.player.id = id;
  this.player.width = width;
  this.player.height = height;
  this.link = url;
  setTimeout("player.play();", 500);
  setTimeout("player.init_unload();", 500);
  return this.player;
}
VLCPlayer.prototype.init_unload = function() {
  $(window).unload(function() {
    player.player.playlist.stop();
  });
}
VLCPlayer.prototype.set = function(pos) {
  try {
    var mrl = this.player.playlist.add(this.url(pos));
    //this.player.playlist.playItem(mrl);
    //this.player.playlist.play(mrl);
  } catch(err) { }
}
VLCPlayer.prototype.get = function() {
  try {
    pos = this.player.input.time;
    if(pos > 0)
      return pos;
  } catch(err) { }
  return -1;
}
VLCPlayer.prototype.pause = function() {
  this.player.playlist.stop();
}
VLCPlayer.prototype.play = function() {
  this.player.playlist.play();
}
VLCPlayer.prototype.mute = function(pos) {
  if(!this.muted) {
    this.player.audio.toggleMute();
    this.muted = true;
  }
}
VLCPlayer.prototype.unmute = function(pos) {
  if(this.muted) {
    this.player.audio.toggleMute();
    this.muted = false;
  }
}

//  ----------------------------------------------------------------------------
//  OggPlay Player 
//  ----------------------------------------------------------------------------
function OggPlayPlayer() {
  this.volume = 1;
}
OggPlayPlayer.prototype = new Player();
OggPlayPlayer.prototype.init = function(id, url, still_url, width, height) {
  this.player = document.createElement('object');
  this.player.type = 'application/liboggplay';
  this.player.setAttribute('src', '');
  this.player.id = id;
  this.player.width = width;
  this.player.height = height;
  this.link = url;
  setTimeout("player.set(0);", 500);
  return this.player;
}
OggPlayPlayer.prototype.set = function(pos) {
  var url=this.url(pos);
  this.player.setCurrentMovie(url);
}
OggPlayPlayer.prototype.get = function() {
  return parseInt(this.player.getPlayPosition());
}
OggPlayPlayer.prototype.play = function() {
  this.player.play();
}
OggPlayPlayer.prototype.pause = function() {
  this.player.pause();
}
OggPlayPlayer.prototype.mute = function(pos) {
  if(!this.muted) {
    this.volume = this.player.getVolume();
    this.player.setVolume(0);
    this.muted = true;
  }
}
OggPlayPlayer.prototype.unmute = function(pos) {
  if(this.muted) {
    this.player.setVolume(this.volume);
    this.muted = false;
  }
}

//  ----------------------------------------------------------------------------
//  Totem Player 
//  ----------------------------------------------------------------------------
function TotemPlayer() {
  this.volume = 1;
}
TotemPlayer.prototype = new Player();
TotemPlayer.prototype.init = function(id, url, still_url, width, height) {
  var autoplay = 'true';
  this.player = document.createElement('embed');
  this.player.type = 'video/ogg';
  this.player.id = id;
  this.player.width = width;
  this.player.height = height;
  this.player.setAttribute('src', url);
  this.player.setAttribute('controller', false);
  this.player.setAttribute('autoplay', autoplay);
  return this.player;
}
TotemPlayer.prototype.play = function() {
  this.isPlaying = true;
  this.player.Play();
}
TotemPlayer.prototype.pause = function() {
  this.isPlaying = false;
  this.player.Stop();
}

//  ----------------------------------------------------------------------------
// Generic Ogg Player
//  ----------------------------------------------------------------------------
function OggPlayer() {
  this.volume = 1;
}
OggPlayer.prototype = new Player();
OggPlayer.prototype.init = function(id, url, still_url, width, height) {
  this.link = url;
  this.still_link = still_url;
  var autoplay = 'true';

  this.player = document.createElement('object');
  this.player.type = 'video/ogg';
  this.player.id = id;
  this.player.width = width;
  this.player.height = height;
  params = {
    'src': url,
    'AutoPlay': autoplay,
    'SCALE': 'Aspect',
    'controller': 'false'
  }
  for(name in params){
    var p = document.createElement('param');
    p.name = name;
    p.value = params[name];
    this.player.appendChild(p);
  }
  return this.player
}
OggPlayer.prototype.play = function() {
  this.player.Play();
}
OggPlayer.prototype.pause = function() {
  this.player.Stop();
}

//  ----------------------------------------------------------------------------
//  Cortado Player 
//  ----------------------------------------------------------------------------
function CortadoPlayer() {
  this.volume = 1;
  this.CortadoLocation="/static/cortado.jar"
}
CortadoPlayer.prototype = new Player();
CortadoPlayer.prototype.init = function(id, url, still_url, width, height) {
  this.link = url;
  this.still_link = still_url;
  var autoplay = 'true';

  this.player = document.createElement('object');
  this.player.setAttribute('classid', 'java:com.fluendo.player.Cortado.class');
  this.player.type = 'application/x-java-applet';
  this.player.setAttribute('archive', this.CortadoLocation);
  this.player.id = id;
  this.player.width = width;
  this.player.height = height;
  var params = {
    'code': 'com.fluendo.player.Cortado',
    'archive': this.CortadoLocation,
    'url': url,
    'local': 'false',
    'keepAspect': 'true',
    'video': 'true',
    'audio': 'true',
    'seekable': 'auto',
    'showStatus': 'hide',
    'autoPlay': autoplay,
    'bufferSize': '200'
  }
  for(name in params){
    var p = document.createElement('param');
    p.name = name;
    p.value = params[name];
    this.player.appendChild(p);
  }
  return this.player;
}
CortadoPlayer.prototype.play = function() {
  this.player.doPlay();
}
CortadoPlayer.prototype.pause = function() {
  try {
    this.player.doStop();
  } catch(err) { }
}


//  ----------------------------------------------------------------------------
//  MSIE Cortado Player 
//  ----------------------------------------------------------------------------
function MSCortadoPlayer() {
  this.volume = 1;
  this.CortadoLocation="http://10.14.2.11/cortado.jar"
}
MSCortadoPlayer.prototype = new CortadoPlayer();
MSCortadoPlayer.prototype.init = function(id, url, still_url, width, height) {
  this.link = url;
  this.still_link = still_url;
  var autoplay = 'true';

  var element = document.createElement('div');
  var obj_html = '' +
  '<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" '+
  '  codebase="http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab" '+
  '  id="' + id + '" '+
  '  width="' + width + '" height="' + height + '">'+
  ' <param name="code" value="com.fluendo.player.Cortado" />'+
  ' <param name="archive" value="' + this.CortadoLocation + '" />'+
  ' <param name="url" value="' + url + '" /> '+
  ' <param name="local" value="false" /> '+
  ' <param name="keepAspect" value="false" /> '+
  ' <param name="video" value="false" /> '+
  ' <param name="audio" value="false" /> '+
  ' <param name="seekable" value="auto" /> '+
  ' <param name="showStatus" value="hide" /> '+
  ' <param name="bufferSize" value="200" /> '+
  ' <param name="autoPlay" value="' + autoplay + '" /> '+
  ' <strong>Your browser does not have a Java Plug-in. <a href="http://java.com/download">Get the latest Java Plug-in here</a>.</strong>' +
  '</object>';
  $(element).html(obj_html);
  this.player = element.firstChild;
  return this.player;
}

//  ----------------------------------------------------------------------------
//  Player detection Functions
//  ----------------------------------------------------------------------------
function supportedMimeType(mimetype) {
  for (var i = navigator.plugins.length; i-- > 0; ) {
    var plugin = navigator.plugins[i];
    if (typeof plugin[mimetype] != "undefined")
      return true;
  }
  return false;
}

function detectVLC() {
  var mimetype="application/x-vlc-plugin";
  var vlc = false;
  var totem = false;
  for (var i = navigator.plugins.length; i-- > 0; ) {
    var plugin = navigator.plugins[i];
    if (typeof plugin[mimetype] != "undefined") {
      if(plugin.name.toLowerCase().match('totem')) {
        totem = true;
      } else {
       vlc = true;
      }
    }
  }
  if(totem) vlc=false;
  return vlc;
}

function detectTotem() {
  var mimetype="video/ogg";
  for (var i = navigator.plugins.length; i-- > 0; ) {
    var plugin = navigator.plugins[i];
    if (typeof plugin[mimetype] != "undefined") {
      if(plugin.name.toLowerCase().match('totem')) {
        return true;
      }
    }
  }
  return false;
}

function detectPlayer() {
  var selectedPlayer = '';
  //native video tag support
  var video = document.createElement('video');
  if (video.canPlayType && video.canPlayType('video/ogg;codecs="theora,vorbis"') == 'probably') {
   selectedPlayer = 'video';
  }

  //plugins, liboggplay or vlc
  if(!selectedPlayer) {
    if ($.browser.msie) {
      selectedPlayer = 'msie_cortado';
    } else if(supportedMimeType('application/x-java-applet') || navigator.javaEnabled()) {
      selectedPlayer = 'cortado';
    } else if (detectTotem()) {
      selectedPlayer = 'totem';
    } else if (detectVLC()) {
      selectedPlayer = 'vlc';
    } else if (supportedMimeType('video/ogg')) {
      selectedPlayer = 'ogg';
    }
  }
  return selectedPlayer;
}

function strpad(str, pad, len, dir) {
  while (str.length < len) {
  if (dir == 'left')
    str = pad + str;
  else if (dir == 'right')
    str = str + pad;
  }
  return str;
}

function pos2npt(pos) {
  var s, ss, mm, ms, hh, npt;
  s = parseInt(pos / 1000)
  ms = pos - s * 1000;
  ss = s % 60;
  mm = ((s - ss) / 60) % 60;
  hh = ((s - (mm * 60) - ss) / 3600) % 60;
  npt = hh+':'+strpad(mm.toString(), '0', 2, 'left');
  npt += ':'+strpad(ss.toString(), '0', 2, 'left');
  npt += '.'+strpad(ms.toString(), '0', 3, 'left');
  return npt;
}

function npt2pos(npt) {
  var ms = 0.0
  var p = npt.split(':')
  for(i=0;i<p.length;i++)
    ms = ms * 60 + parseFloat(p[i])
  return ms * 1000;
}
