var C4=C4||{};
C4.Controller=(function catchUpControllerInit(V){var S="@%_#",Z="videoFinderResults",Q="TIMERANGECOOKIE",N=/chrome/.test(navigator.userAgent.toLowerCase()),j={onClickTimeRangeButton:function K(){var m=f.getViewPreference()?"primeTime":"showAll";
f.setViewPreference(m);
f.enhanceBrowseByDateList();
return false
},onClickBrowseByDateLink:function d(m){f.setOmnitureCookie("omn_intcmp","4oDpage_browsebydate")
},onClickBrowseAtoZ:function X(m){f.setOmnitureCookie("omn_intcmp","4oDpage_explore")
}},f={getCarouselSet:function W(o){var n=[];
o.find("li").each(function m(){var p=V(this);
n.push({title:V.trim(p.find("span.title").text()),url:p.find("h3 a").attr("href"),img:{src:p.find("h3 a").metadata().img_lrgSrc,alt:p.find("h3 a").metadata().img_lrgAlt},synopsis:V.trim(p.find("p.synopsis").text())})
});
return n
},getDateScrollerData:function U(){$this=V("#programmeInformation").find("h3");
var m={todaysDate:$this.metadata().todaysDate,selectedDate:$this.metadata().selectedDate,upperTxDate:$this.metadata().upperTxDate,lowerTxDate:$this.metadata().lowerTxDate};
return m
},getCarouselData:function I(){var n=[];
V("#fourOnDemandHighlights li.fourOnDemandSet").each(function m(){var o=V(this);
n.push({setTitle:V.trim(o.find("h2").text()),setWebSafeTitle:o.find("h2").metadata().setWebSafeTitle,set:f.getCarouselSet(o)})
});
return n
},enhanceCarouselInfo:function E(){if(document.getElementById("fourOnDemandHighlights")){V("#flashContainer").prepend('<div>For an enhanced view of this page <a href="http://get.adobe.com/flashplayer/">click here to install or upgrade Flash</a>. </div>');
f.moveCarouselPOSH();
f.initialiseBrandCarousel(f.getCarouselData())
}},initialiseBrandCarousel:function l(p){var o={salign:"top",allowFullScreen:"false",bgcolor:"#FFFFFF",allowScriptAccess:"always",wmode:"opaque"},n={id:"fourOnDemandCarousel"},m={fourondemand:encodeURIComponent(V.toJSON(p))};
swfobject.embedSWF(swfRoot+"asset/flash/swf/fourondemandcarousel.swf","flashContainer",940,360,"9.0.115",swfRoot+"asset/flash/swf/expressInstall.swf",m,o,n)
},moveCarouselPOSH:function c(){if(swfobject.hasFlashPlayerVersion("9.0.115")){V("h1").addClass("accessibility");
V("#fourOnDemandHighlights").addClass("accessibility");
V("#fourOnDemandCarouselContainer").addClass("flashEnabled");
V("#fourOnDemandCarouselContainer").before('<p class="accessibility">Flash content: <a href="#fourOnDemandHighlights">skip to accessible alternative</a></p>')
}},getVideoFinderOptions:function O(){var m={resultsId:Z,overClass:"videoResultOver",loadingClass:"c4acLoading",cacheLength:50,mustMatch:false,formatMatch:null,matchContains:true,maxItemsToShow:"7",minChars:2,delay:0,width:370,showInPlace:true,omnitureCookie:"omn_intcmp=4oDpage_videofinder",omnitureProp8:"4oD",moreResultsMessage:'<p class="message-banner">THERE ARE MORE THAN 7 RESULTS - KEEP TYPING TO REFINE THE LIST</p>',noResultsMessage:'<p class="message no-results">NO VIDEO MATCHES </p>',formatItem:function(AC,r,t,n){var u=AC.value.toLowerCase();
var x=u.indexOf(n,0);
var y=u.charAt(x+n.length+1);
var s=[n,"-"+n," "+n,n+" "],w=AC.value.toLowerCase(),v=-1,p=[],r;
for(r=0;
r<s.length;
r++){var z=s[r];
v=w.indexOf(s[r]);
if(r==0){if(v==0){break
}}else{if(v>-1){v++;
break
}}}var o=AC.value.substr(0,v);
var AA=AC.value.substr(v,n.length);
var AB=AC.value.substr(v+n.length);
if(AA.indexOf(" ")!=-1){if(AA.charAt(0)===" "){o+=" "
}if(AA.charAt(AA.length-1)===" "){AB=" "+AB
}AA=V.trim(AA)
}p.push("<p>");
if(v>-1){p.push(o,"<em>",AA,"</em>",AB)
}else{p.push(AC.value)
}p.push("</p>");
return p.join("")
}};
return m
},enhanceDateScroller:function h(m){V("#browseByDateContainer").html('<p>For an enhanced view of this page <a href="http://get.adobe.com/flashplayer/">click here to install or upgrade Flash</a>.</p>');
f.initialiseDateScroller(f.getDateScrollerData())
},initialiseDateScroller:function Y(o){var p={salign:"top",allowFullScreen:"false",bgcolor:"#FFFFFF",allowScriptAccess:"always",wmode:"opaque"},n={id:"browseByDate"},m={dateRange:encodeURIComponent(V.toJSON(o))};
swfobject.embedSWF(swfRoot+"asset/flash/swf/browseByDate.swf","browseByDateContainer",476,76,"9.0.115",swfRoot+"asset/flash/swf/expressInstall.swf",m,p,n)
},enhanceBrowseByDateList:function a(){var n=f.getViewPreference()?"showAll":"primeTime";
var m=f.getPreSwitchover();
if(m==="false"){f.toggleBrowseByDateView(n);
if(V("#timeRangeButton").length==0){f.createTimeRangeButton(n)
}else{V("#timeRangeButton").text(f.setButtonText(n))
}}else{n="showAll"
}f.setTimeStateText(n,m);
f.enhanceSubtitlesIndicator()
},toggleBrowseByDateView:function B(n){var m=V("#programmeInformation > ul > li");
(n=="primeTime")?m.filter(":not(.primeTime)").hide():m.show()
},createTimeRangeButton:function e(m){var n=f.setButtonText(m);
V("#programmeInformation").find("h3").prepend('<a href="#" id="timeRangeButton">'+n+"</a>");
V("#timeRangeButton").click(j.onClickTimeRangeButton)
},setButtonText:function H(m){return(m=="primeTime")?"SHOW ALL":"SHOW PRIME-TIME"
},getPreSwitchover:function b(){return V("#programmeInformation").find("h3").metadata().preSwitchover
},getViewPreference:function R(){return(V.cookie(Q))
},setViewPreference:function A(m){if(m=="showAll"){V.cookie(Q,"showAll",{path:"/"})
}else{V.cookie(Q,null,{path:"/"})
}},fetchProgrammeInformation:function F(m){var n="/programmes/4od/episode-list/date/";
V.ajax({type:"GET",url:n+m,dataType:"html",success:function(o){V("#programmeInformation").html(o);
f.enhanceBrowseByDateList(false);
requestStatus="success";
V("#browseByDate")[0].dateResult(requestStatus)
},error:function(){requestStatus="fail";
V("#browseByDate")[0].dateResult(requestStatus);
V("#programmeInformation").html("<p>There was an error fetching the programme information - please try again by refreshing this page, or <a href='http://help.channel4.com/SRVS/CGI-BIN/WEBCGI.EXE/,/?St=333,E=0000000000178520161,K=5919,Sxi=0,COMMAND=NEW,T=PROBLEM2,VARSET_PA=CatchUp,VARSET_SHOWALL=N'>have a look at the 4oD FAQs</a>, or you can <a href='http://help.channel4.com/SRVS/CGI-BIN/WEBCGI.EXE/,/?St=338,E=0000000000178520247,K=2382,Sxi=0,USETEMPLATE=contact_CatchUp.tem'>contact us</a>.</p>")
}})
},setTimeStateText:function J(m,o){var n="";
if(m==="primeTime"&&o==="false"){n=" - Prime-time Programmes"
}else{if(m==="showAll"||(m==="primeTime"&&o==="true")){n=" - All Programmes"
}}if(V("span#timeState").length===0){V("#programmeInformation h3").append("<span id='timeState'>"+n+"</span>")
}V("span#timeState").text(n)
},setTimeStateTextAfterClick:function g(m){if(m==="primeTime"){timeStateStr=" - Prime-time Programmes"
}else{if(m==="showAll"){timeStateStr=" - All Programmes"
}}if(V("span#timeState").length===0){V("#programmeInformation h3").append("<span id='timeState'>"+timeStateStr+"</span>")
}V("span#timeState").text(timeStateStr)
},enhance4oDSponsorship:function M(){V(function(){if((V("#c4ad-Top1 img").length>0&&V("#c4ad-Top1 img").attr("src").indexOf("empty.gif")!=-1)||(V("#fourOnDemandCarouselContainer").length===0)){V("#c4ad-Top1").remove()
}else{V("#c4ad-Top1").show()
}})
},setOmnitureCookie:function k(m,n){V.cookie(m,n)
},enhanceMPU:function P(){V("#c4ad-Middle1").addClass("moduleContainer");
V(function(){var m=V("#c4ad-Middle1").is(":empty");
var n=false;
if(V("#c4ad-Middle1 img").length>0){n=V("#c4ad-Middle1 img").attr("src").indexOf("empty.gif")!=-1
}if(m||n){V("#c4ad-Middle1").remove()
}else{V("#c4ad-Middle1").show()
}if(V("#c4ad-Middle1 img").width()===1){V("#c4ad-Middle1 img").remove()
}})
},enhanceBottomAdsIE:function T(){if(V.browser.msie&&V.browser.version>6&&V.browser.msie<8){V("#c4adFrame-BottomLeft").css("position","absolute").css("top","0");
V("#c4adFrame-BottomRight").css("padding-bottom","20px")
}},enhanceSubtitlesIndicator:function G(){if(V.browser.msie&&V.browser.version<8){V("h4.arrowLink").addClass("ie")
}},setPageReady:function C(){V("#fourOnDemandCarousel")[0].setPageReady()
}};
return{init:function L(){C4.OO.insistRequired({object:V,names:["outerHtml","metadata","setadata","toJSON"]},{object:swfobject,names:["embedSWF"]});
swfobject.onDomLoad();
f.enhanceBrowseByDateList(false);
C4.Brand.Views.addAutocompleteForm();
var m=false;
C4searchAutocomplete("#videoFinderText",Z,m,f.getVideoFinderOptions(),"/programmes/long-form-search/");
f.enhanceCarouselInfo();
f.enhanceDateScroller();
f.enhance4oDSponsorship();
f.enhanceMPU();
f.enhanceSubtitlesIndicator();
V("div#programmeInformation h4.arrowLink a").click(j.onClickBrowseByDateLink);
V("ul#AtoZNavigation a").click(j.onClickBrowseAtoZ);
V(f.setPageReady)
},dateRequest:function D(m){f.fetchProgrammeInformation(m)
},_getOmnitureVars:function i(){return"s_prop22="+c4props("vertical")+"|s_account="+s_account+"|mypage="+document.location.href
},_set$:function(m){V=m
},_setViews:function(m){f=m
},_getViews:function(){return f
},_setEvents:function(m){j=m
},_getEvents:function(){return j
},_reset:(function(){var m=V,n=f,o=j;
return function(){V=m;
f=n;
j=o
}
})()}
})(jQuery);