//|	Citytv class
//|	By Brian Franklin - Brightcove
var Citytv = new function () {

    this.search_value = ''; 		//|	Grab the search value on page load so we know what to toggle when blurred
    this.last_search = ''; 		//|	Keep track of the last search term so we don't re-fetch if it doesn't change
    this.time_index = 0; 		//|	For maintaining the timestamp to determine when a user has paused in typing
    this.search_timer = null; 	//|	Timer pointer for checking typing status
    this.throttle = false; 		//|	Whether or not to throttle based on input lag
    this.dimmed = false; 		//|	Store whether dim the lights has been activated
    this.slider = 0; 			//|	Keep track of which home slider is active
    this.sliding = false; 		//|	Keep things from getting confused
    this.over = null; 			//|	All of the hover-activated overlays can be tricky
    this.over_cycle = null; 		//|	This will be a timer to check overlay hover state to forgive some sloppy mousing
    this.dynamic = []; 			//|	Keep track of dynamic overlays to make them easier to clear
    this.restrict = null; 		//|	To prevent multiple overlays from piling up and showing at once, flickering
    this.sorting = null; 		//|	Keep track of which sort is being performed
    this.search_index = -1; 		//|	Used for scrolling through search results
    this.bc = {}; 				//|	This will store BC player pointers
	this.populatedCarousels = {};	//|	This will store IDs of carousels populated

    this.PRESENTED_BY = "Presented by ";

    //|	Receives callback data for processing
    this.handle = function (s) {

        //|	Trim whitespace from beginning and end of JSON
        s = s.replace(/^\s+|\s+$/g, "");

        //|	Parse that JSON
        s = eval("(" + s + ")");

        if (vorpal.check($('#search_dd'))) { exists = true; document.body.removeChild($('#search_dd')); } else { exists = false; }

        //|	Don't show the typeahead if there are no results
        if ((s[0].shows.length == 0) && (s[1].videos == 0) && (s[2].channels == 0)) { return; }

        var e = document.createElement('div');
        e.className = 'search_dd';
        e.id = 'search_dd'
        e.style.position = 'absolute';

        var html = '';

        if (s[1].videos.length > 0) {
            html += '<div class="category"><div class="container"><h1>VIDEOS</h1><br class="clear" /></div></div><ul>';

            for (var i = 0; i < s[1].videos.length; i++) {

                s[1].videos[i].name = Citytv.add_suffix(s[1].videos[i].name, 20);

                html += '<li' + ((i == 0) ? ' class="first"' : '') + '><div class="item"><table><tr><td><a href="' + bc_root + '/' + s[1].videos[i].video_url + '"><img src="' + s[1].videos[i].thumbnail_url + '" width="60" height="45" alt="' + s[1].videos[i].name + '" title="' + s[1].videos[i].name + '" /></a></td><td class="meta"><h2><a href="' + bc_root + '/' + s[1].videos[i].video_url + '">' + s[1].videos[i].name + '</a></h2>';
                html += '</td></tr></table></div></li>';

            }
            html += '</ul>';
        }
        if (s[0].shows.length > 0) {
            html += '<div class="category"><div class="container"><h1>SHOWS</h1><br class="clear" /></div></div><ul>';

            for (var i = 0; i < s[0].shows.length; i++) {

                s[0].shows[i].name = Citytv.add_suffix(s[0].shows[i].name, 20);

                html += '<li' + ((i == 0) ? ' class="first"' : '') + '><div class="item"><table><tr><td><a href="' + bc_root + '/' + s[0].shows[i].show_url + '"><img src="' + s[0].shows[i].thumbnail_url + '" width="60" height="45" alt="' + s[0].shows[i].name + '" title="' + s[0].shows[i].name + '" /></a></td><td class="meta"><h2><a href="' + bc_root + '/' + s[0].shows[i].show_url + '">' + s[0].shows[i].name + '</a></h2>';

                html += '</td></tr></table></div></li>';
            }
            html += '</ul>';
        }

        if (s[2].channels.length > 0) {
            html += '<div class="category"><div class="container"><h1>CHANNELS</h1><br class="clear" /></div></div><ul>';

            for (var i = 0; i < s[2].channels.length; i++) {

                var channel = s[2].channels[i]
                channel.name = Citytv.add_suffix(channel.name, 20);

                html += '<li' + ((i == 0) ? ' class="first"' : '') + '><div class="item"><table><tr><td><a href="' + bc_root + '/' + channel.channel_url + '"><img src="' + channel.thumbnail_url + '" width="60" height="45" alt="' + channel.name + '" title="' + channel.name + '" /></a></td><td class="meta"><h2><a href="' + bc_root + '/' + channel.channel_url + '">' + channel.name + '</a></h2>';

                if (channel.presentedby != '') {
                    channel.presentedby = Citytv.add_suffix(Citytv.PRESENTED_BY + channel.presentedby, 70);
                    html += '<h3>' + channel.presentedby + '</h3>';
                }
                html += '</td></tr></table></div></li>';
            }

            html += '</ul>';
        }
        html += '<div class="bottom"></div>';

        e.innerHTML = html;

        if (!exists) {

            vorpal.ui.opacity(e, 0);

        }

        document.body.appendChild(e);
        $("#search_dd div.category")[0].className = "category first";

        Citytv.position_search();


        if (!exists) {

            vorpal.ui.animate(e, 500, 1, 'opacity');

        }

    };

    //|	This function executes on an interval, checking the time_index for short pauses in typing
    this.search_cycle = function () {

        //|	Account for throttling delay
        if (((new Date().getTime() / 1000) - Citytv.time_index) > 0.3) {

            Citytv.do_search();

        }

    };



    //|	We call this in a couple places, so it's drawn out
    this.do_search = function () {

        //|	This should be the search box
        var i = $('div.search:first').getElementsByTagName('input')[0];

        //|	We need at least 2 letters and we shouldn't search on the label value
        if ((i.value.length > 1) && (Citytv.last_search != i.value)) {

            Citytv.last_search = i.value;

            //|	Go get the results
            vorpal.io.send(bc_root + '/json.htm?N=0&Ntt=' + i.value + '*&media=typeahead', { 'vcb': Citytv.handle, 'vm': 'GET' });
            //vorpal.io.send('json/search.json',{'vcb':Citytv.handle, 'vm':'GET'});

        } else {

            if ((i.value.length < 2) && vorpal.defined($('#search_dd'))) {

                document.body.removeChild($('#search_dd'));

            }

        }

    };

    //|	This is drawn out to its own function so we can call it on window resize events
    this.position_search = function () {

        var e = $('#search_dd');

        if (vorpal.defined(e)) {

            var search_box = $('div.search:first');
            var coords = vorpal.ui.coords(search_box);

            e.style.top = ((search_box.offsetHeight + coords['top'] + 12) + 'px');
            e.style.left = ((coords['left'] - 8) + 'px');
            e.style.width = ((search_box.offsetWidth + 16) + 'px');

        }

    };

    //| Any element repositioning needs to be added to this to ensure scaling the window doesn't break things
    this.window_resize = function () {

        this.position_search();

    };

    //|	Create four mask divs to black out everything around the player
    this.dim = function () {

        if (Citytv.dimmed) {

            Citytv.dimmed = false;

            vorpal.ui.animate($('#dim-top'), 500, 0, 'opacity', function () {
                document.body.removeChild($('#dim-top'));
            });

            vorpal.ui.animate($('#dim-left'), 500, 0, 'opacity', function () {
                document.body.removeChild($('#dim-left'));
            });

            vorpal.ui.animate($('#dim-right'), 500, 0, 'opacity', function () {
                document.body.removeChild($('#dim-right'));
            });

            vorpal.ui.animate($('#dim-bottom'), 500, 0, 'opacity', function () {
                document.body.removeChild($('#dim-bottom'));
            });

            $('div.dim a:last').innerHTML = 'Dim the Lights';

            return false;

        }

        Citytv.dimmed = true;

        var top = document.createElement('div');
        top.style.background = '#000000';
        top.style.position = 'absolute';

        var left = top.cloneNode(true);
        var right = top.cloneNode(true);
        var bottom = top.cloneNode(true);

        top.id = 'dim-top';
        left.id = 'dim-left';
        right.id = 'dim-right';
        bottom.id = 'dim-bottom';

        var player = $('div.player:first');
        var coords = vorpal.ui.coords(player);
        var win = vorpal.ui.window();
        var offsets = vorpal.ui.offsets();

        top.style.left = ((coords.left + 14) + 'px');
        top.style.top = '0px';
        top.style.width = ((coords.width - 42) + 'px');
        top.style.height = ((coords.top + 11) + 'px');

        bottom.style.left = ((coords.left + 14) + 'px');
        bottom.style.top = ((coords.top - 28 + coords.height) + 'px');
        bottom.style.width = ((coords.width - 42) + 'px');
        bottom.style.height = ((document.body.offsetHeight - (coords.top - 28 + coords.height)) + 'px');

        left.style.left = '0px';
        left.style.top = '0px';
        left.style.width = ((coords.left + 14) + 'px');
        left.style.height = (document.body.offsetHeight + 'px');

        right.style.left = ((coords.left + coords.width - 28) + 'px');
        right.style.top = '0px';
        right.style.width = ((win.x - (coords.left + coords.width - 28)) + 'px');
        right.style.height = (document.body.offsetHeight + 'px');

        vorpal.ui.opacity(top, 0);
        vorpal.ui.opacity(bottom, 0);
        vorpal.ui.opacity(left, 0);
        vorpal.ui.opacity(right, 0);

        document.body.appendChild(top);
        document.body.appendChild(bottom);
        document.body.appendChild(left);
        document.body.appendChild(right);

        vorpal.ui.animate(top, 500, 0.8, 'opacity');
        vorpal.ui.animate(bottom, 500, 0.8, 'opacity');
        vorpal.ui.animate(left, 500, 0.8, 'opacity');
        vorpal.ui.animate(right, 500, 0.8, 'opacity');

        $('div.dim a:last').innerHTML = 'Turn up Lights';

        return false;

    };

    //|	Pop up the IE6 use alert
    this.ie6 = function () {

        if (vorpal.defined($('#ie6'))) {

            document.body.removeChild($('#ie6'));
            document.body.removeChild($('#ie6_overlay'));

        } else {

            var w_coords = vorpal.ui.window();

            var overlay = document.createElement('div');
            overlay.id = 'ie6_overlay';
            overlay.className = 'ie6_overlay';
            overlay.style.width = w_coords.x + 'px';
            overlay.style.height = (w_coords.y + 100) + 'px';
            document.body.appendChild(overlay);

            var popup = document.createElement('div');
            popup.id = 'ie6';
            popup.className = 'ie6';
            popup.innerHTML = '<div class="top"></div><div class="middle"><h1>Compatibility Notice</h1><p>We\'re sorry, but this site doesn\'t support Internet Explorer version 6. It is suggested that you upgrade to a newer browser. You can continue to view the website, but it may not function as expected.</p><input type="button" value="Continue Viewing" onclick="Citytv.ie6(); return false;" style="z-index:1300; position:relative;" /></div><div class="bottom"></div>';
            document.body.appendChild(popup);

            popup.style.top = '100px'; //((Math.round(w_coords.y - 0) / 2) + 'px');
            popup.style.left = ((Math.round(w_coords.x - 566) / 2) + 'px');

        }

    };

    this.add_suffix = function (value, max) {
        var suffix = (value.length > max) ? '...' : '';
        return value.substr(0, max) + suffix;
    }

    //|	Instantiate the homepage video slider, setting default arrow states, etc
    this.factor_home_slider = function (i) {

        Citytv.slider = i;

        var sliders = $('div.home_carousel_inner');
        var slider = sliders[i];

        slider.style.left = '0px';

        var left = $('div.arrows div.left:first');
        var right = $('div.arrows div.right:first');

        vorpal.remove_class(left, 'left_active');

        vorpal.remove_class(right, 'right_active');
        if (vorpal.get_by_class(slider, 'item').length > 4) {

            vorpal.add_class(right, 'right_active');

        }

        vorpal.events.attach(left, 'click', function () {

            Citytv.slide_home('l');

        });

        vorpal.events.attach(right, 'click', function () {

            Citytv.slide_home('r');

        });

    };

    //|	Handle slide functionality for the homepage video slider
    this.slide_home = function (direction) {

        //|	Don't let this continue if the slider is already animating
        if (Citytv.sliding) { return; }

        Citytv.sliding = true;

        var sliders = $('div.home_carousel_inner');
        var slider = sliders[Citytv.slider];

        var left = $('div.arrows div.left:first');
        var right = $('div.arrows div.right:first');

        var l = vorpal.get_by_class(slider, 'item').length;
        var max = 0;
        var min = (0 - ((Math.ceil(l / 4) - 1) * 852));

        //|	IE doesn't know how to do simple math correctly
        if ((slider.offsetLeft % 852) != 0) {
            slider.style.left = (slider.offsetLeft - ((852 + (slider.offsetLeft % 852))) * 2) + 'px';
        }

        if (direction == 'r') {

            if ((slider.offsetLeft - 852) >= min) {
                var offset = slider.offsetLeft - 852;
            } else {
                var offset = slider.offsetLeft;
            }

        } else {

            if ((slider.offsetLeft + 852) <= max) {

                var offset = (slider.offsetLeft + 852);

            } else {

                var offset = max;

            }

        }

        vorpal.ui.animate(slider, 500, offset, 'left', function () { Citytv.sliding = false; });

        //|	Figure out what the arrow states should be
        if (offset == 0) { vorpal.remove_class(left, 'left_active'); } else { vorpal.add_class(left, 'left_active'); }
        if (offset < (0 - ((Math.ceil(l / 4) - 2) * 851))) { vorpal.remove_class(right, 'right_active'); } else { vorpal.add_class(right, 'right_active'); }

    };

    //|	Fires off an AJAX call to get show data for homepage tiles
    this.hover = function (target) {

        Citytv.active = target;

        //|	Climb the DOM until we have the tile that contains the target thumbnail
        var id = target.parentNode.parentNode.parentNode.parentNode.getAttribute('id').split('_')[1];

        vorpal.io.send(bc_root + '/json.htm?media=detail&R=' + id, { 'vcb': Citytv.handle_hover, 'vm': 'GET' });

    };

    //|	Handles the return of AJAX requests for homepage hover tiles
    this.handle_hover = function (s) {

        //|	Prevent popups from flickering in and out of existence if the user isn't hovering over a tile anymore
        if (Citytv.over == null) { return; }

        //|	If another hover menu exists, fade it out and kill it
        if (vorpal.defined($('#hover-menu'))) { $('#hover-menu').id = 'temp'; vorpal.ui.animate($('#temp'), 200, 0, 'opacity', function () { if (vorpal.defined($('#temp'))) { document.body.removeChild($('#temp')); } }); }

        //|	Trim whitespace from beginning and end of JSON
        s = s.replace(/^\s+|\s+$/g, "");

        //|	Parse that JSON
        s = eval("(" + s + ")");

        //|	Prevent piled up requests from sowing if the currently hovered tile isn't the right one
        if (s.video_url.indexOf(Citytv.active.parentNode.parentNode.parentNode.parentNode.getAttribute('id').split('_')[1]) < 0) {

            return;

        }

        //|	Get information from the video name (TODO: should this be in different properties on the JSON object?)
        var chunks = s.name.split(' - ');

        //|	Get the coordinates of the thumbnail for aligning the hover menu
        var coords = vorpal.ui.coords(Citytv.active);

        var e = document.createElement('div');
        e.id = 'hover-menu';
        e.className = 'video_menu';
        var html = '<div class="top"></div><div class="middle"><h1><span>' 
        	+ s.show + ':</span> ' + s.name + '</h1><h2>' + s.season + ' (' + s.length + ')</h2><hr />Genre:</strong> <a href="' + bc_root + '/navigation.htm?'
        	+ s.genre_url + '">' + s.genre + '</a><br />&raquo; <a href="' + bc_root + '/' + s.show_url + '">Visit the ' + s.show + ' Page</a><br />&raquo; <a href="'
        	+ s.citytv_link + '">' + s.citytv_text + '</a><br />';
        if(s.channel_name != '') {
        	html += '&raquo; <a href="' + bc_root + '/' + s.channel_url + '">' + s.channel_name + '</a><br />';
        }
        html += '<hr /><p>' + s.description + '</p></div><div class="bottom"></div>';
        e.innerHTML = html;
        e.style.position = 'absolute';
        e.style.top = (coords.top - 70 + 'px');
        e.style.left = (coords.left + 73 + 'px');
        Citytv.dynamic.push(e);
        vorpal.ui.opacity(e, 0);
        document.body.appendChild(e);
        vorpal.ui.animate(e, 500, 1, 'opacity');
        vorpal.events.attach(e, 'mouseenter', function () { Citytv.over = this; });
        vorpal.events.attach(e, 'mouseleave', function () { Citytv.over = null; });

    };

    //|	Determine what height to display the featured arrows and play button at, depending on title height
    this.position_arrows = function () {

        var left = $('div.featured div.left:first');
        var right = $('div.featured div.right:first');
        var slider = $('div.featured_inner:first');
        var items = $('div.featured_inner div.item');

        var active = items[Math.round(Math.abs(slider.offsetLeft) / 640)];
        var height = vorpal.get_by_class(active, 'title')[0].offsetHeight;
        var play = vorpal.get_by_class(active, 'play_overlay')[0];

        left.style.top = (Math.round(330 - height + ((height - left.offsetHeight) / 2) + 1) + 'px');
        right.style.top = (Math.round(330 - height + ((height - right.offsetHeight) / 2) + 1) + 'px');
        play.style.top = (Math.round(((330 - height) / 2) - 41) + 'px');

    };

    //|	Slide the featured slider
    this.slide_featured = function (direction) {

        //|	Don't let this continue if the slider is already animating
        if (Citytv.sliding) { return; }

        Citytv.sliding = true;

        var left = $('div.featured div.left:first');
        var right = $('div.featured div.right:first');
        var slider = $('div.featured_inner:first');

        //|	This is needed by some browsers to prime the opacity values
        vorpal.ui.opacity(left, 1);
        vorpal.ui.opacity(right, 1);

        //|	This is used in the callback functions below to fade the elements back in
        var fade_in = function () {

            //|	Align the arrows before fading in
            Citytv.position_arrows();

            vorpal.ui.animate(left, 200, 1, 'opacity');
            vorpal.ui.animate(right, 200, 1, 'opacity');

            //|	We have to act on play buttons for all items in the slider
            var plays = $('div.featured div.play_overlay');
            for (var i = 0; i < plays.length; i++) {

                vorpal.ui.animate(plays[i], 200, 1, 'opacity');

            }

        };

        //|	Fade out all play buttons and arrows prior to a slide to prevent really ugly jittering... plus, it looks cool
        var plays = $('div.featured div.play_overlay');
        for (var i = 0; i < plays.length; i++) {

            vorpal.ui.opacity(plays[i], 1);
            vorpal.ui.animate(plays[i], 200, 0, 'opacity');

        }
        vorpal.ui.animate(left, 200, 0, 'opacity');
        vorpal.ui.animate(right, 200, 0, 'opacity', function () {

            var items = $('div.featured_inner div.item');

            //|	IE doesn't know how to do simple math correctly
            if ((slider.offsetLeft % 640) != 0) {
                slider.style.left = (slider.offsetLeft - ((640 + (slider.offsetLeft % 640))) * 2) + 'px';
            }

            if (direction == 'l') {

                if (slider.offsetLeft == 0) {

                    slider.style.left = '-640px';

                    slider.insertBefore(items[(items.length - 1)], items[0]);

                    vorpal.ui.animate(slider, 500, 0, 'left', function () { Citytv.sliding = false; fade_in(); });

                } else {

                    vorpal.ui.animate(slider, 500, (slider.offsetLeft + 640), 'left', function () { Citytv.sliding = false; fade_in(); });

                }

            } else {

                if (slider.offsetLeft == (0 - ((items.length - 1) * 640))) {

                    slider.style.left = (slider.offsetLeft + 640 + 'px');

                    slider.appendChild(items[0].cloneNode(true));

                    slider.removeChild(items[0]);

                    vorpal.ui.animate(slider, 500, (slider.offsetLeft - 640), 'left', function () { Citytv.sliding = false; fade_in(); });

                } else {

                    vorpal.ui.animate(slider, 500, (slider.offsetLeft - 640), 'left', function () { Citytv.sliding = false; fade_in(); });

                }

            }

        });

    };

    //|	Ask the API for show information
    this.more_channels = function (e) {

        if (vorpal.defined($('#channels_dd'))) { return; }

        vorpal.io.send(bc_root + '/json.htm?media=channels&N=0&Nr=AND(Endeca Record Type:Channel Logo)', { 'vcb': Citytv.handle_channels, 'vm': 'GET' });

        return false;

    };

    //|	Ask the API for show information
    this.shows = function (e) {

        if (vorpal.defined($('#shows_dd'))) { return; }

        vorpal.io.send(bc_root + '/json.htm?media=shows&N=0&Nr=AND(Src:Endeca,OR(Src:citytv,Src:cityline))', { 'vcb': Citytv.handle_shows, 'vm': 'GET' });

        return false;

    };

    //|	Handle the show data for the menu drop-down
    this.handle_shows = function (s) {

        //|	Don't flicker or display if we're not hovering over shows anymore
        if (vorpal.defined($('#shows_dd')) || (Citytv.over == null)) { return; }

        //|	Trim whitespace from beginning and end of JSON
        s = s.replace(/^\s+|\s+$/g, "");

        //|	Parse that JSON
        s = eval("(" + s + ")");

        var e = document.createElement('div');
        e.id = 'shows_dd';
        e.className = 'shows_dd';
        e.innerHTML = '<div class="dd_top"><div class="dd_left"></div><div class="dd_middle"></div><div class="dd_right"></div><br class="clear" /></div>';
        e.style.position = 'absolute';
        e.style.top = $('div.header div.navigation:first').offsetTop + $('div.header div.navigation:first').offsetHeight + 'px';
        e.style.left = $('div.header:first').getElementsByTagName('li')[0].offsetLeft - 80 + 'px';
        var leftStart=$('div.header:first').getElementsByTagName('li')[0].offsetLeft - 80;
        if(leftStart<20){ 
        	leftStart=20;
        }
        e.style.left = leftStart + 'px';
        Citytv.dynamic.push(e);
        document.body.appendChild(e);
        vorpal.events.attach(e, 'mouseenter', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = t; 
	        leftStart=$('div.header:first').getElementsByTagName('li')[0].offsetLeft - 80;
	        if(leftStart<20){ 
		        e.style.left = '20px';
	        }
        });
        vorpal.events.attach(e, 'mouseleave', function () { Citytv.over = null; e = null; });
        vorpal.ui.opacity(e, 0);
        vorpal.ui.animate(e, 500, 1, 'opacity');

        //|	Iterate through the shows
        for (var i = 0; i < s.shows.length; i++) {

            //|	Determine what column the show should exist in (assumes a column length of 10 shows)
            var target = Math.floor((i) / 13);

            //|	Create the column if it doesn't exist yet
            if ($('#shows_dd').getElementsByTagName('ul').length <= target) {

                var ul = document.createElement('ul');
                $('#shows_dd div.dd_middle:first').appendChild(ul);

            }

            //|	Add the show
            var li = document.createElement('li');
            li.innerHTML = '<a href="' + bc_root + '/' + s.shows[i].url + '">' + s.shows[i].name + '</a>';

            $('#shows_dd').getElementsByTagName('ul')[target].appendChild(li);

        }

    };

    //|	Handle the channel data for the menu drop-down
    this.handle_channels = function (s) {

        //|	Don't flicker or display if we're not hovering over channels anymore
        if (vorpal.defined($('#channels_dd')) || (Citytv.over == null)) { return; }

        //|	Trim whitespace from beginning and end of JSON
        s = s.replace(/^\s+|\s+$/g, "");

        //|	Parse that JSON
        s = eval("(" + s + ")");

        var isOneColumn = s.channels.length <= 7;

        var e = document.createElement('div');
        e.id = 'channels_dd';
        e.className = 'channels_dd';
        e.innerHTML = '<div class="dd_top"><div class="dd_left"></div><div class="dd_middle"></div><div class="dd_right"></div><br class="clear" /></div>';
        e.style.position = 'absolute';
        e.style.top = $('div.header div.navigation:first').offsetTop + $('div.header div.navigation:first').offsetHeight + 'px';
        e.style.left = $('div.header:first').getElementsByTagName('li')[4].offsetLeft - 16 + 'px';
        Citytv.dynamic.push(e);
        document.body.appendChild(e);
        vorpal.events.attach(e, 'mouseenter', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = t; });
        vorpal.events.attach(e, 'mouseleave', function () { Citytv.over = null; e = null; });
        vorpal.ui.opacity(e, 0);
        vorpal.ui.animate(e, 500, 1, 'opacity');
        var header = document.createElement('h1');
        if (isOneColumn) {
            header.innerHTML = "Watch videos from channels<br/> recommended by Citytv";
        } else {
            header.innerHTML = "Watch videos from channels recommended by Citytv";
        }
        $('#channels_dd div.dd_middle:first').appendChild(header);

        //|	Iterate through the channels
        for (var i = 0; i < s.channels.length; i++) {
            //for (var j = 0; j < 10; j++) {
            //	var i = j % 3;

            //|	Determine what column the show should exist in (assumes a column length of 7 channels)
            var target = Math.floor((i) / 7);

            //|	Create the column if it doesn't exist yet
            if ($('#channels_dd').getElementsByTagName('ul').length <= target) {

                var ul = document.createElement('ul');
                $('#channels_dd div.dd_middle:first').appendChild(ul);

            }

            //|	Add the channel

            var li = document.createElement('li');
            var channel = s.channels[i];

            suffix = (channel.name.length > 25) ? '...' : '';
            channel.name = channel.name.substr(0, 25) + suffix;
            if (channel.presentedby != '') {
                channel.presentedby = Citytv.PRESENTED_BY + channel.presentedby;
            }
            channel.presentedby = Citytv.add_suffix(channel.presentedby, 30);

            var html = '<table><tr><td class="image"><a href="' + bc_root + '/' + channel.url + '"><img src="' + channel.thumbnail_url + '"/></a></td>';
            html += '<td class="info">';
            html += '	<a href="' + bc_root + '/' + channel.url + '">' + channel.name + '</a>';
            html += '	<div class="text">' + channel.presentedby + '</div>';
            html += '</td></tr></table>';
            li.innerHTML = html;

            $('#channels_dd').getElementsByTagName('ul')[target].appendChild(li);

        }
        var height = $("#channels_dd div.dd_middle")[0].offsetHeight + 18;
        $("#channels_dd div.dd_left")[0].style.height = height + "px";
        $("#channels_dd div.dd_right")[0].style.height = height + "px";
        $("#channels_dd div.dd_middle")[0].style.height = height - 8 + "px";

    };


    //|	This function is called on an interval to handle the tricky hover blurs
    this.hover_check = function () {

        //|	Make sure we've left all hover targets
        if (Citytv.over == null) {

            //|	Iterate through all dynamically made hover panes
            if (Citytv.dynamic.length > 0) {

                var target = Citytv.dynamic[0];

                //|	Make sure the target still exists in the DOM
                if (vorpal.defined(target)) {

                    //|	Gracefully fade it out
                    vorpal.ui.animate(target, 500, 0, 'opacity', function () {
                        try {
                            //|	And kill it
                            document.body.removeChild(target);
                        } catch (e) {
                            //|	Handle IE being lame
                        }
                    });

                }

                //|	Remove from the dynamic array to prevent wasted cycles
                Citytv.dynamic.splice(0, 1);

            }

        }

    };

    this.resort = function (s) {

        //|	Trim whitespace from beginning and end of JSON
        s = s.replace(/^\s+|\s+$/g, "");

        //|	Parse that JSON
        s = eval("(" + s + ")");

        var target = $('#' + Citytv.sorting);
        var slider = vorpal.get_by_class(target, 'video_carousel_inner')[0];

        slider.style.left = '0px';

        var left = vorpal.get_by_class(target.parentNode, 'left')[0];
        var right = vorpal.get_by_class(target.parentNode, 'right')[0];

        vorpal.remove_class(left, 'left_active');
        vorpal.remove_class(right, 'right_active');

        var l = vorpal.get_by_class(slider, 'item').length;
        if (l <= 5) { vorpal.remove_class(right, 'right_active'); } else { vorpal.add_class(right, 'right_active'); }

        html = '';

        for (var i = 0; i < s.results.length; i++) {

            html += '<div class="item" id="twominutereplays_46104909001"> <div class="thumb"><div class="image" style="background-image: url(' + s.results[i].thumbnail_url + '); "></div><div class="indicator">' + s.results[i].type + '</div><div class="overlay"><a href="' + bc_root + '/' + s.results[i].video_url + '"><img src="' + bc_root + '/assets/img/play_overlay.png" width="120" alt="90" /></a></div></div><div class="meta"><h1><a href="' + bc_root + '/' + s.results[i].video_url + '">' + s.results[i].name + '</a><span>(' + s.results[i].length + ')</span></h1><h2><a href="' + bc_root + '/' + s.results[i].video_url + '"></a></h2><h3>' + s.results[i].season + '</h3></div></div>';

        }

        slider.innerHTML = html;

        var titles = slider.getElementsByTagName('h1');
        /*
        for (var j = 0; j < titles.length; j++) {

            var t = titles[j].getElementsByTagName('a')[0];

            if (vorpal.defined(t)) {
                counter = 0;
                while ((titles[j].offsetHeight > 40) && (counter < 500)) {
                    t.innerHTML = (t.innerHTML.substr(0, (t.innerHTML.length - 4)) + '...');
                    counter++;
                }
            }

        }*/

    };

	this.fetchData = function (s) {
		
		//|	Trim whitespace from beginning and end of JSON
		s = s.replace(/^\s+|\s+$/g,"");

		//|	Parse that JSON
		s = eval("("+s+")");
		
		var target = $('#'+Citytv.sorting);
		
		var slider = vorpal.get_by_class(target, 'video_carousel_inner')[0];
		
		slider.style.left = '0px';
		
		var left = vorpal.get_by_class(target.parentNode, 'left')[0];
		var right = vorpal.get_by_class(target.parentNode, 'right')[0];

		//vorpal.remove_class(left, 'left_active');
		vorpal.remove_class(right, 'right_active');
		
		var l = vorpal.get_by_class(slider, 'item').length;
		if (l <= 5) { vorpal.remove_class(right, 'right_active'); } else { vorpal.add_class(right, 'right_active');  }
		vorpal.add_class(left, 'left_active');
		
		html = '';
		for (var i = 0; i < s.results.length; i++) {
			html += '<div class="item" id="twominutereplays_46104909001"> <div class="thumb"><div class="image" style="background-image: url('+s.results[i].thumbnail_url+'); "></div><div class="indicator">'+s.results[i].type+'</div><div class="overlay"><a href="'+bc_root+'/'+s.results[i].video_url+'"><img src="'+bc_root+'/assets/img/play_overlay.png" width="120" alt="90" /></a></div></div><div class="meta"><h1><a href="'+bc_root+'/'+s.results[i].video_url+'">'+s.results[i].name+'</a><span>('+s.results[i].length+')</span></h1><h2><a href="'+bc_root+'/'+s.results[i].video_url+'"></a></h2><h3>'+s.results[i].season+'</h3></div></div>';
		}
		slider.innerHTML = html;
		//var titles = slider.getElementsByTagName('h1');
		
	};
	
    this.scroll = function (direction) {
        if (direction == 'd') {

            Citytv.search_index++;

        } else {

            Citytv.search_index--;

        }

        var items = $('#search_dd li');

        Citytv.search_index = (Citytv.search_index < 0) ? (items.length - 1) : Citytv.search_index;
        Citytv.search_index = (Citytv.search_index > (items.length - 1)) ? 0 : Citytv.search_index;

        var search = $('div.search:first').getElementsByTagName('input')[0];

        search.value = $("#search_dd li")[Citytv.search_index].getElementsByTagName('h2')[0].getElementsByTagName('a')[0].innerHTML.trim();
        Citytv.last_search = search.value;

    };

    this.set_links = function (url, title) {

        Citytv.bc.exp.getModule('social').setLink(url);

        var social = $('div.social:first').getElementsByTagName('a');

        social[0].setAttribute('href', 'http://www.facebook.com/sharer.php?u=' + escape(url) + '&t=' + escape(title) + '');
        social[1].setAttribute('href', 'http://twitter.com/home?status=' + escape(title) + ' - ' + escape(url) + '');
        social[2].setAttribute('href', 'http://www.myspace.com/Modules/PostTo/Pages/?u=' + escape(url) + '');
        social[3].setAttribute('href', 'http://www.google.com/bookmarks/mark?op=edit&bkmk=' + escape(url) + '');
        social[4].setAttribute('href', 'http://del.icio.us/post?url=' + escape(url) + '&title=' + escape(title) + '&notes=' + escape(title) + '');
        social[5].setAttribute('href', 'http://digg.com/submit?url=' + escape(url) + '&title=' + escape(title) + '&bodytext=' + escape(title) + '&media=&topic=');

    };

    this.bitly = function () {

        var e = document.createElement('script');
        e.src = 'http://api.bit.ly/shorten?version=2.0.1&longUrl=' + escape(document.location.href) + '&login=cityvideo&apiKey=R_0e7e89a8c5688898a02fb3368c029a8f&callback=Citytv.handle_bitly';

        document.body.appendChild(e);

    };

    this.handle_bitly = function (s) {

        if (s['results'] == null) {
          return;
        }

        this.set_links(s['results'][document.location.href]['shortUrl'], document.title);

    };

    this.advance = function (s) {

        var sliders = $('div.video_carousel');

        for (var i = 0; i < sliders.length; i++) {

            var items = vorpal.get_by_class(sliders[i], 'item');

            for (var j = 0; j < items.length; j++) {

                if (vorpal.has_class(items[j], 'active')) {

                    vorpal.remove_class(items[j], 'active');

                    if (j != (items.length - 1)) {

                        //Citytv.bc.mvp.loadVideo(items[j + 1].id.split('_')[1]);

                        vorpal.add_class(items[j + 1], 'active');
                        var idArray = items[j + 1].id.split('_');;
                        var parameters = 'R=' + idArray[1];
                        if(idArray.length > 2) {
                        	parameters += '&channel=' + idArray[2];
                        }
                        vorpal.io.send(bc_root + '/json.htm?media=detail&' + parameters, { 'vcb': Citytv.handle_advance, 'vm': 'GET' });

                        return;

                    }

                }

            }

        }

    };

    this.handle_advance = function (s) {

        //|	Trim whitespace from beginning and end of JSON
        s = s.replace(/^\s+|\s+$/g, "");

        //|	Parse that JSON
        s = eval("(" + s + ")");
        window.location = bc_root + '/' + s.video_url;
        /*	Citytv.set_links(bc_root+'/'+s.video_url, s.name);
		
        $('div.breadcrumb:first').innerHTML = '<a href="'+bc_root+'/home/0/">Citytv Video</a> <span>&raquo;</span> <a href="'+bc_root+'/navigation.htm?N=0&type=shows&sort=Display"/>Shows</a> <span>&raquo;</span> <a href="'+bc_root+'/'+s.show_url+'">'+s.show+'</a> <span>&raquo;</span> '+s.name;

        $('div.meta_data_area:first').getElementsByTagName('h1')[0].innerHTML = s.show;
        $('div.meta_data_area:first').getElementsByTagName('h2')[0].innerHTML = s.season;
        $('div.meta_data_area:first').getElementsByTagName('h5')[0].innerHTML = '';
        $('div.meta_data_area:first').getElementsByTagName('h1')[1].innerHTML = s.name+' <span>('+s.length+')</span>';
        $('div.meta_data_area:first').getElementsByTagName('p')[0].innerHTML = s.description;
        */
    };

} ();










