/*
myYoutubePlaylist
WordPress Plugin by Jonk, http://jonk.pirateboy.net
Download from http://wordpress.org/extend/plugins/my-youtube-playlist
*/

//TO THE MAIN RELEASER (new feature): its up to you if you want a full jquery implementation or translate the jquery code to native javascript or just let both

//check for ie
ie = false;
if (document.all) {
	var ie = true;
}

//a function to choose another youtube-video without reloading the page
function myYoutubePlaylist_cy(ytSrc,containerId,type,play) {
	if (type == null || type == '') {
		type = 'v';
	}
	document.getElementById(containerId).innerHTML = eval("myYoutubePlaylist_cf('',ytSrc,500,307,type,play)");
}

//a function to load flashmovies. it also prevents ie from adding the ugly border
function myYoutubePlaylist_cf(FlashVars,movie,width,height,type,play) {
	if (play == 'play') {
		play = '&autoplay=1';
	}
	myYoutubePlaylist_cfStr = '<obj' + 'ect width="' + width + '" height="' + height + '" data="' + 'http://www.youtube.com/'+ type +'/'+ movie +'&hl=en&fs=1' + play + '" type="application/x-shockwave-flash">';
	myYoutubePlaylist_cfStr += '<param name="movie" value="http://www.youtube.com/'+ type +'/'+ movie +'&hl=en&fs=1' + play + '"/>';
	myYoutubePlaylist_cfStr += '<param name="allowFullScreen" value="true"/>';
	myYoutubePlaylist_cfStr += '<param name="allowscriptaccess" value="always"/>';
	//myYoutubePlaylist_cfStr += '<param name="FlashVars" value="'+ FlashVars +'">';
	myYoutubePlaylist_cfStr += '<embed src="http://www.youtube.com/'+ type +'/'+ movie +'&hl=en&fs=1'+ play +'" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" allowscriptaccess="always" allowfullscreen="true"/>';
	myYoutubePlaylist_cfStr += '<' + '/object>';
	return myYoutubePlaylist_cfStr;
}

//a function to load all movies with thumbs to a list
function myYoutubePlaylist_dl(allVideos,containerId,targetContainerId) {
	allVideosArr = allVideos.split(', ');
	if (allVideosArr.length > 1) {
		jQuery('#' + containerId).append('<ul class="myYoutubePlaylist_Ul" ></ul>');
		var container_ul=jQuery('#' + containerId).find('ul.myYoutubePlaylist_Ul');
		for (var i=0; i < allVideosArr.length; i++) {
			jQuery.getJSON(
				'https://gdata.youtube.com/feeds/api/videos/' + allVideosArr[i] + '?callback=?&v=2&alt=json',
				function(data) {
				var thisvideoinfos={};
				//type console.log(data); to see the youtube api information object
				thisvideoinfos.thumb_url = data.entry.media$group.media$thumbnail[0].url;//retrieving video first thumbnail
				thisvideoinfos.title = data.entry.title.$t;//retrieving video title
				thisvideoinfos.ID = data.entry.id.$t.split(':')[3];////retrieving video id
				
				if(thisvideoinfos.title.length>25){
					thisvideoinfos.shrinked_title = thisvideoinfos.title.substr(0, 25)+'...';
				}
				else{
					thisvideoinfos.shrinked_title = thisvideoinfos.title;
				}
				
				thisLink = 'javascript:myYoutubePlaylist_cy(\''+thisvideoinfos.ID+'\',\''+targetContainerId+'\',\'\',\''+'play'+'\');'
				container_ul.append('<li>' + '<a href="'+thisLink+'">' + '<img class="myYoutubePlaylist_Img"  alt="' + thisvideoinfos.title + '" title="' + thisvideoinfos.title + '" src="' + thisvideoinfos.thumb_url + '"/>' + '<span class="myYoutubePlaylist_videotitle">' + thisvideoinfos.shrinked_title + '</span>' + '</a>' + '</li>');
				}
			); //youtube API usefull links: http://code.google.com/intl/fr-FR/apis/youtube/2.0/developers_guide_protocol_video_entries.html AND http://code.google.com/intl/fr-FR/apis/youtube/2.0/developers_guide_json.html
		}	
	} else {
		document.getElementById(containerId).style.display = 'none';
	}
}

