var jtcYTC = Class.create({
initialize: function(options){
  this.player = false;
  this.options = Object.extend({
    replace_el: 'viddiv',
    object_id: 'ytPlayer',
    video_id: null,
    width: 500,
    height: 281,
    debug: null
  }, options || {});
  this.debug('initialize()', 'creating video object for '+this.options.video_id+' at '+this.options.replace_el);
},

// This function is called when an error is thrown by the player
onYouTubePlayerError: function(errorCode){
  this.debug('onYouTubePlayerError()', 'An error occured of type: ' + errorCode);
},

// This function is called when the player changes state
onYouTubePlayerStateChange: function(newState){
  this.debug('onYouTubePlayerStateChange()', 'playerState: ' + newState);
  switch (newState){
    case -1: // unstated
    case 0: // ended
      this.loadVideo();
    case 1: // playing
    case 2: // paused
    case 3: // buffering
    case 5: // video cued
    default:
      break;
  }
},

// Display information about the current state of the player
updatePlayerInfo: function(){
  // Also check that at least one function exists since when IE unloads the
  // page, it will destroy the SWF before clearing the interval.
  if (this.player && this.player.getDuration){
    if ($('playerState')) $('playerState').update(this.player.getPlayerState());
    if ($('videoCurrentTime')) $('videoCurrentTime').update(this.player.getCurrentTime());
    if ($('videoDuration')) $('videoDuration').update(this.player.getDuration());
    if ($('bytesTotal')) $('bytesTotal').update(this.player.getVideoBytesTotal());
    if ($('startBytes')) $('startBytes').update(this.player.getVideoStartBytes());
    if ($('bytesLoaded')) $('bytesLoaded').update(this.player.getVideoBytesLoaded());
    if ($('volume')) $('volume').update(this.player.getVolume());
  }
},

getImage: function(idx){
  return 'http://img.youtube.com/vi/'+this.options.video_id+'/'+idx+'.jpg';
},

// Allow the user to set the volume from 0-100
setVideoVolume: function(volume){
  if(isNaN(volume) || volume < 0 || volume > 100){
    this.debug('setVideoVolume()', 'Please enter a valid volume between 0 and 100.');

  } else if(this.player){
    this.player.setVolume(volume);
  }
},

playVideo: function(){
  if(this.player){
    this.debug('playVideo()');
    this.player.playVideo();
  }
},

stopVideo: function(){
  if(this.player){
    this.debug('stopVideo()');
    this.player.stopVideo();
  }
},

pauseVideo: function(){
  if(this.player){
    this.debug('pauseVideo()');
    this.player.pauseVideo();
  }
},

muteVideo: function(){
  if(this.player){
    this.debug('muteVideo()');
    this.player.mute();
  }
},

unMuteVideo: function(){
  if(this.player){
    this.debug('unMuteVideo()');
    this.player.unMute();
  }
},

// This function is automatically called by the player once it loads
onYouTubePlayerReady: function(playerId){
//  if (!this.player){
    this.debug('onYouTubePlayerReady()', 'setting up player polling and event handlers');
    this.player = document.getElementById(this.options.object_id);
    if (!this.player){
      this.debug('onYouTubePlayerReady()', 'this player cannot find itself in the DOM');
      return false;
    }

    var self = this;
    setInterval(function(){self.updatePlayerInfo();}, 250);
    this.updatePlayerInfo();
    this.player.addEventListener("onStateChange", "onYouTubePlayerStateChange");
    this.player.addEventListener("onError", "onYouTubePlayerError");
    if (this.options.video_id) this.loadVideo();
//  }
},

loadVideo: function(id,autoplay){
  id = id || this.options.video_id;
  if (this.player){
    this.debug('loadVideo()', 'loading video, '+id);
    this.options.video_id = id;
    this.player.clearVideo();
    if (autoplay){
      this.player.loadVideoById(id);
    }else{
      this.player.cueVideoById(id);
    }
  }
},

loadPlayer: function(){
  this.debug('loadPlayer()', 'calling swfobject::embedSWF() for '+this.options.object_id);
  swfobject.embedSWF(
    'http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid='+this.options.object_id,
    this.options.replace_el, this.options.width, this.options.height, '8.0.0',
    null, null, {allowfullscreen:'false',allowscriptaccess:'always',wmode:'opaque'}, {id:this.options.object_id}
  );
},

debug: function(method, msg){
  msg = msg||'';
  if (this.options.debug && $(this.options.debug)){ $(this.options.debug).insert({top:'jtcYTC::'+method+': '+msg+'<br/>'}); }
}

});

