function setupRollovers() {
  if (!document.getElementsByTagName)
    return;
  var all_links = document.getElementsByTagName('a');
  for (var i = 0; i < all_links.length; i++) {
    var link = all_links[i]; 
    if (link.className &&
        (' ' + link.className + ' ').indexOf(' rollover ') != -1) {
      if (link.childNodes &&
          link.childNodes.length == 1 &&
          link.childNodes[0].nodeName.toLowerCase() == 'img') {
        link.onmouseover = mouseover;
        link.onmouseout = mouseout;
        link.onclick = click ;
      }
    }
  }
}

function find_target(e)
{
  /* Begin the DOM events part, which you */
  /* can ignore for now if it's confusing */
  var target; 

  if (window.event && window.event.srcElement) 
    target = window.event.srcElement;
  else if (e && e.target)
    target = e.target;
  if (!target)
    return null;

  while (target != document.body &&
      target.nodeName.toLowerCase() != 'a')
    target = target.parentNode;

  if (target.nodeName.toLowerCase() != 'a')
    return null;

  return target;
}

function updatePic() {
    var pref= img_tag.src.substring(0,34) ;  
    var n = img_tag.src.substring(34,35) ; 
    if (n=='d') {n=1; 
    var suff = img_tag.src.substring(41) ;
    } 
    else { 
    n = parseInt(n) + 1;
    if (n==4) n=1 ;
    var suff = img_tag.src.substring(35) ;
    } 
 
    img_tag.src = pref + n + suff ; 

}
function mouseover(e) {
  var target = find_target(e);
  if (!target) return;
 
  // the only child node of the a tag in target will be an img tag

   img_tag = target.childNodes[0];

  // Take the "src", which names an image called "something.ext",
  // Make it point to "something_over.ext"
  // This is done with a regular expression
  //img_tag.src = img_tag.src.replace(/(\.[^.]+)$/, '_over$1');
 
    var pref= img_tag.src.substring(0,34) ;  
    var n = img_tag.src.substring(34,35) ;  
    if (n=='d') {n=1; 
    var suff = img_tag.src.substring(41) ;
    } 
    else { 
    n = parseInt(n) + 1;
    if (n==4) n=1 ;
    var suff = img_tag.src.substring(35) ;
    } 
 
    img_tag.src = pref + n + suff ; 

  



    timerId=setInterval("updatePic()",300); 
}
function mouseout(e) {
  var target = find_target(e);
  if (!target) return;

  // the only child node of the A-tag in |target| will be an IMG-tag
  var img_tag = target.childNodes[0];

  // Take the "src", which names an image as "something_over.ext",
  // Make it point to "something.ext"
  // This is done with a regular expression
  //img_tag.src = img_tag.src.replace(/_over(\.[^.]+)$/, '$1');
    clearInterval(timerId); 
     
}

function click(e) {
  var target = find_target(e);
  if (!target) return;

  // the only child node of the A-tag in |target| will be an IMG-tag
  var img_tag = target.childNodes[0];
  var playerUrl = target.href ; 
  loadVideo(playerUrl, true);
 
    return false; 
}


function loadVideo(playerUrl, autoplay) {
 
  swfobject.embedSWF(
      playerUrl + '&rel=1&border=0&fs=1&autoplay=' + 
      (autoplay?1:0), 'player', '290', '250', '9.0.0', false, 
      false, {allowfullscreen: 'true'});
 
}

function showMyVideos1(data) {
window.videoFeed=data; 
showMyVideos2();
}

function makeVideo(entry) {
    var title = entry.title.$t.substr(0, 20);
    var fulltitle= entry.title.$t;
    var thumbnailUrl = entry.media$group.media$thumbnail[0].url;
    var playerUrl = entry.media$group.media$content[0].url;
    if (entry.yt$statistics) 
      viewCount=entry.yt$statistics.viewCount;

  	var li = document.createElement('li');
	var anc = document.createElement('a');
      anc.className="rollover";
	anc.href = playerUrl;
	var img = document.createElement('img');
	img.src = thumbnailUrl;
	img.title = fulltitle + " views("+viewCount+")";
	img.alt = "image alt";
	img.width = "120" ;
	img.height = "90" ;
	anc.appendChild(img);
	li.appendChild(anc);
	return li;
}
 

function showMyVideos2() {
  
  var feed = videoFeed.feed;
  var entries = feed.entry || [];
  	// 'Empty' the ul by removing all of its children
	var ul = document.getElementById('vids');
	while (ul.hasChildNodes()) {
	  ul.removeChild(ul.firstChild);
	}
	// Loop over the photos and display them all
 	for (var i = 0; i<entries.length; i++) {
	  ul.appendChild(makeVideo(entries[i]));
	}
  
  if (entries.length > 0) {
    loadVideo(entries[0].media$group.media$content[0].url, false);
     setupRollovers();
 }
else {alert("No videos found for author: " + document.getElementById('getauthor').value);
}
}



function loadJSON(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url ;
 
document.getElementsByTagName('head')[0].appendChild(script);
}

function loadFeed() {
 
loadJSON(
"http://gdata.youtube.com/feeds/api/videos?q=Mandarin+-sex&alt=json-in-script&format=5&time=this_week&callback=showMyVideos1&max-results=30&orderby=viewCount"
);
}

function loadTopVids(timeSelected) {
 
debugger
timeSelected=parseInt(timeSelected);
switch (timeSelected)
{
case 0:
  var timeParm="all_time";
  break;
case 1:
  var timeParm="today";
  break;
case 2:
  var timeParm="this_week";
  break;
case 3:
  var timeParm="this_month";
  break;
}
loadJSON(
"http://gdata.youtube.com/feeds/api/videos?q=Mandarin+-sex&alt=json-in-script&format=5&callback=showMyVideos1&max-results=30&time="+timeParm+"&orderby=viewCount");
}

window.onload=loadFeed;

 