//|	Onload events
vorpal.events.attach(document, 'domready', function () {

    //|	This should be our search box
    var i = $('div.search:first').getElementsByTagName('input')[0];

    //|	Store the current label in it
    Citytv.search_value = i.value;

    //|	Clear the label when focus occurs and start the search_cycle timer
    vorpal.events.attach(i, 'focus', function (e) {

        e = e || window.event;
        t = e.srcElement || e.target;

        if (t.value == Citytv.search_value) {

            t.value = '';

            t.style.color = '#000000';

            if (Citytv.throttle) {

                Citytv.search_timer = setInterval(Citytv.search_cycle, 10);

            }

        }

    });

    var icon = vorpal.get_by_class($('div.search:first'), 'right')[0];
    icon.style.cursor = 'pointer';
    vorpal.events.attach(icon, 'click', function (e) {

        $('div.search:first').getElementsByTagName('form')[0].submit();

    });

    //|	Return the stored value when focus is lost and the value is empty
    vorpal.events.attach(i, 'blur', function (e) {

        e = e || window.event;
        t = e.srcElement || e.target;

        if (t.value == '') {

            t.value = Citytv.search_value;
            t.style.color = '#bbbbbb';

        }

    });

    //|	Update the time_index when typing occurs to throttle requests, or search otherwise
    vorpal.events.attach(i, 'keyup', function (e) {

        e = e || window.event;
        t = e.srcElement || e.target;
        k = e.charCode || e.keyCode;

        if (Citytv.throttle) {

            Citytv.time_index = (new Date().getTime() / 1000);

        } else {

            Citytv.do_search();

        }

        if (k == 38) {

            Citytv.scroll('u');

        } else if (k == 40) {

            Citytv.scroll('d');

        }

    });



    //|	Reposition all dynamic absolute elements when the window resizes
    vorpal.events.attach(window, 'resize', function () {

        Citytv.position_search();

    });

    //|	IE :(
    window.onresize = Citytv.position_search;

    //|	Initiate the cycle function for overlay removal
    Citytv.over_cycle = setInterval(Citytv.hover_check, 300);

    //|	Add events to the shows menu item
    var shows = $('div.header:first').getElementsByTagName('li')[1];

    vorpal.events.attach(shows.getElementsByTagName('img')[0], 'click', function (e) { Citytv.over = shows; Citytv.shows(); });
    vorpal.events.attach(shows, 'mouseenter', function (e) { Citytv.over = shows; Citytv.shows(); });
    vorpal.events.attach(shows, 'mouseleave', function (e) { Citytv.over = null; });

    //|	Add events to the more channels menu item
    var more_channels = $('div.header:first').getElementsByTagName('li')[4];

    vorpal.events.attach(more_channels.getElementsByTagName('img')[0], 'click', function (e) { Citytv.over = more_channels; Citytv.more_channels(); });
    vorpal.events.attach(more_channels, 'mouseenter', function (e) { Citytv.over = more_channels; Citytv.more_channels(); });
    vorpal.events.attach(more_channels, 'mouseleave', function (e) { Citytv.over = null; });

    //|	If this is the homepage, instantiate the slider
    if (vorpal.defined($('#home_carousel_episodes'))) {

        Citytv.position_arrows();

        //|	Toggle popular episodes
        vorpal.events.attach($('div.toggle div.left:first'), 'click', function () {

            vorpal.add_class($('div.toggle div.left:first'), 'active');
            vorpal.remove_class($('div.toggle div.right:first'), 'active');
            $('#home_carousel_clips').style.display = 'none';
            $('#home_carousel_episodes').style.display = 'block';
            Citytv.factor_home_slider(0);

        });

        //|	Toggle popular clips
        vorpal.events.attach($('div.toggle div.right:first'), 'click', function () {

            vorpal.add_class($('div.toggle div.right:first'), 'active');
            vorpal.remove_class($('div.toggle div.left:first'), 'active');
            $('#home_carousel_episodes').style.display = 'none';
            $('#home_carousel_clips').style.display = 'block';
            Citytv.factor_home_slider(1);

        });

        var items = $('div.home_list div.item');

        //|	Add events to the video tiles that will have hover states
        for (var i = 0; i < items.length; i++) {

            //if (items[i].getElementsByTagName('img').length > 1) {

            vorpal.ui.opacity(items[i].getElementsByTagName('img')[0], 0.01);

            vorpal.events.attach(items[i].getElementsByTagName('img')[0], 'mouseenter', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = t; Citytv.hover(t); vorpal.ui.animate(t, 200, 1, 'opacity'); });
            vorpal.events.attach(items[i].getElementsByTagName('img')[0], 'mouseleave', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = null; vorpal.ui.animate(t, 200, 0, 'opacity'); });

            //}				

            var titles = items[i].getElementsByTagName('h1');

            for (var j = 0; j < titles.length; j++) {

                var t = titles[j].getElementsByTagName('a')[0];

                if (vorpal.defined(t)) {
                    counter = 0;
                    while ((titles[j].offsetHeight > 60) && (counter < 500)) {
                        t.innerHTML = (t.innerHTML.substr(0, (t.innerHTML.length - 4)) + '...');
                        counter++;
                    }
                }

            }

        }

        //|	Prepare the home slider for use
        Citytv.factor_home_slider(0);

        //|	Add events for the left slide button
        vorpal.events.attach($('div.featured div.left:first'), 'click', function () {

            Citytv.slide_featured('l');

        });

        //|	Add events for the right slide button
        vorpal.events.attach($('div.featured div.right:first'), 'click', function () {

            Citytv.slide_featured('r');

        });

    } else {

        if (!vorpal.defined($('#episodes')) && !vorpal.defined($('#clips'))) {

            var items = $('div.item');

            //|	Add events to the video tiles that will have hover states
            for (var i = 0; i < items.length; i++) {

                if (vorpal.defined(vorpal.get_by_class(items[i], 'overlay'))) {

                    try {

                        vorpal.ui.opacity(items[i].getElementsByTagName('img')[0], 0);

                        vorpal.events.attach(items[i].getElementsByTagName('img')[0], 'mouseenter', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = t; vorpal.ui.animate(t, 200, 1, 'opacity'); });
                        vorpal.events.attach(items[i].getElementsByTagName('img')[0], 'mouseleave', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = null; vorpal.ui.animate(t, 200, 0, 'opacity'); });

                    } catch (e) {

                        //|	Whoops

                    }

                }

            }

        }

    }

    //|	Add show page events
    if (vorpal.defined($('.tab1_content')) && vorpal.defined($('.tab2_content'))) {

        var tabs = $('div.tabs div.tab');

        //|	Episode toggle
        vorpal.events.attach(tabs[0], 'click', function () {

            var tabs = $('div.tabs div.tab');
            $('.tab2_content').style.display = 'none';
            $('.tab1_content').style.display = 'block';
            vorpal.add_class(tabs[0], 'active');
            vorpal.remove_class(tabs[1], 'active');
            return false;

        });

        //|	Clip toggle
        vorpal.events.attach(tabs[1], 'click', function () {

            var tabs = $('div.tabs div.tab');
            $('.tab1_content').style.display = 'none';
            $('.tab2_content').style.display = 'block';
            vorpal.add_class(tabs[1], 'active');
            vorpal.remove_class(tabs[0], 'active');
            return false;

        });
    }
    if (vorpal.defined($('#episodes')) && vorpal.defined($('#clips'))) {

        var items = $('div.item');

        //|	Add events to the video tiles that will have hover states
        for (var i = 0; i < items.length; i++) {

            //if (vorpal.defined(vorpal.get_by_class(items[i], 'overlay'))) {

            if (items[i].getElementsByTagName('img').length > 1) {

                try {

                    vorpal.ui.opacity(items[i].getElementsByTagName('img')[1], 0.01);

                    vorpal.events.attach(items[i].getElementsByTagName('img')[1], 'mouseenter', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = t; vorpal.ui.animate(t, 200, 1, 'opacity'); });
                    vorpal.events.attach(items[i].getElementsByTagName('img')[1], 'mouseleave', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = null; vorpal.ui.animate(t, 200, 0, 'opacity'); });

                } catch (e) {

                    //|	Do nothing

                }

            } else {

                try {

                    vorpal.ui.opacity(items[i].getElementsByTagName('img')[0], 0.01);

                    vorpal.events.attach(items[i].getElementsByTagName('img')[0], 'mouseenter', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = t; vorpal.ui.animate(t, 200, 1, 'opacity'); });
                    vorpal.events.attach(items[i].getElementsByTagName('img')[0], 'mouseleave', function (e) { e = e || window.event; t = e.srcElement || e.target; Citytv.over = null; vorpal.ui.animate(t, 200, 0, 'opacity'); });

                } catch (e) {

                    //|	Do nothing

                }

            }

            //}

        }

    }

    //|	Detect IE6 and notify users
    if ((vorpal.browser == 'explorer') && (vorpal.version < 7) && (vorpal.get_cookie('accept-ie6') == null)) {

        Citytv.ie6();

        //|	This cookie should prevent the message from showing on every load
        vorpal.set_cookie('accept-ie6', 'true');

    }

    //|	This should be dim the lights
    var dim = $('div.dim:first');

    //|	If the dim link is found, we're on the video page
    if (vorpal.defined(dim)) {

        vorpal.events.attach(dim, 'click', function () {

            Citytv.dim();

            dim.style.position = 'absolute';
            dim.style.zIndex = 100;

            return false;

        });

        var sliders = $('div.video_carousel');
		var sortButtonClicked = 0;

        for (var i = 0; i < sliders.length; i++) {

            var sorts = sliders[i].getElementsByTagName('li');
        	if(sorts.length > 0) {
				vorpal.events.attach(sorts[0], 'click', function (e) {
	
					e = e || window.event;
					t = e.srcElement || e.target;
	
					var li = t.parentNode.parentNode.getElementsByTagName('li');
					var id = vorpal.get_by_class(t.parentNode.parentNode.parentNode.parentNode, 'video_carousel_outer')[0].id;
					var q = queryString[id + '_airdate'];
					vorpal.io.send(bc_root + '/json.htm?' + q, { 'vcb': Citytv.resort, 'vm': 'GET' });
					vorpal.remove_class(li[2], 'active');
					vorpal.remove_class(li[1], 'active');
					vorpal.add_class(li[0], 'active');
					Citytv.sorting = id;
					Citytv.populatedCarousels["key"+id] = 1;			
	
				});
	
				vorpal.events.attach(sorts[1], 'click', function (e) {
	
					e = e || window.event;
					t = e.srcElement || e.target;
	
					var li = t.parentNode.parentNode.getElementsByTagName('li');
					var id = vorpal.get_by_class(t.parentNode.parentNode.parentNode.parentNode, 'video_carousel_outer')[0].id;
					var q = queryString[id + '_popular'];
					vorpal.io.send(bc_root + '/json.htm?' + q, { 'vcb': Citytv.resort, 'vm': 'GET' });
					vorpal.remove_class(li[2], 'active');
					vorpal.remove_class(li[0], 'active');
					vorpal.add_class(li[1], 'active');
					Citytv.sorting = id;
					Citytv.populatedCarousels["key"+id] = 1;			
				});

				vorpal.events.attach(sorts[2], 'click', function (e) {
	
					e = e || window.event;
					t = e.srcElement || e.target;
	
					var li = t.parentNode.parentNode.getElementsByTagName('li');
					var id = vorpal.get_by_class(t.parentNode.parentNode.parentNode.parentNode, 'video_carousel_outer')[0].id;
					var q = queryString[id + '_expiring'];
					vorpal.io.send(bc_root + '/json.htm?' + q, { 'vcb': Citytv.resort, 'vm': 'GET' });
					vorpal.remove_class(li[1], 'active');
					vorpal.remove_class(li[0], 'active');
					vorpal.add_class(li[2], 'active');
					Citytv.sorting = id;
					Citytv.populatedCarousels["key"+id] = 1;			

				});
				
			}

            var titles = sliders[i].getElementsByTagName('h1');
            /*
            for (var j = 0; j < titles.length; j++) {

                var t = titles[j].getElementsByTagName('a')[0];

                if (vorpal.defined(t)) {
                    counter = 0;
                    while ((titles[j].offsetHeight > 40) && (counter < 500)) {
                        t.innerHTML = (t.innerHTML.substr(0, (t.innerHTML.length - 4)) + '...');
                        counter++;
                    }
                }

            }*/

            var left = vorpal.get_by_class(sliders[i], 'left')[0];
            var right = vorpal.get_by_class(sliders[i], 'right')[0];

            vorpal.remove_class(left, 'left_active');
            vorpal.remove_class(right, 'right_active');

            var slider = right.parentNode.parentNode;
            slider = vorpal.get_by_class(slider, 'video_carousel_outer')[0];
            slider = vorpal.get_by_class(slider, 'video_carousel_inner')[0];
            var l = vorpal.get_by_class(slider, 'item').length;
            if (l <= 5) { vorpal.remove_class(right, 'right_active'); } else { vorpal.add_class(right, 'right_active'); }

            vorpal.events.attach(left, 'click', function (e) {

                e = e || window.event;
                t = e.srcElement || e.target;

                //|	Don't let this continue if the slider is already animating
                if (Citytv.sliding) { return; }

                Citytv.sliding = true;

                var slider = t.parentNode.parentNode;
                slider = vorpal.get_by_class(slider, 'video_carousel_outer')[0];
                slider = vorpal.get_by_class(slider, 'video_carousel_inner')[0];
                var left = t;
                var right = left.parentNode.getElementsByTagName('div')[1];
                var l = vorpal.get_by_class(slider, 'item').length;
                var max = 0;
                var min = (0 - ((Math.ceil(l / 5) - 1) * 870));

                //|	IE doesn't know how to do simple math correctly
                if ((slider.offsetLeft % 870) != 0) {
                    slider.style.left = (slider.offsetLeft - ((870 + (slider.offsetLeft % 870))) * 2) + 'px';
                }

                if ((slider.offsetLeft + 870) <= max) {

                    var offset = (slider.offsetLeft + 870);

                } else {

                    var offset = max;

                }

                vorpal.ui.animate(slider, 500, offset, 'left', function () { Citytv.sliding = false; });

                //|	Figure out what the arrow states should be
                if (offset == 0) { vorpal.remove_class(left, 'left_active'); } else { vorpal.add_class(left, 'left_active'); }
                if (offset < (0 - ((Math.ceil(l / 5) - 1) * 869))) { vorpal.remove_class(right, 'right_active'); } else { vorpal.add_class(right, 'right_active'); }

            });

            vorpal.events.attach(right, 'click', function (e) {

                e = e || window.event;
                t = e.srcElement || e.target;

                //|	Don't let this continue if the slider is already animating
                if (Citytv.sliding) { return; }

                Citytv.sliding = true;

                var slider = t.parentNode.parentNode;
                slider = vorpal.get_by_class(slider, 'video_carousel_outer')[0];
                slider = vorpal.get_by_class(slider, 'video_carousel_inner')[0];
                var right = t;
                var left = right.parentNode.getElementsByTagName('div')[0];
                var l = vorpal.get_by_class(slider, 'item').length;
                var max = 0;
                var min = (0 - ((Math.ceil(l / 5) - 1) * 870));

                //|	IE doesn't know how to do simple math correctly
                if ((slider.offsetLeft % 870) != 0) {
                    slider.style.left = (slider.offsetLeft - ((870 + (slider.offsetLeft % 870))) * 2) + 'px';
                }

                if ((slider.offsetLeft - 870) >= min) {
                    var offset = slider.offsetLeft - 870;
                } else {
                    var offset = slider.offsetLeft;
                }

              //Do AJAX request for right slider once 
				var id = vorpal.get_by_class(t.parentNode.parentNode, 'video_carousel_outer')[0].id;
				if (Citytv.populatedCarousels["key"+id] == null && typeof(queryString) != "undefined") {
					Citytv.populatedCarousels["key"+id] = 1;
					var q = queryString[id+'_airdate'];
					vorpal.io.send(bc_root+'/json.htm?'+q, {'vcb':Citytv.fetchData, 'vm':'GET'});		
					Citytv.sorting = id;
				} 
				//end Ajax request
				
                vorpal.ui.animate(slider, 500, offset, 'left', function () { Citytv.sliding = false; });

                //|	Figure out what the arrow states should be
                if (offset == 0) { vorpal.remove_class(left, 'left_active'); } else { vorpal.add_class(left, 'left_active'); }
                if (offset < (0 - ((Math.ceil(l / 5) - 1) * 869))) { vorpal.remove_class(right, 'right_active'); } else { vorpal.add_class(right, 'right_active'); }

            });

        }

    }

    var right = $('div.shows_right:first');

    if (vorpal.defined(right)) {

        var titles = right.getElementsByTagName('h1');

       /* for (var j = 0; j < titles.length; j++) {

            var t = titles[j].getElementsByTagName('a')[0];

            if (vorpal.defined(t)) {
                counter = 0;
                while ((titles[j].offsetHeight > 40) && (counter < 500)) {
                    t.innerHTML = (t.innerHTML.substr(0, (t.innerHTML.length - 4)) + '...');
                    counter++;
                }
            }

        }*/

    }

    var left = $('div.videos_left:first');

    if (vorpal.defined(left)) {

        var titles = left.getElementsByTagName('h1');
        /*
        for (var j = 0; j < titles.length; j++) {

            var t = titles[j].getElementsByTagName('a')[0];

            if (vorpal.defined(t)) {
                counter = 0;
                while ((titles[j].offsetHeight > 40) && (counter < 500)) {
                    t.innerHTML = (t.innerHTML.substr(0, (t.innerHTML.length - 4)) + '...');
                    counter++;
                }
            }

        }*/

    }

    if (vorpal.defined($('div.link:first')) && vorpal.defined($('div.tabs:first'))) {

        while ($('div.link:first').offsetHeight > 50) {

            var a = $('div.link:first').getElementsByTagName('a')[0];

            if (a.innerHTML.indexOf('info on')) {
                a.innerHTML = a.innerHTML.substr(0, (a.innerHTML.indexOf('info on') + 4)) + '...';
            } else {
                a.innerHTML = a.innerHTML.substr(0, (a.innerHTML.length - 4)) + '...';
            }

        }

    }

    setTimeout(function () {

        var spon = $('div.sponsor:first');

        if (vorpal.defined(spon) && spon != '') {

            if (spon.innerHTML.indexOf('grey.gif') > 0) {

                spon.parentNode.removeChild(spon);

            }

        }

    }, 300);

});

