var TwinFormatFlashVideoPlayer = Class.create(/* 
    BaseVideoPlayer
  */ {
    'extensions': {
      'moviestar' : '.h264.flv',
      'flash 8'   : '.vp6.flv',
      'download'  : '.mp4'
    },
    'skins': {
      'moviestar' : 'skin_as_3.swf',
      'flash 8'   : 'skin_as_2.swf'
    },
    'players': {
      'moviestar' : 'as3.swf',
      'flash 8'   : 'as2.swf'
    },
    'versions': {
      'moviestar' : [9, 0, 97],
      'flash 8'   : [8, 0, 0]
    },
    'defaults': {
      'background_colour': '#ffffff',
      'container': 'player',
      'id': 'flash-video-player',        
      'height': 309,
      'width': 549,
      'player_path': '/++resource++base/swf/flash_video_player',
      'video_path': '/++resource++base/videos',
      'skin_path': '/++resource++base/swf/flash_video_player_skin',
      'skin_colour': false,
      'skin_external': false,
      'skin_autohide': true,
      'fullscreen_skin' : false,
      'round_corners': false,
      'switch_version': false,
      'first_play_video_name' : '',
      'loop_video' : false,
      'rewind_video'  : false
    },
    '_get_flash_capabilities': function () { log('getting flash capabilities');
      var versions = $H(this.versions);
      var supported_version = deconcept.SWFObjectUtil.getPlayerVersion();
      return versions.keys().find(
        function ($key) {
          var candidate_version = versions.get($key);
          var major = candidate_version[0];
          var minor = candidate_version[1];
          var rev   = candidate_version[2];
          if ( supported_version.major > major
               || ( supported_version.major == major && supported_version.minor > minor )
               || ( supported_version.major == major && supported_version.minor == minor && supported_version.rev >= rev ) ) {
            return true;
          }
          else {
            return false;
          }
        }
      );
    },
    '_render_player': function () {
      var so = new SWFObject(
        this.player, 
        this.id, 
        this.options.width, 
        this.options.height,
        this.versions[
          this.version_name
        ].join('.'),
        this.options.background_colour
      );

      so.addParam('swliveconnect', 'true');
      so.addParam('allowScriptAccess', 'always');
      so.addParam('allowfullscreen', 'true');
      so.addParam('quality', 'autohigh');
      so.addParam('wmode', 'transparent');
      so.addVariable('player_id', this.id);
      so.write(this.options.container);
    },
    '_render_fallback': function () {
      var container = $(this.options.container);
      container.rehide('.hide-to-begin-with');
      var fallback_container = container.select('.fallback').first();
      fallback_container.show();
    },
    '_play_video': function ($video_name) {
      this.flash.play_new_video(
        this.options.video_path.append_trailing_slash() + 
        $video_name.append_trailing_slash() +
        $video_name + this.extension
      );
    },
    'handle_flash_loaded': function (event) { 
      if (event.memo.id == this.id) {
        log('flash:loaded');
        this.flash = $(this.id);
        // set the skin
        if( this.flash.change_skin )
          this.flash.change_skin(this.skin);
        // set the skin colour

        if ( this.options.skin_colour && this.flash.skin_colour )
          this.flash.skin_colour(this.options.skin_colour);

        // skin autohide
        if( this.flash.skin_autohide )
          this.flash.skin_autohide(this.options.skin_autohide);
        // rounded corners?
        if( this.flash.rounded_corners )
          this.flash.rounded_corners(this.options.round_corners);

        if (this.options.first_play_video_name && this.flash.play_new_video ) {
          this._play_video(this.options.first_play_video_name);
          //this._play_video('organic_ice');
        }

        if( this.options.loop_video && this.flash.loop_video )
          this.flash.loop_video(this.options.loop_video);
          
        if( this.options.rewind_video && this.flash.rewind_video )
          this.flash.rewind_video(this.options.rewind_video);
         
        if( this.options.fullscreen_skin && this.flash.add_fullscreen_listener )
          this.flash.add_fullscreen_listener("");

      }
    },
    'initialize' : function($id){
      var defaults = this.defaults;
      // grab user's options
      this.options  = Object.extend(
        defaults, 
        arguments[1] || {}
      );
      
      if( typeof($id) != "string" && typeof($id) != "number" )
        this.id = this.options.id;
      else
        this.id = $id;

      if (this.options.switch_version) {
        // change version used
        this.versions = {
          'flash 8' : [9, 0, 97],
          'moviestar'   : [8, 0, 0]
        };
      }
      if (this.options.skin_external) {
        if( typeof(this.options.skin_external) != 'number' )
          this.options.skin_external = 46;

        this.options.height += this.options.skin_external;
      }
      this.version_name = this._get_flash_capabilities();
      if (!this.version_name) { 
        this._render_fallback();
      }
      else {
        this.player = this.options.player_path.append_trailing_slash() + this.players[this.version_name];
        this.skin = this.options.skin_path.append_trailing_slash() + this.skins[this.version_name];
        this.extension = this.extensions[this.version_name];
        if (this.options.skin_external) {
          this.options.skin_autohide = false;
        }
        observe('flash:loaded', this.handle_flash_loaded.bindAsEventListener(this));
        this._render_player();
      }
    }
  }
);