// BlzMediaPlayerWMP.js
//
// Bullseye is freely distributable under the terms of new BSD license.
// Copyright (c) 2006-2009, makoto_kw (makoto.kw@gmail.com) All rights reserved.

Blz.MediaPlayer.Lyra = Class.create();
Blz.MediaPlayer.Lyra.prototype = Object.extend(new Blz.MediaPlayer.Base(), {
  initialize: function() {
    this.lyra;
    this.media;
    this.usePlayer = "Lyra";
    this.version = "";
  },
  
  start: function(core) {
    try {
      this.lyra = core;
      this.media = core.createMediaElement('media');
      this.version = this.lyra.version;
    } catch(e) {
      Blz.Widget.print("create Lyra.createMediaElement : " + e);
      return false;
    } 
    return true;
  },

  dispose: function() {
    try {
      if (this.media) {
        this.media.close();
      }
    } catch(e) {
      Blz.Widget.print("dispose : " + e);
    } finally {
      this.lyra = null;
      this.media = null;
    }
  },
  
  getPlayerName: function() { return this.usePlayer; },
  getPlayerVersion: function() { return this.version; },
  
  isPlayingUserContent: function() {
    var isUserContent = false;
    try {
      isUserContent = true; // TODO
    } catch (e) {
      Blz.Widget.print("isPlayingUserContent : " + e);
    }
    return isUserContent;
  },
  
  play: function() {
    try {
      this.media.play();
    } catch (e) {
      Blz.Widget.print("play : " + e);
    }
  },
  
  playPause: function() {
    try {
      if (Lyra.playStates.playing == this.media.getPlayState())
        this.media.pause();
      else
        this.media.play();
    } catch (e) {
      Blz.Widget.print("playPause : " + e);
    }
  },
  
  playUrl: function(url) {
    try {
      this.media.open(url);
    } catch (e) {
      Blz.Widget.print("play : " + e);
    }
  },
  
  isRunning: function() {
    return (this.media) ? true : false;
  },
  
  getCurrentTrackLength: function() {
    var length = 0;
    try {
      if (this.media) {
        length = this.media.getDuration();
        length = Math.floor(length/1000);
      }
    } catch (e) {
      Blz.Widget.print("getCurrentTrackLength : " + e);
    }
    return length;
  },

  getPlayerPosition: function() {
    var pos = 0;
    try {
      if (this.media) {
        pos = this.media.getPlayPosition();
        pos = Math.floor(pos/1000);
      }
    } catch (e) {
      Blz.Widget.print("getPlayerPosition : " + e);
    }
    return pos;
  },
  
  getPlayerStatus: function() {
    var status = ePlayerState_Stopped;
    try {
      var state = this.media.getPlayState();
      switch (state)
      {
        case Lyra.playStates.acquiringLicense:
        case Lyra.playStates.buffering:
        case Lyra.playStates.opening:
        case Lyra.playStates.individualizing:
          status = ePlayerState_Opening;
          break;
        case Lyra.playStates.closed:
        case Lyra.playStates.stopped:
          status = ePlayerState_Stopped;
          break;
        case Lyra.playStates.paused:
          status = ePlayerState_Paused;
          break;
        case Lyra.playStates.playing:
          status = ePlayerState_Playing;
          break;
      }
    } catch (e) {
      Blz.Widget.print("getPlayerStatus : " + e);
    }
    return status;
  },
  
  getStreamingStatus: function() { return eStreamingState_Stopped; },
  
  hasVolume: function() { return true; },
  setVolume: function(volume) {
    try {
      if (this.media) {
        // 0-100 to 0-1
        var v = volume/100;
        this.media.setVolume(v);
      }
    } catch (e) {
      Blz.Widget.print("setVolume : " + e);
    }
  },
  
  getVolume: function() {
    var volume = 0;
    try {
      if (this.media) {
        volume = Math.floor(100 * this.media.getVolume());
      }
    } catch (e) {
      Blz.Widget.print("getVolume : " + e);
    }
    return volume;
  },
  
  hasEventNotify: function() {
    //return true;
    return false;
  }
});