function onTemplateLoaded(experienceID) {

    Citytv.bc.exp = brightcove.getExperience(experienceID);

    Citytv.bc.mvp = Citytv.bc.exp.getModule(APIModules.VIDEO_PLAYER);
    Citytv.bc.mexp = Citytv.bc.exp.getModule(APIModules.EXPERIENCE);
    Citytv.bc.mcon = Citytv.bc.exp.getModule(APIModules.CONTENT);

    Citytv.bc.mexp.addEventListener(BCExperienceEvent.TEMPLATE_READY, onTemplateReady);
    BrightcoveTool.fillInAdsAfterBrightcove(experienceID)

}

function onTemplateReady() {

    Citytv.bitly();
    Citytv.bc.mvp.addEventListener(BCMediaEvent.COMPLETE, Citytv.advance);
    Citytv.bc.exp.getModule('social').setLink(document.location.href);

}


function bcFiltering(pIndex) {
    var filter = document.getElementById("filtering_" + pIndex);
    if (filter.style.display == "block" || filter.style.display == '') {
        filter.style.display = "none";
        document.getElementById("filtering_icon_" + pIndex).style.backgroundPosition = "bottom left";
    } else {
        filter.style.display = "block";
        document.getElementById("filtering_icon_" + pIndex).style.backgroundPosition = "top left";
    }
}

document.write("<style type='text/css' rel='stylesheet'>div.ie6_overlay { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bc_root + "/assets/img/ie6_overlay.png', sizingMethod='scale'); height: 200px; left: 0; position: absolute; top: 0; width: 200px; z-index: 1000; }" +
					"div.ie6 { left: 100px; position: absolute; top: 100px; z-index: 2000; }" +
					"div.ie6 div.top { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bc_root + "/assets/img/ie6_background_top.png', sizingMethod='crop'); height: 19px; width: 566px; }" +
					"div.ie6 div.middle { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bc_root + "/assets/img/ie6_background_middle.png', sizingMethod='scale'); padding: 10px 29px; width: 508px; }" +
					"div.ie6 div.middle h1 { font-size: 18px; line-height: 18px; margin: 0 0 10px; }" +
					"div.ie6 div.middle p { font-size: 12px; line-height: 18px; }" +
					"div.ie6 div.middle input { margin: 20px 0 0; }" +
					"div.ie6 div.bottom { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + bc_root + "/assets/img/ie6_background_bottom.png', sizingMethod='crop'); height: 19px; width: 566px; }</style>");

