/* YOUTUBE PLAYER */
//
// YouTube JavaScript API 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
//

var ytplayer_playlist = [];
var ytplayer_playitem = 0;
var ytplayer_maxvideothumbs = 5;
var ytplayer_initialvideoid = '';
var ytplayer_mainholder = 'ytplayer_div1';
var ytplayer_videothumbholder = 'ytplayer_div2';

// start here
var ytplayer_playlistid = jQuery('#'+ytplayer_mainholder).attr('rel');
ytplayer_render_playlist();
//swfobject.addLoadEvent( ytplayer_render_playlist );

function ytplayer_render_player(){
	swfobject.embedSWF(
		'http://www.youtube.com/v/' + ytplayer_initialvideoid + '&enablejsapi=1&rel=0&fs=1',
		ytplayer_mainholder,
		'756',
		'474',
		'8',
		null,
		null,
		{
			allowScriptAccess: 'always',
			allowFullScreen: 'true'
		},
		{
			id: 'ytplayer_object'
		}
	);
}

function ytplayer_render_playlist(){
	//var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/B2A4E1367126848D?v=2&alt=json&callback=?';
	var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/'+ytplayer_playlistid+'?v=2&alt=json&callback=?';
	var videoURL= 'http://www.youtube.com/watch?v=';
	jQuery.getJSON(playListURL, function(data) {
	    var list_data = "";
	    var count = 0;
	    jQuery.each(data.feed.entry, function(i, item) {
	    	count++;
	    	if(count <= ytplayer_maxvideothumbs){
		        var feedTitle = item.title.$t;
		        var feedURL = item.link[1].href;
		        var fragments = feedURL.split("/");
		        var videoID = fragments[fragments.length - 2];
		        var url = videoURL + videoID;
		        var thumb = "http://img.youtube.com/vi/"+ videoID +"/default.jpg";
		        list_data += '<a href="#ytplayer" title="'+ feedTitle +'"><img src="'+ thumb +'" alt="'+ feedTitle+'" /></a>';
		        
		        if(count == 1) ytplayer_initialvideoid = videoID;
		        //list_data += '<li><a href="'+ url +'" title="'+ feedTitle +'"><img alt="'+ feedTitle+'" src="'+ thumb +'"</a></li>';
	    	}
	    });
	    jQuery(list_data).appendTo('#'+ytplayer_videothumbholder);
	});
	
	swfobject.addLoadEvent( ytplayer_render_player );	
}

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 ] );
	}
}

//
// 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_playitem += 1;
		ytplayer_playitem %= ytplayer_playlist.length;
		ytplayer_playlazy( 5000 );
	}
}

//
// Error Handler
// * Sets up the video index variable
// * Calls the lazy play function
//
function ytplayer_error( error ){
	if ( error ){
		ytplayer_playitem += 1;
		ytplayer_playitem %= ytplayer_playlist.length;
		ytplayer_playlazy( 5000 );
	}
}

