/**
  * Define the History Channel EPG
 **/
HC.epg = {

    /**
     * Private Constants
     */

    SITE_NAME           : "HISTORY", // bio/military history
    ENVIRONMENT_MODE    : "development",
    CACHE               : false,

    // use keyboard to control epg slider
    useKeyboard         : true,

    // define the channel instances
    CHANNELS            : [],
    currentChannel      : null,
    
    imagesScheduleDetail: '',

    // TODO: Is this used?
    duration            : "1440",

    // object to hold cache data
    _epg_data           : {},

    // date time constants
    timeZoneOffset      : "0",

    // channel listing vars
    start               : 0,
    currentPane         : 3,
    increment           : 1,

    // currentTimeOfDay pane
    currentPaneDay      : 0,

    iLastEnd            : 0,
    iLastStart          : 0,
    subpanelOffset      : 0,

    programs: [],

    init : function(startChannel, startDate, startEventId){

		this.startDate = startDate;
		
        HC.epg.currentChannel = startChannel;
        
        HC.epg.injectHTML();
        HC.epg.buildChannelNav();
        HC.epg.buildDayNav();
		
		// by default select the first week
		HC.epg.currentWeek = 1;
		HC.epg.weekSelector(HC.epg.currentWeek);

        // bind event clicks for slider and navigation
        HC.epg.bindEventHandlers();

        //try
        //{
            HC.epg.setProgram(startChannel, startDate, startEventId);
        //}
        //catch (err)
        //{
        //    HC.epg.setProgram("1875", new Date(), -1);
        //}

    },

    setProgram : function(channelId, date, eventId) {
        var channel = channelId;
        HC.epg.currentChannel = channel;
        HC.epg.selectNav.call($('#'+channel));

        HC.epg.currentPaneDay = HC.epg._epg_data[$.JSONDate(date)].position;
        HC.epg.selectNav.call($('.'+HC.epg.getDayClassFromDate(date)));

        HC.epg.getData($.JSONDate(date), channelId);

        if (eventId !== -1)
        {
            prog = HC.epg.programs[eventId];
            daySection = HC.epg.getDaySection(prog.startDate);

            HC.epg.currentPane = parseInt(daySection, 10);
            HC.epg.scrollToThis(daySection);
            HC.epg.selectNav.call($('#'+HC.epg.timeOfDay_format[daySection].text));

            prog.selectFunction();
        }
    },

    getDaySection: function(date)
    {
        var hour = date.getHours();

        var i = 1;

        while(i < HC.epg.timeOfDay_format.length)
        {
            if (hour < HC.epg.timeOfDay_format[i].hour)
                return i-1;

            i++;
        }

        //Goto last timespan
        return i-1;
    },

    getDayClassFromDate: function(date)
    {
		// no longer using prettyDates for day names i.e today, tomorrow
        // return prettyDate($.ISODate("c", date));
		var mnth = date.getMonth() + 1;
		return ("0"+date.getDate()).slice(-2) + ("0"+mnth).slice(-2);
    },

    injectHTML : function(){

        // inject EPG slider and programme details blocks
        $("#epgwrapper").append("<div id=\"slider\"></div>");
        $("#epgwrapper").append("<div id=\"detail\"></div>");

        // inject slider controls
        $("#slider").append("<div class=\"controls\"></div>");
        $("#slider").append("<div class=\"scroll\"></div>");

        // inject navigational elements (channels, days, timeofday)
        $(".controls").append("<ul id=\"channels\"></ul>");
        $(".controls").append("<ul id=\"navigation\"></ul>");
        $(".controls").append("<ul id=\"timeofday\"></ul>");

        // inject scroll container (form daily programme listing)
        $(".scroll").append("<div class=\"scrollContainer\"></div>");

        $("#timeofday").append("<li><a id=\""+HC.epg.timeOfDay_format[0].text+"\" href=\"#\"><span class=\"0\">"+HC.epg.timeOfDay_format[0].text+"</span></a></li>");
        $("#timeofday").append("<li><a id=\""+HC.epg.timeOfDay_format[1].text+"\" href=\"#\"><span class=\"1\">"+HC.epg.timeOfDay_format[1].text+"</span></a></li>");
        $("#timeofday").append("<li><a id=\""+HC.epg.timeOfDay_format[2].text+"\" href=\"#\"><span class=\"2\">"+HC.epg.timeOfDay_format[2].text+"</span></a></li>");
        $("#timeofday").append("<li><a id=\""+HC.epg.timeOfDay_format[3].text+"\" href=\"#\"><span class=\"3\">"+HC.epg.timeOfDay_format[3].text+"</span></a></li>");

        $(".controls").append("<div id=\"nav\"><a href=\"#\" class=\"prev\"><em></em></a><a href=\"#\" class=\"next\"><em></em></a></div>");

        // inject programme detail block elements
        $("#detail").append("<div id=\"media\"><img width=\"480\" height=\"270\" src=\"" + HC.epg.imagesScheduleDetail + "\"></div>");
        $("#detail").append("<div id=\"schedule\"></div>");
        $("#detail").append("<div id=\"actionBtns\"></div>");

        // programme schedule info
        $("#schedule").append("<h3></h3>");
        $("#schedule").append("<h4></h4>");
        $("#schedule").append("<p class=\"desc\"></p>");
        $("#schedule").append("<p class=\"showurl\"><a class=\"redArrowOnWhite\" href=\"\" title=\"\">"+HC.epg.epgStrings["visitShowTitle"]+"</a></p>");
        $("#schedule").append("<p class=\"biourl\"><a class=\"redArrowOnWhite\" href=\"\" title=\"\">"+HC.epg.epgStrings["visitBioTitle"]+"</a></p>");

        // remind me, sky+ record action buttons
        $("#actionBtns").append("<a class=\"btnWide skyRecordNonTransparent-2\" title=\""+HC.epg.epgStrings["recordTitle"]+"\" href=\"#\"><span class=\"fLeft\">"+HC.epg.epgStrings["recordBtn"]+"</span>&nbsp;<img src=\"" + HC.epg.imagesScheduleSkyRecord + "\" alt=\"sky\" /></a>");
        // $("#actionBtns").append("<a class=\"btnWide remindMeNonTransparent-2\" title=\"remind me\" href=\"#\">Remind me</a>");
        //$("#actionBtns").append("<a class=\"btnWide shareNonTransparent-2\" title=\"share this series\" href=\"#\">Share this series</a>");

    },

    buildChannelNav : function(){
        // create the channels navigation
        jQuery.each(HC.epg.CHANNELS, function(i,val) {
            $('#channels').append("<li><a id=\""+val.id+"\" class=\""+i+"\" href=\""+val.url+"\">"+val.title+"</a></li>");
        });
    },

    buildDayNav : function(){
		
		// tip out all the days and hide everything but week1
        for (var i = HC.epg.startDay; i < HC.epg.days; i++){
                // var x = new Date();
				var x = new Date(this.startDate);
                x.setDate(x.getDate()+i);
				
				var mnth = x.getMonth() + 1;
				
                rel = x.getFullYear() + HC.epg.addLeadingZero(mnth) + HC.epg.addLeadingZero(x.getDate());
                var y = x;
                var z = $.ISODate("c", y);
				

				// new day name format
				var daytitle = '<b>' + ("0"+x.getDate()).slice(-2) + '</b><sup>/' + ("0"+mnth).slice(-2) + '</sup>';
				var classname = ("0"+x.getDate()).slice(-2) + ("0"+mnth).slice(-2);
				
				// add a class so we can identify the weeks (wk2 is hidden on load by default)
				if(i <= 6) {
					wkClass = "wk1";
				} else {
					wkClass = "wk2";
				}
				
                $('#navigation').append("<li class=\""+wkClass+"\"><a href=\"#\" class=\""+classname+"\" rel=\""+rel+"0000\"><span class=\""+rel+"0000\">"+daytitle+"</span></a></li>");
                var f = rel+"0000";

                // generate a dateFrom value for parsing by showsInRange function
                dateFrom = new Date(x.getFullYear(),x.getMonth(),x.getUTCDate());

                // create and object for managing the days (also used for caching daily data)
                HC.epg._epg_data[f] = {"data" : null, "position" : i, "rel" : rel+"0000", "dateFrom" : dateFrom};
        }
		if(HC.epg.days > 7){
			// append the week selector
			$('#navigation').prepend("<li id=\"thisWeek\" class=\"wk2\"><a href=\"#\" class=\"selected\"><em><img src=\""+HC.epg.imagesNavThisWeekButton+"\">"+HC.epg.weekLinkTitle.wk1+"</em></a></li>");
			$('#navigation').append("<li id=\"nextWeek\" class=\"wk1\"><a href=\"#\" class=\"selected\"><em>"+HC.epg.weekLinkTitle.wk2+"<img src=\""+HC.epg.imagesNavNextWeekButton+"\"></em></a></li>");
		}
    },

	weekSelector : function(wk) {
		// set the global currentWeek
		HC.epg.currentWeek = wk;
		
		// hide/show the relevant week nav
		$("#navigation" + " li[class*=wk"+wk+"]").show();
		$("#navigation" + " li[class!=wk"+wk+"]").hide();

		// select the first day of the current week
		wk = $("#navigation" + " li:not([id*=this]):visible:first");
		
		// activate the first day and move the pane
		HC.epg.currentPaneDay = HC.epg._epg_data[wk[0].firstChild.rel].position;
        HC.epg.selectNav.call($('.'+wk[0].firstChild.className));
        HC.epg.getData(wk[0].firstChild.rel,HC.epg.currentChannel);
	
	},

    bindEventHandlers : function(){
        $("#channels li a").click(function(e){
            var channel = e.currentTarget.id;
            HC.epg.currentChannel = channel;
            HC.epg.selectNav.call($('#'+channel));
            HC.epg.getData(HC.epg.now,HC.epg.currentChannel);
            return false;
        });
        $("a.prev").bind("click", function(e){ HC.epg.moveSlider("prev");return false; });
        $("a.next").bind("click", function(e){ HC.epg.moveSlider("next");return false; });
        $("#navigation span").click(function(e){
            HC.epg.currentPaneDay = HC.epg._epg_data[e.currentTarget.className].position;
            HC.epg.selectNav.call($('.'+e.currentTarget.parentNode.className));
            HC.epg.getData(e.currentTarget.className,HC.epg.currentChannel);
            return false;
        });
        $("#timeofday span").click(function(e){
            HC.epg.currentPane = parseInt(e.currentTarget.className, 10);
            HC.epg.scrollToThis(e.currentTarget.className);
            HC.epg.selectNav.call($('#'+HC.epg.timeOfDay_format[e.currentTarget.className].text));
            return false;
        });
		$("#thisWeek").bind("click", function(e){
			HC.epg.currentWeek = 1;
			HC.epg.weekSelector(HC.epg.currentWeek);
			return false; 
		});
		$("#nextWeek").bind("click", function(e){
			HC.epg.currentWeek = 2;
			HC.epg.weekSelector(HC.epg.currentWeek);
			return false; 
		});
		
        if(HC.epg.useKeyboard){
            // bind left,right,up and down keypresses
            $(window).keydown(function (e) {

                if ((e.keyCode == 38 || e.keyCode == 39)) {
                    HC.epg.moveSlider("next");
                    return false;
                }

                if ((e.keyCode == 37 || e.keyCode == 40)) {
                    HC.epg.moveSlider("prev");
                    return false;
                }
            });
        }
    },

    moveSlider : function(e) {

        // setup the scroll object
        var $scroll = $('#slider .scroll').css('overflow', 'hidden');
		
        // TODO: tidy this
        if(e == 'prev'){
            if(HC.epg.currentPane > 0){
                HC.epg.currentPane = HC.epg.currentPane - HC.epg.increment;
                // init the scroll
                $scroll.stop().scrollTo( HC.epg.timeOfDay_format[HC.epg.currentPane].position, 500 );
            } else {
                // load the next bucket
                if(HC.epg.currentPaneDay > HC.epg.startDay){
                    HC.epg.currentPaneDay--;

					// if were in another switch to that week
					if(HC.epg.currentPaneDay < 7){
						if(HC.epg.currentWeek !== 1){
							HC.epg.currentWeek = 1;
							HC.epg.weekSelector(HC.epg.currentWeek);
						}
					}

                    var dt = new Date(this.startDate);
                    d.setDate(d.getDate()+HC.epg.currentPaneDay);
                    var jd = d;
                    var gotoDate = $.JSONDate(jd);

                    // highlight the correct day nav
                    var el = $('a[rel="'+gotoDate+'"]');
                    HC.epg.selectNav.call(el);

                    HC.epg.getData(gotoDate,HC.epg.currentChannel);

                    HC.epg.reset();
                }
            }
        }
        if(e == 'next'){
            if(HC.epg.currentPane < 3){
                HC.epg.currentPane = HC.epg.currentPane + HC.epg.increment;
                // init the scroll
                $scroll.stop().scrollTo( HC.epg.timeOfDay_format[HC.epg.currentPane].position, 500 );
            } else {
                // load the previous bucket
                if(HC.epg.currentPaneDay < (HC.epg.days - 1)){
					
					HC.epg.currentPaneDay++;
					
					// if were in another switch to that week
					if(HC.epg.currentPaneDay > 6){
						if(HC.epg.currentWeek !== 2){
							HC.epg.currentWeek = 2;
							HC.epg.weekSelector(HC.epg.currentWeek);
						}
					}

                    var dt = new Date(this.startDate);
                    d.setDate(d.getDate()+HC.epg.currentPaneDay);
                    var jd = d;
                    var gotoDate = $.JSONDate(jd);

                    // highlight the correct day nav
                    var el = $('a[rel="'+gotoDate+'"]');
                    HC.epg.selectNav.call(el);

                    HC.epg.getData(gotoDate,HC.epg.currentChannel);

                    HC.epg.reset();
                }
            }
        }

        // select the correct timeofday
        HC.epg.selectNav.call($('#'+HC.epg.timeOfDay_format[HC.epg.currentPane].text));

    },

    scrollToThis : function(value) {
        var $scroll = $('#slider .scroll').css('overflow', 'hidden');
        $scroll.stop().scrollTo( HC.epg.timeOfDay_format[value].position, 500 );
    },

    // handle day nav selection
    selectNav : function () {
        $(this)
            .parents('ul:first')
                .find('a')
                    .removeClass('selected')
                .end()
            .end()
            // select the nav item
            .addClass('selected');
    },

    getData : function(id,channel){

        HC.epg.reset();

        var $subcontainer = $('.scrollContainer');
        $subcontainer.empty();
        $subcontainer.html('fetching data...');

        // reset
        currentPane = 0;

        // LShift implementation need to do something like this
        // get the data (dataType = jsonp)

        // HC.epg.fetchJSONP(id);
        HC.epg.dataManager.get(id,channel);

    },

    // create the channel blocks for each selected day
    addChannels : function (id,data,fromDate){

        var $subcontainer = $('.scrollContainer');
        $subcontainer.fadeOut("fast");

        $subcontainer.empty();

        $subcontainer.append("<ul id=\"programmes\" class=\"panel"+id+"\"></ul>");
        var $daypanel = $('.panel'+id);

        var NOW = new Date(this.startDate);
        NOW = $.JSONDate(fromDate);

        HC.epg.subpanelOffset = 0;
        
        var activeProgramme = null;
		var hours = null;

        jQuery.each(data, function(i, val) {

            // d = new Date(parseInt(val.start, 10)); 
 	                // to force UTC time - replace .getTime()-1000*60*60 with just .getTime()
 	                d = new Date((new Date(parseInt(val.start, 10))).getTime()-1000*60*60), 
            duration = val.dur;
            fdate = $.JSONDate(d);
            fulldate = $.JSONFullDate(d);

            // get program start and end
            // used to highlight currently showin program
            var PROG_DURATION   = (val.dur) * 1000;
            var PROG_START      = val.start;
            var PROG_END        = parseInt(PROG_START, 10) + parseInt(PROG_DURATION, 10);
            var PROG_HOURS      = d.getUTCHours();

			hours = hours + PROG_DURATION;

            // calculate show starttime
            var showStart = new Date(parseInt(val.start, 10));
            var showTime = new Date(showStart);
            // calculate the show endtime (start + duration)
            var t = showTime.setMinutes(showTime.getMinutes() + parseInt(val.dur, 10));
            // fromDate is passed to the function (it is the current days start time ie 00.00 hours)
            var f = fromDate.getTime();

            //################## INSERTED
            //work out whether there is a thumbnail for the program and if so set an image for its inner HTML
            if (val.thumbnailURL !== null)
                progImageHTML = "<img src=\"" + val.thumbnailURL + "\" />";
            else
                progImageHTML = "<div style=\"width : 153px; height : 86px\" />";
//TODO: set default image here DL
			
            // if the program is scheduled outside of current day 24hour period
            if(!(fdate >= NOW)){
				
                if(t > f) {
					
                    // work out the difference and add the extra time to the EPG
                    diff = ((t - f) / 1000);
                    timediff = Math.floor(diff / 60);

                    $daypanel.append("<li id=\"subpanel_"+i+"\" class=\"subpanel prog"+timediff+"\"><a href=\"#\" rel=\""+val.eventId+"\"><span class=\"time\">"+HC.epg.formatTime(d)+"</span><span class=\"progImage\">" + progImageHTML + "<span class=\"\"></span></span><span class=\"title\">"+val.title+"</span></a></li>");
                }

            } else {
				
                // TODO: deal with 'programme data unvailable'
                diff = ((t - f) / 1000);
                timediff = Math.floor(diff / 60);

                var E = showTime.getTime();
                var S = showStart.getTime();

                if(HC.epg.iLastEnd === 0) {
                    HC.epg.iLastEnd = E;
                }

                if ((S) > (HC.epg.iLastEnd)) {

                    var Q = "Programme information unavailable";
                    var P = (((S - HC.epg.iLastEnd) / 1000) / 60 / 60) * 164;

                    HC.epg.subpanelOffset = P;

                    $daypanel.append("<li id=\"subpanel_"+i+"\" class=\"subpanel offair\" style=\"width:" + P + "px\"><span class=\"text\">"+Q+"</span></li>");

                } else {

					// calculate panel width based on duration and apply to the href
					//  I used this figure [2.56] as it gave the best results across the days
					var w = Math.round(duration * 2.56);
                    $daypanel.append("<li id=\"subpanel_"+i+"\" class=\"subpanel progGeneric\"><a href=\"#\" rel=\""+val.eventId+"\" style=\"width:" + w + "px\"><span class=\"time\">"+HC.epg.formatTime(d)+"</span><span class=\"progImage\">" + progImageHTML + "<span class=\"\"></span></span><span class=\"title\">"+val.title+"</span></a></li>");

                    //if( (NOW > PROG_START) && (NOW < PROG_END) ){

                    if (activeProgramme === null && (PROG_HOURS >= 18)) {
                        activeProgramme = i;
                        $('li > .subpanel').removeClass('selected');
                    }

                    selectProg = function(){
                        HC.epg.updateProgramDetails(val);
                        $('li').removeClass('selected');
                        $('.progImage > span').removeClass('imgBorderActive');
                        $('li#subpanel_'+i).addClass('selected');
                        $('li#subpanel_'+i+' a .progImage span').addClass('imgBorderActive');
                        return false;
                    };

                    HC.epg.programs[val.eventId] = {
                            selectFunction : selectProg,
                            startDate : showStart,
                            data : val
                    };

                    $('#subpanel_'+i+' a').bind('click', selectProg);

                }

                HC.epg.iLastEnd = E;
                HC.epg.iLastStart = S;

            }
        });

        var $subpanels = $('.panel'+id+' > .subpanel');

        // calculate a new width for the bucket container
        $daypanel.css({
            'float' : 'left',
            'position' : 'relative', // IE fix to ensure overflow is hidden
            'width' : ((446 * $subpanels.length) + HC.epg.subpanelOffset)
        });

        $subcontainer.fadeIn("slow");

        // hightlight the current program
        if (activeProgramme !== null) {
        	$('li#subpanel_'+activeProgramme+' a').click();
        }
        HC.epg.currentPane = 3;
        HC.epg.scrollToThis(HC.epg.currentPane);

        HC.epg.iLastEnd = 0;
        HC.epg.iLastStart = 0;

    },
    
    updateProgramDetails: function(obj) {
		
        var d = new Date(parseInt(obj.start, 10));
		// var d = new Date(parseInt(obj.start, 10)); 
 		// to force UTC time - replace .getTime()-1000*60*60 with just .getTime()
 		var d = new Date((new Date(parseInt(obj.start, 10))).getTime()-1000*60*60); 
 		var mediaHTML = "";
        if (obj.detailImageURL !== null)
            mediaHTML = '<img width=\"480\" height=\"270\" src=\"' + obj.detailImageURL + '\">';

        $('#detail #media').html(mediaHTML);
        $('#detail #schedule h3').html(obj.title);
        $('#detail #schedule h4').html(HC.epg.formatDate(d));
        $('#detail #schedule p.desc').html(obj.shortDesc);

		// if the eventId is not passed hide the record with sky button for this show
		if(!obj.eventId){
			$('#actionBtns .skyRecordNonTransparent-2').hide();
		} else {
			// else: generate record now link and open in new window
	        $('#detail #actionBtns .skyRecordNonTransparent-2').attr({
				'href'		:'http://tv.sky.com/tvlistings?channelId='+obj.channelId+'&eventId='+obj.eventId,
				'target' 	:'_blank'
			});
		}
		
        var pageLink = function (selector, url) {
            if (url == null) {
                $(selector).attr("style", "display : none;");
            } else {
                $(selector).attr("style", "");
                $(selector + ' a').attr("href", url);
            }
        };
        
        pageLink('#detail #schedule p.showurl', obj.showPageURL);
        pageLink('#detail #schedule p.biourl', obj.bioPageURL);

		/* generate sharethis object for this show
		 var object = SHARETHIS.addEntry({
			title: obj.title,
			summary: obj.shortDesc,
			icon: obj.detailImageURL,
			url: obj.showPageURL
			}, {button:false,onclick:HC.epg.shareThisCallback});

		var element = document.getElementById("sharethis_0");
		object.attachButton(element);

    },

	shareThisCallback: function(SharedObject) {
		// need to find a way to stop the page jump
		*/
	},

    // add AM or PM
    formatTime : function(date) {
        var hour = HC.epg.addLeadingZero(date.getHours()),
            min = HC.epg.addLeadingZero(date.getMinutes()) + "";

         //return html encoded hour + ':' + min + ' ' + ampm;
        return "<span class=\"hr\">"+hour+"</span><sup>:"+min+"</sup>";
    },

    // format the date/time for programme details i.e TUESDAY 18:00
    formatDate : function(date) {
        var dayNames = HC.epg.dayNames;
        return dayNames[date.getDay(date)] + ' ' + HC.epg.addLeadingZero(date.getHours()) + ':' + HC.epg.addLeadingZero(date.getMinutes());
    },

    // add leading zero (for programme listing)
    addLeadingZero : function(iValue) {
        if (iValue < 10) {
            iValue = ("0" + iValue);
        }
        return iValue;
    },

    // reset to default values
    reset : function() {

        // move the slider to default timeofday
        HC.epg.currentPane = 3;

        // select the correct timeofday
        HC.epg.selectNav.call($('#'+HC.epg.timeOfDay_format[HC.epg.currentPane].text));
        // setup the scroll object
        HC.epg.scrollToThis(HC.epg.currentPane);
    }

};

