/**
 Flash Player Singleton
  Author: carlosg
  
  Requires that swfobject.js and mediaplayer.swf are in the scope of this script.
  -htdocs/app_root/
  |-index.html
  |-mediaplayer.swf
  |--/Scripts
  |---swfobject.js
  |---customFlashPlayer.js

*/
var FlashPlayer = null;
FlashPlayer = function() {
  //Private members
//  var members = {
//  }
  var videoObj = null;
  var PARAMS = { 
                loop : "false",
                menu: "false",
                quality: "best", 
                scale: "noscale",
                allowscriptaccess:"always",
                FlashVars: ""
  };
  var ATTRIBUTES = {
                data: "",
                width: "490",
                height: "247"
  };
  var PLAYER = "mediaplayer.swf";
  var PARENT_DIV = "tvHolder";
  
  //Private Methods
  function checkElement(elementID){
    // check if SWF hasn't been removed, if this is the case, create a new alternative content container
    var c = document.getElementById(elementID);
    if (!c) {
      var d = document.createElement("div");
      d.setAttribute("id", elementID);
      document.getElementById(PARENT_DIV).appendChild(d);
    }
    return true;
  };
  
  // This is an useless parameter to the player but serves as a hack so that 
  // IExplorer actually updates the player.
  function updatePlayer(){
    ATTRIBUTES.data = PLAYER + '?rid=' + new Date().getTime();
  };
  
  function createObject(target){
    checkElement(target);
    updatePlayer();
    videoObj = swfobject.createSWF(ATTRIBUTES, PARAMS, target);
//    videoObj = swfobject.embedSWF(ATTRIBUTES.data, target, ATTRIBUTES.width, ATTRIBUTES.height, '9.0.115', false, );
    return true;
  };
  
  return { // Public Members
   loadVideo: function(target, url) {
      PARAMS.FlashVars = url;
      createObject(target);
      return true;
    },
   removeVideo: function(target){
     swfobject.removeSWF(target);
   },
   getVideoObj: function(){
     return videoObj;
   }
  };
}();