<!--

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

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');
  if(json.feed.entry){
	  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;

	appendOptionLast('<a href="javascript:playVideo(\''+getVideoId(url)+'\',false,\''+addslashes(entry.title.$t)+'\',true)"><img src="'+thumb+'" onmouseout="mouseOutImage(this)" onmouseover="mousOverImage(this,\''+getVideoId(url)+'\',1)"></a><br />'+entry.title.$t.substr(0,30)+'',getVideoId(url),'ul1')


	}
  }else{
  	divid.innerHTML = 'No Results Found';

  }
  hideLightbox();

}

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

}


function insertVideos(div,typ,q,results,start){
  start = start + 1;

  if(typ == "mostviewed")
	q = "Most Viewed";


  if(typ == "linked")
	q = "Most Linked";

  youtubediv[q.toLowerCase()] = div;

  var script = document.createElement('script');

  if(typ == "search"){
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/api/videos?vq='+q+'&start-index='+start+'&max-results='+results+'&alt=json-in-script&callback=youtubeInit');
	if(document.title)
 		document.title = "Search: "+q.replace("+"," ")+" - YouTube Fast Search";
  }
  if(typ == "hot"){
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?alt=json-in-script&callback=youtubeInit&start-index=1&max-results=50');
	if(document.title)
 		document.title = "Recently Featured - YouTube Fast Search";
  }
  if(typ == "mostviewed"){
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/api/standardfeeds/most_viewed?time=this_month&alt=json-in-script&callback=youtubeInit&start-index=1&max-results=50');
	if(document.title)
 		document.title = "Most Viewed This Month - YouTube Fast Search";
  }

  if(typ == "linked"){
  	script.setAttribute('src', 'http://gdata.youtube.com/feeds/api/standardfeeds/most_linked?time=this_month&alt=json-in-script&callback=youtubeInit&start-index=1&max-results=50');
	if(document.title)
 		document.title = "Most Linked This Month - YouTube Fast Search";
  }

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

//-->