var C4=C4||{};
C4.Controller=(function controllerInit($){var _ESCAPECHARACTER="@%_#",_VIEWPREFERENCECOOKIE="enhancedEnabled",Events={},Views={Daily:{getViewPreference:function getViewPreference(){return($.cookie(_VIEWPREFERENCECOOKIE))
},setViewPreference:function setViewPreference(pref){if(pref=="unenhanced"){$.cookie(_VIEWPREFERENCECOOKIE,"false",{path:"/"})
}else{$.cookie(_VIEWPREFERENCECOOKIE,"true",{path:"/"})
}},isShowEnhanced:function isShowEnhanced(){var state;
switch(Views.Daily.getViewPreference()){case"true":state="enhanced";
break;
case"false":state="unenhanced";
break;
default:state="unset"
}return(state)
},getTodaysDate:function(){return $("#todaysDate").text()
},getSelectedDate:function(){return $("#selectedDate").text()
},getUpperTxDate:function(){return $("#upperTxDate").text()
},getLowerTxDate:function(){return $("#lowerTxDate").text()
},getEpgContextPath:function(){return $("#epgContextPath").text()
},getTitle:function getTitle(_slot){var html=$(_slot).find("h3");
var $html=$(html).clone().find("span").remove().end();
return $.trim($html.text())
},getStartTime:function getStartTime(_slot){return $(_slot).find("h3 .slotTime").text()
},getDuration:function getDuration(_slot){var duration=$(_slot).find(".slotTime").attr("title").split(":");
duration=$.trim(duration[duration.length-1]);
return duration
},getPrimaryImageUrl:function getPrimaryImageUrl(_slot){return $(_slot).metadata().primaryImageUrl
},getLightboxImageUrl:function getLightboxImageUrl(_slot){return $(_slot).metadata().lightboxImageUrl
},getSynopsis:function getSynopsis(_slot){return $.trim($(_slot).find(".synopsis").text())
},getCategories:function getCategories(_slot){var list=[];
$(_slot).find(".categories li").each(function categoryIterator(){var $this=$(this);
list.push({title:$.trim($this.text()),url:$this.find("a").attr("href")})
});
return list
},getIndicators:function getIndicators(_slot){var indicators=[];
$(_slot).find(".indicators span").each(function(){var $this=$(this);
indicators.push({abbr:$.trim($this.text()),name:$.trim($this.attr("title"))})
});
return indicators
},getExternalLink:function getExternalLink(_slot){var $link=$(_slot).find("h3 a");
return{href:$link.attr("href")}
},getCatchupUrl:function getCatchupUrl(_slot){var $link=$(_slot).find(".fourOnDemandLink");
return $link.length>0?$link.attr("href"):false
},getSlot:function getSlot(_slot){var $slot=$(_slot);
with(Views.Daily){var slot={startTime:getStartTime($slot),duration:getDuration($slot),slotTitle:getTitle($slot),programmeUrl:getExternalLink($slot),primaryImageUrl:getPrimaryImageUrl($slot),lightboxImageUrl:getLightboxImageUrl($slot),synopsis:getSynopsis($slot),indicators:getIndicators($slot),categories:getCategories($slot)};
var catchupUrl=getCatchupUrl($slot);
if(catchupUrl){slot.catchupUrl=catchupUrl
}return slot
}},getSlots:function getSlots(channelId){with(Views.Daily){var slots=[];
$("#SlotList_"+channelId+" .slot").each(function(){slots.push(getSlot($(this)))
});
return slots
}},getLogoUrl:function getLogoUrl(channelId){return $("#ChannelSelector"+channelId+" img").attr("src")
},getChannel:function getChannel(channelName,channelId){with(Views.Daily){return{name:channelName,slots:getSlots(channelId),logoUrl:getLogoUrl(channelId)}
}},getChannels:function getChannels(){with(Views.Daily){var channels=[getChannel("Channel 4","C4"),getChannel("E4","E4"),getChannel("More 4","M4"),getChannel("Film 4","F4"),getChannel("4 Music","4M")];
return channels
}},getNowOnData:function getNowOnData(){return{channel:window.location.hash.substring(1,3),slot:window.location.hash.substring(4)}
},buildProgrammeInformation:function buildProgrammeInformation(){with(Views.Daily){return{todaysDate:getTodaysDate(),selectedDate:getSelectedDate(),upperTxDate:getUpperTxDate(),lowerTxDate:getLowerTxDate(),channels:getChannels()}
}},initialiseFlash:function initialiseFlash(programmeInformation){var ampEsc="c4_flash_amp";
var PARAMS={salign:"top",allowFullScreen:"true",bgcolor:"#FFFFFF",allowScriptAccess:"always",wmode:"opaque"},ATTRIBUTES={id:"DailyView"},flashvars={strQuotesEscapeChar:_ESCAPECHARACTER,programmeInformation:encodeURIComponent($.toJSON(programmeInformation)),contextPath:encodeURIComponent(Views.Daily.getEpgContextPath()),startHour:window.location.hash.substring(4,6),nowOn:encodeURIComponent($.toJSON(Views.Daily.getNowOnData()))};
swfHeight=770;
$("#flashWrapper").css({minHeight:swfHeight});
swfobject.embedSWF(swfRoot+"asset/flash/swf/epg/daily.swf","flashContainer","960 ",swfHeight,"9.0.0",swfRoot+"asset/flash/swf/expressInstall.swf",flashvars,PARAMS,ATTRIBUTES)
},hidePOSH:function hidePOSH(){$("#EpgContainer, h1").hide()
},appendPOSH:function appendPOSH(){$("#EpgContainer, h1").appendTo("#flashContainer").show()
},addVersionButton:function addVersionButton(versionType,whereToPutIt){var versionText;
switch(versionType){case"enhanced":versionText="Enhanced";
break;
case"unenhanced":versionText="Accessible";
break;
default:versionText="Enhanced"
}$(whereToPutIt).before($('<a class="boxLink" href="#" id="EnableFlashButton">'+versionText+" Version</a>").click(function(){Views.Daily.setViewPreference(versionType);
if(versionType==="enhanced"){Views.Daily.initialiseFlash(Views.Daily.buildProgrammeInformation());
Views.Daily.hidePOSH()
}else{document.location.href=document.location
}}))
},hideFlashButton:function hideFlashButton(){$("#EnableFlashButton").addClass("accessibility")
},enhanceProgrammeInformation:function enhanceProgrammeInformation(){if(Views.Daily.isShowEnhanced()==="unenhanced"){Views.Daily.addVersionButton("enhanced",'#EpgContainer *:first:not("#outOfRangeMessage")')
}if(Views.Daily.isShowEnhanced()==="enhanced"&&($("#outOfRangeMessage").length<1)){$("#flashContainer").prepend('<div>For more content and an enhanced view of this page <a href="http://get.adobe.com/flashplayer/">click here to install or upgrade Flash</a>. </div>');
Views.Daily.addVersionButton("unenhanced","#flashWrapper");
Views.Daily.hideFlashButton();
Views.Daily.appendPOSH();
Views.Daily.initialiseFlash(Views.Daily.buildProgrammeInformation())
}},showProgrammeInformation:function showProgrammeInformation(){if(Views.Daily.isShowEnhanced()==="unset"){Views.Daily.setViewPreference()
}if((Views.Daily.isShowEnhanced()==="unenhanced"||Views.Daily.isShowEnhanced()==="unset")||($("#outOfRangeMessage").length>0)){$("#EpgContainer, h1").show()
}$("#c4footer").css("margin-top","0")
}}};
return{init:function epgDailyController(){C4.OO.insistRequired({object:$,names:["toJSON","cookie","metadata"]},{});
Views.Daily.showProgrammeInformation();
Views.Daily.enhanceProgrammeInformation()
},_set$:function(obj){$=obj
},_setViews:function(obj){Views=obj
},_getViews:function(){return Views
},_reset:(function(){var ref$=$,refViews=Views;
return function(){$=ref$;
Views=refViews
}
})()}
})(jQuery);