if (typeof(YouTubeSearch) == "undefined")
YouTubeSearch = { };
YouTubeSearch.init = function() {
this.init = function(){}
youTubePlayer = document.createElement("div");
document.getElementsByTagName('body')[0].appendChild(youTubePlayer)
html = []
html.push('<div id="youTubePlayer" style="display: none">')
html.push('<div class="youTubePlayerHeader"><a class="youTubePlayerClose" href="javascript:YouTubeSearch.close()" title=""><span><wicket:message key="closeVideo">Close</wicket:message></span></a></div>');
if (YouTubeSearch.childHtml) {
html.push(YouTubeSearch.childHtml);
}
html.push('<div class="youTubePlayerObject" id="youTubePlayerContainer"></div>')
html.push('</div>')
youTubePlayer.innerHTML=html.join('');
}
YouTubeSearch.search = function(artist, title, results, startPlaying) {
this.init();
document.getElementById('youTubePlayer').style.display="none";
if (this.indicator) {
document.getElementById(this.indicator).style.display = "block"; 
}
var query = (title) ? encodeURIComponent(artist) + " " + encodeURIComponent(title) : encodeURIComponent(artist);
var callback = (startPlaying == true) ? "_displayPlay" : "_displayWait";
var url = "http://gdata.youtube.com/feeds/api/videos?v=2&q="+query+"&alt=json-in-script&callback=YouTubeSearch."+callback+"&max-results="+results+"&format=5&orderby=published";
if (this.youTubeScript) {
this._jsonUnload(this.youTubeScript);
}
this.youTubeScript = this._jsonLoad(url);
this.currentQuery = query;
}
YouTubeSearch._jsonLoad = function(url, callback) {
var script = document.createElement("script")
script.setAttribute("type", "text/javascript")
script.setAttribute("src", url)
script.setAttribute("charset", "utf-8")
document.getElementsByTagName('head')[0].appendChild(script);
if (callback) {
if (script.addEventListener) {
 script.addEventListener("load", callback, false)
} else if (document.all && !window.opera) {
 script.onreadystatechange=function(){
if (script.readyState == "loaded" || script.readyState == "complete") {
callback()
script.onReadyStateChange = null
}
}
}
}
return script;
}
YouTubeSearch._jsonUnload = function(script) {
document.getElementsByTagName('head')[0].removeChild(script);
}
YouTubeSearch.play = function(playerUrl, startPlaying) {
var id = "youTubePlayerObject";
if (!document.getElementById(id)) {
var c = document.createElement("div");
c.setAttribute("id", id);
document.getElementById("youTubePlayerContainer").appendChild(c);
}
var att = {
data:playerUrl + '&rel=0&border=0&fs=1&showsearch=0&enablejsapi=1&autoplay=' + (startPlaying?1:0),
width:"405",
height:"245"
}
var par = {allowfullscreen: 'true', allowscriptaccess: 'always'}
document.getElementById('youTubePlayer').style.display="block"
player = swfobject.createSWF(att, par, id)
}
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("youTubePlayerObject");
ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
}
function onytplayerStateChange(newState) {
switch(newState) {
case 0: if (YouTubeSearch.onVideoEnded) YouTubeSearch.onVideoEnded(); break;
case 1: if (YouTubeSearch.onVideoPlaying) YouTubeSearch.onVideoPlaying(); break;
case 2: if (YouTubeSearch.onVideoPause) YouTubeSearch.onVideoPause(); break;
case 3: if (YouTubeSearch.onVideoBuffering) YouTubeSearch.onVideoBuffering(); break;
case 5: if (YouTubeSearch.onVideoCued) YouTubeSearch.onVideoCued(); break;
}
}
YouTubeSearch._displayPlay=function(data) {
this._display(data, true);
}
YouTubeSearch._displayWait=function(data) {
this._display(data, false);
}
YouTubeSearch._display=function(data, startPlaying) {
var feed = data.feed;
this.entries = feed.entry || [];
var html = ['<table align="center" border="0" cellpadding="0" cellspacing="5" class="videos"><tbody><tr>'];
for (var i = 0; i < this.entries.length; i++) {
var entry = this.entries[i];
var title = entry.title.$t;
var thumbnailUrl = entry.media$group.media$thumbnail[0].url;
var playerUrl = entry.media$group.media$content[0].url;
html.push('<td align="center" valign="top"><a href="javascript:YouTubeSearch.play(\'', playerUrl, '\', true)"><img src="',
thumbnailUrl, '" width="130" height="97"/></a></td>');
}
html.push('</tr></tbody></table>');
document.getElementById('youTubeResults').innerHTML = html.join('');
if (this.entries.length > 0 && startPlaying) {
this.play(this.entries[0].media$group.media$content[0].url, true);
}
if (this.onLoad) {
this.onLoad();
}
if (this.indicator) {
document.getElementById(this.indicator).style.display = "none"; 
}
}
YouTubeSearch.close = function() {
swfobject.removeSWF('youTubePlayerObject');
document.getElementById('youTubePlayer').style.display="none";
 }
YouTubeSearch.setPlaylist = function(playList, startPlaying) {
this._playList = playList
if (startPlaying) this.playIndex(0)
}
YouTubeSearch.defaultSearch = YouTubeSearch.search
