
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";
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 = ['<ul class="videos">'];
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('<li><a href="javascript:YouTubeSearch.play(\'', playerUrl, '\', true)" title="', title,
'"><img src="',
thumbnailUrl, '" width="130" height="97"/><span class="title"><strong>', title, '</strong></span></a></li>');
}
html.push('</ul><div class="youTubeBranding"><a href="http://www.youtube.com/" target="_blank"><img src="http://code.google.com/apis/youtube/images/badge3.gif" /></a></div>')
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.playIndex = function(index, lang) {
if (this._playList && this._playList['length'] && this._playList.length > index) {
this.defaultSearch(this._playList[index]['artist'], this._playList[index]['song'], 1, true, lang)
YouTubeSearch.onVideoEnded = function(){YouTubeSearch.playIndex(index + 1, lang)};
}
}
YouTubeSearch.defaultSearch = YouTubeSearch.search

if (typeof(YouTubeSearch) == "undefined")
YouTubeSearch = { };
YouTubeSearch.childHtml = '<div class="youTubePlayerLyrics" id="youTubePlayerLyrics" style="display: none;">' +
'<strong><span id="youTubeLyricsArtist"></span> - <span id="youTubeLyricsTitle"></span></strong>' +
'<p id="youTubeLyricsText"></p>' +
'<small><a href="#" id="youTubeLyricsUrl" rel="nofollow"></a></small>' +
'</div>';
YouTubeSearch.lyricSearch = function(artist, title, results, startPlaying, lang) {
this.search(artist, title, results, startPlaying);
document.getElementById('youTubePlayerLyrics').style.display = "none";
if (this.indicator) {
document.getElementById(this.indicator).style.display = "block"; 
}
var url = "http://www.songtexte.com/api?artist=" + encodeURIComponent(artist) + "&song=" + encodeURIComponent(title) + "&fmt=json&l=" + encodeURIComponent(lang)
if (this.lyricWikiScript) {
this._jsonUnload(this.lyricWikiScript);
}
this.lyricWikiScript = this._jsonLoad(url, YouTubeSearch._writeLyrics);
}
YouTubeSearch._writeLyrics = function (){
document.getElementById('youTubeLyricsArtist').innerHTML = song.artist;
document.getElementById('youTubeLyricsTitle').innerHTML = song.song;
document.getElementById('youTubeLyricsText').innerHTML = song.lyrics.replace(/\n/g, "<br />");
var url = document.getElementById('youTubeLyricsUrl');
url.innerHTML = song.url;
url.href = song.url;
document.getElementById('youTubePlayerLyrics').style.display = "block";
}
YouTubeSearch.defaultSearch = YouTubeSearch.lyricSearch