/******************************************************************************
What's On Module Singleton
******************************************************************************/


PBS.tvschedules.whatson = function () {


    /* Private attributes and methods. */


    /* Public Object */
    var obj = {
        zip: '',
        //transport: '',
        navigation_transport: '',
        headend_id: '',
        tvdata_names: [],
        matrix: {},

        // The defaults for the module implementation customizations.
        // These can be overridden by the query string to the module JS include.
        config: {
            'localization': '',
            'provider': 'enabled',
            'favorites': 'enabled',
            'timeblocks': 'both',
            'feed_id': '',
            'tvlink': 'enabled',
            'shop': 'enabled',
            'content': ''
        },

        // These are empty strings because we need them to evaluate to false AND
        // be safe to send over GET without converting to a true value like
        // the strings 'false' or 'undefined'.
        localization_headend_id: '',
        provider_finder: '',
        disable_favorites: '',
        disable_tvlink: '',
        disable_shop: '',


        /* Event chains. */
        // Initialize the module!
        init: function () {
            PBS.tvschedules.whatson.initChain.go();
        },


        // This event chain initiaslizes the What's On module.
        // It manages default module configuration, implementation
        // customizations, and reacts appropriately to user state. 
        // If we don't have what we need in terms of localization, we preempt
        // module initialization with a stationfinder entry point.
        initChain: function () {
            var obj = {

                script_id: 'PBS.tvschedules.whatson.script',
                
                script_dom_node: null,
                
                // The defaults for the module implementation customizations.
                // These can be overridden by the query string to the module JS
                // include.
                config: {},
                
                go: function () {
                    this.customizations();
                },

                customizations: function () {

                    // Grab the implementation customizations from the module query 
                    // string (the query string of the module JS include, not 
                    // the including page).
                    this.script_dom_node = YAHOO.util.Dom.get(this.script_id);
                    var module_params = PBS.tvschedules.parseURL(this.script_dom_node.src);
                    for (key in module_params) {
                        value = module_params[key];
                        PBS.tvschedules.whatson.config[key] = value;
                    }
                    if (
                        (PBS.tvschedules.whatson.config['provider'] != 'disabled') &&
                        (PBS.tvschedules.whatson.config['localization'])
                    ) {
                        PBS.tvschedules.whatson.provider_finder = true;
                    }
                    if (PBS.tvschedules.whatson.config['favorites'] != 'enabled') {
                        PBS.tvschedules.whatson.disable_favorites = true;
                    }
                    if (PBS.tvschedules.whatson.config['tvlink'] != 'enabled') {
                        PBS.tvschedules.whatson.disable_tvlink = true;
                    }
                    if (PBS.tvschedules.whatson.config['shop'] != 'enabled') {
                        PBS.tvschedules.whatson.disable_shop = true;
                    }

                    // Grab the overrides from the query string of the including page.
                    var url = document.location.toString();
                    var page_params = PBS.tvschedules.parseURL(url);
                    PBS.tvschedules.user.page_params = page_params;

                    // Auto-localization.$
                    if (
                        PBS.tvschedules.whatson.config['localization'] &&
                        !PBS.tvschedules.user.cookies['pbsol.station']
                    ) {
                        //alert('autolocalize!');
                        PBS.tvschedules.cookie.setCookieValue(
                            'pbsol.station',
                            PBS.tvschedules.whatson.config['localization'],
                            'PBS.tvschedules.whatson.initChain.autolocalizeCallback'
                        );
                    // Stationfinder localization.
                    } else if (
                        (PBS.tvschedules.user.cookies['pbsol.station'] == '') &&
                        (PBS.tvschedules.whatson.config['localization'] == '') &&
                        !page_params['pbs_tvsm_sf_station']
                    ) {
                        // If we don't have a station value from the cookie, module 
                        // query string, or page query string, we'd better render 
                        // the stationfinder in-page zip entry.
                        this.stationFinder();
                    } else {
                        // Fire up the What's On module.
                        this.initHTML();
                    }

                },

                autolocalizeCallback: function (args) {
                    PBS.tvschedules.whatson.initChain.initHTML();
                },


                stationFinder: function () {
                    var URL = PBS.tvschedules.config.base + '/py-publisher/modules/dtv/stationfinder/' + PBS.tvschedules.config.version + '/views/zipInPage';
                    var callback = 'PBS.tvschedules.whatson.initChain.stationFinderCallback';
                    PBS.tvschedules.data.get(URL, callback, {});
                },

                stationFinderCallback: function (args) {
                    var markup = args['markup'];
                    var div = document.createElement('div');
                    div.id = 'pbs_tvschedules_modules-whatson-zip_in_page_fab';
                    div.style.display = 'none';
                    div.innerHTML = markup;
                    PBS.tvschedules.stationfinder.zip_in_page_dom_ids.push(div.id);
                    this.script_dom_node.parentNode.insertBefore(div, this.script_dom_node);
                    div.style.display = 'block';
                    var zip_entry = document.getElementById('pbs_tvschedules_modules-sf-zipcode_input');
                    zip_entry.onkeydown = PBS.tvschedules.stationfinder.submitZip;
                },

                // Use the data utility to get the initial UI from the server.
                initHTML: function () {

                    // Extract query string arguments to support browsers that can't set cookies across domains.
                    // If there are no query string arguments, then this data
                    // will be extracted by the server component from the cookie
                    // data in the request headers.
                    var query_params = PBS.tvschedules.parseURL(document.location.toString());
                    PBS.tvschedules.whatson.getParams();

                    var zip = '';
                    if (query_params['pbs_tvsm_sf_zip']) {
                        zip = query_params['pbs_tvsm_sf_zip'];
                    }

                    var headend_id = '';
                    if (query_params['pbs_tvsm_sf_headend_id']) {
                        headend_id = query_params['pbs_tvsm_sf_headend_id'];
                    }

                    //var transport = PBS.tvschedules.whatson.transport;
                    //if (query_params['pbs_tvsm_sf_transport']) {
                        //transport = query_params['pbs_tvsm_sf_transport'];
                    //}

                    var tvdata_names = '';
                    if (query_params['pbs_tvsm_sf_tvdata_names']) {
                        tvdata_names = query_params['pbs_tvsm_sf_tvdata_names'];
                    }

                    var URL = PBS.tvschedules.config.base + '/py-publisher/modules/dtv/whatson/' + PBS.tvschedules.config.version + '/views/whatson';
                    var callback = 'PBS.tvschedules.whatson.initChain.initCallback';

                    /*
                    alert(
                        'pbs_tvsm_sf_zip: ' + zip + '\n' + 
                        'pbs_tvsm_sf_headend_id: ' + headend_id + '\n' + 
                        'pbs_tvsm_sf_nav_transport: ' + PBS.tvschedules.whatson.navigation_transport + '\n' +
                        'pbs_tvsm_sf_tvdata_names: ' + tvdata_names + '\n' + 
                        'pbs_tvsm_localization_tvdata_name: ' + PBS.tvschedules.whatson.config['localization'] + '\n' +
                        'pbs_tvsm_cookie_settable: ' + PBS.tvschedules.user.cookie_settable + '\n' +
                        'pbs_tvsm_provider_finder:' + PBS.tvschedules.whatson.provider_finder + '\n' +
                        'pbs_tvsm_disable_favorites: ' + PBS.tvschedules.whatson.disable_favorites + '\n' +
                        'pbs_tvsm_disable_tvlink: ' + PBS.tvschedules.whatson.disable_tvlink + '\n' +
                        'pbs_tvsm_disable_shop: ' + PBS.tvschedules.whatson.disable_shop + '\n' +
                        'pbs_tvsm_w_timeblocks: ' + PBS.tvschedules.whatson.config['timeblocks'] + '\n' +
                        'pbs_tvsm_w_feed_id: ' + PBS.tvschedules.whatson.config['feed_id'] + '\n' +
                        'pbs_tvsm_w_content: ' + PBS.tvschedules.whatson.config['content'] + '\n'
                    );
                    */

                    // Send the arguments to the whatson view as overrides.
                    PBS.tvschedules.data.get( 
                        URL, 
                        callback, 
                        { 
                            'pbs_tvsm_sf_zip': zip,
                            'pbs_tvsm_sf_headend_id': headend_id,
                            'pbs_tvsm_sf_nav_transport': PBS.tvschedules.whatson.navigation_transport,
                            'pbs_tvsm_sf_tvdata_names': tvdata_names,
                            'pbs_tvsm_localization_tvdata_name': PBS.tvschedules.whatson.config['localization'],
                            'pbs_tvsm_cookie_settable': PBS.tvschedules.user.cookie_settable,
                            'pbs_tvsm_provider_finder': PBS.tvschedules.whatson.provider_finder,
                            'pbs_tvsm_disable_favorites': PBS.tvschedules.whatson.disable_favorites,
                            'pbs_tvsm_disable_tvlink': PBS.tvschedules.whatson.disable_tvlink,
                            'pbs_tvsm_disable_shop': PBS.tvschedules.whatson.disable_shop,
                            'pbs_tvsm_w_timeblocks': PBS.tvschedules.whatson.config['timeblocks'],
                            'pbs_tvsm_w_feed_id': PBS.tvschedules.whatson.config['feed_id'],
                            'pbs_tvsm_w_content': PBS.tvschedules.whatson.config['content']
                        } 
                    );
        
                },

                /* 
                All callbacks that have to be reached by external utilities have 
                to be public members of module and chain objects so that they can 
                be reached by external callers such as the PBS.tvschedules.data utility.  
                */
                initCallback: function (args) {

                    // Grab the shell markup.
                    var markup = args['markup'];
                    var matrix = args['matrix'];
                    var navigation_transport = args['navigation_transport'];
                    var localization_headend_id = args['localization_headend_id'];
                    var cookie_name = args['cookie_name'];
                    var cookie_value = args['cookie_value'];

                    PBS.tvschedules.whatson.matrix = matrix;
                    PBS.tvschedules.whatson.navigation_transport = navigation_transport;
                    PBS.tvschedules.whatson.localization_headend_id = localization_headend_id;
                    PBS.tvschedules.user.cookies[cookie_name] = cookie_value;


                    // The module might be initialized from an external system
                    // (e.g. preferences when a new provider is selected.)
                    // Because of this, we should check for the pre-existence
                    // of the module dom node and remove it.
                    try {
                        var fab = document.getElementById('pbs_tvschedules_modules-whatson-module_fab');
                        this.script_dom_node.parentNode.removeChild(fab);
                    } catch(e) {
                    }

                    var div = document.createElement('div');
                    div.id = 'pbs_tvschedules_modules-whatson-module_fab';
                    div.style.display = 'none';
                    div.style.width = '';
                    div.innerHTML = markup;

                    this.script_dom_node.parentNode.insertBefore(div, this.script_dom_node);


                    /* Initial load actions here. */
                    // Display the first station.
                    var primary_index = PBS.tvschedules.whatson.getPrimaryStationMatrixIndex();
                    PBS.tvschedules.whatson.setStation(primary_index);
                    //PBS.tvschedules.whatson.setStation(0);
                    for (var i in matrix['stations']) {
                        var station = matrix['stations'][i];
                        if (station['feeds'].length) {
                            PBS.tvschedules.whatson.setFeed(i, 0);
                        }
                    }

                    // Take care of all of the preference-related user interface initializations.
                    PBS.tvschedules.whatson.initPreferences();

                    // Manage the provider select links.
                    PBS.tvschedules.whatson.manageTransportLinks();

                    /* Implementation customizations enacted here.  */
                    // No current implementation customization for What's On module.
                    

                    // Show the markup! Booyah!!
                    div.style.display = 'block';

                    PBS.tvschedules.preferences.module_init_preferences.push(PBS.tvschedules.whatson.initPreferences)

                }

            };

            return obj;

        }(),
        


        /*********************** utility **********************/
        // TODO move this to util.js?
        getParams: function () {

            var cookie_value = PBS.tvschedules.user.cookies['pbsol.sta_extended'];
            var cookie_params = PBS.tvschedules.user.parseStaExtendedCookieValue(cookie_value);
            var query_params = PBS.tvschedules.parseURL(document.location.toString());

            // We're going to look for values in the cookie first, and
            // on the query string as an override.
            if (cookie_params['z']) {
                this.zip = cookie_params['z'];
            }
            if (query_params['pbs_tvsm_sf_zip']) {
                this.zip = query_params['pbs_tvsm_sf_zip'];
            }

            //if (cookie_params['t']) {
                //this.transport = cookie_params['t'];
            //}
            //if (query_params['pbs_tvsm_sf_transport']) {
                //this.transport = query_params['pbs_tvsm_sf_transport'];
            //}
            // This sets navigation_transport the first time getParams
            // is called. We need to do this because there are user
            // actions that rely on navigation_transport having a value.
            // navigation_transport's default value is the value from
            // the cookie or query string, which can't be statically
            // set. So we set it here.
            //if (this.navigation_transport == '') {
                //this.navigation_transport = this.transport;
            //}

            if (cookie_params['p']) {
                this.headend_id = cookie_params['p'];
            }
            if (query_params['pbs_tvsm_sf_headend_id']) {
                this.headend_id = query_params['pbs_tvsm_sf_headend_id'];
            }

            if (cookie_params['s']) {
                this.tvdata_names = cookie_params['s'].join('|');
            }
            if (query_params['pbs_tvsm_sf_tvdata_names']) {
                this.tvdata_names = query_params['pbs_tvsm_sf_tvdata_names'];
            }

        },


        // Turn all star ids off.$
        clearFavorites: function () {
            var star_ids = PBS.tvschedules.whatson.matrix['star_ids'];
            for (var i in star_ids) {
                var star_id = star_ids[i];
                YAHOO.util.Dom.removeClass(
                    star_id,
                    'pbs_tvschedules_modules-whatson-favorites_addremove_icon_over'
                );
                YAHOO.util.Dom.removeClass(
                    star_id,
                    'pbs_tvschedules_modules-whatson-favorites_addremove_icon_on'
                );
                YAHOO.util.Dom.addClass(
                    star_id,
                    'pbs_tvschedules_modules-whatson-favorites_addremove_icon'
                );
            }
        },


        getFavoriteFeeds: function () {
            // Get the list of favorite feed ids.
            var favorite_feed_ids = [];
            if (PBS.tvschedules.whatson.config['localization']) {
                favorite_feed_ids = PBS.tvschedules.user.getStationFavoriteChannels(
                    PBS.tvschedules.whatson.config['localization']
                )
            } else {
                favorite_feed_ids = PBS.tvschedules.user.getFavoriteChannels()
            }
        },


        manageFavorites: function () {
            // Get the list of favorite feed ids.
            var favorite_feed_ids = PBS.tvschedules.whatson.getFavoriteFeeds();
            PBS.tvschedules.whatson.clearFavorites();
            // For each favorite feed id lookup the span dom id.
            for (var i in favorite_feed_ids) {
                var feed_id = favorite_feed_ids[i];
                var dom_id = PBS.tvschedules.whatson.matrix['feed_id_to_favorite_id'][feed_id];
                var dom_node = document.getElementById(dom_id);
                // Change the CSS classname assingments on the approriate elements to
                // reflect favorite status.
                dom_node.className = 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_on';
            }
        },


        // Initialize preferences.
        // TODO Note, the paths to the matrix and other whatson members are
        // fully qualified because this might be run in a different context by
        // modules_init_preferences. This should be fixed by modifying
        // modules_init_preferences to take a context when a module's
        // initPreferences method is registered.
        initPreferences: function () {

            /* Cookie-enabled specific initializations. */
            if (PBS.tvschedules.user.cookie_settable) {

                /* Handle favorite channels */
                PBS.tvschedules.whatson.manageFavorites();

            } else { /* Non-cookie-enabled specific initializations. */

                // pass

            }

        },


        rollFeed: function (id) {
            if (!( document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-feed_id_open')) {
                if (document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-feed_id_over') {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-feed_id';
                } else {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-feed_id_over';
                }
            }
        }, 


        rollChannelFavorite: function (id) {
            if (!(document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_on')) {
                if (document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_over') {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-favorites_addremove_icon';
                } else {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_over';
                }
            }
        },


        setChannelFavorite: function (id, feed_id, event) {

            if (window.event) {
                window.event.cancelBubble = true;
            } else {
                event.stopPropagation();
            }
    
            if (document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_over') {
                var finalize = function () {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_on';
                }
                if (this.config['localization']) {
                    PBS.tvschedules.user.addStationFavoriteChannel.go(
                        this.config['localization'],
                        feed_id,
                        finalize,
                        this
                    );
                } else {
                    PBS.tvschedules.user.addFavoriteChannel.go(
                        feed_id,
                        finalize,
                        this
                    );
                }
            } else if (document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_on') {
                var finalize = function () {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-favorites_addremove_icon_over';
                }
                if (this.config['localization']) {
                    PBS.tvschedules.user.removeStationFavoriteChannel.go(
                        this.config['localization'],
                        feed_id,
                        finalize,
                        this
                    );
                } else {
                    PBS.tvschedules.user.removeFavoriteChannel.go(
                        feed_id,
                        finalize,
                        this
                    );
                }
            }

        },


        getPrimaryStationMatrixIndex: function () {
            var matrix = PBS.tvschedules.whatson.matrix;
            var primary_station = PBS.tvschedules.user.cookies['pbsol.station'];
            var stations = matrix['stations'];
            for (var i in stations) {
                var station = stations[i];
                var tvdata_name = station['tvdata_name'];
                if (tvdata_name == primary_station) {
                    return i;
                }
            }
            return 0;
        },


        setStation: function (station_index) {
            var stations = this.matrix['stations'];
            for (var i in stations) {
                var station = stations[i];
                var feeds_container_id = station['feeds_container_id'];
                document.getElementById(feeds_container_id).style.display = 'none';
            }
            var id = stations[station_index]['feeds_container_id'];
            document.getElementById(id).style.display = '';
        },


        setFeed: function (station_index, feed_index) {

            var station = this.matrix['stations'][station_index];

            for (var j in station['feeds']) {
                var feed = station['feeds'][j];

                var airdates_container_id = feed['airdates_container_id'];
                document.getElementById(airdates_container_id).style.display = 'none';

                var feed_header_id = feed['feed_header_id'];
                document.getElementById(feed_header_id).className = 'pbs_tvschedules_modules-whatson-feed_id';
            }

            var feed = station['feeds'][feed_index];

            var airdates_container_id = feed['airdates_container_id'];
            document.getElementById(airdates_container_id).style.display = '';

            var feed_header_id = feed['feed_header_id'];
            document.getElementById(feed_header_id).className = 'pbs_tvschedules_modules-whatson-feed_id_open';
        },


        rollTimeblock: function (id) {
            if (!(document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-timeblock_tab_on')) {
                if (document.getElementById(id).className == 'pbs_tvschedules_modules-whatson-timeblock_tab_over') {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-timeblock_tab';
                } else {
                    document.getElementById(id).className = 'pbs_tvschedules_modules-whatson-timeblock_tab_over';
                }           
            }
        },


        setTimeBlock: function (block) {

            var stations = this.matrix['stations'];

            for (var i in stations) {

                var station = stations[i];
                var feeds = station['feeds'];
                var now_tab_id = station['now_tab_id'];
                var now_tab = document.getElementById(now_tab_id);
                var tonight_tab_id = station['tonight_tab_id'];
                var tonight_tab = document.getElementById(tonight_tab_id);

                if (block == 'now') {
                    now_tab.className = 'pbs_tvschedules_modules-whatson-timeblock_tab_on';
                    tonight_tab.className = 'pbs_tvschedules_modules-whatson-timeblock_tab';
                }
                if (block == 'tonight') {
                    now_tab.className = 'pbs_tvschedules_modules-whatson-timeblock_tab';
                    tonight_tab.className = 'pbs_tvschedules_modules-whatson-timeblock_tab_on';
                }

                for (var j in feeds) {
                    var feed = feeds[j];
                    var now_id = feed['now_id'];
                    var tonight_id = feed['tonight_id'];
                    if (block == 'now') {
                        document.getElementById(now_id).style.display = '';
                        document.getElementById(tonight_id).style.display = 'none';
                    }
                    if (block == 'tonight') {
                        document.getElementById(now_id).style.display = 'none';
                        document.getElementById(tonight_id).style.display = '';
                    }
                }
            }
        },


        manageTransportLinks: function () {

            var module = PBS.tvschedules.whatson;

            var cable_selected = document.getElementById('pbs_tvschedules_modules-whatson-provider_select_cable');
            var over_the_air_selected = document.getElementById('pbs_tvschedules_modules-whatson-provider_select_over_the_air');
            var over_the_air_only = document.getElementById('pbs_tvschedules_modules-whatson-provider_select_over_the_air_only');

            /*
            alert(
                "module.config['localization']: " + module.config['localization'] + '\n' +
                "module.headend_id: " + module.headend_id + '\n' +
                "module.localization_headend_id: " + module.localization_headend_id + '\n' +
                "module.navigation_transport: " + module.navigation_transport + '\n'
            );
            */

            if (
                (!module.config['localization'] && module.headend_id) || 
                (module.config['localization'] && module.localization_headend_id)
            ) {
                if (module.navigation_transport == 'b') {
                    cable_selected.style.display = 'none';
                    over_the_air_selected.style.display = '';
                    over_the_air_only.style.display = 'none';
                } else {
                    cable_selected.style.display = '';
                    over_the_air_selected.style.display = 'none';
                    over_the_air_only.style.display = 'none';
                }
            } else {
                cable_selected.style.display = 'none';
                over_the_air_selected.style.display = 'none';
                over_the_air_only.style.display = '';
            }


        },


        setNavigationTransport: function () {

            var obj = {

                go: function (navigation_transport) {
                    PBS.tvschedules.loading.show();
                    PBS.tvschedules.whatson.navigation_transport = navigation_transport;
                    this.getLineUp();
                },

                getLineUp: function () {

                    PBS.tvschedules.whatson.getParams();

                    var URL = PBS.tvschedules.config.base + '/py-publisher/modules/dtv/whatson/' + PBS.tvschedules.config.version + '/views/lineup';
                    var callback = 'PBS.tvschedules.whatson.setNavigationTransport.getLineUpCallback';

                    /*
                    alert(
                        'pbs_tvsm_sf_zip: ' + PBS.tvschedules.whatson.zip + '\n' +
                        'pbs_tvsm_sf_headend_id: ' + PBS.tvschedules.whatson.headend_id + '\n' +
                        'pbs_tvsm_sf_nav_transport: ' + PBS.tvschedules.whatson.navigation_transport + '\n' +
                        'pbs_tvsm_sf_tvdata_names: ' + PBS.tvschedules.whatson.tvdata_names + '\n' +
                        'pbs_tvsm_localization_tvdata_name: ' + PBS.tvschedules.whatson.config['localization'] + '\n' +
                        'pbs_tvsm_cookie_settable: ' + PBS.tvschedules.user.cookie_settable + '\n' +
                        'pbs_tvsm_disable_favorites: ' + PBS.tvschedules.whatson.disable_favorites + '\n' +
                        'pbs_tvsm_disable_tvlink: ' + PBS.tvschedules.whatson.disable_tvlink + '\n' +
                        'pbs_tvsm_disable_shop: ' + PBS.tvschedules.whatson.disable_shop + '\n' +
                        'pbs_tvsm_w_timeblocks: ' + PBS.tvschedules.whatson.config['timeblocks'] + '\n' +
                        'pbs_tvsm_w_feed_id: ' + PBS.tvschedules.whatson.config['feed_id'] + '\n' +
                        'pbs_tvsm_w_content: ' + PBS.tvschedules.whatson.config['content'] + '\n'
                    );
                    */

                    PBS.tvschedules.data.get(
                        URL,
                        callback,
                        {
                            'pbs_tvsm_sf_zip': PBS.tvschedules.whatson.zip,
                            'pbs_tvsm_sf_headend_id': PBS.tvschedules.whatson.headend_id,
                            'pbs_tvsm_sf_nav_transport': PBS.tvschedules.whatson.navigation_transport,
                            'pbs_tvsm_sf_tvdata_names': PBS.tvschedules.whatson.tvdata_names,
                            'pbs_tvsm_localization_tvdata_name': PBS.tvschedules.whatson.config['localization'],
                            'pbs_tvsm_cookie_settable': PBS.tvschedules.user.cookie_settable,
                            'pbs_tvsm_disable_favorites': PBS.tvschedules.whatson.disable_favorites,
                            'pbs_tvsm_disable_tvlink': PBS.tvschedules.whatson.disable_tvlink,
                            'pbs_tvsm_disable_shop': PBS.tvschedules.whatson.disable_shop,
                            'pbs_tvsm_w_timeblocks': PBS.tvschedules.whatson.config['timeblocks'],
                            'pbs_tvsm_w_feed_id': PBS.tvschedules.whatson.config['feed_id'],
                            'pbs_tvsm_w_content': PBS.tvschedules.whatson.config['content']
                        }
                    );
        
                },

                getLineUpCallback: function (args) {

                    var markup = args['markup'];
                    var matrix = args['matrix'];
                    PBS.tvschedules.whatson.matrix = matrix;

                    document.getElementById('pbs_tvschedules_modules-whatson-lineup_container').innerHTML = markup;

                    // Display the first station.
                    var primary_index = PBS.tvschedules.whatson.getPrimaryStationMatrixIndex();
                    PBS.tvschedules.whatson.setStation(primary_index);
                    //PBS.tvschedules.whatson.setStation(0);
                    for (var i in matrix['stations']) {
                        var station = matrix['stations'][i];
                        if (station['feeds'].length) {
                            PBS.tvschedules.whatson.setFeed(i, 0);
                        }
                    }

                    // Take care of all of the preference-related user interface initializations.
                    PBS.tvschedules.whatson.initPreferences();

                    // Toggle the lineup control links.
                    PBS.tvschedules.whatson.manageTransportLinks();

                    PBS.tvschedules.loading.hide();

                }

            };

            return obj;
            
        }(),


        filterLineUp: function () {

            var filter_form_select = document.getElementById('pbs_tvschedules_modules-whatson-filter_form_select');
            var filter = filter_form_select.value;

            if (filter == 'none') {
                // Go through all the stations and feeds in the current lineup.
                for (var i in this.matrix['stations']) {
                    var station = this.matrix['stations'][i];
                    var tvdata_name = station['tvdata_name'];
                    var station_container_id = station['station_container_id'];
                    // Show all stations.
                    document.getElementById(station_container_id).style.display = '';
                    for (var j in station['feeds']) {
                        var feed = station['feeds'][j];
                        var feed_container_id = feed['feed_container_id']; // The DOM id of the element that contains the feed.
                        // Show all feeds.
                        document.getElementById(feed_container_id).style.display = '';
                    }
                }
            }

            if (filter == 'favorite') {

                var favorite_feed_ids = [];
                if (PBS.tvschedules.whatson.config['localization']) {
                    favorite_feed_ids = PBS.tvschedules.user.getStationFavoriteChannels(
                        PBS.tvschedules.whatson.config['localization']
                    )
                } else {
                    favorite_feed_ids = PBS.tvschedules.user.getFavoriteChannels()
                }

                // Go through all the feed ids in the current lineup.
                var first_favorite_feed = true;
                for (var i in this.matrix['stations']) {
                    var station = this.matrix['stations'][i];
                    for ( var j in station['feeds']) {
                        var feed = station['feeds'][j];
                        var feed_id = feed['feed_id']; // The feed id from the database.
                        var feed_container_id = feed['feed_container_id']; // The DOM id of the element that contains the feed.
                        var airdates_container_id = feed['airdates_container_id']; // The DOM id of the element that contains the feed's airdates.
                        // If it's not a favorite, hide it.
                        if (!PBS.tvschedules.isInArray(favorite_feed_ids, feed_id)) {
                            document.getElementById(feed_container_id).style.display = 'none';
                        } else { // It is a favorite.
                            if (first_favorite_feed) {
                                this.setFeed(i, j);
                                first_favorite_feed = false;
                            }
                        }
                    }
                }
            }

            if (filter == 'primary') {

                // Get cookie value from user utility.
                var primary_station = PBS.tvschedules.user.cookies['pbsol.station'];

                // Go through all of the stations in the current lineup.
                for (var i in this.matrix['stations']) {
                    var station = this.matrix['stations'][i];
                    var tvdata_name = station['tvdata_name'];
                    var station_container_id = station['station_container_id'];
                    var feeds_container_id = station['feeds_container_id'];
                    // If it's not the primary, then hide it.
                    if (tvdata_name != primary_station) {
                        document.getElementById(station_container_id).style.display = 'none';
                        document.getElementById(feeds_container_id).style.display = 'none';
                    } else { // If it is the primary, make sure it's open.
                        document.getElementById(station_container_id).style.display = '';
                        document.getElementById(feeds_container_id).style.display = '';
                    }
                }

            }
        }


    };

    return obj;

}(); // The parens here cause the anonymous function to execute and return.
PBS.tvschedules.module_inits.push({method: 'PBS.tvschedules.whatson.init', data: 'PBS.tvschedules.whatson', scope: true});
