var MOVIE_MANAGER = {
  
  players: new Array(),
  
  addPlayer: function( eID, eClass ){
    var p = new Player( MOVIE_MANAGER.players.length, eID, eClass );
    MOVIE_MANAGER.players.push(p);
    p.build();
    return p;
  }
}

var flashPlayerforAS;
function sendToJavaScript(value) {
  flashPlayerforAS.continuetoNext(value)
}

Player = function( mID, eID, eClass ) {
  
  var movieID = mID;
  flashPlayerforAS = this;
  var flashElementID = eID;//Where it gets embedded
  var flashElementClass = eClass;
  var jFlashElement;
  var jQuicktimeElement;
  
  var waitingQueue = new Array();
  
  var playing = false;
  var currentMovie = "";
  var loadOnReady = false;

  var master = this;
    
  this.continuetoNext = function (value){
    while(waitingQueue.length > 0){
      var callback = waitingQueue.pop();
      callback();
    }
  }
  this.playerReady = function (vars){
    
    jFlashElement = $("#"+vars.id);
    jFlashElement.get(0).addModelListener("STATE", "MOVIE_MANAGER.players[" + movieID + "].flashStateListener");
    jFlashElement.get(0).addControllerListener("STOP", "MOVIE_MANAGER.players[" + movieID + "].stopTest");
    
    if(loadOnReady && currentMovie != ""){
      master.loadMovie();
    }

  }
  
  this.waitForComplete = function(callback,message){
    if(callback != null && typeof(callback) == "function"){
      waitingQueue.push(callback);
    }
  }

  this.stopTest = function(){
    master.play();
  }

  this.flashStateListener = function(stateObj){
    
    currentState = stateObj.newstate; 
    previousState = stateObj.oldstate;
    if(currentState == "COMPLETED" && previousState == "PLAYING"){
      /* Do callbacks when complete */
      while(waitingQueue.length > 0){
        var callback = waitingQueue.pop();
        callback();
      }
    } else if(currentState == "PLAYING" && previousState == "BUFFERING"){
      jFlashElement.get(0).sendEvent('REDRAW');
      // Started Playing...
    }
  }
  
  this.loadMovie = function(item) {
    
    if(item) currentMovie = '/videos/'+item+'.mov';
    
    if(currentMovie != ""){
      if(jFlashElement) {
        var p = jFlashElement.get(0);
        
        p.sendEvent('LOAD', currentMovie);
        
        if(playing)master.play();
        
      }  else {
        loadOnReady = true;
      }
    }
  }
  
  this.stop = function(){
    playing = false;
    if(jFlashElement){
      jFlashElement.get(0).sendEvent('STOP');
    }
  }
  
  this.play = function(){
    playing = true;
    if(jFlashElement){
      jFlashElement.get(0).sendEvent('PLAY', 'true');
    }
  }
  
  this.pause = function(){
    playing = false;
    if(jFlashElement){
      jFlashElement.get(0).sendEvent('PLAY', 'false');
    }
  }
    
  //setup
  this.build = function() {
    
    //check for flash 9.0.115 or greater
    if(flash){
      
      var flashvars = { 
        skin:"/flash/display_skin.swf", 
        //plugins:"/flash/accessibility.swf",
        controlbar:"over",
        stretching:"uniform",
        readyfunc:"MOVIE_MANAGER.players[" + movieID + "].playerReady"
      };
      var params = { allowScriptAccess:"always", allowFullScreen:"true", wmode:"opaque", bgcolor:"#000000" };
      var attributes = {id:flashElementID, name:flashElementID};
      swfobject.embedSWF("/flash/player.swf", flashElementID, "100%", "100%", "9.0.0", null, flashvars, params, attributes);
      $("#"+flashElementID).attr("class",flashElementClass);
    } else {
      
      // What to do if we don't have flash...
      
    }
    
  }
  
  //this.build();
  
};