/**
 Flash Player Singleton
  Author: carlosg
  
  Requires that swfobject.js and mediaplayer.swf are in the scope of this 
  script. The defaults are as follows
  -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: "519",
                height: "290"
  };
  var PLAYER = "mediaplayer.swf";
  var PARENT_DIV = "video_frame";
  
  //Private Methods
  
  /**
  * Check if SWF hasn't been removed, if this is the case, create a new 
  * alternative content container.
  * Params:
  *   -elementID: String. The ID of the DOM element that will be used to embed
  *   the video.
  * Returns:
  *   -true.
  */
  function _checkElement(elementID){
    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 adds an useless parameter to the player but serves as a hack so that 
  * IExplorer actually updates the player and doesn't keep using a previous one
  * that is cached'.
  * Params:
  *   -none.
  * Returns
  *   -none.
  */ 
  function _updatePlayer(){
    ATTRIBUTES.data = PLAYER + '?rid=' + new Date().getTime();
  };
  
  /**
  * Private method that uses the target id to create the flash object that will
  * play the video. Resets the flashvars after video has been loaded
  * Params:
  *   -target: String. The id of the element that has the embedded video.
  * Returns:
  *   - true.
  */
  function _createObject(target){
    _checkElement(target);
    _updatePlayer();
    videoObj = swfobject.createSWF(ATTRIBUTES, PARAMS, target);
    PARAMS.FlashVars = "";
    return true;
  };
  
  return { // Public Access Methods
    /**
    * This method loads the video. The parameters are the target DOM element to
    * use and an url to the flv file to play.
    * Params:
    *   -target: String. The id of the element that has the embedded video.
    *   -url: String. The url of the flv video to play.
    *  Returns:
    *   - true.
    */
    loadVideo: function(target, url) {
      PARAMS.FlashVars += "&file=" + url;
      _createObject(target);
      return true;
    },
    /**
    *  Removes the video flash object. Needs the id of the DOM element that has
    *  the embedded video.
    *  Params:
    *   - target: String. The id of the element that has the embedded video.
    *  Returns:
    *   - none.
    */
    removeVideo: function(target){
      swfobject.removeSWF(target);
    },
    /**  
    *  Returns the instance of swfObject if needed to be manipulated via JS
    *  Params:
    *   - none
    *  Returns:
    *   - Object. The swfObject used to display video. If null it means no video
    *   has been instanciated.
    */
    getVideoObj: function(){
      return videoObj;
    },
    /**  
    *  This sets the parent div that contains the video div that will be used 
    *  to embed the video. The parent div is important because it's used for
    *  certain validations.
    *  Params:
    *   -elementID: string. Sets the id of the parent div.
    *  Returns:
    *   -boolean. True if parent div exists and updated. False otherwise.
    */
    setParentElement: function(elementID){
      var c = document.getElementById(elementID);
      if (!c) {
        return false;
      }
      PARENT_DIV = elementID;
      return true;
    },
    /**
    * Use this method to set the dimensions of the player.
    * Params:
    *   -width: An intenger value, can be be a string. Represents the width of 
    *   the video object.
    *   -length: An intenger value, can be be a string. Represents the length of 
    *   the video object.
    * Returns:
    *   -Object: A JS object with two members: width and length. They contain 
    *   the new values of ATTRIBUTES.
    */
    setDimentions: function(width, length){
      if (!isNaN(width)){
        ATTRIBUTES.width = String(width);
      }
      if (!isNaN(length)){
        ATTRIBUTES.length = String(length);
      }
      return {'width': ATTRIBUTES.width, 'length': ATTRIBUTES.length};
    },
    /**
    * Returns the current dimension attributes of the video object.
    * Params:
    *   -none
    * Returns:
    *   -Object: A JS object with two members: width and length. They contain 
    *   the new values of ATTRIBUTES.
    */
    getAttributes: function(){
      return {'width': ATTRIBUTES.width, 'length': ATTRIBUTES.length};
    },
    /**
    * Sets the player file path if the default will not be used
    * Params:
    *   -newPlayer: String - This has the path and filename of the player.
    * Returns:
    *   -String. The PLAYER string that will be used.
    */
    setPlayer: function(newPlayer){
      if (newPlayer && newPlayer != ''){
        PLAYER = newPlayer;
      }
      return PLAYER;
    },
    /**
    * Sets the player's FlashVars, these will be reset after the video loads.
    * Params:
    *   -flashVars: String - Contains FlashVars to be used. Can be an empty 
    *    string and this will reset the FlashVars.
    * Returns:
    *   -String. The PLAYER string that will be used.
    */
    setFlashVars: function(flashVars){
      if (flashVars) PARAMS.FlashVars = flashVars;
      return true;
    }
  };
}();