var playerRef,currentEpisode,lastHash,atShowRoot=window.location.pathname.charAt(window.location.pathname.length-1)=="/",showPageTitle=document.title,episodeURLs=new Array(),bareBones=window.location.hash.substring(1,4)=="bb=";if(bareBones)
window.location.hash=window.location.hash.substring(4);if(!atShowRoot)
{var urlParts=window.location.pathname.split("/");window.location.replace("http://"+window.location.host+"/"+urlParts[1]+"/"+window.location.search+"#"+urlParts[2]);}
$(document).ready(function()
{swfobject.embedSWF("swf/player.swf","player","640","360","9.0.0","swf/expressInstall.swf",flashVars,{allowfullscreen:"true",allowscriptaccess:"always",wmode:"opaque"},null,function(e){playerRef=e.ref;});$("#episodes a").each(wrangleEpisodeLink);$("#cts").click(skipAd);if(bareBones)
$("#comments").hide();else
$.get("pop-vids"+window.location.search,function(data)
{var popVidsContainer=$("#pop-vids");popVidsContainer.html(data);$("a",popVidsContainer).each(wranglePopVidLink);$(".tn-title",popVidsContainer).each(trimThumbTitle);$(".scroller",popVidsContainer).each(makeListScroller);});});function playerReady(defaultVideo)
{var prevButton=$("#prev-episode"),nextButton=$("#next-episode"),autoplay=false;prevButton.click(prevButton.hasClass(INACTIVE)?returnFalse:prevEpisode);nextButton.click(nextButton.hasClass(INACTIVE)?returnFalse:nextEpisode);if(atShowRoot)
{if(window.location.hash)
{var hash=window.location.hash.substring(1);if(hash.substring(0,3)=="ap=")
{hash=hash.substring(3);var urlParts=window.location.pathname.split("/");window.location.replace("http://"+window.location.host+"/"+urlParts[1]+"/"+window.location.search+"#"+hash);autoplay=true;scrollToVideo();}
var index=findEpisodeIndexFromHash(hash);if(index!=null)
defaultVideo=index;}
lastHash=window.location.hash;setInterval(checkHash,500);}
document.body.appendChild(document.createElement("div")).setAttribute("id","fb-root");if(!bareBones)
{FB.Event.subscribe('xfbml.render',insertAds);FB.init({appId:"112187098825018",status:true,cookie:false,xfbml:true});}
return{video:defaultVideo,autoplay:autoplay};}
function wrangleEpisodeLink(index)
{var _this=$(this),hrefParts=_this.attr("href").split("/"),href=hrefParts[0]+"/"+window.location.search+"#"+hrefParts[1].replace("?cts=1","");_this.attr("href",href);_this.click(switchEpisode).data("episodeIndex",index);episodeURLs.push(href);}
function wranglePopVidLink()
{var _this=$(this),hrefParts=_this.attr("href").split("/");if(window.location.pathname.indexOf(hrefParts[0])>-1)
_this.click(switchEpisode).data("episodeIndex",findEpisodeIndexFromHash(hrefParts[1].substr(hrefParts[1].indexOf("#")+1)));}
function checkHash()
{if(window.location.hash!=lastHash)
{var index=findEpisodeIndexFromHash(window.location.hash);if(index!=null)
playerRef.switchVideo(index,true);lastHash=window.location.hash;}}
function findEpisodeIndexFromHash(hash)
{if(hash.substring(0,1)=="#")
hash=hash.substring(1);if(hash.substring(0,3)=="ap=")
hash=hash.substring(3);var episode=$("#episodes a[href="+window.location.pathname.split("/")[1]+"/"+window.location.search+"#"+hash+"]");return episode?episode.data("episodeIndex"):null;}
function switchEpisode()
{scrollToVideo();playerRef.switchVideo($(this).data("episodeIndex"));return false;}
function scrollToVideo()
{$("html,body").animate({scrollTop:$("#after-header").offset().top});}
function nextEpisode()
{playerRef.switchVideo(currentEpisode+1,true);return false;}
function prevEpisode()
{playerRef.switchVideo(currentEpisode-1,true);return false;}
function setNowPlaying(index,name,description)
{$("#fb-like").html("<fb:like href=\""
+window.location.protocol+"//"+window.location.hostname+window.location.pathname+makeNameURL(name)
+"\" send=\"true\" width=\"450\" show_faces=\"false\" font=\"verdana\"></fb:like>");FB.XFBML.parse($("#fb-like")[0]);var prevButton=$("#prev-episode"),nextButton=$("#next-episode"),maxIndex=$("#episodes a").size()-1;$("#episode-number").html("Episode&nbsp;"+(index+1));$("#episode-name").text(name);$("#episode-description").text(description);prevButton.attr("href",index>0?episodeURLs[index-1]:"");nextButton.attr("href",index<maxIndex?episodeURLs[index+1]:"");if(index>0&&prevButton.hasClass(INACTIVE))
{prevButton.removeClass(INACTIVE);prevButton.unbind("click").click(prevEpisode);}
else if(index==0&&!prevButton.hasClass(INACTIVE))
{prevButton.addClass(INACTIVE);prevButton.unbind("click").click(returnFalse);prevButton.removeAttr("href");}
if(index<maxIndex&&nextButton.hasClass(INACTIVE))
{nextButton.removeClass(INACTIVE);nextButton.unbind("click").click(nextEpisode);}
else if(index==maxIndex&&!nextButton.hasClass(INACTIVE))
{nextButton.addClass(INACTIVE);nextButton.unbind("click").click(returnFalse);nextButton.removeAttr("href");}
if(atShowRoot)
{lastHash="#"+
(window.location.hash=makeNameURL(name));$(document).attr("title",showPageTitle+": "+name);}
currentEpisode=index;}
function showCompanionAd(location,code)
{var adSpot="div.ad."+location;$(adSpot).text("ADVERTISEMENT").append(code);}
function showTidalCompanion(width,height,type,src,click)
{console.log("Comanion: "+width+"x"+height+" ("+type+")\nsrc: "+src+"\nclick: "+click);var adSpot;if(width==300&&height==250)
adSpot="mpu";else if(width==728&&height==90)
adSpot="leader";else
return false;switch(type)
{case"application/x-shockwave-flash":var flashVars="src="+src+"&amp;click="+click;code="<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+width+"\" height=\""+height+"\">"+"<param name=\"movie\" value=\"swf/tidal-companion.swf\" />"+"<param name=\"allowFullScreen\" value=\"true\" />"+"<param name=\"wmode\" value=\"opaque\" />"+"<param name=\"scale\" value=\"noscale\" />"+"<param name=\"flashVars\" value=\""+flashVars+"\" />"+"<!--[if !IE]>-->"+"<object type=\"application/x-shockwave-flash\" data=\"swf/tidal-companion.swf\" width=\""+width+"\" height=\""+height+"\">"+"<param name=\"allowFullScreen\" value=\"true\" />"+"<param name=\"wmode\" value=\"opaque\" />"+"<param name=\"scale\" value=\"noscale\" />"+"<param name=\"flashVars\" value=\""+flashVars+"\" />"+"</object>"+"<!--<![endif]-->"+"</object>";break;case"image/gif":case"image/jpeg":case"image/jpg":code='<a href="'+click+'" target="_blank"><img src="'+src+'" width=\""+width+"\" height=\""+height+"\" /></a>';break;case"iframe":code='<iframe src="'+src+'" width="'+width+'" height="'+height+'"></iframe>';break;default:return false;}
showCompanionAd(adSpot,code);return true;}
function showCTS()
{$("#cts").slideDown(300);}
function hideCTS()
{$("#cts").slideUp(300);}
function skipAd()
{var adLink=playerRef.skipAd();hideCTS();window.open(adLink,"","width=800,height=600").blur();window.focus();}
function makeNameURL(name)
{return encodeURIComponent(name.toLowerCase().replace(/[ :\/]/g,"-").replace(/&/g,"and").replace(/é/g,"e")).replace(/[,.()']/g,"").replace(/%[A-F\d]{2}/g,"");}
function returnFalse()
{return false;}
