﻿/* audioPlayer.js */

/* see:
http://developer.longtailvideo.com/trac/wiki/FlashAPI#Addinglisteners
http://developer.longtailvideo.com/trac/wiki/FlashEvents#Viewevents
http://www.longtailvideo.com/support/forum/JavaScript-Interaction/14834/Event-listener-for-BUFFER-in-JavaScript
(for listeners, view source on http://home5.inet.tele.dk/nyboe/flash/mediaplayer4/JW_API_xmpl_4-3-4-0.html)
*/

var _player;
function playerReady(obj) {
	_player = document.getElementById(obj['id']);
    // if we want to know how much is buffered..
    // _player.addModelListener("BUFFER",   "onPlayerBufferChange");
    // _player.addModelListener("STATE",   "onPlayerStateChange");
};


var _playingNow = -1;
var _currentPlayer = null;
var _allPlayers = new Array();

function audioPlayer(id, from, to, trackNames){
  
  _allPlayers[id] = this;
  
  // init..
  this.id = id;
  this.current = from;
  this.from = from;
  this.to = to;
  this.trackNames = trackNames;
  this.playing = false;
  
  this.play = function(){ 
    if(false == this.playing || _playingNow != this.current){
        if(_playingNow != this.current){
             _player.sendEvent("ITEM", this.current);
        }else{
            _player.sendEvent("PLAY");
        };
        this.showPlaying();
    };  
    _playingNow= this.current;
    _currentPlayer = this;
    this.playing = true;
    this.resetOthers();
  };
  
  this.pause = function(){
    
    if(false == this.playing) return;
    
    _player.sendEvent("PLAY");
    _currentPlayer = this;
    this.showPlaying("");
    this.playing = false;
    
  };
  
  this.next = function(){ 
    this.current += 1;
    if(this.current > this.to) this.current = this.from;
    _player.sendEvent("ITEM", this.current);
    _currentPlayer = this;
    this.playing = true;
    this.showPlaying();
    this.resetOthers();
  };
  
  this.prev = function(){ 
    this.current -= 1;
    if(this.current < this.from) this.current = this.to;
    _player.sendEvent("ITEM", this.current);
    _currentPlayer = this;
    this.playing = true;
    this.showPlaying();
    this.resetOthers();
  };
  
  this.reset = function(){
    this.current = this.from;
    this.playing = false;
    this.showPlaying('');
  };
  
  this.resetOthers = function(){
    for(var i in _allPlayers){
        if(_allPlayers[i].id != this.id){
            _allPlayers[i].reset();
        };  
    };
  };
  
  this.buffering = function(){
    this.showPlaying('buffering....');
  };
  this.paused = function(){
    this.showPlaying('paused....');
  };
  this.playing = function(){
    this.showPlaying('playing....');
  };
  
  
  
  this.getCurrentTrack = function(){
    var currentTrackIndex = (this.from - this.current) * -1;
    return this.trackNames[currentTrackIndex];
  };
  
  this.showPlaying = function(msg){
    if(typeof(msg) == 'undefined'){
      msg = "Playing: " + this.getCurrentTrack();
    };
    if(msg == "") msg = "&#160;";
    document.getElementById('label_' + this.id).innerHTML = msg;
  };
};

function onPlayerStateChange(obj){
    /*
    e.g:
    obj.newstate BUFFERING
    obj.oldstate PLAYING
    obj.newstate PLAYING
    obj.oldstate BUFFERING
    obj.newstate PAUSED
    obj.oldstate PLAYING
    */
    log("obj.newstate", obj.newstate);
    log("_currentPlayer", _currentPlayer);
    log("TODO", "");
    
    if(null != _currentPlayer){
        switch(obj.newstate){
            case "BUFFERING":
                _currentPlayer.buffering();
                break;
            case "PAUSED":
                _currentPlayer.paused();
                break;
            case "PLAYING":
                _currentPlayer.playing();
                break;
        };
    
    };
};

function onPlayerBufferChange(obj){
    log("obj.percentage", obj.percentage);
};

function log(v1,v2){
    try{
        window.loadFirebugConsole();
        console.log(v1, v2);
    }catch(e){};
};
