/* youtube-fast_search.js
   02 oct 2007 jw - works:
     - Trying it out, expanding comments.
 */

/* BEGIN configure */
// 1 - video stills only 
// 0 - video still plus description, stars, etc.
var cleanReturn = 1; 
// 1 - play video inline doesn't seem to make any difference here -
//     set last parameter $overlay$ to 1 in the function call.
// 0 - redirect to youtube page and play video.
var inlineVideo = 0; 
/* END configure */



var i = 0;
var youtubediv = new Array();

function hideOverlay(){
	var overlay = document.getElementById('youtubeoverlay');
	overlay.style.display = 'none';
	overlay.innerHTML = "";
}


// The video playbox, that comes up when you click on a video image.
function videoOverlay(id, vidtitle) {

	var objBody = document.getElementsByTagName("body").item(0);

	if(objBody) {

        var video = document.createElement('div');

	    video.setAttribute('id', 'youtubeoverlay');

	    video.innerHTML = '<div id="youtubecontent"><a href="javascript:hideOverlay()" class="close" title="Close to choose another video">Close</a><br /><object width="510" height="420" title="Click to go to this video at YouTube.com (new window)"><param name="movie" value="http://www.youtube.com/v/'+id+'"></param><param name="autoplay" value="1"><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+id+'&autoplay=1" type="application/x-shockwave-flash" wmode="transparent" width="510" height="420"></embed></object><br />' + '<span class="vidtitle">' + vidtitle + '</span></div>';
	    objBody.insertBefore(video, objBody.firstChild);
	} else {
		alert('no body element. please add');
	}
}



function mousOverImage(name,id,nr){

	if (name) {
		imname = name;
    }

	imname.src = "http://img.youtube.com/vi/" + id + "/" + nr + '.jpg';
	nr++;
	if (nr > 3) {
		nr = 1;
    }
	timer =  setTimeout("mousOverImage(false,'" + id + "'," + nr + ");",1000);

}


function getVideoId(url){
    var match = url.lastIndexOf('=');
    if (match) {
        id = url.substring(match+1);
        return id;
    }
}


function getId(string){
    var match = string.lastIndexOf("'s Videos");
    if (match != -1) {
      id = string.substring(0,match);
      return id.toLowerCase();
    }

    var match = string.lastIndexOf("query");
    if (match != -1) {
      id = string.substring(match+7);
      return id.toLowerCase();
    }

}


function listVideos(json,divid) {

    var ul = document.createElement('ul');
    ul.setAttribute('id', 'youtubelist');

    for (var i = 0; i < json.feed.entry.length; i++) {
        var entry = json.feed.entry[i];

        for (var k = 0; k < entry.link.length; k++) {
            if (entry.link[k].rel == 'alternate') {
                url = entry.link[k].href;
                break;
            }
        }

        var thumb = entry['media$group']['media$thumbnail'][1].url;

        var li = document.createElement('li');
    
        li.setAttribute('id', 'youtubebox');
        if (cleanReturn == 1) {

            var vid_title = entry.title.$t; 

            if (inlineVideo == 1) {

                // show the video in a "popup" in the same website:
       	        li.innerHTML = '<a href="javascript:videoOverlay(\'' + 
                    getVideoId(url) + '\'' + ', \'' + vid_title +
                    '\');"><img src="' + thumb + 
                    '" id="youtubethumb" title="' + 
                    vid_title +
                    '"  onmouseout="clearTimeout(timer)" onmouseover="mousOverImage(this,\'' + 
                    getVideoId(url) + '\',2)"></a>';

	        } else {

                // go to the video at youtube:
        	    li.innerHTML = '<a href="' + url + 
                    '"><img src="' + thumb + 
                    '" id="youtubethumb" title="' +
                    vid_title + 
                    '" onmouseout="clearTimeout(timer)" onmouseover="mousOverImage(this,\'' +
                    getVideoId(url) + '\',2)"></a>';
		    }

        } else {

            // The youtube description, star ratings, etc.
            li.innerHTML = entry.content.$t;
        }

        ul.appendChild(li);
      }

      document.getElementById(divid).appendChild(ul);

} // End function



function youtubeInit(root) {
  //this hacks the layer for mutiple json queries
  id = getId(root.feed.title.$t);
  //alert(youtubediv[id] + id);
  listVideos(root, youtubediv[id]);

}


function insertVideos(div,typ,q,results,overlay){
    inlineVideo = overlay;
    youtubediv[q.toLowerCase()] = div;

    var script = document.createElement('script');
    if(typ == "search") {
  	    script.setAttribute('src', 'http://gdata.youtube.com/feeds/videos?vq='+q+'&max-results='+results+'&alt=json-in-script&callback=youtubeInit');

    }

    if (typ == "user") {
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/users/'+q+'/uploads?max-results='+results+'&alt=json-in-script&callback=youtubeInit');
    }

    if (typ == "playlist") {
	    // doesn't function
	    alert('oops.. working on it');
  	    script.setAttribute('src', 'http://gdata.youtube.com/feeds/playlists/'+q+'?max-results='+results+'&alt=json-in-script&callback=youtubeInit');
    }

    script.setAttribute('id', 'jsonScript');
    script.setAttribute('type', 'text/javascript');
    document.documentElement.firstChild.appendChild(script);

} // end function

/* e o f */
