///////////////////////// singleton pattern start ///////////////////////////
/**
 * This script is created for citytv video player version detection. By specify 
 * a jQuery selector and replacement message, any number of page player version
 * detection message should be displayed in the container.
 * @see #Singleton(args)
 * @author Henry Li
 * @version 0.1
 */
var FlashDetector = (function(){
  var debug = window.location.search.indexOf('debug=') > -1;
  /**
   * Logs a message to the browser's console.
   * Messages are logged if and only if:
   *  1) browser search string contains 'debug=';
   *  2) the global variable named "console" is an object.
   * Otherwise, the message is ignore.
   *
   * @param {String} msg  The message to write to the browser's console.
   */
  function log(msg) {
    if (!debug || typeof console != 'object') return;
  
    // clear console for each page refresh:
    if(msg == 'console.clear()') {
      console.clear();
      return;
    }

    // Internal helper
    function pad0(num) { return (+num < 10 ? '0' : '') + num; } 
    function pad00(num) { return (+num < 10 ? '00' : (+num < 100 ? '0' : '')) + num; } 

    var n = new Date();
    msg = pad0(n.getMinutes()) + ':' + pad0(n.getSeconds()) + ':' + pad00(n.getMilliseconds()) + '  --  ' + msg
    if(msg.indexOf('ERROR: ') > 0){
      console.error(msg);
    } else {
      console.info(msg);
    }
  }

  /**
   * @classDescription With JavaScript, singletons serve as a namespace provider
   *  which isolate implementation code from the global namespace so-as to 
   *  provide a single point of access for functions.
   * @param (args): an object containing arguments for the singleton
   */
  function Singleton(args) {
    
    
    ///////////////////////// private methods //////////////////////////
    var checkFlashVer = function(){
    	
      var playerVersion = swfobject.getFlashPlayerVersion();
      var output = "You have Flash player " + playerVersion.major + "." + playerVersion.minor + "." + playerVersion.release + " installed"; 
      log(output);    	
      return playerVersion.major > 9;
    }
    
    var run = function(conf){
      log('selector: '+conf.containerId+',\n message: '+conf.message);
      
      if(checkFlashVer()){
      	log('valid flash version found!');
      } else {
      	log('Invalid flash version found! Show error message: ');
		jQuery(conf.containerId).html(conf.message);
		jQuery(conf.containerId+' a').css({'text-decoration':'underline'});
      	if(jQuery.browser.msie){
          jQuery(conf.containerId).css({'font-size':'13px'});
      	}
      }
    }
    
    
    ///////////////////////// public properties //////////////////////////

    //set args variable to args passed or empty object if none provided.
    var args = args || {};
    //set the name parameter
    this.name = 'FlashDetector';
    
    ///////////////////////// public methods //////////////////////////
    
    /**
     * log debug message to the browser console.
     * @param (msg)
     */
    this.log = function(msg) {
      log(msg);
    }
    
    /**
     * This is a public method for getting an instance
     * @param (args)
     * @return a singleton instance of a singleton object
     * @type Singleton
     */
    this.detectFlash = function(conf) {
      run(conf);
      // debug = false; 
    }

  }
  
 //instance holder
  var instance;

 //this is an emulation of static variables and methods
  var _static = {

   /**
    * This is a method for getting an instance
    * @param (args)
    * @return a singleton instance of a singleton object
    * @type Singleton
    */
    getInstance: function (args){
      if (instance === undefined) {
        instance = new Singleton(args);
      } else {
        log('instance found!');
      }
      return instance;
    }

  };
  return _static;
})();

///////////////////////// singleton pattern ends ///////////////////////////

jQuery(function() {
  var detector = FlashDetector.getInstance();
  detector.detectFlash({
  	containerId:'div.player_area div.container div.left div.breadcrumb',
  	message:'<img src="/video/assets/img/alert_icon.png" border="0"> Latest version of Flash is required to view this content. Click <a href="http://get.adobe.com/flashplayer/" target="_blank">here</a> to upgrade your player.'
  });
});

