// requires Prototype.js

Event.observe(document, 'dom:loaded', function() {
    // video clips
    $('videoClipsTab').select('.thumbnailLink').each(function(elm) {
	var flvUrl;
	
	// Note: if the show name link and the episode name link
	// ever differ, show name will win.

	// show name link
	var showName = $(elm.parentNode).select('h3 a');
	if (showName.length == 1) {
	  showName = showName[0];

	  // get the flv url
	  flvUrl = showName.href;

	  showName.onclick = function(){return false};
	  showName.observe('click', function() {
	      doVideoClipClick(elm);
	      return false;
	    });
	}

	// episode name link
	var episodeName = $(elm.parentNode).select('.episodeName a');
	if (episodeName.length == 1) {
	  episodeName = episodeName[0];

	  // get the flv url, or override if it's already been set
	  if (!flvUrl) flvUrl = episodeName.href;
	  else episodeName.href = flvUrl;

	  episodeName.onclick = function(){return false};
	  episodeName.observe('click', function() {
	      doVideoClipClick(elm);
	      return false;
	    });
	}

	// thumbnail link
	elm.onclick = function(){return false};
	if (flvUrl) {
	  elm.href = flvUrl;
	  elm.observe('click', function() { doVideoClipClick(elm); });
	}
      });

    // info popups
    $('playerBlockTabs').select('.thumbnailBlock').each(function(elm) {
	elm.select('.infoLink a').each(function(elm) {
	    elm.onclick = function(){return false};
	    elm.observe('click', handleInfoLinkClick);
	  });
      });

    // add click behavior to the 'return' link
    getElm_returnLink($('playerBlock')).onclick = function() {return false};
    getElm_returnLink($('playerBlock')).observe('click', function() {
	setPlayerBlockContent($('videoClipsLink'), 'videoClipsTab');
	return false;
      });

    // tab links
    $('fullEpisodesLink').onclick = function() {return false};
    $('fullEpisodesLink').observe('click', function(e) {
	setPlayerBlockContent(this, 'fullEpisodesTab');
	return false;
      });
    $('videoClipsLink').onclick = function() {return false};
    $('videoClipsLink').observe('click', function(e) {
	setPlayerBlockContent(this, 'videoClipsTab');
	return false;
      });
  });

function getElm_returnLink(container) {
  return container.select('#bottomLine a.blueText')[0];
}

var getClipDetails = (function(){
    function getElm_showLink(container) {
      return container.select('h3 a')[0];
    }
    
    function getElm_clipName(container) {
      return container.select('p.episodeName')[0];
    }
    
    function getElm_clipDuration(container) {
      return container.select('span.episodeDuration')[0];
    }
    
    function getElm_description(container) {
      return container.select('p.thumbInfo')[0];
    }
    
    return (function(elm) {
	var options = {};
	options.showname = getElm_showLink(elm).innerHTML;
	options.showhref = getElm_showLink(elm).href;
	options.clipname = getElm_clipName(elm).innerHTML;
	options.clipduration = getElm_clipDuration(elm).innerHTML;

	var descriptionELm = getElm_description(elm);
	var description = descriptionELm._originalHTML
	  ? descriptionELm._originalHTML
	  : descriptionELm.innerHTML
	options.description = description;
	options.seeallhref = options.showhref;
	options.seealltext = "See All "+options.showname+" Videos";
	return options;
      });
  })();

var setPlayerDetails = (function(){
    function getElm_showLink(container) {
      return container.select('#showNameLine .lhs a')[0];
    }
    
    function getElm_clipDetails(container) {
      return container.select('p.clipName')[0];
    }
    
    function getElm_description(container) {
      return container.select('p.description')[0];
    }
    
    function getElm_seeAllLink(container) {
      return container.select('a.seeAllLink')[0];
    }
    
    return (function(options) {
	var container = $('playerBlock');
	
	getElm_showLink(container).href = options.showhref;
	getElm_showLink(container).innerHTML = options.showname;
	//Nielsen reporting variables
	mn_showTitle = options.showname;
	
	var clipText = options.clipname + ' ' + options.clipduration;
	getElm_clipDetails(container).innerHTML = clipText;
	
	getElm_description(container).innerHTML = options.description;
	
	getElm_seeAllLink(container).href = options.seeallhref;
	getElm_seeAllLink(container).innerHTML = options.seealltext;
      });
  })();

// Toggles the display of (shows or hides) the white info popup box in a episode/video clip thumbnail block
function toggleClipInfo(thisElem) {
  var elem = thisElem;

  while (!Element.hasClassName(elem.parentNode, "thumbnailBlock") 
	 && typeof(elem.parentNode) != 'undefined') {
    elem = elem.parentNode;
  }
  
  if (!Element.hasClassName(elem.parentNode, "thumbnailBlock"))
    return false;
  
  var thumbnailBlockElem = elem.parentNode;
  thumbnailBlockElem.style.position = "relative";
  
  var thumbInfoBoxElem = thumbnailBlockElem.select(".thumbInfoBox")[0];
  if (thumbInfoBoxElem == undefined || thumbInfoBoxElem == null)
    return false;
  
  if (thumbInfoBoxElem.style.display == "none" || thumbInfoBoxElem.style.display == "") {
    
    thumbInfoBoxElem.style.left = '149px';
    thumbInfoBoxElem.style.top = '0px';
    
    thumbInfoBoxElem.style.display = "block";

    // crop the info text
    var infoElm = thumbInfoBoxElem.select('.thumbInfo')[0];
    var txt = infoElm.innerHTML;
    infoElm._originalHTML = txt;
    infoElm.innerHTML = txt.substr(0, 150) + "...";
  }
  else {
    thumbInfoBoxElem.style.display = "none";
  }
}

function handleVideoClipClick(e) {
  doVideoClipClick(this);
  return false;
}

function doVideoClipClick(elm) {
  setPlayerDetails(getClipDetails(elm.parentNode));
  setPlayerBlockContent(false, 'playerBlock');
  writePlayer(elm.href);
}

function handleInfoLinkClick(e) {
  toggleClipInfo(this);
  return false;
}

// function to switch between the 3 'tabs' in the video player block
function setPlayerBlockContent(thisElem, id) {
  // show the selected tab
  $('playerBlockTabs').className = "selected-"+id;
  
  // mark the link as selected
  if (thisElem != false) {
    thisElem.addClassName('selected');
    
    if (thisElem.id == "fullEpisodesLink") {
      $("videoClipsLink").removeClassName('selected');
    }
    else if (thisElem.id == "videoClipsLink") {
      $("fullEpisodesLink").removeClassName('selected');
    }
  }
}
