  // YouTube JavaScript Player With Playlist
  // http://911-need-code-help.blogspot.com/2009/10/youtube-javascript-player-with-playlist.html
  // Revision 1 [2009-10-12]
  //
  // Prerequisites
  // 1) Create following elements in your HTML:
  // -- a) ytplayer: a named anchor
  // -- b) ytplayer_div1: placeholder div for YouTube JavaScript Player
  // -- c) ytplayer_div2: container div for playlist
  // 2) Include SWFObject library from http://code.google.com/p/swfobject/
  //
  // Variables
  // -- ytplayer_playlist: an array containing YouTube Video IDs
  // -- ytplayer_playitem: index of the video to be played at any given time
  //
  // Addaswyd ar gyfer Curiad, gan Dafydd Tomos. 2010-01-27
  function ytplayer_render_player( )
  {
    swfobject.embedSWF
    (
      'http://www.youtube.com/v/' + ytplayer_playlist[ ytplayer_playitem ] + '&enablejsapi=1&rel=0&fs=1&autoplay=1',
      'ytplayer_div1',
      '585',
      '330',
      '8',
      null,
      null,
      {
        allowScriptAccess: 'always',
        allowFullScreen: 'true',
      },
      {
        id: 'ytplayer_object'
      }
    );
  }
  function ytplayer_render_playlist( )
  {
    for ( var i = 0; i < ytplayer_playlist.length; i++ )
    {
      var img = document.createElement( "img" );
      img.src = "http://img.youtube.com/vi/" + ytplayer_playlist[ i ] + "/default.jpg";
      var a = document.createElement( "a" );
      a.href = "#ytplayer";
      //
      // Thanks to some nice people who answered this question:
      // http://stackoverflow.com/questions/1552941/variables-in-anonymous-functions-can-someone-explain-the-following
      //
      a.onclick = (
        function( j )
        {
          return function( )
          {
            ytplayer_playitem = j;
            ytplayer_playlazy( 1000 );
          };
        }
      )( i );
      a.appendChild( img );
      document.getElementById( "ytplayer_div2" ).appendChild( a );
    }
  }
  function ytplayer_playlazy( delay )
  {
    //
    // Thanks to the anonymous person posted this tip:
    // http://www.tipstrs.com/tip/1084/Static-variables-in-Javascript
    //
    if ( typeof ytplayer_playlazy.timeoutid != 'undefined' )
    {
      window.clearTimeout( ytplayer_playlazy.timeoutid );
    }
    ytplayer_playlazy.timeoutid = window.setTimeout( ytplayer_play, delay );

  }
  function ytplayer_play( )
  {
    var o = document.getElementById( 'ytplayer_object' );
    if ( o )
    {
      o.loadVideoById( ytplayer_playlist[ ytplayer_playitem ], ytplayer_playoffset );
    }
  }
  //
  // Ready Handler (this function is called automatically by YouTube
  // JavaScript Player when it is ready)
  // * Sets up handler for other events
  //
  function onYouTubePlayerReady( playerid )
  {
    var o = document.getElementById( 'ytplayer_object' );
    if ( o )
    {
      o.addEventListener( "onStateChange", "ytplayer_statechange" );
      o.addEventListener( "onError", "ytplayer_error" );
    }
  }
  //
  // State Change Handler
  // * Sets up the video index variable
  // * Calls the lazy play function
  //
  function ytplayer_statechange( state )
  {
    if ( state == 0 )
    {
      ytplayer_getnextitem();
    }

  }
  //
  // Error Handler
  // * Sets up the video index variable
  // * Calls the lazy play function
  //
  function ytplayer_error( error )
  {
    if ( error )
    {
      ytplayer_getnextitem();
      ytplayer_playlazy( 5000 );
    }
  }

  //
  // Cross-browser XML-HTTP Object Creator
  //
  function CrossHttpObject( )
  {
    var o = null;
    try
    {
      o = new XMLHttpRequest( );
    }
    catch ( e )
    {
      try
      {
        o = new ActiveXObject( "MSXML2.XMLHTTP" );
      }
      catch ( e )
      {
        o = new ActiveXObject( "MICROSOFT.XMLHTTP" );
      }
    }
    return o;
  }

  // get next video id by ajax
  function ytplayer_getnextitem ( ) {

    var http = CrossHttpObject( );
    http.onreadystatechange = function( )
    {
      if ( http.readyState == 4 )
      {
	var jsondata = eval("("+http.responseText+")");
	ytplayer_playlist = [ jsondata.track.id ];
	ytplayer_playoffset = jsondata.track.offset;
	document.getElementById('ynchwarae').innerHTML = 'Yn Chwarae: '+jsondata.track.trackinfo;
        ytplayer_playlazy( 0 );
      }
    };
    http.open( 'POST', 'rheolwr-fideo.php', true );
    http.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
    http.setRequestHeader( 'Connection', 'close' );
    http.send( 'prevID=' + ytplayer_playlist[ ytplayer_playitem ] );
    //http.send(null);
  }



// cod ychwanegol yn benodol i Curiad

function negesYnChwarae (neges) {

  document.write('<h4><span id="ynchwarae">' + neges + '</span></h4>');

}

function negesNeidio () {

  document.write('<h5 id="negesNeidio"><a href="#" onClick="ytplayer_getnextitem();">');
  document.write('[Dwi ddim yn hoffi hwn - neidio i\'r fideo nesa!]</a></h5>');

}