/**
  * CACHE JSON REQUESTS
 **/
HC.epg.dataManager = {

    cache_epg_data  : HC.epg._epg_data,
    cache_epg_item  : null,
    show_getter : null,

    init : function(show_getter) {
        this.show_getter = show_getter;
    },

    get : function(id, channelId) {

        // set the item to cache
        this.cache_epg_item = id;

        var c = HC.epg.dataManager.cache_epg_data[id].data;
        var d = HC.epg.dataManager.cache_epg_data[id].dateFrom;

        // if cache is false and there is no cache data for this day perform JSON API query
        if ((HC.epg.CACHE === false) || (c === null)) {

        	var dInt = d.getTime();
            var shows = HC.epg.dataManager.show_getter(dInt, dInt + 60000*HC.epg.duration, channelId);

            HC.epg.addChannels(HC.epg.currentChannel, shows, d);
            HC.epg.dataManager.cache_epg_data[''+this.cache_epg_item+''].data = shows;

        } else {
            // add the channels using the cached value
            HC.epg.addChannels(HC.epg.currentChannel,c,d);
        }
    }

};

HC.epg.start = function(config) {
    
	// images used in EPG 
	HC.epg.imagesScheduleDetail = config.res.imagesScheduleDetail;
	HC.epg.imagesScheduleSkyRecord = config.res.imagesScheduleSkyRecord;
	HC.epg.imagesNavThisWeekButton = config.res.imagesNavThisWeekButton;
	HC.epg.imagesNavNextWeekButton = config.res.imagesNavNextWeekButton;
	
    HC.epg.CHANNELS = config.channels;
    HC.epg.dataManager.init(config.showGetter);

	// new configs added
	HC.epg.days = config.days;
	HC.epg.startDay = config.startDay;
	HC.epg.weekLinkTitle = config.weekLinkTitle;
	HC.epg.dayNames = config.dayNames;
	HC.epg.epgStrings = config.epgStrings;
	HC.epg.timeOfDay_format = config.timeOfDay_format;
	
	HC.epg.isoDate = $.ISODate("c", config.startDate);
    // just returns a date in a format required to call Sky JSON data for today
    HC.epg.now = $.JSONDate(config.startDate);

    HC.epg.init(config.startChannel, config.startDate, config.startEventId);
}

